STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_generate_dhcp_config.py
Go to the documentation of this file.
1"""generate_dhcp_config.py: what star-dhcp-sync runs as root on every config save.
2
3Run: python3 -m pytest daq-server/deploy/dhcp -q
4"""
5import ipaddress
6from pathlib import Path
7
8import pytest
9
10import generate_dhcp_config as g
11
12SUBNET = ipaddress.ip_network("192.168.2.0/24")
13
14
15def board(ip, mac, board_id=None):
16 b = {"ip": ip, "mac": mac}
17 if board_id is not None:
18 b["board_id"] = board_id
19 return b
20
21
22def cfg(**boards):
23 return {"boards": boards}
24
25
27 # The same vectors as test_board_net.cpp and ota_upload.py --self-test.
28 assert g.recovery_address("4a:27:e2:3a:ad:71") == "169.254.174.113"
29 assert g.recovery_address("00:00:00:00:00:00") == "169.254.1.0"
30 assert g.recovery_address("00:00:00:00:fe:ff") == "169.254.1.255"
31 assert g.recovery_address("00:00:00:00:ff:01") == "169.254.2.1"
32
33
35 # 0x00 and 0xfe both map to third octet 1; same last byte.
36 c = cfg(a=board("192.168.2.21", "aa:aa:aa:aa:00:10"),
37 b=board("192.168.2.22", "bb:bb:bb:bb:fe:10"))
38 with pytest.raises(SystemExit):
39 g.collect_reservations(c, SUBNET)
40
41
43 # The board takes its ID from its address, so the server would file it wrong.
44 with pytest.raises(SystemExit):
45 g.collect_reservations(cfg(rtd=board("192.168.2.31", "4a:27:e2:3a:ad:71", 32)), SUBNET)
46 ok = g.collect_reservations(cfg(rtd=board("192.168.2.31", "4a:27:e2:3a:ad:71", 31)), SUBNET)
47 assert ok == [("rtd", "4a:27:e2:3a:ad:71", "192.168.2.31")]
48
49
50IP_ADDR = """\
511: lo inet 127.0.0.1/8 scope host lo\\ valid_lft forever preferred_lft forever
522: enp4s0 inet 192.168.2.20/24 brd 192.168.2.255 scope global enp4s0\\ valid_lft forever
532: enp4s0 inet 169.254.0.20/16 scope global enp4s0\\ valid_lft forever
543: wlx0cef15768c39 inet 10.0.0.5/24 brd 10.0.0.255 scope global dynamic wlx0cef15768c39
55"""
56
57
59 # Not the laptop adapter name that used to be hardcoded.
60 assert g.interfaces_in_subnet(IP_ADDR, SUBNET) == ["enp4s0"]
61 assert g.interfaces_in_subnet(IP_ADDR, ipaddress.ip_network("172.16.0.0/16")) == []
62
63
64CONFIG = """
65[boards.rtd_board]
66ip = "192.168.2.31"
67board_id = 31
68mac = "4a:27:e2:3a:ad:71"
69"""
70
71
73 c = tmp_path / "config.toml"
74 c.write_text(CONFIG)
75 assert g.main(["--config", str(c), "--out-dir", str(tmp_path), "--hosts-only"]) == 0
76 assert (tmp_path / "star-dhcp.hosts").read_text().strip().endswith(
77 "4a:27:e2:3a:ad:71,192.168.2.31,rtd-board,infinite")
78 assert not (tmp_path / "star-dhcp.conf").exists()
79
80
82 hosts = tmp_path / "star-dhcp.hosts"
83 hosts.write_text("served before\n")
84 c = tmp_path / "config.toml"
85 c.write_text(CONFIG.replace("board_id = 31", "board_id = 32"))
86 with pytest.raises(SystemExit):
87 g.main(["--config", str(c), "--out-dir", str(tmp_path), "--hosts-only"])
88 assert hosts.read_text() == "served before\n"
89 assert not (tmp_path / "star-dhcp.hosts.tmp").exists()
test_a_refused_config_leaves_the_served_list_alone(Path tmp_path)
test_hosts_only_writes_just_the_reservations(Path tmp_path)