]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/networkd-test.py
3 # networkd integration test
4 # This uses temporary configuration in /run and temporary veth devices, and
5 # does not write anything on disk or change any system configuration;
6 # but it assumes (and checks at the beginning) that networkd is not currently
9 # This can be run on a normal installation, in QEMU, nspawn (with
10 # --private-network), LXD (with "--config raw.lxc=lxc.aa_profile=unconfined"),
11 # or LXC system containers. You need at least the "ip" tool from the iproute
12 # package; it is recommended to install dnsmasq too to get full test coverage.
14 # ATTENTION: This uses the *installed* networkd, not the one from the built
17 # (C) 2015 Canonical Ltd.
18 # Author: Martin Pitt <martin.pitt@ubuntu.com>
20 # systemd is free software; you can redistribute it and/or modify it
21 # under the terms of the GNU Lesser General Public License as published by
22 # the Free Software Foundation; either version 2.1 of the License, or
23 # (at your option) any later version.
25 # systemd is distributed in the hope that it will be useful, but
26 # WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 # Lesser General Public License for more details.
30 # You should have received a copy of the GNU Lesser General Public License
31 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
41 networkd_active
= subprocess
.call(['systemctl', 'is-active', '--quiet',
42 'systemd-networkd']) == 0
43 have_dnsmasq
= shutil
.which('dnsmasq')
45 RESOLV_CONF
= '/run/systemd/resolve/resolv.conf'
48 @unittest.skipIf(networkd_active
,
49 'networkd is already active')
52 self
.iface
= 'test_eth42'
53 self
.if_router
= 'router_eth42'
54 self
.workdir_obj
= tempfile
.TemporaryDirectory()
55 self
.workdir
= self
.workdir_obj
.name
56 self
.config
= '/run/systemd/network/test_eth42.network'
58 # avoid "Failed to open /dev/tty" errors in containers
59 os
.environ
['SYSTEMD_LOG_TARGET'] = 'journal'
61 # determine path to systemd-networkd-wait-online
62 for p
in ['/usr/lib/systemd/systemd-networkd-wait-online',
63 '/lib/systemd/systemd-networkd-wait-online']:
65 self
.networkd_wait_online
= p
68 self
.fail('systemd-networkd-wait-online not found')
70 # get current journal cursor
71 out
= subprocess
.check_output(['journalctl', '-b', '--quiet',
72 '--no-pager', '-n0', '--show-cursor'],
73 universal_newlines
=True)
74 self
.assertTrue(out
.startswith('-- cursor:'))
75 self
.journal_cursor
= out
.split()[-1]
79 subprocess
.call(['systemctl', 'stop', 'systemd-networkd'])
81 def writeConfig(self
, fname
, contents
):
82 os
.makedirs(os
.path
.dirname(fname
), exist_ok
=True)
83 with
open(fname
, 'w') as f
:
85 self
.addCleanup(os
.remove
, fname
)
87 def show_journal(self
, unit
):
88 '''Show journal of given unit since start of the test'''
90 print('---- %s ----' % unit
)
92 subprocess
.call(['journalctl', '-b', '--no-pager', '--quiet',
93 '--cursor', self
.journal_cursor
, '-u', unit
])
95 def create_iface(self
, ipv6
=False):
96 '''Create test interface with DHCP server behind it'''
98 raise NotImplementedError('must be implemented by a subclass')
100 def shutdown_iface(self
):
101 '''Remove test interface and stop DHCP server'''
103 raise NotImplementedError('must be implemented by a subclass')
105 def print_server_log(self
):
106 '''Print DHCP server log for debugging failures'''
108 raise NotImplementedError('must be implemented by a subclass')
110 def do_test(self
, coldplug
=True, ipv6
=False, extra_opts
='',
111 online_timeout
=10, dhcp_mode
='yes'):
112 subprocess
.check_call(['systemctl', 'start', 'systemd-resolved'])
113 self
.writeConfig(self
.config
, '''\
118 %s''' % (self
.iface
, dhcp_mode
, extra_opts
))
121 # create interface first, then start networkd
122 self
.create_iface(ipv6
=ipv6
)
123 subprocess
.check_call(['systemctl', 'start', 'systemd-networkd'])
125 # start networkd first, then create interface
126 subprocess
.check_call(['systemctl', 'start', 'systemd-networkd'])
127 self
.create_iface(ipv6
=ipv6
)
130 subprocess
.check_call([self
.networkd_wait_online
, '--interface',
131 self
.iface
, '--timeout=%i' % online_timeout
])
134 # check iface state and IP 6 address; FIXME: we need to wait a bit
135 # longer, as the iface is "configured" already with IPv4 *or*
136 # IPv6, but we want to wait for both
137 for timeout
in range(10):
138 out
= subprocess
.check_output(['ip', 'a', 'show', 'dev', self
.iface
])
139 if b
'state UP' in out
and b
'inet6 2600' in out
and b
'inet 192.168' in out
:
143 self
.fail('timed out waiting for IPv6 configuration')
145 self
.assertRegex(out
, b
'inet6 2600::.* scope global .*dynamic')
146 self
.assertRegex(out
, b
'inet6 fe80::.* scope link')
148 # should have link-local address on IPv6 only
149 out
= subprocess
.check_output(['ip', '-6', 'a', 'show', 'dev', self
.iface
])
150 self
.assertRegex(out
, b
'inet6 fe80::.* scope link')
151 self
.assertNotIn(b
'scope global', out
)
153 # should have IPv4 address
154 out
= subprocess
.check_output(['ip', '-4', 'a', 'show', 'dev', self
.iface
])
155 self
.assertIn(b
'state UP', out
)
156 self
.assertRegex(out
, b
'inet 192.168.5.\d+/.* scope global dynamic')
158 # check networkctl state
159 out
= subprocess
.check_output(['networkctl'])
160 self
.assertRegex(out
, ('%s\s+ether\s+routable\s+unmanaged' % self
.if_router
).encode())
161 self
.assertRegex(out
, ('%s\s+ether\s+routable\s+configured' % self
.iface
).encode())
163 out
= subprocess
.check_output(['networkctl', 'status', self
.iface
])
164 self
.assertRegex(out
, b
'Type:\s+ether')
165 self
.assertRegex(out
, b
'State:\s+routable.*configured')
166 self
.assertRegex(out
, b
'Address:\s+192.168.5.\d+')
168 self
.assertRegex(out
, b
'2600::')
170 self
.assertNotIn(b
'2600::', out
)
171 self
.assertRegex(out
, b
'fe80::')
172 self
.assertRegex(out
, b
'Gateway:\s+192.168.5.1')
173 self
.assertRegex(out
, b
'DNS:\s+192.168.5.1')
174 except (AssertionError, subprocess
.CalledProcessError
):
175 # show networkd status, journal, and DHCP server log on failure
176 with
open(self
.config
) as f
:
177 print('\n---- %s ----\n%s' % (self
.config
, f
.read()))
178 print('---- interface status ----')
180 subprocess
.call(['ip', 'a', 'show', 'dev', self
.iface
])
181 print('---- networkctl status %s ----' % self
.iface
)
183 subprocess
.call(['networkctl', 'status', self
.iface
])
184 self
.show_journal('systemd-networkd.service')
185 self
.print_server_log()
188 for timeout
in range(50):
189 with
open(RESOLV_CONF
) as f
:
191 if 'nameserver 192.168.5.1\n' in contents
:
195 self
.fail('nameserver 192.168.5.1 not found in ' + RESOLV_CONF
)
198 # check post-down.d hook
199 self
.shutdown_iface()
201 def test_coldplug_dhcp_yes_ip4(self
):
202 # we have a 12s timeout on RA, so we need to wait longer
203 self
.do_test(coldplug
=True, ipv6
=False, online_timeout
=15)
205 def test_coldplug_dhcp_yes_ip4_no_ra(self
):
206 # with disabling RA explicitly things should be fast
207 self
.do_test(coldplug
=True, ipv6
=False,
208 extra_opts
='IPv6AcceptRA=False')
210 def test_coldplug_dhcp_ip4_only(self
):
211 # we have a 12s timeout on RA, so we need to wait longer
212 self
.do_test(coldplug
=True, ipv6
=False, dhcp_mode
='ipv4',
215 def test_coldplug_dhcp_ip4_only_no_ra(self
):
216 # with disabling RA explicitly things should be fast
217 self
.do_test(coldplug
=True, ipv6
=False, dhcp_mode
='ipv4',
218 extra_opts
='IPv6AcceptRA=False')
220 def test_coldplug_dhcp_ip6(self
):
221 self
.do_test(coldplug
=True, ipv6
=True)
223 def test_hotplug_dhcp_ip4(self
):
224 # With IPv4 only we have a 12s timeout on RA, so we need to wait longer
225 self
.do_test(coldplug
=False, ipv6
=False, online_timeout
=15)
227 def test_hotplug_dhcp_ip6(self
):
228 self
.do_test(coldplug
=False, ipv6
=True)
230 def test_route_only_dns(self
):
231 self
.writeConfig('/run/systemd/network/myvpn.netdev', '''\
235 MACAddress=12:34:56:78:9a:bc''')
236 self
.writeConfig('/run/systemd/network/myvpn.network', '''\
240 Address=192.168.42.100
242 Domains= ~company''')
244 self
.do_test(coldplug
=True, ipv6
=False,
245 extra_opts
='IPv6AcceptRouterAdvertisements=False')
247 with
open(RESOLV_CONF
) as f
:
249 # ~company is not a search domain, only a routing domain
250 self
.assertNotRegex(contents
, 'search.*company')
251 # our global server should appear
252 self
.assertIn('nameserver 192.168.5.1\n', contents
)
255 @unittest.skipUnless(have_dnsmasq
, 'dnsmasq not installed')
256 class DnsmasqClientTest(ClientTestBase
, unittest
.TestCase
):
257 '''Test networkd client against dnsmasq'''
263 def create_iface(self
, ipv6
=False):
264 '''Create test interface with DHCP server behind it'''
267 subprocess
.check_call(['ip', 'link', 'add', 'name', self
.iface
, 'type',
268 'veth', 'peer', 'name', self
.if_router
])
270 # give our router an IP
271 subprocess
.check_call(['ip', 'a', 'flush', 'dev', self
.if_router
])
272 subprocess
.check_call(['ip', 'a', 'add', '192.168.5.1/24', 'dev', self
.if_router
])
274 subprocess
.check_call(['ip', 'a', 'add', '2600::1/64', 'dev', self
.if_router
])
275 subprocess
.check_call(['ip', 'link', 'set', self
.if_router
, 'up'])
278 self
.dnsmasq_log
= os
.path
.join(self
.workdir
, 'dnsmasq.log')
279 lease_file
= os
.path
.join(self
.workdir
, 'dnsmasq.leases')
281 extra_opts
= ['--enable-ra', '--dhcp-range=2600::10,2600::20']
284 self
.dnsmasq
= subprocess
.Popen(
285 ['dnsmasq', '--keep-in-foreground', '--log-queries',
286 '--log-facility=' + self
.dnsmasq_log
, '--conf-file=/dev/null',
287 '--dhcp-leasefile=' + lease_file
, '--bind-interfaces',
288 '--interface=' + self
.if_router
, '--except-interface=lo',
289 '--dhcp-range=192.168.5.10,192.168.5.200'] + extra_opts
)
291 def shutdown_iface(self
):
292 '''Remove test interface and stop DHCP server'''
295 subprocess
.check_call(['ip', 'link', 'del', 'dev', self
.if_router
])
296 self
.if_router
= None
302 def print_server_log(self
):
303 '''Print DHCP server log for debugging failures'''
305 with
open(self
.dnsmasq_log
) as f
:
306 sys
.stdout
.write('\n\n---- dnsmasq log ----\n%s\n------\n\n' % f
.read())
309 class NetworkdClientTest(ClientTestBase
, unittest
.TestCase
):
310 '''Test networkd client against networkd server'''
316 def create_iface(self
, ipv6
=False):
317 '''Create test interface with DHCP server behind it'''
319 # run "router-side" networkd in own mount namespace to shield it from
320 # "client-side" configuration and networkd
321 (fd
, script
) = tempfile
.mkstemp(prefix
='networkd-router.sh')
322 self
.addCleanup(os
.remove
, script
)
323 with os
.fdopen(fd
, 'w+') as f
:
326 mkdir -p /run/systemd/network
327 mkdir -p /run/systemd/netif
328 mount -t tmpfs none /run/systemd/network
329 mount -t tmpfs none /run/systemd/netif
330 [ ! -e /run/dbus ] || mount -t tmpfs none /run/dbus
331 # create router/client veth pair
332 cat << EOF > /run/systemd/network/test.netdev
341 cat << EOF > /run/systemd/network/test.network
346 Address=192.168.5.1/24
356 # run networkd as in systemd-networkd.service
357 exec $(systemctl cat systemd-networkd.service | sed -n '/^ExecStart=/ { s/^.*=//; p}')
358 ''' % {'ifr': self
.if_router
, 'ifc': self
.iface
, 'addr6': ipv6
and 'Address=2600::1/64' or ''})
362 subprocess
.check_call(['systemd-run', '--unit=networkd-test-router.service',
363 '-p', 'InaccessibleDirectories=-/etc/systemd/network',
364 '-p', 'InaccessibleDirectories=-/run/systemd/network',
365 '-p', 'InaccessibleDirectories=-/run/systemd/netif',
366 '--service-type=notify', script
])
368 # wait until devices got created
369 for timeout
in range(50):
370 out
= subprocess
.check_output(['ip', 'a', 'show', 'dev', self
.if_router
])
371 if b
'state UP' in out
and b
'scope global' in out
:
375 def shutdown_iface(self
):
376 '''Remove test interface and stop DHCP server'''
379 subprocess
.check_call(['systemctl', 'stop', 'networkd-test-router.service'])
380 # ensure failed transient unit does not stay around
381 subprocess
.call(['systemctl', 'reset-failed', 'networkd-test-router.service'])
382 subprocess
.call(['ip', 'link', 'del', 'dev', self
.if_router
])
383 self
.if_router
= None
385 def print_server_log(self
):
386 '''Print DHCP server log for debugging failures'''
388 self
.show_journal('networkd-test-router.service')
390 @unittest.skip('networkd does not have DHCPv6 server support')
391 def test_hotplug_dhcp_ip6(self
):
394 @unittest.skip('networkd does not have DHCPv6 server support')
395 def test_coldplug_dhcp_ip6(self
):
398 def test_search_domains(self
):
400 # we don't use this interface for this test
401 self
.if_router
= None
403 self
.writeConfig('/run/systemd/network/test.netdev', '''\
407 MACAddress=12:34:56:78:9a:bc''')
408 self
.writeConfig('/run/systemd/network/test.network', '''\
412 Address=192.168.42.100
414 Domains= one two three four five six seven eight nine ten''')
416 subprocess
.check_call(['systemctl', 'start', 'systemd-networkd'])
418 for timeout
in range(50):
419 with
open(RESOLV_CONF
) as f
:
421 if ' one' in contents
:
424 self
.assertRegex(contents
, 'search .*one two three four')
425 self
.assertNotIn('seven\n', contents
)
426 self
.assertIn('# Too many search domains configured, remaining ones ignored.\n', contents
)
428 def test_search_domains_too_long(self
):
430 # we don't use this interface for this test
431 self
.if_router
= None
433 name_prefix
= 'a' * 60
435 self
.writeConfig('/run/systemd/network/test.netdev', '''\
439 MACAddress=12:34:56:78:9a:bc''')
440 self
.writeConfig('/run/systemd/network/test.network', '''\
444 Address=192.168.42.100
446 Domains={p}0 {p}1 {p}2 {p}3 {p}4'''.format(p
=name_prefix
))
448 subprocess
.check_call(['systemctl', 'start', 'systemd-networkd'])
450 for timeout
in range(50):
451 with
open(RESOLV_CONF
) as f
:
453 if ' one' in contents
:
456 self
.assertRegex(contents
, 'search .*{p}0 {p}1 {p}2'.format(p
=name_prefix
))
457 self
.assertIn('# Total length of all search domains is too long, remaining ones ignored.', contents
)
459 if __name__
== '__main__':
460 unittest
.main(testRunner
=unittest
.TextTestRunner(stream
=sys
.stdout
,