-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_docker.py
120 lines (99 loc) · 3.62 KB
/
test_docker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
import subprocess
import time
import requests
import sys
DOCKER_COMPOSE_FILE = "compose.yaml"
BEACON_PORT = 8088
SMTP4DEV_PORT = 5080
def run_compose_cmd(*args):
"""Helper to run a docker compose command."""
cmd = ["docker", "compose", "-f", DOCKER_COMPOSE_FILE] + list(args)
print(f"> Running: {' '.join(cmd)}")
subprocess.check_call(cmd)
# todo: refactor
def main():
try:
print("Starting Beacon test.")
print("---------------------")
run_compose_cmd("down")
run_compose_cmd("up", "-d", "--build")
print("Test API...")
print("---------------------")
for _ in range(10):
try:
resp = requests.get(f"http://localhost:{BEACON_PORT}")
resp.raise_for_status()
print("Beacon Up")
break
except requests.RequestException as ex:
print("Waiting for Beacon to be ready...", ex)
time.sleep(1)
else:
print("Beacon did not become ready in time.")
sys.exit(1)
# POST request
print("Sending POST to /services/sly-fox/beat")
post_url = f"http://localhost:{BEACON_PORT}/services/sly-fox/beat"
resp = requests.post(post_url)
resp.raise_for_status()
print("POST succeeded!")
# GET request
print("Sending GET to /services/sly-fox/status")
get_url = f"http://localhost:{BEACON_PORT}/services/sly-fox/status"
resp = requests.get(get_url)
resp.raise_for_status()
print("GET succeeded! Response:")
print(resp.text)
print("Test Sending report...")
print("---------------------")
# Wait for smtp4dev to be up
smtp_health_url = f"http://localhost:{SMTP4DEV_PORT}/api/Version"
for _ in range(10):
try:
resp = requests.get(smtp_health_url)
resp.raise_for_status()
print("SMTP Up")
break
except requests.RequestException:
print("Waiting for smtp4dev to be ready...")
time.sleep(1)
else:
print("smtp4dev did not become ready in time.")
sys.exit(1)
# Count emails before
email_count_before = get_email_count()
print(f"Email count before: {email_count_before}")
# Trigger the report in the container
# "docker compose exec beacon /app/beacon report --config /app/beacon.yaml"
run_compose_cmd("exec", "beacon", "/app/beacon", "report", "--config", "/app/beacon.yaml")
# Count emails after
email_count_after = get_email_count()
print(f"Email count after: {email_count_after}")
if email_count_before == email_count_after:
print("Testing: FAIL - email not sent")
print("-------------")
sys.exit(1)
else:
print("Testing: SUCCESS")
print("----------------")
sys.exit(0)
except subprocess.CalledProcessError as e:
print(f"Command failed: {e}")
sys.exit(1)
except requests.RequestException as e:
print(f"HTTP request failed: {e}")
sys.exit(1)
def get_email_count():
"""
Fetches the list of messages from smtp4dev and counts
how many contain 'you@example.fake' in the message data.
"""
smtp_messages_url = f"http://localhost:{SMTP4DEV_PORT}/api/Messages"
resp = requests.get(smtp_messages_url)
resp.raise_for_status()
messages_json = resp.json()
results = messages_json["results"]
return len(results)
if __name__ == "__main__":
main()