1{
2 system ? builtins.currentSystem,
3 config ? { },
4 pkgs ? import ../.. { inherit system config; },
5}:
6
7with import ../../lib/testing-python.nix { inherit system pkgs; };
8
9let
10 lib = pkgs.lib;
11 # this is intended as a client test since you shouldn't use NetworkManager for a router or server
12 # so using systemd-networkd for the router vm is fine in these tests.
13 router = import ./router.nix { networkd = true; };
14 qemu-common = import ../../lib/qemu-common.nix { inherit (pkgs) lib pkgs; };
15 clientConfig =
16 extraConfig:
17 lib.recursiveUpdate {
18 networking.useDHCP = false;
19
20 # Make sure that only NetworkManager configures the interface
21 networking.interfaces = lib.mkForce {
22 eth1 = { };
23 };
24 networking.networkmanager = {
25 enable = true;
26 # this is needed so NM doesn't generate 'Wired Connection' profiles and instead uses the default one
27 settings.main.no-auto-default = "*";
28 ensureProfiles.profiles.default = {
29 connection = {
30 id = "default";
31 type = "ethernet";
32 interface-name = "eth1";
33 autoconnect = true;
34 };
35 };
36 };
37 } extraConfig;
38 testCases = {
39 startup = {
40 name = "startup";
41 nodes.client = {
42 networking.useDHCP = false;
43 networking.networkmanager.enable = true;
44 };
45 testScript = ''
46 with subtest("NetworkManager is started automatically at boot"):
47 client.wait_for_unit("NetworkManager.service")
48 '';
49 };
50 static = {
51 name = "static";
52 nodes = {
53 inherit router;
54 client = clientConfig {
55 networking.networkmanager.ensureProfiles.profiles.default = {
56 ipv4.method = "manual";
57 ipv4.addresses = "192.168.1.42/24";
58 ipv4.gateway = "192.168.1.1";
59 ipv6.method = "manual";
60 ipv6.addresses = "fd00:1234:5678:1::42/64";
61 ipv6.gateway = "fd00:1234:5678:1::1";
62 };
63 };
64 };
65 testScript = ''
66 start_all()
67 router.systemctl("start network-online.target")
68 router.wait_for_unit("network-online.target")
69 client.wait_for_unit("NetworkManager.service")
70
71 with subtest("Wait until we have an ip address on each interface"):
72 client.wait_until_succeeds("ip addr show dev eth1 | grep -q '192.168.1'")
73 client.wait_until_succeeds("ip addr show dev eth1 | grep -q 'fd00:1234:5678:1:'")
74
75 with subtest("Test if icmp echo works"):
76 client.wait_until_succeeds("ping -c 1 192.168.3.1")
77 client.wait_until_succeeds("ping -c 1 fd00:1234:5678:3::1")
78 router.wait_until_succeeds("ping -c 1 192.168.1.42")
79 router.wait_until_succeeds("ping -c 1 fd00:1234:5678:1::42")
80 '';
81 };
82 auto = {
83 name = "auto";
84 nodes = {
85 inherit router;
86 client = clientConfig {
87 networking.networkmanager.ensureProfiles.profiles.default = {
88 ipv4.method = "auto";
89 ipv6.method = "auto";
90 };
91 };
92 };
93 testScript = ''
94 start_all()
95 router.systemctl("start network-online.target")
96 router.wait_for_unit("network-online.target")
97 client.wait_for_unit("NetworkManager.service")
98
99 with subtest("Wait until we have an ip address on each interface"):
100 client.wait_until_succeeds("ip addr show dev eth1 | grep -q '192.168.1'")
101 client.wait_until_succeeds("ip addr show dev eth1 | grep -q 'fd00:1234:5678:1:'")
102
103 with subtest("Test if icmp echo works"):
104 client.wait_until_succeeds("ping -c 1 192.168.1.1")
105 client.wait_until_succeeds("ping -c 1 fd00:1234:5678:1::1")
106 router.wait_until_succeeds("ping -c 1 192.168.1.2")
107 router.wait_until_succeeds("ping -c 1 fd00:1234:5678:1::2")
108 '';
109 };
110 dns = {
111 name = "dns";
112 nodes = {
113 inherit router;
114 dynamic = clientConfig {
115 networking.networkmanager.ensureProfiles.profiles.default = {
116 ipv4.method = "auto";
117 };
118 };
119 static = clientConfig {
120 networking.networkmanager.ensureProfiles.profiles.default = {
121 ipv4 = {
122 method = "auto";
123 ignore-auto-dns = "true";
124 dns = "10.10.10.10";
125 dns-search = "";
126 };
127 };
128 };
129 };
130 testScript = ''
131 start_all()
132 router.systemctl("start network-online.target")
133 router.wait_for_unit("network-online.target")
134 dynamic.wait_for_unit("NetworkManager.service")
135 static.wait_for_unit("NetworkManager.service")
136
137 dynamic.wait_until_succeeds("cat /etc/resolv.conf | grep -q '192.168.1.1'")
138 static.wait_until_succeeds("cat /etc/resolv.conf | grep -q '10.10.10.10'")
139 static.wait_until_fails("cat /etc/resolv.conf | grep -q '192.168.1.1'")
140 '';
141 };
142 dispatcherScripts = {
143 name = "dispatcherScripts";
144 nodes.client = clientConfig {
145 networking.networkmanager.dispatcherScripts = [
146 {
147 type = "pre-up";
148 source = pkgs.writeText "testHook" ''
149 touch /tmp/dispatcher-scripts-are-working
150 '';
151 }
152 ];
153 };
154 testScript = ''
155 start_all()
156 client.wait_for_unit("NetworkManager.service")
157 client.wait_until_succeeds("stat /tmp/dispatcher-scripts-are-working")
158 '';
159 };
160 envsubst = {
161 name = "envsubst";
162 nodes.client =
163 let
164 # you should never write secrets in to your nixos configuration, please use tools like sops-nix or agenix
165 secretFile = pkgs.writeText "my-secret.env" ''
166 MY_SECRET_IP=fd00:1234:5678:1::23/64
167 '';
168 in
169 clientConfig {
170 networking.networkmanager.ensureProfiles.environmentFiles = [ secretFile ];
171 networking.networkmanager.ensureProfiles.profiles.default = {
172 ipv6.method = "manual";
173 ipv6.addresses = "$MY_SECRET_IP";
174 };
175 };
176 testScript = ''
177 start_all()
178 client.wait_for_unit("NetworkManager.service")
179 client.wait_until_succeeds("ip addr show dev eth1 | grep -q 'fd00:1234:5678:1:'")
180 client.wait_until_succeeds("ping -c 1 fd00:1234:5678:1::23")
181 '';
182 };
183 };
184in
185lib.mapAttrs (lib.const (
186 attrs:
187 makeTest (
188 attrs
189 // {
190 name = "${attrs.name}-Networking-NetworkManager";
191 meta = {
192 maintainers = [ ];
193 };
194
195 }
196 )
197)) testCases