Skip to content

Commit 54c311d

Browse files
split up diagnose from init-config
1 parent cb8bea2 commit 54c311d

File tree

10 files changed

+131
-127
lines changed

10 files changed

+131
-127
lines changed

integration/scripts/test_configure.py

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,45 @@
11
#!/usr/bin/env python3
2-
from fabric import Result
3-
4-
import re
52
import utils as u
6-
import test_start as start
7-
8-
9-
def _diagnose_result_is_successful(result: Result) -> bool:
10-
return re.search(r"All \d+ checks passed", result.stdout) is not None
113

124

135
@u.print_test_decorator
146
def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
15-
"""
16-
Test to validate connection of observe-agent to Observe
17-
18-
Args:
19-
remote_host (Host): instance to ssh into
20-
env_vars (dict): environment variables passed into for testing
21-
22-
Raises:
23-
ValueError: Something failed with initial config or observe-agent -> observe connection
24-
"""
25-
267
init_command = r'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent init-config --token {} --observe_url {}'.format(
278
env_vars["observe_token"], env_vars["observe_url"]
289
)
29-
diagnose_command = r'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent diagnose'
3010

3111
# Set up correct config with observe url and token
3212
result = remote_host.run_command(init_command)
33-
34-
start.run_test_windows(remote_host, env_vars)
35-
36-
# Check diagnose command
37-
result = remote_host.run_command(diagnose_command)
38-
if _diagnose_result_is_successful(result):
39-
print(" ✅ observe-agent -> observe validation passed! ")
40-
else:
13+
if result.exited != 0 or result.stderr:
4114
u.print_remote_result(result)
42-
raise ValueError(
43-
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
44-
)
15+
raise ValueError("❌ Error in init-config")
4516

4617

4718
@u.print_test_decorator
4819
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
49-
docker_prefix = u.get_docker_prefix(remote_host, u.get_random_test_name(), False)
50-
result = remote_host.run_command(docker_prefix + " config")
51-
u.print_remote_result(result)
52-
u.die("❌ skipping docker test")
53-
container_name = u.get_random_test_name()
54-
env_vars["container_name"] = container_name
55-
start.run_test_docker(remote_host, env_vars)
56-
57-
container_id = u.get_docker_container(remote_host, container_name)
58-
exec_prefix = f"sudo docker exec {container_id} ./observe-agent"
20+
docker_prefix = u.get_docker_prefix(remote_host, False)
5921
init_command = "{} init-config --token {} --observe_url {}".format(
60-
exec_prefix, env_vars["observe_token"], env_vars["observe_url"]
22+
docker_prefix, env_vars["observe_token"], env_vars["observe_url"]
6123
)
62-
diagnose_command = exec_prefix + " diagnose"
6324

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

70-
# Restart the process
71-
result = remote_host.run_command(f"sudo docker restart {container_id}")
72-
if result.exited != 0 or result.stderr:
73-
u.print_remote_result(result)
74-
raise ValueError("❌ Error in restart")
75-
76-
# Check diagnose command
77-
result = remote_host.run_command(diagnose_command)
78-
if _diagnose_result_is_successful(result):
79-
print(" ✅ observe-agent -> observe validation passed! ")
80-
else:
81-
u.print_remote_result(result)
82-
raise ValueError(
83-
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
84-
)
85-
8631

8732
@u.print_test_decorator
8833
def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
89-
"""
90-
Test to validate connection of observe-agent to Observe
91-
92-
Args:
93-
remote_host (Host): instance to ssh into
94-
env_vars (dict): environment variables passed into for testing
95-
96-
Raises:
97-
ValueError: Something failed with initial config or observe-agent -> observe connection
98-
"""
99-
10034
init_command = "sudo observe-agent init-config --token {} --observe_url {}".format(
10135
env_vars["observe_token"], env_vars["observe_url"]
10236
)
103-
diagnose_command = "observe-agent diagnose"
10437

10538
# Set up correct config with observe url and token
10639
result = remote_host.run_command(init_command)
107-
108-
start.run_test_linux(remote_host, env_vars)
109-
110-
# Check diagnose command
111-
result = remote_host.run_command(diagnose_command)
112-
if _diagnose_result_is_successful(result):
113-
print(" ✅ observe-agent -> observe validation passed! ")
114-
else:
40+
if result.exited != 0 or result.stderr:
11541
u.print_remote_result(result)
116-
raise ValueError(
117-
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
118-
)
42+
raise ValueError("❌ Error in init-config")
11943

12044

12145
if __name__ == "__main__":

