1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-16 09:57:26 +03:00

Merge pull request #11084 from poettering/networkd-test-fix

unbreak networkd-test.py
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-11 11:37:05 +01:00 committed by GitHub
commit 3bf72311c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 137 additions and 12 deletions

24
test/mkosi.build.networkd-test Executable file
View File

@ -0,0 +1,24 @@
#!/bin/sh
set -ex
# First, source in the main build script
. "$SRCDIR"/mkosi.build
mkdir -p "$DESTDIR"/usr/local/bin
cp "$SRCDIR"/test/networkd-test.py "$DESTDIR"/usr/local/bin/networkd-test.py
mkdir -p "$DESTDIR"/etc/systemd/system
cat > "$DESTDIR"/etc/systemd/system/networkd-test.service <<EOF
[Unit]
Description=networkd test service
SuccessAction=exit
FailureAction=exit
[Service]
ExecStart=/usr/local/bin/networkd-test.py
EOF
mkdir -p "$DESTDIR"/etc/systemd/system/multi-user.target.wants/
ln -s ../networkd-test.service "$DESTDIR"/etc/systemd/system/multi-user.target.wants/
systemctl --root="$DESTDIR" disable systemd-networkd.service

View File

@ -0,0 +1,80 @@
# Puts together an nspawn container and runs networkd-test.py in it, inside a
# network namespace and everything. Run this with "mkosi
# --default=mkosi.default.networkd-test boot". This will start the test and
# eventually exit with sucess in case the test succeeded.
[Distribution]
Distribution=fedora
Release=29
[Output]
Format=raw_btrfs
Bootable=yes
KernelCommandLine=printk.devkmsg=on
OutputDirectory=../mkosi.output
Output=networkd-test.raw
[Partitions]
RootSize=3G
[Packages]
BuildPackages=
audit-libs-devel
bzip2-devel
cryptsetup-devel
dbus-devel
diffutils
docbook-style-xsl
elfutils-devel
gcc
gettext
git
gnu-efi
gnu-efi-devel
gnutls-devel
gperf
hostname
iptables-devel
kmod-devel
libacl-devel
libblkid-devel
libcap-devel
libcurl-devel
libgcrypt-devel
libidn2-devel
libmicrohttpd-devel
libmount-devel
libseccomp-devel
libselinux-devel
libtool
libxkbcommon-devel
libxslt
lz4
lz4-devel
m4
meson
pam-devel
pcre2-devel
pkgconfig
python3-devel
python3-lxml
qrencode-devel
tree
xz-devel
Packages=
dnsmasq
iproute
libidn2
polkit
python3
# Share caches with the top-level mkosi
BuildDirectory=../mkosi.builddir
Cache=../mkosi.cache
# Run our own script
BuildScript=mkosi.build.networkd-test
BuildSources=..
NSpawnSettings=mkosi.nspawn.networkd-test

View File

@ -0,0 +1,2 @@
[Network]
Private=yes

View File

