|
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import infamy |
10 | | -import time |
| 10 | +import infamy.iface as iface |
| 11 | +from infamy import until |
11 | 12 |
|
12 | 13 | TEST_MESSAGES = [ |
13 | 14 | ("router1", "Message from router1"), |
|
16 | 17 | ("other", "Message from a different host"), |
17 | 18 | ] |
18 | 19 |
|
| 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 | + |
19 | 41 | with infamy.Test() as test: |
20 | 42 | with test.step("Set up topology and attach to DUTs"): |
21 | 43 | env = infamy.Env() |
|
91 | 113 | } |
92 | 114 | }) |
93 | 115 |
|
| 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 | + |
94 | 125 | with test.step("Configure client to forward logs to server"): |
95 | 126 | _, client_link = env.ltop.xlate("client", "link") |
96 | 127 |
|
|
131 | 162 | } |
132 | 163 | }) |
133 | 164 |
|
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) |
135 | 173 |
|
136 | 174 | with test.step("Send log messages with different hostnames"): |
137 | 175 | for hostname, message in TEST_MESSAGES: |
138 | 176 | clientssh.runsh(f"logger -t test -p daemon.info -H {hostname} -h 10.0.0.1 '{message}'") |
139 | | - time.sleep(2) |
140 | 177 |
|
141 | 178 | 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) |
154 | 180 |
|
155 | 181 | 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) |
168 | 183 |
|
169 | 184 | 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) |
178 | 187 |
|
179 | 188 | test.succeed() |
0 commit comments