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