Skip to content

Commit 15667ed

Browse files
committed
test(docker): resolve hardcoded pathing for docker UI tests
- Resolved hardcoded `/workspaces/NetAlertX` path in Docker UI tests to use dynamic pathlib resolution, making tests portable across runners. - Improved container hostname resolution safety in Docker UI tests.
1 parent 7f4909d commit 15667ed

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

test/docker_tests/test_ldap_ui.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ def test_ldap_ui_login(tmp_path: pathlib.Path):
5050
"""
5151
project_name = f"netalertx-ldap-{int(time.time())}"
5252

53-
workspace_dir = pathlib.Path("/workspaces/NetAlertX")
54-
if workspace_dir.exists():
55-
base_dir = workspace_dir / "test" / "tmp_ldap_ui"
56-
base_dir.mkdir(parents=True, exist_ok=True)
57-
else:
58-
base_dir = tmp_path
53+
repo_root = pathlib.Path(__file__).resolve().parents[2]
54+
base_dir = tmp_path
5955

6056
# Create test data directories for NetAlertX
6157
_create_test_data_dirs(base_dir)
62-
subprocess.run(["chmod", "-R", "777", str(base_dir / "test_data")])
58+
subprocess.run(["chmod", "-R", "777", str(base_dir / "test_data")], check=True)
6359

6460
# Create LDAP bootstrap directory
6561
ldap_bootstrap_dir = base_dir / "ldap_bootstrap"
@@ -157,14 +153,25 @@ def get_container_name(service):
157153
ldap_container = get_container_name("ldap")
158154

159155
# Connect the test container to the compose network to access the internal IPs directly
160-
# The test container name is usually 'netalertx-test-container' or we can find it via hostname
156+
# We parse the container ID out of /proc/self/cgroup or fallback to hostname
161157
test_container_id = socket.gethostname()
158+
try:
159+
with open("/proc/self/cgroup", "r") as f:
160+
for line in f:
161+
if "docker" in line:
162+
test_container_id = line.split("/")[-1].strip()
163+
break
164+
except Exception:
165+
pass
166+
162167
network_name = f"{project_name}_default"
163168
print(f"Connecting test container {test_container_id} to network {network_name}...")
164169
try:
165-
subprocess.run(["docker", "network", "connect", network_name, test_container_id], check=False)
170+
res = subprocess.run(["docker", "network", "connect", network_name, test_container_id], check=False, capture_output=True, text=True)
171+
if res.returncode != 0 and "already exists" not in res.stderr:
172+
print(f"Warning: failed to connect to network: {res.stderr}")
166173
except Exception as e:
167-
print(f"Failed to connect network: {e}")
174+
print(f"Failed to connect to network {network_name}: {e}")
168175

169176
# Wait for LDAP to become ready
170177
print("Waiting for LDAP server to accept connections...")
@@ -223,8 +230,9 @@ def get_container_name(service):
223230

224231
# Run the selenium test directly from the test container targeting the compose stack's IP
225232
print(f"Running UI test against {container_name} on port 20211...")
233+
ldap_ui_test = repo_root / "test" / "ui" / "test_ui_ldap_login.py"
226234
cmd = [
227-
"pytest", "/workspaces/NetAlertX/test/ui/test_ui_ldap_login.py", "-v", "-s"
235+
"pytest", str(ldap_ui_test), "-v", "-s"
228236
]
229237

230238
env = os.environ.copy()

test/plugins/test_fritzbox.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _fc_with_mac(self, mac):
222222
return fc
223223

224224
def test_returns_device_dict(self):
225-
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"), "fritz.box")
225+
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"))
226226
assert device is not None
227227
assert "mac_address" in device
228228
assert device["hostname"] == "Guest WiFi Network"
@@ -232,13 +232,13 @@ def test_returns_device_dict(self):
232232

233233
def test_guest_mac_has_locally_administered_bit(self):
234234
"""First byte must be 0x02 — locally-administered, unicast."""
235-
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"), "fritz.box")
235+
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"))
236236
first_byte = int(device["mac_address"].split(":")[0], 16)
237237
assert first_byte == 0x02
238238

239239
def test_guest_mac_format_is_valid(self):
240240
"""MAC must be 6 colon-separated lowercase hex pairs."""
241-
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"), "fritz.box")
241+
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"))
242242
parts = device["mac_address"].split(":")
243243
assert len(parts) == 6
244244
for part in parts:
@@ -248,27 +248,27 @@ def test_guest_mac_format_is_valid(self):
248248
def test_guest_mac_is_deterministic(self):
249249
"""Same Fritz!Box MAC must always produce the same guest MAC."""
250250
fc = self._fc_with_mac("AA:BB:CC:DD:EE:FF")
251-
mac1 = fritzbox.create_guest_wifi_device(fc, "fritz.box")["mac_address"]
252-
mac2 = fritzbox.create_guest_wifi_device(fc, "fritz.box")["mac_address"]
251+
mac1 = fritzbox.create_guest_wifi_device(fc)["mac_address"]
252+
mac2 = fritzbox.create_guest_wifi_device(fc)["mac_address"]
253253
assert mac1 == mac2
254254

255255
def test_different_fritzbox_macs_produce_different_guest_macs(self):
256-
mac_a = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:01"), "fritz.box")["mac_address"]
257-
mac_b = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:02"), "fritz.box")["mac_address"]
256+
mac_a = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:01"))["mac_address"]
257+
mac_b = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:02"))["mac_address"]
258258
assert mac_a != mac_b
259259

260260
def test_no_fritzbox_mac_uses_fallback(self):
261261
"""When DeviceInfo returns no MAC, fall back to 02:00:00:00:00:01."""
262262
fc = MagicMock()
263263
fc.call_action.return_value = {"NewMACAddress": ""}
264-
device = fritzbox.create_guest_wifi_device(fc, "fritz.box")
264+
device = fritzbox.create_guest_wifi_device(fc)
265265
assert device["mac_address"] == "02:00:00:00:00:01"
266266

267267
def test_device_info_exception_returns_none(self):
268268
"""If DeviceInfo call raises, create_guest_wifi_device must return None."""
269269
fc = MagicMock()
270270
fc.call_action.side_effect = Exception("Connection refused")
271-
device = fritzbox.create_guest_wifi_device(fc, "fritz.box")
271+
device = fritzbox.create_guest_wifi_device(fc)
272272
assert device is None
273273

274274
def test_known_mac_produces_known_guest_mac(self):
@@ -281,7 +281,7 @@ def test_known_mac_produces_known_guest_mac(self):
281281
digest = hashlib.md5(f"GUEST:{fritzbox_mac}".encode()).digest()
282282
expected = "02:" + ":".join(f"{b:02x}" for b in digest[:5])
283283

284-
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"), "fritz.box")
284+
device = fritzbox.create_guest_wifi_device(self._fc_with_mac("AA:BB:CC:DD:EE:FF"))
285285
assert device["mac_address"] == expected
286286

287287

0 commit comments

Comments
 (0)