2 # SPDX-License-Identifier: LGPL-2.1+
3 # systemd-networkd tests
15 from shutil
import copytree
17 network_unit_file_path
='/run/systemd/network'
18 networkd_runtime_directory
='/run/systemd/netif'
19 networkd_ci_path
='/run/networkd-ci'
20 network_sysctl_ipv6_path
='/proc/sys/net/ipv6/conf'
21 network_sysctl_ipv4_path
='/proc/sys/net/ipv4/conf'
23 dnsmasq_config_file
='/run/networkd-ci/test-dnsmasq.conf'
24 dnsmasq_pid_file
='/run/networkd-ci/test-test-dnsmasq.pid'
25 dnsmasq_log_file
='/run/networkd-ci/test-dnsmasq-log-file'
27 def is_module_available(module_name
):
28 lsmod_output
= subprocess
.check_output('lsmod', universal_newlines
=True)
29 module_re
= re
.compile(r
'^{0}\b'.format(re
.escape(module_name
)), re
.MULTILINE
)
30 return module_re
.search(lsmod_output
) or not subprocess
.call(["modprobe", module_name
])
32 def expectedFailureIfModuleIsNotAvailable(module_name
):
34 if not is_module_available(module_name
):
35 return unittest
.expectedFailure(func
)
42 os
.makedirs(network_unit_file_path
, exist_ok
=True)
43 os
.makedirs(networkd_ci_path
, exist_ok
=True)
45 shutil
.rmtree(networkd_ci_path
)
46 copytree(os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), 'conf'), networkd_ci_path
)
48 subprocess
.check_call('systemctl stop systemd-networkd.socket', shell
=True)
51 shutil
.rmtree(networkd_ci_path
)
53 subprocess
.check_call('systemctl stop systemd-networkd.service', shell
=True)
54 subprocess
.check_call('systemctl start systemd-networkd.socket', shell
=True)
55 subprocess
.check_call('systemctl start systemd-networkd.service', shell
=True)
60 def read_link_attr(self
, link
, dev
, attribute
):
61 with
open(os
.path
.join(os
.path
.join(os
.path
.join('/sys/class/net/', link
), dev
), attribute
)) as f
:
62 return f
.readline().strip()
64 def link_exits(self
, link
):
65 return os
.path
.exists(os
.path
.join('/sys/class/net', link
))
67 def link_remove(self
, links
):
69 if os
.path
.exists(os
.path
.join('/sys/class/net', link
)):
70 subprocess
.call(['ip', 'link', 'del', 'dev', link
])
73 def read_ipv6_sysctl_attr(self
, link
, attribute
):
74 with
open(os
.path
.join(os
.path
.join(network_sysctl_ipv6_path
, link
), attribute
)) as f
:
75 return f
.readline().strip()
77 def read_ipv4_sysctl_attr(self
, link
, attribute
):
78 with
open(os
.path
.join(os
.path
.join(network_sysctl_ipv4_path
, link
), attribute
)) as f
:
79 return f
.readline().strip()
81 def copy_unit_to_networkd_unit_path(self
, *units
):
83 shutil
.copy(os
.path
.join(networkd_ci_path
, unit
), network_unit_file_path
)
84 if (os
.path
.exists(os
.path
.join(networkd_ci_path
, unit
+ '.d'))):
85 copytree(os
.path
.join(networkd_ci_path
, unit
+ '.d'), os
.path
.join(network_unit_file_path
, unit
+ '.d'))
87 def remove_unit_from_networkd_path(self
, units
):
89 if (os
.path
.exists(os
.path
.join(network_unit_file_path
, unit
))):
90 os
.remove(os
.path
.join(network_unit_file_path
, unit
))
91 if (os
.path
.exists(os
.path
.join(network_unit_file_path
, unit
+ '.d'))):
92 shutil
.rmtree(os
.path
.join(network_unit_file_path
, unit
+ '.d'))
94 def start_dnsmasq(self
):
95 subprocess
.check_call('dnsmasq -8 /var/run/networkd-ci/test-dnsmasq-log-file --log-queries=extra --log-dhcp --pid-file=/var/run/networkd-ci/test-test-dnsmasq.pid --conf-file=/dev/null --interface=veth-peer --enable-ra --dhcp-range=2600::10,2600::20 --dhcp-range=192.168.5.10,192.168.5.200 -R --dhcp-leasefile=/var/run/networkd-ci/lease --dhcp-option=26,1492 --dhcp-option=option:router,192.168.5.1 --dhcp-option=33,192.168.5.4,192.168.5.5', shell
=True)
99 def stop_dnsmasq(self
, pid_file
):
100 if os
.path
.exists(pid_file
):
101 with
open(pid_file
, 'r') as f
:
102 pid
= f
.read().rstrip(' \t\r\n\0')
103 os
.kill(int(pid
), signal
.SIGTERM
)
107 def search_words_in_file(self
, word
):
108 if os
.path
.exists(dnsmasq_log_file
):
109 with
open (dnsmasq_log_file
) as in_file
:
110 contents
= in_file
.read()
112 for part
in contents
.split():
115 print("%s, %s" % (word
, part
))
119 def remove_lease_file(self
):
120 if os
.path
.exists(os
.path
.join(networkd_ci_path
, 'lease')):
121 os
.remove(os
.path
.join(networkd_ci_path
, 'lease'))
123 def remove_log_file(self
):
124 if os
.path
.exists(dnsmasq_log_file
):
125 os
.remove(dnsmasq_log_file
)
127 def start_networkd(self
):
128 if (os
.path
.exists(os
.path
.join(networkd_runtime_directory
, 'state'))):
129 subprocess
.check_call('systemctl stop systemd-networkd', shell
=True)
130 os
.remove(os
.path
.join(networkd_runtime_directory
, 'state'))
131 subprocess
.check_call('systemctl start systemd-networkd', shell
=True)
133 subprocess
.check_call('systemctl restart systemd-networkd', shell
=True)
139 class DHCPServer(threading
.Thread
):
140 def __init__(self
, name
):
141 threading
.Thread
.__init
__(self
)
145 self
.start_dhcp_server()
147 def start_dhcp_server(self
):
148 sock
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
150 server_address
= ('0.0.0.0', 67)
151 sock
.bind(server_address
)
153 print('Starting DHCP Server ...\n')
154 data
, addr
= sock
.recvfrom(1024) # buffer size is 1024 bytes
163 class NetworkdNetDevTests(unittest
.TestCase
, Utilities
):
165 links
=['bridge99', 'bond99', 'bond99', 'vlan99', 'test1', 'macvtap99',
166 'macvlan99', 'ipvlan99', 'vxlan99', 'veth99', 'vrf99', 'tun99',
167 'tap99', 'vcan99', 'geneve99', 'dummy98', 'ipiptun99', 'sittun99', '6rdtun99',
168 'gretap99', 'vtitun99', 'vti6tun99','ip6tnl99', 'gretun99', 'ip6gretap99',
169 'wg99', 'dropin-test', 'erspan-test']
171 units
= ['25-bridge.netdev', '25-bond.netdev', '21-vlan.netdev', '11-dummy.netdev', '21-vlan.network',
172 '21-macvtap.netdev', 'macvtap.network', '21-macvlan.netdev', 'macvlan.network', 'vxlan.network',
173 '25-vxlan.netdev', '25-ipvlan.netdev', 'ipvlan.network', '25-veth.netdev', '25-vrf.netdev',
174 '25-tun.netdev', '25-tun.netdev', '25-vcan.netdev', '25-geneve.netdev', '25-ipip-tunnel.netdev',
175 '25-ip6tnl-tunnel.netdev', '25-ip6gre-tunnel.netdev', '25-sit-tunnel.netdev', '25-6rd-tunnel.netdev',
176 '25-erspan-tunnel.netdev', '25-gre-tunnel.netdev', '25-gretap-tunnel.netdev', '25-vti-tunnel.netdev',
177 '25-vti6-tunnel.netdev', '12-dummy.netdev', 'gre.network', 'ipip.network', 'ip6gretap.network',
178 'gretun.network', 'ip6tnl.network', '25-tap.netdev', 'vti6.network', 'vti.network', 'gretap.network',
179 'sit.network', '25-ipip-tunnel-independent.netdev', '25-wireguard.netdev', '6rd.network', '10-dropin-test.netdev']
182 self
.link_remove(self
.links
)
185 self
.link_remove(self
.links
)
186 self
.remove_unit_from_networkd_path(self
.units
)
188 def test_dropin(self
):
189 self
.copy_unit_to_networkd_unit_path('10-dropin-test.netdev')
191 self
.start_networkd()
193 self
.assertTrue(self
.link_exits('dropin-test'))
195 output
= subprocess
.check_output(['ip', 'link', 'show', 'dropin-test']).rstrip().decode('utf-8')
197 self
.assertRegex(output
, '00:50:56:c0:00:28')
199 def test_bridge(self
):
200 self
.copy_unit_to_networkd_unit_path('25-bridge.netdev')
201 self
.start_networkd()
203 self
.assertTrue(self
.link_exits('bridge99'))
205 self
.assertEqual('900', self
.read_link_attr('bridge99', 'bridge', 'hello_time'))
206 self
.assertEqual('900', self
.read_link_attr('bridge99', 'bridge', 'max_age'))
207 self
.assertEqual('900', self
.read_link_attr('bridge99', 'bridge','forward_delay'))
208 self
.assertEqual('900', self
.read_link_attr('bridge99', 'bridge','ageing_time'))
209 self
.assertEqual('9', self
.read_link_attr('bridge99', 'bridge','priority'))
210 self
.assertEqual('1', self
.read_link_attr('bridge99', 'bridge','multicast_querier'))
211 self
.assertEqual('1', self
.read_link_attr('bridge99', 'bridge','multicast_snooping'))
212 self
.assertEqual('1', self
.read_link_attr('bridge99', 'bridge','stp_state'))
215 self
.copy_unit_to_networkd_unit_path('25-bond.netdev')
216 self
.start_networkd()
218 self
.assertTrue(self
.link_exits('bond99'))
220 self
.assertEqual('802.3ad 4', self
.read_link_attr('bond99', 'bonding', 'mode'))
221 self
.assertEqual('layer3+4 1', self
.read_link_attr('bond99', 'bonding', 'xmit_hash_policy'))
222 self
.assertEqual('1000', self
.read_link_attr('bond99', 'bonding', 'miimon'))
223 self
.assertEqual('fast 1', self
.read_link_attr('bond99', 'bonding', 'lacp_rate'))
224 self
.assertEqual('2000', self
.read_link_attr('bond99', 'bonding', 'updelay'))
225 self
.assertEqual('2000', self
.read_link_attr('bond99', 'bonding', 'downdelay'))
226 self
.assertEqual('4', self
.read_link_attr('bond99', 'bonding', 'resend_igmp'))
227 self
.assertEqual('1', self
.read_link_attr('bond99', 'bonding', 'min_links'))
228 self
.assertEqual('1218', self
.read_link_attr('bond99', 'bonding', 'ad_actor_sys_prio'))
229 self
.assertEqual('811', self
.read_link_attr('bond99', 'bonding', 'ad_user_port_key'))
230 self
.assertEqual('00:11:22:33:44:55', self
.read_link_attr('bond99', 'bonding', 'ad_actor_system'))
233 self
.copy_unit_to_networkd_unit_path('21-vlan.netdev', '11-dummy.netdev', '21-vlan.network')
235 self
.start_networkd()
237 self
.assertTrue(self
.link_exits('vlan99'))
239 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'vlan99']).rstrip().decode('utf-8')
240 self
.assertTrue(output
, 'REORDER_HDR')
241 self
.assertTrue(output
, 'LOOSE_BINDING')
242 self
.assertTrue(output
, 'GVRP')
243 self
.assertTrue(output
, 'MVRP')
244 self
.assertTrue(output
, '99')
246 def test_macvtap(self
):
247 self
.copy_unit_to_networkd_unit_path('21-macvtap.netdev', '11-dummy.netdev', 'macvtap.network')
249 self
.start_networkd()
251 self
.assertTrue(self
.link_exits('macvtap99'))
253 def test_macvlan(self
):
254 self
.copy_unit_to_networkd_unit_path('21-macvlan.netdev', '11-dummy.netdev', 'macvlan.network')
256 self
.start_networkd()
258 self
.assertTrue(self
.link_exits('macvlan99'))
260 @expectedFailureIfModuleIsNotAvailable('ipvlan')
261 def test_ipvlan(self
):
262 self
.copy_unit_to_networkd_unit_path('25-ipvlan.netdev', '11-dummy.netdev', 'ipvlan.network')
264 self
.start_networkd()
266 self
.assertTrue(self
.link_exits('ipvlan99'))
269 self
.copy_unit_to_networkd_unit_path('25-veth.netdev')
271 self
.start_networkd()
273 self
.assertTrue(self
.link_exits('veth99'))
275 def test_dummy(self
):
276 self
.copy_unit_to_networkd_unit_path('11-dummy.netdev')
278 self
.start_networkd()
280 self
.assertTrue(self
.link_exits('test1'))
283 self
.copy_unit_to_networkd_unit_path('25-tun.netdev')
285 self
.start_networkd()
287 self
.assertTrue(self
.link_exits('tun99'))
290 self
.copy_unit_to_networkd_unit_path('25-tap.netdev')
292 self
.start_networkd()
294 self
.assertTrue(self
.link_exits('tap99'))
296 @expectedFailureIfModuleIsNotAvailable('vrf')
298 self
.copy_unit_to_networkd_unit_path('25-vrf.netdev')
300 self
.start_networkd()
302 self
.assertTrue(self
.link_exits('vrf99'))
304 @expectedFailureIfModuleIsNotAvailable('vcan')
306 self
.copy_unit_to_networkd_unit_path('25-vcan.netdev')
308 self
.start_networkd()
310 self
.assertTrue(self
.link_exits('vcan99'))
312 @expectedFailureIfModuleIsNotAvailable('wireguard')
313 def test_wireguard(self
):
314 self
.copy_unit_to_networkd_unit_path('25-wireguard.netdev')
316 self
.start_networkd()
318 if shutil
.which('wg'):
319 subprocess
.call('wg')
321 self
.assertTrue(self
.link_exits('wg99'))
323 def test_geneve(self
):
324 self
.copy_unit_to_networkd_unit_path('25-geneve.netdev')
326 self
.start_networkd()
328 self
.assertTrue(self
.link_exits('geneve99'))
330 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'geneve99']).rstrip().decode('utf-8')
331 self
.assertTrue(output
, '192.168.22.1')
332 self
.assertTrue(output
, '6082')
333 self
.assertTrue(output
, 'udpcsum')
334 self
.assertTrue(output
, 'udp6zerocsumrx')
336 def test_ipip_tunnel(self
):
337 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-ipip-tunnel.netdev', 'ipip.network')
338 self
.start_networkd()
340 self
.assertTrue(self
.link_exits('dummy98'))
341 self
.assertTrue(self
.link_exits('ipiptun99'))
343 def test_gre_tunnel(self
):
344 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-gre-tunnel.netdev', 'gretun.network')
345 self
.start_networkd()
347 self
.assertTrue(self
.link_exits('dummy98'))
348 self
.assertTrue(self
.link_exits('gretun99'))
350 def test_gretap_tunnel(self
):
351 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-gretap-tunnel.netdev', 'gretap.network')
352 self
.start_networkd()
354 self
.assertTrue(self
.link_exits('dummy98'))
355 self
.assertTrue(self
.link_exits('gretap99'))
357 def test_ip6gretap_tunnel(self
):
358 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-ip6gre-tunnel.netdev', 'ip6gretap.network')
359 self
.start_networkd()
361 self
.assertTrue(self
.link_exits('dummy98'))
362 self
.assertTrue(self
.link_exits('ip6gretap99'))
364 def test_vti_tunnel(self
):
365 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-vti-tunnel.netdev', 'vti.network')
366 self
.start_networkd()
368 self
.assertTrue(self
.link_exits('dummy98'))
369 self
.assertTrue(self
.link_exits('vtitun99'))
371 def test_vti6_tunnel(self
):
372 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-vti6-tunnel.netdev', 'vti6.network')
373 self
.start_networkd()
375 self
.assertTrue(self
.link_exits('dummy98'))
376 self
.assertTrue(self
.link_exits('vti6tun99'))
378 def test_ip6tnl_tunnel(self
):
379 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-ip6tnl-tunnel.netdev', 'ip6tnl.network')
380 self
.start_networkd()
382 self
.assertTrue(self
.link_exits('dummy98'))
383 self
.assertTrue(self
.link_exits('ip6tnl99'))
385 def test_sit_tunnel(self
):
386 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-sit-tunnel.netdev', 'sit.network')
387 self
.start_networkd()
389 self
.assertTrue(self
.link_exits('dummy98'))
390 self
.assertTrue(self
.link_exits('sittun99'))
392 def test_6rd_tunnel(self
):
393 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', '25-6rd-tunnel.netdev', '6rd.network')
394 self
.start_networkd()
396 self
.assertTrue(self
.link_exits('dummy98'))
397 self
.assertTrue(self
.link_exits('sittun99'))
399 def test_erspan_tunnel(self
):
400 self
.copy_unit_to_networkd_unit_path('25-erspan-tunnel.netdev')
401 self
.start_networkd()
403 self
.assertTrue(self
.link_exits('erspan-test'))
405 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'erspan-test']).rstrip().decode('utf-8')
407 self
.assertTrue(output
, '172.16.1.200')
408 self
.assertTrue(output
, '172.16.1.100')
409 self
.assertTrue(output
, '101')
411 def test_tunnel_independent(self
):
412 self
.copy_unit_to_networkd_unit_path('25-ipip-tunnel-independent.netdev')
414 self
.start_networkd()
415 self
.assertTrue(self
.link_exits('ipiptun99'))
417 def test_vxlan(self
):
418 self
.copy_unit_to_networkd_unit_path('25-vxlan.netdev', 'vxlan.network','11-dummy.netdev')
420 self
.start_networkd()
422 self
.assertTrue(self
.link_exits('vxlan99'))
424 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'vxlan99']).rstrip().decode('utf-8')
425 self
.assertRegex(output
, "999")
426 self
.assertRegex(output
, '5555')
427 self
.assertRegex(output
, 'l2miss')
428 self
.assertRegex(output
, 'l3miss')
429 self
.assertRegex(output
, 'udpcsum')
430 self
.assertRegex(output
, 'udp6zerocsumtx')
431 self
.assertRegex(output
, 'udp6zerocsumrx')
432 self
.assertRegex(output
, 'remcsumtx')
433 self
.assertRegex(output
, 'remcsumrx')
434 self
.assertRegex(output
, 'gbp')
436 class NetworkdNetWorkTests(unittest
.TestCase
, Utilities
):
437 links
= ['dummy98', 'test1', 'bond199']
439 units
= ['12-dummy.netdev', 'test-static.network', 'configure-without-carrier.network', '11-dummy.netdev',
440 '23-primary-slave.network', '23-test1-bond199.network', '11-dummy.netdev', '23-bond199.network',
441 '25-bond-active-backup-slave.netdev', '12-dummy.netdev', '23-active-slave.network',
442 'routing-policy-rule.network', '25-fibrule-port-range.network', '25-address-section.network',
443 '25-address-section-miscellaneous.network', '25-route-section.network', '25-route-type.network',
444 '25-route-tcp-window-settings.network', '25-route-gateway.network', '25-route-gateway-on-link.network',
445 '25-address-link-section.network', '25-ipv6-address-label-section.network', '25-link-section-unmanaged.network',
446 '25-sysctl.network', '25-route-reverse-order.network']
449 self
.link_remove(self
.links
)
452 self
.link_remove(self
.links
)
453 self
.remove_unit_from_networkd_path(self
.units
)
455 def test_static_address(self
):
456 self
.copy_unit_to_networkd_unit_path('12-dummy.netdev', 'test-static.network')
457 self
.start_networkd()
459 self
.assertTrue(self
.link_exits('dummy98'))
460 output
= subprocess
.check_output(['networkctl', 'status', 'dummy98']).rstrip().decode('utf-8')
462 self
.assertRegex(output
, '192.168.0.15')
463 self
.assertRegex(output
, '192.168.0.1')
464 self
.assertRegex(output
, 'routable')
466 def test_configure_without_carrier(self
):
467 self
.copy_unit_to_networkd_unit_path('configure-without-carrier.network', '11-dummy.netdev')
468 self
.start_networkd()
470 self
.assertTrue(self
.link_exits('test1'))
471 output
= subprocess
.check_output(['networkctl', 'status', 'test1']).rstrip().decode('utf-8')
473 self
.assertRegex(output
, '192.168.0.15')
474 self
.assertRegex(output
, '192.168.0.1')
475 self
.assertRegex(output
, 'routable')
477 def test_bond_active_slave(self
):
478 self
.copy_unit_to_networkd_unit_path('23-active-slave.network', '23-bond199.network', '25-bond-active-backup-slave.netdev', '12-dummy.netdev')
479 self
.start_networkd()
481 self
.assertTrue(self
.link_exits('dummy98'))
482 self
.assertTrue(self
.link_exits('bond199'))
483 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'bond199']).rstrip().decode('utf-8')
485 self
.assertRegex(output
, 'active_slave dummy98')
487 def test_bond_primary_slave(self
):
488 self
.copy_unit_to_networkd_unit_path('23-primary-slave.network', '23-test1-bond199.network', '25-bond-active-backup-slave.netdev', '11-dummy.netdev')
489 self
.start_networkd()
491 self
.assertTrue(self
.link_exits('test1'))
492 self
.assertTrue(self
.link_exits('bond199'))
493 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'bond199']).rstrip().decode('utf-8')
495 self
.assertRegex(output
, 'primary test1')
497 def test_routing_policy_rule(self
):
498 self
.copy_unit_to_networkd_unit_path('routing-policy-rule.network', '11-dummy.netdev')
499 self
.start_networkd()
501 self
.assertTrue(self
.link_exits('test1'))
502 output
= subprocess
.check_output(['ip', 'rule']).rstrip().decode('utf-8')
504 self
.assertRegex(output
, '111')
505 self
.assertRegex(output
, 'from 192.168.100.18')
506 self
.assertRegex(output
, r
'tos (?:0x08|throughput)\s')
507 self
.assertRegex(output
, 'iif test1')
508 self
.assertRegex(output
, 'oif test1')
509 self
.assertRegex(output
, 'lookup 7')
511 def test_routing_policy_rule_port_range(self
):
512 self
.copy_unit_to_networkd_unit_path('25-fibrule-port-range.network', '11-dummy.netdev')
513 self
.start_networkd()
515 self
.assertTrue(self
.link_exits('test1'))
516 output
= subprocess
.check_output(['ip', 'rule']).rstrip().decode('utf-8')
518 self
.assertRegex(output
, '111')
519 self
.assertRegex(output
, 'from 192.168.100.18')
520 self
.assertRegex(output
, '1123-1150')
521 self
.assertRegex(output
, '3224-3290')
522 self
.assertRegex(output
, 'tcp')
523 self
.assertRegex(output
, 'lookup 7')
525 def test_address_preferred_lifetime_zero_ipv6(self
):
526 self
.copy_unit_to_networkd_unit_path('25-address-section-miscellaneous.network', '12-dummy.netdev')
527 self
.start_networkd()
529 self
.assertTrue(self
.link_exits('dummy98'))
531 output
= subprocess
.check_output(['ip', 'address', 'show', 'dummy98']).rstrip().decode('utf-8')
533 self
.assertRegex(output
, 'inet 10.2.3.4/16 brd 10.2.255.255 scope link deprecated dummy98')
534 self
.assertRegex(output
, 'inet6 2001:db8:0:f101::1/64 scope global')
536 def test_ip_route(self
):
537 self
.copy_unit_to_networkd_unit_path('25-route-section.network', '12-dummy.netdev')
538 self
.start_networkd()
540 self
.assertTrue(self
.link_exits('dummy98'))
542 output
= subprocess
.check_output(['ip', 'route', 'list', 'dev', 'dummy98']).rstrip().decode('utf-8')
544 self
.assertRegex(output
, '192.168.0.1')
545 self
.assertRegex(output
, 'static')
546 self
.assertRegex(output
, '192.168.0.0/24')
548 def test_ip_route_reverse(self
):
549 self
.copy_unit_to_networkd_unit_path('25-route-reverse-order.network', '12-dummy.netdev')
550 self
.start_networkd()
552 self
.assertTrue(self
.link_exits('dummy98'))
554 output
= subprocess
.check_output(['ip', '-6', 'route', 'show', 'dev', 'dummy98']).rstrip().decode('utf-8')
556 self
.assertRegex(output
, '2001:1234:5:8fff:ff:ff:ff:ff')
557 self
.assertRegex(output
, '2001:1234:5:8f63::1')
559 def test_ip_route_blackhole_unreachable_prohibit(self
):
560 self
.copy_unit_to_networkd_unit_path('25-route-type.network', '12-dummy.netdev')
561 self
.start_networkd()
563 self
.assertTrue(self
.link_exits('dummy98'))
565 output
= subprocess
.check_output(['ip', 'route', 'list']).rstrip().decode('utf-8')
567 self
.assertRegex(output
, 'blackhole')
568 self
.assertRegex(output
, 'unreachable')
569 self
.assertRegex(output
, 'prohibit')
571 subprocess
.call(['ip', 'route', 'del', 'blackhole', '202.54.1.2'])
572 subprocess
.call(['ip', 'route', 'del', 'unreachable', '202.54.1.3'])
573 subprocess
.call(['ip', 'route', 'del', 'prohibit', '202.54.1.4'])
575 def test_ip_route_tcp_window(self
):
576 self
.copy_unit_to_networkd_unit_path('25-route-tcp-window-settings.network', '11-dummy.netdev')
577 self
.start_networkd()
579 self
.assertTrue(self
.link_exits('test1'))
581 output
= subprocess
.check_output(['ip', 'route', 'list']).rstrip().decode('utf-8')
583 self
.assertRegex(output
, 'initcwnd 20')
584 self
.assertRegex(output
, 'initrwnd 30')
586 def test_ip_route_gateway(self
):
587 self
.copy_unit_to_networkd_unit_path('25-route-gateway.network', '12-dummy.netdev')
588 self
.start_networkd()
590 self
.assertTrue(self
.link_exits('dummy98'))
592 output
= subprocess
.check_output(['ip', 'route', 'list', 'dev', 'dummy98', 'default']).rstrip().decode('utf-8')
594 self
.assertRegex(output
, 'default')
595 self
.assertRegex(output
, 'via')
596 self
.assertRegex(output
, '149.10.124.64')
597 self
.assertRegex(output
, 'proto')
598 self
.assertRegex(output
, 'static')
600 output
= subprocess
.check_output(['ip', 'route', 'list', 'dev', 'dummy98', 'src', '149.10.124.58']).rstrip().decode('utf-8')
602 self
.assertRegex(output
, '149.10.124.48/28')
603 self
.assertRegex(output
, 'proto')
604 self
.assertRegex(output
, 'kernel')
605 self
.assertRegex(output
, 'scope')
606 self
.assertRegex(output
, 'link')
608 def test_ip_route_gateway_on_link(self
):
609 self
.copy_unit_to_networkd_unit_path('25-route-gateway-on-link.network', '12-dummy.netdev')
610 self
.start_networkd()
612 self
.assertTrue(self
.link_exits('dummy98'))
614 output
= subprocess
.check_output(['ip', 'route', 'list', 'dev', 'dummy98', 'default']).rstrip().decode('utf-8')
616 self
.assertRegex(output
, 'default')
617 self
.assertRegex(output
, 'via')
618 self
.assertRegex(output
, '149.10.125.65')
619 self
.assertRegex(output
, 'proto')
620 self
.assertRegex(output
, 'static')
621 self
.assertRegex(output
, 'onlink')
623 output
= subprocess
.check_output(['ip', 'route', 'list', 'dev', 'dummy98', 'src', '149.10.124.58']).rstrip().decode('utf-8')
625 self
.assertRegex(output
, '149.10.124.48/28')
626 self
.assertRegex(output
, 'proto')
627 self
.assertRegex(output
, 'kernel')
628 self
.assertRegex(output
, 'scope')
629 self
.assertRegex(output
, 'link')
631 def test_ip_link_mac_address(self
):
632 self
.copy_unit_to_networkd_unit_path('25-address-link-section.network', '12-dummy.netdev')
633 self
.start_networkd()
635 self
.assertTrue(self
.link_exits('dummy98'))
637 output
= subprocess
.check_output(['ip', 'link', 'show', 'dummy98']).rstrip().decode('utf-8')
639 self
.assertRegex(output
, '00:01:02:aa:bb:cc')
641 def test_ip_link_unmanaged(self
):
642 self
.copy_unit_to_networkd_unit_path('25-link-section-unmanaged.network', '12-dummy.netdev')
643 self
.start_networkd()
645 self
.assertTrue(self
.link_exits('dummy98'))
647 output
= subprocess
.check_output(['networkctl', 'status', 'dummy98']).rstrip().decode('utf-8')
649 self
.assertRegex(output
, 'unmanaged')
651 def test_ipv6_address_label(self
):
652 self
.copy_unit_to_networkd_unit_path('25-ipv6-address-label-section.network', '12-dummy.netdev')
653 self
.start_networkd()
655 self
.assertTrue(self
.link_exits('dummy98'))
657 output
= subprocess
.check_output(['ip', 'addrlabel', 'list']).rstrip().decode('utf-8')
659 self
.assertRegex(output
, '2004:da8:1::/64')
661 def test_sysctl(self
):
662 self
.copy_unit_to_networkd_unit_path('25-sysctl.network', '12-dummy.netdev')
663 self
.start_networkd()
665 self
.assertTrue(self
.link_exits('dummy98'))
667 self
.assertEqual(self
.read_ipv6_sysctl_attr('dummy98', 'forwarding'), '1')
668 self
.assertEqual(self
.read_ipv6_sysctl_attr('dummy98', 'use_tempaddr'), '2')
669 self
.assertEqual(self
.read_ipv6_sysctl_attr('dummy98', 'dad_transmits'), '3')
670 self
.assertEqual(self
.read_ipv6_sysctl_attr('dummy98', 'hop_limit'), '5')
671 self
.assertEqual(self
.read_ipv6_sysctl_attr('dummy98', 'proxy_ndp'), '1')
672 self
.assertEqual(self
.read_ipv4_sysctl_attr('dummy98', 'forwarding'),'1')
673 self
.assertEqual(self
.read_ipv4_sysctl_attr('dummy98', 'proxy_arp'), '1')
675 class NetworkdNetWorkBrideTests(unittest
.TestCase
, Utilities
):
676 links
= ['dummy98', 'test1', 'bridge99']
678 units
= ['11-dummy.netdev', '12-dummy.netdev', '26-bridge.netdev', '26-bridge-slave-interface-1.network',
679 '26-bridge-slave-interface-2.network', 'bridge99.network']
682 self
.link_remove(self
.links
)
685 self
.link_remove(self
.links
)
686 self
.remove_unit_from_networkd_path(self
.units
)
688 def test_bridge_property(self
):
689 self
.copy_unit_to_networkd_unit_path('11-dummy.netdev', '12-dummy.netdev', '26-bridge.netdev',
690 '26-bridge-slave-interface-1.network', '26-bridge-slave-interface-2.network',
692 self
.start_networkd()
694 self
.assertTrue(self
.link_exits('dummy98'))
695 self
.assertTrue(self
.link_exits('test1'))
696 self
.assertTrue(self
.link_exits('bridge99'))
698 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'test1']).rstrip().decode('utf-8')
700 self
.assertRegex(output
, 'master')
701 self
.assertRegex(output
, 'bridge')
703 output
= subprocess
.check_output(['ip', '-d', 'link', 'show', 'dummy98']).rstrip().decode('utf-8')
705 self
.assertRegex(output
, 'master')
706 self
.assertRegex(output
, 'bridge')
708 output
= subprocess
.check_output(['ip', 'addr', 'show', 'bridge99']).rstrip().decode('utf-8')
710 self
.assertRegex(output
, '192.168.0.15')
711 self
.assertRegex(output
, '192.168.0.1')
713 output
= subprocess
.check_output(['bridge', '-d', 'link', 'show', 'dummy98']).rstrip().decode('utf-8')
715 self
.assertRegex(output
, 'cost 400')
716 self
.assertRegex(output
, 'hairpin on')
717 self
.assertRegex(output
, 'flood on')
718 self
.assertRegex(output
, 'fastleave on')
720 class NetworkdNetWorkLLDPTests(unittest
.TestCase
, Utilities
):
723 units
= ['23-emit-lldp.network', '24-lldp.network', '25-veth.netdev']
726 self
.link_remove(self
.links
)
729 self
.link_remove(self
.links
)
730 self
.remove_unit_from_networkd_path(self
.units
)
733 self
.copy_unit_to_networkd_unit_path('23-emit-lldp.network', '24-lldp.network', '25-veth.netdev')
734 self
.start_networkd()
736 self
.assertTrue(self
.link_exits('veth99'))
738 output
= subprocess
.check_output(['networkctl', 'lldp']).rstrip().decode('utf-8')
740 self
.assertRegex(output
, 'veth-peer')
741 self
.assertRegex(output
, 'veth99')
743 class NetworkdNetworkRATests(unittest
.TestCase
, Utilities
):
746 units
= ['25-veth.netdev', 'ipv6-prefix.network', 'ipv6-prefix-veth.network']
749 self
.link_remove(self
.links
)
752 self
.link_remove(self
.links
)
753 self
.remove_unit_from_networkd_path(self
.units
)
755 def test_ipv6_prefix_delegation(self
):
756 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'ipv6-prefix.network', 'ipv6-prefix-veth.network')
757 self
.start_networkd()
759 self
.assertTrue(self
.link_exits('veth99'))
761 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
763 self
.assertRegex(output
, '2002:da8:1:0')
765 class NetworkdNetworkDHCPServerTests(unittest
.TestCase
, Utilities
):
766 links
= ['veth99', 'dummy98']
768 units
= ['25-veth.netdev', 'dhcp-client.network', 'dhcp-server.network', '12-dummy.netdev', '24-search-domain.network',
769 'dhcp-client-timezone-router.network', 'dhcp-server-timezone-router.network']
772 self
.link_remove(self
.links
)
775 self
.link_remove(self
.links
)
776 self
.remove_unit_from_networkd_path(self
.units
)
778 def test_dhcp_server(self
):
779 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-client.network', 'dhcp-server.network')
780 self
.start_networkd()
782 self
.assertTrue(self
.link_exits('veth99'))
786 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
788 self
.assertRegex(output
, '192.168.5.*')
789 self
.assertRegex(output
, 'Gateway: 192.168.5.1')
790 self
.assertRegex(output
, 'DNS: 192.168.5.1')
791 self
.assertRegex(output
, 'NTP: 192.168.5.1')
793 def test_domain(self
):
794 self
.copy_unit_to_networkd_unit_path( '12-dummy.netdev', '24-search-domain.network')
795 self
.start_networkd()
797 self
.assertTrue(self
.link_exits('dummy98'))
799 output
= subprocess
.check_output(['networkctl', 'status', 'dummy98']).rstrip().decode('utf-8')
801 self
.assertRegex(output
, 'Address: 192.168.42.100')
802 self
.assertRegex(output
, 'DNS: 192.168.42.1')
803 self
.assertRegex(output
, 'Search Domains: one')
805 def test_emit_router_timezone(self
):
806 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-client-timezone-router.network', 'dhcp-server-timezone-router.network')
807 self
.start_networkd()
809 self
.assertTrue(self
.link_exits('veth99'))
811 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
813 self
.assertRegex(output
, 'Gateway: 192.168.5.*')
814 self
.assertRegex(output
, '192.168.5.*')
815 self
.assertRegex(output
, 'Europe/Berlin')
817 class NetworkdNetworkDHCPClientTests(unittest
.TestCase
, Utilities
):
818 links
= ['veth99', 'dummy98']
820 units
= ['25-veth.netdev', 'dhcp-server-veth-peer.network','dhcp-client-ipv6-only.network',
821 'dhcp-client-ipv4-only-ipv6-disabled.network', 'dhcp-client-ipv4-only.network',
822 'dhcp-client-ipv4-dhcp-settings.network', 'dhcp-client-anonymize.network',
823 'dhcp-client-ipv6-rapid-commit.network', 'dhcp-client-route-table.network',
824 'dhcp-v4-server-veth-peer.network', 'dhcp-client-listen-port.network',
825 'dhcp-client-route-metric.network', 'dhcp-client-critical-connection.network']
828 self
.link_remove(self
.links
)
829 self
.stop_dnsmasq(dnsmasq_pid_file
)
832 self
.link_remove(self
.links
)
833 self
.remove_unit_from_networkd_path(self
.units
)
834 self
.stop_dnsmasq(dnsmasq_pid_file
)
835 self
.remove_lease_file()
836 self
.remove_log_file()
838 def test_dhcp_client_ipv6_only(self
):
839 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network','dhcp-client-ipv6-only.network')
840 self
.start_networkd()
842 self
.assertTrue(self
.link_exits('veth99'))
846 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
848 self
.assertRegex(output
, '2600::')
849 self
.assertNotRegex(output
, '192.168.5')
851 def test_dhcp_client_ipv4_only(self
):
852 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network','dhcp-client-ipv4-only-ipv6-disabled.network')
853 self
.start_networkd()
855 self
.assertTrue(self
.link_exits('veth99'))
859 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
861 self
.assertNotRegex(output
, '2600::')
862 self
.assertRegex(output
, '192.168.5')
864 def test_dhcp_client_ipv4_ipv6(self
):
865 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv6-only.network',
866 'dhcp-client-ipv4-only.network')
867 self
.start_networkd()
869 self
.assertTrue(self
.link_exits('veth99'))
873 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
875 self
.assertRegex(output
, '2600::')
876 self
.assertRegex(output
, '192.168.5')
878 def test_dhcp_client_settings(self
):
879 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv4-dhcp-settings.network')
880 self
.start_networkd()
882 self
.assertTrue(self
.link_exits('veth99'))
886 output
= subprocess
.check_output(['ip', 'address', 'show', 'dev', 'veth99']).rstrip().decode('utf-8')
888 self
.assertRegex(output
, '12:34:56:78:9a:bc')
889 self
.assertRegex(output
, '192.168.5')
890 self
.assertRegex(output
, '1492')
892 output
= subprocess
.check_output(['ip', 'route']).rstrip().decode('utf-8')
894 self
.assertRegex(output
, 'default.*dev veth99 proto dhcp')
896 self
.search_words_in_file('vendor class: SusantVendorTest')
897 self
.search_words_in_file('client MAC address: 12:34:56:78:9a:bc')
898 self
.search_words_in_file('client provides name: test-hostname')
899 self
.search_words_in_file('26:mtu')
901 def test_dhcp6_client_settings_rapidcommit_true(self
):
902 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv6-only.network')
903 self
.start_networkd()
905 self
.assertTrue(self
.link_exits('veth99'))
909 output
= subprocess
.check_output(['ip', 'address', 'show', 'dev', 'veth99']).rstrip().decode('utf-8')
911 self
.assertRegex(output
, '12:34:56:78:9a:bc')
913 self
.assertTrue(self
.search_words_in_file('14:rapid-commit'))
915 def test_dhcp6_client_settings_rapidcommit_false(self
):
916 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv6-rapid-commit.network')
917 self
.start_networkd()
919 self
.assertTrue(self
.link_exits('veth99'))
923 output
= subprocess
.check_output(['ip', 'address', 'show', 'dev', 'veth99']).rstrip().decode('utf-8')
925 self
.assertRegex(output
, '12:34:56:78:9a:bc')
927 self
.assertFalse(self
.search_words_in_file('14:rapid-commit'))
929 def test_dhcp_client_settings_anonymize(self
):
930 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-anonymize.network')
931 self
.start_networkd()
933 self
.assertTrue(self
.link_exits('veth99'))
936 self
.assertFalse(self
.search_words_in_file('VendorClassIdentifier=SusantVendorTest'))
937 self
.assertFalse(self
.search_words_in_file('test-hostname'))
938 self
.assertFalse(self
.search_words_in_file('26:mtu'))
940 def test_dhcp_client_listen_port(self
):
941 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-listen-port.network')
942 dh_server
= DHCPServer("dhcp_server")
945 self
.start_networkd()
947 self
.assertTrue(self
.link_exits('veth99'))
952 self
.assertRegex(str(port
), '5555')
953 self
.assertRegex(str(ip
), '0.0.0.0')
957 def test_dhcp_route_table_id(self
):
958 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-v4-server-veth-peer.network', 'dhcp-client-route-table.network')
959 self
.start_networkd()
962 self
.assertTrue(self
.link_exits('veth99'))
964 output
= subprocess
.check_output(['ip', 'route', 'show', 'table', '12']).rstrip().decode('utf-8')
967 self
.assertRegex(output
, 'veth99 proto dhcp')
968 self
.assertRegex(output
, '192.168.5.1')
970 def test_dhcp_route_metric(self
):
971 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-v4-server-veth-peer.network', 'dhcp-client-route-metric.network')
972 self
.start_networkd()
975 self
.assertTrue(self
.link_exits('veth99'))
977 output
= subprocess
.check_output(['ip', 'route', 'show', 'dev', 'veth99']).rstrip().decode('utf-8')
980 self
.assertRegex(output
, 'metric 24')
982 def test_dhcp_route_criticalconnection_true(self
):
983 self
.copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-v4-server-veth-peer.network', 'dhcp-client-critical-connection.network')
984 self
.start_networkd()
987 self
.assertTrue(self
.link_exits('veth99'))
989 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
992 self
.assertRegex(output
, '192.168.5.*')
993 # Stoping dnsmasq as networkd won't be allowed to renew the DHCP lease.
994 self
.stop_dnsmasq(dnsmasq_pid_file
)
996 # Sleep for 120 sec as the dnsmasq minimum lease time can only be set to 120
999 output
= subprocess
.check_output(['networkctl', 'status', 'veth99']).rstrip().decode('utf-8')
1001 self
.assertRegex(output
, '192.168.5.*')
1003 if __name__
== '__main__':
1004 unittest
.main(testRunner
=unittest
.TextTestRunner(stream
=sys
.stdout
,