Skip to content

Commit

Permalink
use container names to handle multiple at once
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-mattcotter committed Nov 12, 2024
1 parent 28c5dd3 commit cedc8e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
19 changes: 15 additions & 4 deletions integration/scripts/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
if _diagnose_result_is_successful(result):
print(" ✅ observe-agent -> observe validation passed! ")
else:
print(result)
u.print_remote_result(result)
raise ValueError(
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
)


@u.print_test_decorator
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
container_name = u.get_random_test_name()
env_vars["container_name"] = container_name
start.run_test_docker(remote_host, env_vars)

container_id = u.get_docker_container(remote_host)
container_id = u.get_docker_container(remote_host, container_name)
exec_prefix = f"sudo docker exec {container_id} ./observe-agent"
init_command = "{} init-config --token {} --observe_url {}".format(
exec_prefix, env_vars["observe_token"], env_vars["observe_url"]
Expand All @@ -57,13 +59,22 @@ def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:

# Set up correct config with observe url and token
result = remote_host.run_command(init_command)
if result.exited != 0 or result.stderr:
u.print_remote_result(result)
raise ValueError("❌ Error in init-config")

# Restart the process
result = remote_host.run_command(f"sudo docker restart {container_id}")
if result.exited != 0 or result.stderr:
u.print_remote_result(result)
raise ValueError("❌ Error in restart")

# Check diagnose command
result = remote_host.run_command(diagnose_command)
if _diagnose_result_is_successful(result):
print(" ✅ observe-agent -> observe validation passed! ")
else:
print(result)
u.print_remote_result(result)
raise ValueError(
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
)
Expand Down Expand Up @@ -97,7 +108,7 @@ def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
if _diagnose_result_is_successful(result):
print(" ✅ observe-agent -> observe validation passed! ")
else:
print(result)
u.print_remote_result(result)
raise ValueError(
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
)
Expand Down
5 changes: 3 additions & 2 deletions integration/scripts/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:

@u.print_test_decorator
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
docker_prefix = u.get_docker_prefix(remote_host, True)
container_name = env_vars.get("container_name", u.get_random_test_name())
docker_prefix = u.get_docker_prefix(remote_host, container_name, True)
start_command = "start"
start_timeout = 30 # how long to wait for observe-agent to start

Expand All @@ -105,7 +106,7 @@ def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
print("✅ Observe Agent started successfully: " + result.stdout)

# Get Observe Agent Container ID
container_id = u.get_docker_container(remote_host)
container_id = u.get_docker_container(remote_host, container_name)
status_command = f"sudo docker exec {container_id} ./observe-agent status"

# Check Agent Status
Expand Down
2 changes: 1 addition & 1 deletion integration/scripts/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:

@u.print_test_decorator
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
docker_prefix = u.get_docker_prefix(remote_host, False)
docker_prefix = u.get_docker_prefix(remote_host, u.get_random_test_name(), False)
config_file_linux = "/etc/observe-agent/observe-agent.yaml"
version_pattern = re.compile(r"^\d+\.\d+\.\d+(-[A-Za-z0-9-]+)?$")
home_dir = "/home/{}".format(env_vars["user"])
Expand Down
58 changes: 30 additions & 28 deletions integration/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from paramiko.ssh_exception import AuthenticationException, NoValidConnectionsError

import os
import random
import sys
import time

Expand Down Expand Up @@ -247,6 +248,12 @@ def test_conection(self, timeout=60) -> None:
raise RuntimeError(" ❌ The SSH connection failed")


def get_random_test_name() -> str:
random.seed(time.time())
num_digits = 6
return "test-" + str(random.randint(10 ** (num_digits - 1), (10**num_digits) - 1))


def get_docker_image(remote_host: Host) -> str:
result = remote_host.run_command(
'sudo docker images --format "{{.Repository}}:{{.Tag}}"'
Expand All @@ -258,7 +265,7 @@ def get_docker_image(remote_host: Host) -> str:
return images[0]


def get_docker_prefix(remote_host: Host, detach: bool) -> str:
def get_docker_prefix(remote_host: Host, name: str, detach: bool) -> str:
image = get_docker_image(remote_host)
return f'sudo docker run {"-d --restart always" if detach else ""} \
--mount type=bind,source=/proc,target=/hostfs/proc,readonly \
Expand All @@ -268,41 +275,36 @@ def get_docker_prefix(remote_host: Host, detach: bool) -> str:
--mount type=bind,source=/var/log,target=/hostfs/var/log,readonly \
--mount type=bind,source=/var/lib/docker/containers,target=/var/lib/docker/containers,readonly \
--mount type=bind,source=$(pwd)/observe-agent.yaml,target=/etc/observe-agent/observe-agent.yaml \
--name {name} \
--pid host {image}'


def get_docker_container(remote_host: Host, retries: int = 3) -> str:
get_container_command = 'sudo docker ps --filter "status=running" --format "{{.ID}} {{.Image}} {{.CreatedAt}}"'
def get_docker_container(remote_host: Host, name: str) -> str:
get_container_command = (
'sudo docker ps --filter "status=running" --filter "name='
+ name
+ '" --format "{{.ID}} {{.Image}} {{.CreatedAt}}"'
)
result = remote_host.run_command(get_container_command)
running = [
line.strip() for line in result.stdout.splitlines() if "SNAPSHOT" in line
]
if len(running) == 0:
if retries > 0 and result.stderr == "":
output = result.stdout.strip()
print(
"❌ Error in finding observe-agent container; retrying in 1s..."
+ ("\n" + output if output != "" else ""),
file=sys.stderr,
)
time.sleep(1)
return get_docker_container(remote_host, retries - 1)
elif retries == 0:
# No retries left, see if we can get logs from stopped containers to help debug.
result = remote_host.run_command('sudo docker ps --format "{{.ID}}"')
if result.stdout != "":
container_ids = result.stdout.splitlines()
for container_id in container_ids:
print(
"Logs for container {}:".format(container_id),
file=sys.stderr,
)
result = remote_host.run_command(
"sudo docker logs {}".format(container_id)
)
print_remote_result(result)
else:
# No container matched our filter. Get logs from all containers to help debug.
result = remote_host.run_command('sudo docker ps --format "{{.ID}}"')
if result.stdout != "":
container_ids = result.stdout.splitlines()
for container_id in container_ids:
print(
"Logs for container {}:".format(container_id),
file=sys.stderr,
)
result = remote_host.run_command(
"sudo docker logs {}".format(container_id)
)
print_remote_result(result)
else:
print_remote_result(result)
die(
"❌ Error in finding observe-agent container; command output:\n{}\ncommand error:\n{}".format(
result.stdout or "<empty>",
Expand All @@ -312,7 +314,7 @@ def get_docker_container(remote_host: Host, retries: int = 3) -> str:
return ""
if len(running) > 1:
die(
"❌ Error in finding observe-agent container, to many snapshots running:\n"
"❌ Error in finding observe-agent container, too many snapshots running:\n"
+ result.stdout
)
# Only one snapshot running; return the ID from the first line.
Expand Down

0 comments on commit cedc8e1

Please sign in to comment.