Big refactoring sweep through tests

This commit is contained in:
Teknique 2023-06-17 11:34:09 -07:00
parent b545c5c2be
commit 553ea7a3e0
No known key found for this signature in database
GPG Key ID: 54D9906A294B7FF3
5 changed files with 134 additions and 105 deletions

View File

@ -1,34 +1,37 @@
from typing import Callable, Awaitable
import os import os
import pytest from functools import cache
pytest_plugins = ('pytest_asyncio',) from typing import AsyncGenerator
import pytest_asyncio
import veilid import veilid
from veilid.json_api import _JsonVeilidAPI
pytest_plugins = ("pytest_asyncio",)
################################################################## @cache
VEILID_SERVER = os.getenv("VEILID_SERVER") def server_info() -> tuple[str, int]:
if VEILID_SERVER is not None: """Return the hostname and port of the test server."""
vsparts = VEILID_SERVER.split(":") VEILID_SERVER = os.getenv("VEILID_SERVER")
VEILID_SERVER = vsparts[0] if VEILID_SERVER is None:
if len(vsparts) == 2: return "localhost", 5959
VEILID_SERVER_PORT = int(vsparts[1])
else:
VEILID_SERVER_PORT = 5959
else:
VEILID_SERVER = "localhost"
VEILID_SERVER_PORT = 5959
################################################################## hostname, *rest = VEILID_SERVER.split(":")
if rest:
return hostname, int(rest[0])
return hostname, 5959
async def simple_connect_and_run(func: Callable[[veilid.VeilidAPI], Awaitable]):
api = await veilid.json_api_connect(VEILID_SERVER, VEILID_SERVER_PORT, simple_update_callback) async def simple_update_callback(update: veilid.VeilidUpdate):
print(f"VeilidUpdate: {update}")
@pytest_asyncio.fixture
async def api_connection() -> AsyncGenerator[_JsonVeilidAPI, None]:
hostname, port = server_info()
api = await veilid.json_api_connect(hostname, port, simple_update_callback)
async with api: async with api:
# purge routes to ensure we start fresh # purge routes to ensure we start fresh
await api.debug("purge routes") await api.debug("purge routes")
await func(api) yield api
async def simple_update_callback(update: veilid.VeilidUpdate):
print("VeilidUpdate: {}".format(update))

View File

@ -1,39 +1,46 @@
# Basic veilid tests # Basic veilid tests
import veilid import socket
import pytest import pytest
from . import * import veilid
from . import api_connection, simple_update_callback
################################################################## ##################################################################
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_connect(): async def test_connect(api_connection):
async def func(api: veilid.VeilidAPI):
pass pass
await simple_connect_and_run(func)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_node_id(): async def test_get_node_id(api_connection):
async def func(api: veilid.VeilidAPI): state = await api_connection.get_state()
# get our own node id node_ids = state.config.config.network.routing_table.node_id
state = await api.get_state()
node_id = state.config.config.network.routing_table.node_id.pop() assert len(node_ids) >= 1
await simple_connect_and_run(func)
for node_id in node_ids:
assert node_id[4] == ":"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_fail_connect(): async def test_fail_connect():
with pytest.raises(Exception): with pytest.raises(socket.gaierror) as exc:
api = await veilid.json_api_connect("fuahwelifuh32luhwafluehawea", 1, simple_update_callback) await veilid.json_api_connect(
async with api: "fuahwelifuh32luhwafluehawea", 1, simple_update_callback
pass )
assert exc.value.errno == socket.EAI_NONAME
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_version(): async def test_version(api_connection):
async def func(api: veilid.VeilidAPI): v = await api_connection.veilid_version()
v = await api.veilid_version() print(f"veilid_version: {v.__dict__}")
print("veilid_version: {}".format(v.__dict__)) assert v.__dict__.keys() >= {"_major", "_minor", "_patch"}
vstr = await api.veilid_version_string()
print("veilid_version_string: {}".format(vstr))
await simple_connect_and_run(func)
vstr = await api_connection.veilid_version_string()
print(f"veilid_version_string: {vstr}")

View File

@ -1,42 +1,53 @@
# Crypto veilid tests # Crypto veilid tests
import veilid
import pytest import pytest
from . import * import veilid
from veilid.api import CryptoSystem
from . import api_connection
################################################################## ##################################################################
@pytest.mark.asyncio
async def test_best_crypto_system():
async def func(api: veilid.VeilidAPI):
bcs = await api.best_crypto_system()
await simple_connect_and_run(func)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_crypto_system(): async def test_best_crypto_system(api_connection):
async def func(api: veilid.VeilidAPI): bcs: CryptoSystem = await api_connection.best_crypto_system()
cs = await api.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)
assert await bcs.default_salt_length() == 16
@pytest.mark.asyncio
async def test_get_crypto_system(api_connection):
cs: CryptoSystem = await api_connection.get_crypto_system(
veilid.CryptoKind.CRYPTO_KIND_VLD0
)
assert await cs.default_salt_length() == 16
# clean up handle early # clean up handle early
del cs del cs
await simple_connect_and_run(func)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_crypto_system_invalid(): async def test_get_crypto_system_invalid(api_connection):
async def func(api: veilid.VeilidAPI): with pytest.raises(veilid.VeilidAPIErrorInvalidArgument) as exc:
with pytest.raises(veilid.VeilidAPIError): await api_connection.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_NONE)
cs = await api.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_NONE)
await simple_connect_and_run(func) assert exc.value.context == "unsupported cryptosystem"
assert exc.value.argument == "kind"
assert exc.value.value == "NONE"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_hash_and_verify_password(): async def test_hash_and_verify_password(api_connection):
async def func(api: veilid.VeilidAPI): bcs = await api_connection.best_crypto_system()
bcs = await api.best_crypto_system()
nonce = await bcs.random_nonce() nonce = await bcs.random_nonce()
salt = nonce.to_bytes() salt = nonce.to_bytes()
# Password match # Password match
phash = await bcs.hash_password(b"abc123", salt) phash = await bcs.hash_password(b"abc123", salt)
assert await bcs.verify_password(b"abc123", phash) assert await bcs.verify_password(b"abc123", phash)
# Password mismatch # Password mismatch
phash2 = await bcs.hash_password(b"abc1234", salt) phash2 = await bcs.hash_password(b"abc1234", salt)
assert not await bcs.verify_password(b"abc12345", phash) assert not await bcs.verify_password(b"abc12345", phash)
await simple_connect_and_run(func)