integration/scripts/test_diagnose.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
from fabric import Result
3+
4+
import re
5+
import utils as u
6+
7+
8+
def _check_diagnose_result(result: Result) -> bool:
9+
passed = re.search(r"All \d+ checks passed", result.stdout) is not None
10+
if passed:
11+
print(" ✅ observe-agent -> observe validation passed! ")
12+
else:
13+
u.print_remote_result(result)
14+
raise ValueError(
15+
f"❌ Failed: observe-agent -> observe validation (regex on diagnose output did not match)"
16+
)
17+
18+
19+
@u.print_test_decorator
20+
def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
21+
diagnose_command = r'Set-Location "C:\Program Files\Observe\observe-agent"; ./observe-agent diagnose'
22+
23+
# Check diagnose command
24+
result = remote_host.run_command(diagnose_command)
25+
_check_diagnose_result(result)
26+
27+
28+
@u.print_test_decorator
29+
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
30+
container_id = u.get_docker_container(remote_host)
31+
exec_prefix = f"sudo docker exec {container_id} ./observe-agent"
32+
diagnose_command = exec_prefix + " diagnose"
33+
34+
# Check diagnose command
35+
result = remote_host.run_command(diagnose_command)
36+
_check_diagnose_result(result)
37+
38+
39+
@u.print_test_decorator
40+
def run_test_linux(remote_host: u.Host, env_vars: dict) -> None:
41+
diagnose_command = "observe-agent diagnose"
42+
43+
# Check diagnose command
44+
result = remote_host.run_command(diagnose_command)
45+
_check_diagnose_result(result)
46+
47+
48+
if __name__ == "__main__":
49+
50+
env_vars = u.get_env_vars(need_observe=True)
51+
remote_host = u.Host(
52+
host_ip=env_vars["host"],
53+
username=env_vars["user"],
54+
key_file_path=env_vars["key_filename"],
55+
password=env_vars["password"],
56+
)
57+
58+
# Test SSH Connection before starting test of interest
59+
remote_host.test_conection(int(env_vars["machine_config"]["sleep"]))
60+
61+
if (
62+
"redhat" in env_vars["machine_config"]["distribution"]
63+
or "debian" in env_vars["machine_config"]["distribution"]
64+
):
65+
run_test_linux(remote_host, env_vars)
66+
elif "windows" in env_vars["machine_config"]["distribution"]:
67+
run_test_windows(remote_host, env_vars)
68+
elif "docker" in env_vars["machine_config"]["distribution"]:
69+
run_test_docker(remote_host, env_vars)

