1# In contrast to systemd-networkd-dhcpserver, this test configures
2# the router with a static DHCP lease for the client's MAC address.
3import ./make-test-python.nix (
4 { lib, ... }:
5 {
6 name = "systemd-networkd-dhcpserver-static-leases";
7 meta = with lib.maintainers; {
8 maintainers = [ veehaitch ];
9 };
10 nodes = {
11 router = {
12 virtualisation.vlans = [ 1 ];
13 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
14 networking = {
15 useNetworkd = true;
16 useDHCP = false;
17 firewall.enable = false;
18 };
19 systemd.network = {
20 networks = {
21 # systemd-networkd will load the first network unit file
22 # that matches, ordered lexiographically by filename.
23 # /etc/systemd/network/{40-eth1,99-main}.network already
24 # exists. This network unit must be loaded for the test,
25 # however, hence why this network is named such.
26 "01-eth1" = {
27 name = "eth1";
28 networkConfig = {
29 DHCPServer = true;
30 Address = "10.0.0.1/24";
31 };
32 dhcpServerStaticLeases = [
33 {
34 MACAddress = "02:de:ad:be:ef:01";
35 Address = "10.0.0.10";
36 }
37 ];
38 };
39 };
40 };
41 };
42
43 client = {
44 virtualisation.vlans = [ 1 ];
45 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
46 systemd.network = {
47 enable = true;
48 links."10-eth1" = {
49 matchConfig.OriginalName = "eth1";
50 linkConfig.MACAddress = "02:de:ad:be:ef:01";
51 };
52 networks."40-eth1" = {
53 matchConfig.Name = "eth1";
54 networkConfig = {
55 DHCP = "ipv4";
56 IPv6AcceptRA = false;
57 };
58 # This setting is important to have the router assign the
59 # configured lease based on the client's MAC address. Also see:
60 # https://github.com/systemd/systemd/issues/21368#issuecomment-982193546
61 dhcpV4Config.ClientIdentifier = "mac";
62 linkConfig.RequiredForOnline = "routable";
63 };
64 };
65 networking = {
66 useDHCP = false;
67 firewall.enable = false;
68 interfaces.eth1 = lib.mkForce { };
69 };
70 };
71 };
72 testScript = ''
73 start_all()
74
75 with subtest("check router network configuration"):
76 router.systemctl("start systemd-networkd-wait-online.service")
77 router.wait_for_unit("systemd-networkd-wait-online.service")
78 eth1_status = router.succeed("networkctl status eth1")
79 assert "Network File: /etc/systemd/network/01-eth1.network" in eth1_status, \
80 "The router interface eth1 is not using the expected network file"
81 assert "10.0.0.1" in eth1_status, "Did not find expected router IPv4"
82
83 with subtest("check client network configuration"):
84 client.systemctl("start systemd-networkd-wait-online.service")
85 client.wait_for_unit("systemd-networkd-wait-online.service")
86 eth1_status = client.succeed("networkctl status eth1")
87 assert "Network File: /etc/systemd/network/40-eth1.network" in eth1_status, \
88 "The client interface eth1 is not using the expected network file"
89 assert "10.0.0.10" in eth1_status, "Did not find expected client IPv4"
90
91 with subtest("router and client can reach each other"):
92 client.wait_until_succeeds("ping -c 5 10.0.0.1")
93 router.wait_until_succeeds("ping -c 5 10.0.0.10")
94 '';
95 }
96)