@ -53,6 +53,7 @@ def setUpModule():
if (subprocess.call(['systemctl', 'is-active', '--quiet', 'systemd-networkd.service']) == 0 and
subprocess.call(['systemd-detect-virt', '--quiet']) != 0):
raise unittest.SkipTest('not virtualized and networkd is already active')
# Ensure we don't mess with an existing networkd config
for u in ['systemd-networkd.socket', 'systemd-networkd', 'systemd-resolved']:
if subprocess.call(['systemctl', 'is-active', '--quiet', u]) == 0:
@ -60,6 +61,12 @@ def setUpModule():
running_units.append(u)
else:
stopped_units.append(u)
# create static systemd-network user for networkd-test-router.service (it
# needs to do some stuff as root and can't start as user; but networkd
# still insists on the user)
subprocess.call(['adduser', '--system', '--no-create-home', 'systemd-network'])
for d in ['/etc/systemd/network', '/run/systemd/network',
'/run/systemd/netif', '/run/systemd/resolve']:
if os.path.isdir(d):
@ -68,6 +75,9 @@ def setUpModule():
if os.path.isdir('/run/systemd/resolve'):
os.chmod('/run/systemd/resolve', 0o755)
shutil.chown('/run/systemd/resolve', 'systemd-resolve', 'systemd-resolve')
if os.path.isdir('/run/systemd/netif'):
os.chmod('/run/systemd/netif', 0o755)
shutil.chown('/run/systemd/netif', 'systemd-network', 'systemd-network')
# Avoid "Failed to open /dev/tty" errors in containers.
os.environ['SYSTEMD_LOG_TARGET'] = 'journal'
@ -75,11 +85,6 @@ def setUpModule():
# Ensure the unit directory exists so tests can dump files into it.
os.makedirs(NETWORK_UNITDIR, exist_ok=True)
# create static systemd-network user for networkd-test-router.service (it
# needs to do some stuff as root and can't start as user; but networkd
# still insists on the user)
subprocess.check_call(['adduser', '--system', '--no-create-home', 'systemd-network'])
def tearDownModule():
global tmpmounts
@ -106,13 +111,17 @@ class NetworkdTestingUtilities:
list(peer_options))
self.addCleanup(subprocess.call, ['ip', 'link', 'del', 'dev', peer])
def write_config(self, path, contents):
""""Write a configuration file, and queue it to be removed."""
with open(path, 'w') as f:
f.write(contents)
self.addCleanup(os.remove, path)
def write_network(self, unit_name, contents):
"""Write a network unit file, and queue it to be removed."""
unit_path = os.path.join(NETWORK_UNITDIR, unit_name)
with open(unit_path, 'w') as unit:
unit.write(contents)
self.addCleanup(os.remove, unit_path)
self.write_config(os.path.join(NETWORK_UNITDIR, unit_name), contents)
def write_network_dropin(self, unit_name, dropin_name, contents):
"""Write a network unit drop-in, and queue it to be removed."""
@ -700,6 +709,7 @@ Domains= ~company ~lab''')
subprocess.check_call(['mount', '--bind', '/dev/null', '/etc/hostname'])
self.addCleanup(subprocess.call, ['umount', '/etc/hostname'])
subprocess.check_call(['systemctl', 'stop', 'systemd-hostnamed.service'])
self.addCleanup(subprocess.call, ['systemctl', 'stop', 'systemd-hostnamed.service'])
self.create_iface(dnsmasq_opts=['--dhcp-host={},192.168.5.210,testgreen'.format(self.iface_mac)])
self.do_test(coldplug=None, extra_opts='IPv6AcceptRA=False', dhcp_mode='ipv4')
@ -732,9 +742,18 @@ Domains= ~company ~lab''')
orig_hostname = socket.gethostname()
self.addCleanup(socket.sethostname, orig_hostname)
if not os.path.exists('/etc/hostname'):
self.writeConfig('/etc/hostname', orig_hostname)
self.write_config('/etc/hostname', "foobarqux")
else:
self.write_config('/run/hostname.tmp', "foobarqux")
subprocess.check_call(['mount', '--bind', '/run/hostname.tmp', '/etc/hostname'])
self.addCleanup(subprocess.call, ['umount', '/etc/hostname'])
socket.sethostname("foobarqux");
subprocess.check_call(['systemctl', 'stop', 'systemd-hostnamed.service'])
self.addCleanup(subprocess.call, ['systemctl', 'stop', 'systemd-hostnamed.service'])
self.create_iface(dnsmasq_opts=['--dhcp-host={},192.168.5.210,testgreen'.format(self.iface_mac)])
self.do_test(coldplug=None, extra_opts='IPv6AcceptRA=False', dhcp_mode='ipv4')
@ -744,7 +763,7 @@ Domains= ~company ~lab''')
out = subprocess.check_output(['ip', '-4', 'a', 'show', 'dev', self.iface])
self.assertRegex(out, b'inet 192.168.5.210/24 .* scope global dynamic')
# static hostname wins over transient one, thus *not* applied
self.assertEqual(socket.gethostname(), orig_hostname)
self.assertEqual(socket.gethostname(), "foobarqux")
except AssertionError:
self.show_journal('systemd-networkd.service')
self.show_journal('systemd-hostnamed.service')