From 91c3143a2f7dd3cece1a4c624a4f48536f823073 Mon Sep 17 00:00:00 2001 From: Tyler Levy Conde Date: Fri, 30 Aug 2024 14:29:23 -0600 Subject: [PATCH] The tests are more functional --- src/soluble/conf.py | 3 +-- src/soluble/salt/ssh.py | 2 +- src/soluble/soluble/init.py | 4 +-- tests/helpers/cmd.py | 3 ++- tests/helpers/container.py | 49 +++++++++++++++++-------------------- tests/soluble/test_init.py | 2 -- 6 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/soluble/conf.py b/src/soluble/conf.py index 1c465c8..807cd32 100644 --- a/src/soluble/conf.py +++ b/src/soluble/conf.py @@ -9,8 +9,7 @@ for opt in ssh_parser._get_all_options() if opt.dest and not any( - key in opt.dest - for key in ("log", "out", "crash", "version", "color", "python2") + key in opt.dest for key in ("log", "out", "version", "color", "python2") ) } diff --git a/src/soluble/salt/ssh.py b/src/soluble/salt/ssh.py index 1121667..431a2c2 100644 --- a/src/soluble/salt/ssh.py +++ b/src/soluble/salt/ssh.py @@ -11,7 +11,7 @@ async def run_command( cmd = hub.lib.shutil.which("salt-ssh") assert cmd, "Could not find salt-ssh" - full_command = f"{cmd} '{target}' --roster-file={roster} {command} --log-level=quiet --hard-crash {options}" + full_command = f"{cmd} '{target}' --roster-file={roster} {command} --log-level={hub.OPT.pop_config.log_level} {options}" if capture_output: full_command += " --no-color --out=json" if config_dir: diff --git a/src/soluble/soluble/init.py b/src/soluble/soluble/init.py index f1d2960..8396a55 100644 --- a/src/soluble/soluble/init.py +++ b/src/soluble/soluble/init.py @@ -114,14 +114,14 @@ async def setup(hub, name: str): hub.log.info("Soluble setup") await hub.salt.ssh.run_command( name, - f"test.ping", + f"-H", ) async def run(hub, name: str) -> int: """This is where a soluble plugin runs its primary function""" hub.log.info("Soluble run") - await hub.salt.ssh.run_command(name, f"test.ping", capture_output=False) + await hub.salt.ssh.run_command(name, f"grains.items", capture_output=False) return 0 diff --git a/tests/helpers/cmd.py b/tests/helpers/cmd.py index 9d911a4..f971558 100644 --- a/tests/helpers/cmd.py +++ b/tests/helpers/cmd.py @@ -1,6 +1,7 @@ async def run(hub, subcommand: str, target: str = "*", *args): with hub.test.container.roster() as rf: - command = f"{hub.lib.sys.executable} -m soluble --ssh-option='-o StrictHostKeyChecking=no' --log-level=debug --salt-config-dir {hub.test.SALT_CONFIG_DIR} -R {rf} {subcommand} '{target}' {' '.join(args)}" + command = f"{hub.lib.sys.executable} -m soluble --no-host-keys -i --log-level=debug --salt-config-dir {hub.test.SALT_CONFIG_DIR} -R {rf} {subcommand} '{target}' {' '.join(args)}" + print(f"Running command: {command}") # Run the command asynchronously process = await hub.lib.asyncio.create_subprocess_shell( diff --git a/tests/helpers/container.py b/tests/helpers/container.py index 7d423ce..9de7afc 100644 --- a/tests/helpers/container.py +++ b/tests/helpers/container.py @@ -23,46 +23,41 @@ def next_free_port(hub, host, port: int = 2222) -> int: return port -async def create(hub, username: str = "user", password: str = "pass"): +async def create(hub, username: str = "root", password: str = "pass"): host = "localhost" client = hub.lib.docker.from_env() 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( - "linuxserver/openssh-server:latest", - command=["/bin/sh", "-c", "while true; do sleep 1; done"], + "python:3.10-slim", + command=[ + "/bin/sh", + "-c", + f""" + apt-get update && \ + apt-get install -y openssh-server && \ + echo "PermitRootLogin yes" >> /etc/ssh/sshd_config && \ + service ssh restart && \ + echo 'root:{password}' | chpasswd && \ + while true; do sleep 1; done + """, + ], detach=True, - ports={"2222/tcp": port}, + ports={"22/tcp": port}, hostname=target_name, network="bridge", environment={ - "PUID": pugid, - "PGID": pugid, - "TZ": "Etc/UTC", - "SUDO_ACCESS": "true", - "PASSWORD_ACCESS": "true", - "USER_NAME": username, + "user": username, "USER_PASSWORD": password, }, ) - - # Enable SSH Tunneling - container.exec_run( - cmd="sed -i 's/^AllowTcpForwarding no/AllowTcpForwarding yes/' /etc/ssh/sshd_config", - privileged=True, - detach=True, - ) - if username == "root": - container.exec_run( - cmd="sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config", - privileged=True, - detach=True, - ) + print(f"Created container: {container.id}") # Wait for SSH service to be available + print(f"Trying to connect to container: {container.id}", end="") for _ in range(60): + print(".", end="") try: async with hub.lib.asyncssh.connect( host=host, @@ -78,13 +73,15 @@ async def create(hub, username: str = "user", password: str = "pass"): container.stop() container.remove() raise RuntimeError("Could not connect to container") + print("\nSuccess!") hub.test.ROSTER[target_name] = container hub.test.ROSTER[target_name] = { "host": "localhost", "port": port, - "username": username, - "password": password, + "user": username, + "passwd": password, + "minion_opts": {"master": "localhost"}, } return hub.test.ROSTER[target_name] diff --git a/tests/soluble/test_init.py b/tests/soluble/test_init.py index f73d2ac..6129673 100644 --- a/tests/soluble/test_init.py +++ b/tests/soluble/test_init.py @@ -7,7 +7,5 @@ def test_help(hub): async def test_cli(hub, salt_master): - print("creating container") await hub.test.container.create() - print("running command") await hub.test.cmd.run("init")