View File

@ -1,34 +1,37 @@
# Routing context veilid tests # Routing context veilid tests
import veilid
import pytest
import asyncio import asyncio
import json import json
from . import *
import pytest
import veilid
from . import api_connection, server_info
################################################################## ##################################################################
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_routing_contexts(): async def test_routing_contexts(api_connection):
async def func(api: veilid.VeilidAPI): rc = await api_connection.new_routing_context()
rc = await api.new_routing_context()
rcp = await rc.with_privacy() rcp = await rc.with_privacy()
rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED) rcps = await rcp.with_sequencing(veilid.Sequencing.ENSURE_ORDERED)
rcpsr = await rcps.with_custom_privacy(veilid.Stability.RELIABLE) await rcps.with_custom_privacy(veilid.Stability.RELIABLE)
await simple_connect_and_run(func)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_routing_context_app_message_loopback(): async def test_routing_context_app_message_loopback():
app_message_queue = asyncio.Queue() app_message_queue = asyncio.Queue()
async def app_message_queue_update_callback(update: veilid.VeilidUpdate): async def app_message_queue_update_callback(update: veilid.VeilidUpdate):
if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE:
await app_message_queue.put(update) await app_message_queue.put(update)
api = await veilid.json_api_connect(VEILID_SERVER, VEILID_SERVER_PORT, app_message_queue_update_callback) hostname, port = server_info()
api = await veilid.json_api_connect(
hostname, port, app_message_queue_update_callback
)
async with api: async with api:
# purge routes to ensure we start fresh # purge routes to ensure we start fresh
await api.debug("purge routes") await api.debug("purge routes")
@ -46,23 +49,24 @@ async def test_routing_context_app_message_loopback():
await rc.app_message(prr, message) await rc.app_message(prr, message)
# we should get the same message back # we should get the same message back
update: veilid.VeilidUpdate = await asyncio.wait_for(app_message_queue.get(), timeout=10) update: veilid.VeilidUpdate = await asyncio.wait_for(
app_message_queue.get(), timeout=10
)
appmsg: veilid.VeilidAppMessage = update.detail appmsg: veilid.VeilidAppMessage = update.detail
assert appmsg.message == message assert appmsg.message == message
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_routing_context_app_call_loopback(): async def test_routing_context_app_call_loopback():
app_call_queue = asyncio.Queue() app_call_queue = asyncio.Queue()
async def app_call_queue_update_callback(update: veilid.VeilidUpdate): async def app_call_queue_update_callback(update: veilid.VeilidUpdate):
if update.kind == veilid.VeilidUpdateKind.APP_CALL: if update.kind == veilid.VeilidUpdateKind.APP_CALL:
await app_call_queue.put(update) await app_call_queue.put(update)
api = await veilid.json_api_connect(VEILID_SERVER, VEILID_SERVER_PORT, app_call_queue_update_callback) hostname, port = server_info()
api = await veilid.json_api_connect(hostname, port, app_call_queue_update_callback)
async with api: async with api:
# purge routes to ensure we start fresh # purge routes to ensure we start fresh
await api.debug("purge routes") await api.debug("purge routes")
@ -77,10 +81,14 @@ async def test_routing_context_app_call_loopback():
# send an app message to our own private route # send an app message to our own private route
request = b"abcd1234" request = b"abcd1234"
app_call_task = asyncio.create_task(rc.app_call(prr, request), name = "app call task") app_call_task = asyncio.create_task(
rc.app_call(prr, request), name="app call task"
)
# we should get the same request back # we should get the same request back
update: veilid.VeilidUpdate = await asyncio.wait_for(app_call_queue.get(), timeout=10) update: veilid.VeilidUpdate = await asyncio.wait_for(
app_call_queue.get(), timeout=10
)
appcall: veilid.VeilidAppCall = update.detail appcall: veilid.VeilidAppCall = update.detail
assert appcall.message == request assert appcall.message == request

View File

@ -1072,5 +1072,5 @@ class _JsonCryptoSystem(CryptoSystem):
async def json_api_connect( async def json_api_connect(
host: str, port: int, update_callback: Callable[[VeilidUpdate], Awaitable] host: str, port: int, update_callback: Callable[[VeilidUpdate], Awaitable]
) -> VeilidAPI: ) -> _JsonVeilidAPI:
return await _JsonVeilidAPI.connect(host, port, update_callback) return await _JsonVeilidAPI.connect(host, port, update_callback)