Skip to content

Commit

Permalink
Add basic init test
Browse files Browse the repository at this point in the history
  • Loading branch information
Akm0d committed Aug 28, 2024
1 parent 07c1e6b commit 011c94c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
25 changes: 25 additions & 0 deletions tests/helpers/cmd.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 8 additions & 9 deletions tests/helpers/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def __init__(hub):
hub.test.TARGETS = {}
hub.test.ROSTER = {}


def next_free_port(hub, host, port: int = 2222) -> int:
Expand All @@ -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(
Expand Down Expand Up @@ -79,23 +78,23 @@ 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,
"password": password,
"container": container,
}

return hub.test.TARGETS[target_name]
return hub.test.ROSTER[target_name]


@contextlib.contextmanager
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
7 changes: 6 additions & 1 deletion tests/soluble/test_init.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 011c94c

Please sign in to comment.