integration/scripts/test_start.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ def _check_status_loop(
2525
agent_status = False
2626
for _ in range(start_timeout):
2727
metrics_dict = defaultdict(list)
28-
result = remote_host.run_command(status_command)
28+
try:
29+
result = remote_host.run_command(status_command)
30+
except Exception as e:
31+
print("Ignoring exception: ", e)
32+
time.sleep(1)
33+
continue
2934
for line in result.stdout.splitlines():
3035
if ":" in line:
3136
metric, value = line.split(":", 1)
@@ -37,13 +42,12 @@ def _check_status_loop(
3742
print("✅ Observe Agent is active and running without errors!")
3843
agent_status = True
3944
break
40-
else:
41-
print(
42-
"❌ Observe Agent is not running. Retry Count is {}/{}...".format(
43-
_ + 1, start_timeout
44-
)
45+
print(
46+
"❌ Observe Agent is not running. Retry Count is {}/{}...".format(
47+
_ + 1, start_timeout
4548
)
46-
time.sleep(1)
49+
)
50+
time.sleep(1)
4751
return agent_status
4852

4953

@@ -93,9 +97,7 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
9397

9498
@u.print_test_decorator
9599
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
96-
u.upload_default_docker_config(env_vars, remote_host)
97-
container_name = env_vars.get("container_name", u.get_random_test_name())
98-
docker_prefix = u.get_docker_prefix(remote_host, container_name, True)
100+
docker_prefix = u.get_docker_prefix(remote_host, True)
99101
start_command = "start"
100102
start_timeout = 30 # how long to wait for observe-agent to start
101103

@@ -107,7 +109,7 @@ def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
107109
print("✅ Observe Agent started successfully: " + result.stdout)
108110

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

113115
# Check Agent Status

integration/scripts/test_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def run_test_windows(remote_host: u.Host, env_vars: dict) -> None:
7171
@u.print_test_decorator
7272
def run_test_docker(remote_host: u.Host, env_vars: dict) -> None:
7373
u.upload_default_docker_config(env_vars, remote_host)
74-
docker_prefix = u.get_docker_prefix(remote_host, u.get_random_test_name(), False)
74+
docker_prefix = u.get_docker_prefix(remote_host, False)
7575
config_file_linux = "/etc/observe-agent/observe-agent.yaml"
7676
version_pattern = re.compile(r"^\d+\.\d+\.\d+(-[A-Za-z0-9-]+)?$")
7777

integration/scripts/utils.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from paramiko.ssh_exception import AuthenticationException, NoValidConnectionsError
66

77
import os
8-
import random
98
import sys
109
import time
1110

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

250249

251-
def get_random_test_name() -> str:
252-
random.seed(time.time())
253-
num_digits = 6
254-
return "test-" + str(random.randint(10 ** (num_digits - 1), (10**num_digits) - 1))
255-
256-
257250
def get_docker_image(remote_host: Host) -> str:
258251
result = remote_host.run_command(
259252
'sudo docker images --format "{{.Repository}}:{{.Tag}}"'
@@ -265,17 +258,16 @@ def get_docker_image(remote_host: Host) -> str:
265258
return images[0]
266259

267260

268-
def get_docker_prefix(remote_host: Host, name: str, detach: bool) -> str:
261+
def get_docker_prefix(remote_host: Host, detach: bool) -> str:
269262
image = get_docker_image(remote_host)
270-
return f'sudo docker run {"-d --restart always" if detach else ""} \
263+
return f'sudo docker run {"-d --restart on-failure" if detach else ""} \
271264
--mount type=bind,source=/proc,target=/hostfs/proc,readonly \
272265
--mount type=bind,source=/snap,target=/hostfs/snap,readonly \
273266
--mount type=bind,source=/boot,target=/hostfs/boot,readonly \
274267
--mount type=bind,source=/var/lib,target=/hostfs/var/lib,readonly \
275268
--mount type=bind,source=/var/log,target=/hostfs/var/log,readonly \
276269
--mount type=bind,source=/var/lib/docker/containers,target=/var/lib/docker/containers,readonly \
277270
--mount type=bind,source=$(pwd)/observe-agent.yaml,target=/etc/observe-agent/observe-agent.yaml \
278-
--name {name} \
279271
--pid host {image}'
280272

281273

@@ -290,12 +282,8 @@ def upload_default_docker_config(env_vars: dict, remote_host: Host) -> None:
290282
remote_host.put_file(local_path=observe_agent_file_path, remote_path=home_dir)
291283

292284

293-
def get_docker_container(remote_host: Host, name: str) -> str:
294-
get_container_command = (
295-
'sudo docker ps --filter "status=running" --filter "name='
296-
+ name
297-
+ '" --format "{{.ID}} {{.Image}} {{.CreatedAt}}"'
298-
)
285+
def get_docker_container(remote_host: Host) -> str:
286+
get_container_command = 'sudo docker ps --filter "status=running" --format "{{.ID}} {{.Image}} {{.CreatedAt}}"'
299287
result = remote_host.run_command(get_container_command)
300288
running = [
301289
line.strip() for line in result.stdout.splitlines() if "SNAPSHOT" in line

integration/tests/integration.tftest.hcl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ run "setup_observe_variables" {
1515
}
1616

1717

18-
1918
run "test_ec2_connection" {
2019
module {
2120
source = "observeinc/collection/aws//modules/testing/exec"
@@ -41,8 +40,6 @@ run "test_ec2_connection" {
4140
}
4241

4342

44-
45-
4643
run "test_install" {
4744
module {
4845
source = "observeinc/collection/aws//modules/testing/exec"
@@ -68,8 +65,6 @@ run "test_install" {
6865
}
6966

7067

71-
72-
7368
run "test_version" {
7469
module {
7570
source = "observeinc/collection/aws//modules/testing/exec"
@@ -95,8 +90,6 @@ run "test_version" {
9590
}
9691

9792

98-
99-
10093
run "test_configure" {
10194
module {
10295
source = "observeinc/collection/aws//modules/testing/exec"
@@ -123,6 +116,7 @@ run "test_configure" {
123116
}
124117
}
125118

119+
126120
run "test_start" {
127121
module {
128122
source = "observeinc/collection/aws//modules/testing/exec"
@@ -147,3 +141,27 @@ run "test_start" {
147141
}
148142
}
149143

144+
145+
run "test_diagnose" {
146+
module {
147+
source = "observeinc/collection/aws//modules/testing/exec"
148+
version = "2.9.0"
149+
}
150+
151+
variables {
152+
command = "python3 ./scripts/test_diagnose.py"
153+
env_vars = {
154+
HOST = run.setup_ec2.public_ip
155+
USER = run.setup_ec2.user_name
156+
KEY_FILENAME = run.setup_ec2.private_key_path
157+
PASSWORD = run.setup_ec2.password
158+
MACHINE_NAME = run.setup_ec2.machine_name
159+
MACHINE_CONFIG = run.setup_ec2.machine_config
160+
}
161+
}
162+
163+
assert {
164+
condition = output.error == ""
165+
error_message = "Error in Diagnose Test"
166+
}
167+
}

0 commit comments

Comments
 (0)