diff --git a/tests/helpers/cmd.py b/tests/helpers/cmd.py new file mode 100644 index 0000000..22d54f0 --- /dev/null +++ b/tests/helpers/cmd.py @@ -0,0 +1,25 @@ +async def run(hub, subcommand: str, target: str = "*", *args): + with hub.test.container.roster() as rf: + command = f"{hub.lib.sys.executable} -m soluble -R {rf} {subcommand} '{target}' {' '.join(args)}" + + # Run the command asynchronously + process = await hub.lib.asyncio.create_subprocess_shell( + command, + stdout=hub.lib.asyncio.subprocess.PIPE, + stderr=hub.lib.asyncio.subprocess.PIPE, + ) + + # Capture the stdout and stderr + stdout, stderr = await process.communicate() + + # Decode the output + stdout = stdout.decode() + stderr = stderr.decode() + + # Assert the process was successful + assert ( + process.returncode == 0 + ), f"Command failed with code {process.returncode}: {stderr}" + + # Return the captured stdout + return stdout diff --git a/tests/helpers/container.py b/tests/helpers/container.py index dba65e0..e13b7aa 100644 --- a/tests/helpers/container.py +++ b/tests/helpers/container.py @@ -2,7 +2,7 @@ def __init__(hub): - hub.test.TARGETS = {} + hub.test.ROSTER = {} def next_free_port(hub, host, port: int = 2222) -> int: @@ -22,12 +22,11 @@ def next_free_port(hub, host, port: int = 2222) -> int: return port -async def create_ssh_target( - hub, host: str, username: str = "user", password: str = "pass" -): +async def create(hub, username: str = "user", password: str = "pass"): + host = "localhost" client = hub.lib.docker.from_env() - port = hub.test.container.next_free_port(host) - target_name = f"soluble_test_{hub.lib.uuid.uuid4()}" + port = hub.test.container.next_free_port("localhost") + target_name = f"soluble_agent_{hub.lib.uuid.uuid4()}" pugid = "0" if username == "root" else "1000" container = client.containers.run( @@ -79,7 +78,7 @@ async def create_ssh_target( container.remove() raise RuntimeError("Could not connect to container") - hub.test.TARGETS[target_name] = { + hub.test.ROSTER[target_name] = { "name": target_name, "port": port, "username": username, @@ -87,7 +86,7 @@ async def create_ssh_target( "container": container, } - return hub.test.TARGETS[target_name] + return hub.test.ROSTER[target_name] @contextlib.contextmanager @@ -95,7 +94,7 @@ def roster(hub): """ Return roster file for all created containers """ - roster = hub.test.TARGETS + roster = hub.test.ROSTER with hub.lib.tempfile.NamedTemporaryFile(suffix=".yaml") as fh: hub.lib.yaml.safe_dump(roster, fh) yield fh.name diff --git a/tests/soluble/test_init.py b/tests/soluble/test_init.py index fa28906..8c0fcde 100644 --- a/tests/soluble/test_init.py +++ b/tests/soluble/test_init.py @@ -1,6 +1,11 @@ from unittest import mock -def test_cli(hub): +def test_help(hub): with mock.patch("sys.argv", ["soluble"]): hub.soluble.init.cli() + + +async def test_cli(hub): + await hub.test.container.create() + assert await hub.test.cmd.run("init")