Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
The tests are more functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Akm0d committed Aug 30, 2024
1 parent b568e1f commit 91c3143
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/soluble/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/soluble/salt/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/soluble/soluble/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/cmd.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
49 changes: 23 additions & 26 deletions tests/helpers/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand Down
2 changes: 0 additions & 2 deletions tests/soluble/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 91c3143

Please sign in to comment.