Skip to content

Commit 209faa7

Browse files
committed
test: attempt to stabilize syslog hostname filtering
- Refactor to use a common function for all messages - Use until() to retry check_log() Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 9386f00 commit 209faa7

File tree

1 file changed

+44
-35
lines changed
  • test/case/syslog/hostname_filter

1 file changed

+44
-35
lines changed

test/case/syslog/hostname_filter/test.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"""
88

99
import infamy
10-
import time
10+
import infamy.iface as iface
11+
from infamy import until
1112

1213
TEST_MESSAGES = [
1314
("router1", "Message from router1"),
@@ -16,6 +17,27 @@
1617
("other", "Message from a different host"),
1718
]
1819

20+
def verify_log_content(ssh, logfile, expected_hosts, test):
21+
"""Verify log file contains only messages from expected hosts."""
22+
def check_log():
23+
rc = ssh.runsh(f"cat /var/log/{logfile} 2>/dev/null")
24+
log_content = rc.stdout if rc.returncode == 0 else ""
25+
26+
expected_messages = [msg for host, msg in TEST_MESSAGES if host in expected_hosts]
27+
missing = [msg for msg in expected_messages if msg not in log_content]
28+
if missing:
29+
return False
30+
31+
if expected_hosts: # Only check for unwanted if filtering by hostname
32+
unwanted_messages = [msg for host, msg in TEST_MESSAGES if host not in expected_hosts]
33+
found = [msg for msg in unwanted_messages if msg in log_content]
34+
if found:
35+
test.fail(f"Found unwanted messages in /var/log/{logfile}: {found}")
36+
37+
return True
38+
39+
until(check_log, attempts=20)
40+
1941
with infamy.Test() as test:
2042
with test.step("Set up topology and attach to DUTs"):
2143
env = infamy.Env()
@@ -91,6 +113,15 @@
91113
}
92114
})
93115

116+
with test.step("Wait for server interface to be operational"):
117+
until(lambda: iface.get_param(server, server_link, "oper-status") == "up", attempts=20)
118+
119+
with test.step("Verify server IP address is configured"):
120+
until(lambda: iface.address_exist(server, server_link, "10.0.0.1", proto="static"), attempts=20)
121+
122+
with test.step("Verify syslog server is listening on UDP port 514"):
123+
until(lambda: "10.0.0.1:514" in serverssh.runsh("ss -ulnp 2>/dev/null | grep :514 || true").stdout, attempts=20)
124+
94125
with test.step("Configure client to forward logs to server"):
95126
_, client_link = env.ltop.xlate("client", "link")
96127

@@ -131,49 +162,27 @@
131162
}
132163
})
133164

134-
time.sleep(2)
165+
with test.step("Wait for client interface to be operational"):
166+
until(lambda: iface.get_param(client, client_link, "oper-status") == "up", attempts=20)
167+
168+
with test.step("Verify client IP address is configured"):
169+
until(lambda: iface.address_exist(client, client_link, "10.0.0.2", proto="static"), attempts=20)
170+
171+
with test.step("Verify network connectivity between client and server"):
172+
until(lambda: clientssh.runsh("ping -c 1 -W 1 10.0.0.1 >/dev/null 2>&1").returncode == 0, attempts=10)
135173

136174
with test.step("Send log messages with different hostnames"):
137175
for hostname, message in TEST_MESSAGES:
138176
clientssh.runsh(f"logger -t test -p daemon.info -H {hostname} -h 10.0.0.1 '{message}'")
139-
time.sleep(2)
140177

141178
with test.step("Verify router1 log contains only router1 messages"):
142-
rc = serverssh.runsh("cat /var/log/router1 2>/dev/null")
143-
log_content = rc.stdout if rc.returncode == 0 else ""
144-
145-
router1_messages = [msg for host, msg in TEST_MESSAGES if host == "router1"]
146-
missing = [msg for msg in router1_messages if msg not in log_content]
147-
if missing:
148-
test.fail(f"Missing router1 messages in /var/log/router1: {missing}")
149-
150-
unwanted_messages = [msg for host, msg in TEST_MESSAGES if host != "router1"]
151-
found = [msg for msg in unwanted_messages if msg in log_content]
152-
if found:
153-
test.fail(f"Found unwanted messages in /var/log/router1: {found}")
179+
verify_log_content(serverssh, "router1", ["router1"], test)
154180

155181
with test.step("Verify router2 log contains only router2 messages"):
156-
rc = serverssh.runsh("cat /var/log/router2 2>/dev/null")
157-
log_content = rc.stdout if rc.returncode == 0 else ""
158-
159-
router2_messages = [msg for host, msg in TEST_MESSAGES if host == "router2"]
160-
missing = [msg for msg in router2_messages if msg not in log_content]
161-
if missing:
162-
test.fail(f"Missing router2 messages in /var/log/router2: {missing}")
163-
164-
unwanted_messages = [msg for host, msg in TEST_MESSAGES if host != "router2"]
165-
found = [msg for msg in unwanted_messages if msg in log_content]
166-
if found:
167-
test.fail(f"Found unwanted messages in /var/log/router2: {found}")
182+
verify_log_content(serverssh, "router2", ["router2"], test)
168183

169184
with test.step("Verify all-hosts log contains all messages"):
170-
rc = serverssh.runsh("cat /var/log/all-hosts 2>/dev/null")
171-
log_content = rc.stdout if rc.returncode == 0 else ""
172-
173-
expected_messages = [msg for _, msg in TEST_MESSAGES]
174-
missing = [msg for msg in expected_messages if msg not in log_content]
175-
176-
if missing:
177-
test.fail(f"Missing messages in /var/log/all-hosts: {missing}")
185+
all_hosts = [host for host, _ in TEST_MESSAGES]
186+
verify_log_content(serverssh, "all-hosts", all_hosts, test)
178187

179188
test.succeed()

0 commit comments

Comments
 (0)