1"""generate_dhcp_config.py: what star-dhcp-sync runs as root on every config save.
3Run: python3 -m pytest daq-server/deploy/dhcp -q
6from pathlib
import Path
10import generate_dhcp_config
as g
12SUBNET = ipaddress.ip_network(
"192.168.2.0/24")
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
23 return {
"boards": boards}
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"
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)
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")]
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
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")) == []
68mac = "4a:27:e2:3a:ad:71"
73 c = tmp_path /
"config.toml"
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()
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_recovery_address_matches_firmware_and_ota_tool()
test_a_refused_config_leaves_the_served_list_alone(Path tmp_path)
test_hosts_only_writes_just_the_reservations(Path tmp_path)
board(ip, mac, board_id=None)
test_interface_is_the_one_on_the_board_subnet()
test_board_id_must_match_its_address()
test_two_boards_sharing_a_recovery_address_are_refused()