test: Fix networkd test for an already running service
Mount tmpfses over the networkd and resolved config and state directories, and stop the services beforehand. This ensures that the test does not mess with an existing networkd/resolved setup. At least for ethernet setups, this does not sever existing links, so is good enough for the CI cases we are interested in (QEMU and LXC). Relax the skip check to only skip the test when trying to run this on real iron, but start running it in virtual machines now. This allows us to run the test on Ubuntu 18.04 LTS in CI, which uses both services by default.
This commit is contained in:
parent
207f5f4d93
commit
c4a0a2d5f8
|
@ -37,16 +37,36 @@ NETWORKD_WAIT_ONLINE = shutil.which('systemd-networkd-wait-online',
|
||||||
|
|
||||||
RESOLV_CONF = '/run/systemd/resolve/resolv.conf'
|
RESOLV_CONF = '/run/systemd/resolve/resolv.conf'
|
||||||
|
|
||||||
|
tmpmounts = []
|
||||||
|
running_units = []
|
||||||
|
stopped_units = []
|
||||||
|
|
||||||
|
|
||||||
def setUpModule():
|
def setUpModule():
|
||||||
|
global tmpmounts
|
||||||
|
|
||||||
"""Initialize the environment, and perform sanity checks on it."""
|
"""Initialize the environment, and perform sanity checks on it."""
|
||||||
if NETWORKD_WAIT_ONLINE is None:
|
if NETWORKD_WAIT_ONLINE is None:
|
||||||
raise OSError(errno.ENOENT, 'systemd-networkd-wait-online not found')
|
raise OSError(errno.ENOENT, 'systemd-networkd-wait-online not found')
|
||||||
|
|
||||||
# Do not run any tests if the system is using networkd already.
|
# Do not run any tests if the system is using networkd already and it's not virtualized
|
||||||
if subprocess.call(['systemctl', 'is-active', '--quiet',
|
if (subprocess.call(['systemctl', 'is-active', '--quiet', 'systemd-networkd.service']) == 0 and
|
||||||
'systemd-networkd.service']) == 0:
|
subprocess.call(['systemd-detect-virt', '--quiet']) != 0):
|
||||||
raise unittest.SkipTest('networkd is already active')
|
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:
|
||||||
|
subprocess.call(['systemctl', 'stop', u])
|
||||||
|
running_units.append(u)
|
||||||
|
else:
|
||||||
|
stopped_units.append(u)
|
||||||
|
for d in ['/etc/systemd/network', '/run/systemd/network',
|
||||||
|
'/run/systemd/netif', '/run/systemd/resolve']:
|
||||||
|
if os.path.isdir(d):
|
||||||
|
subprocess.check_call(["mount", "-t", "tmpfs", "none", d])
|
||||||
|
tmpmounts.append(d)
|
||||||
|
if os.path.isdir('/run/systemd/resolve'):
|
||||||
|
os.chmod('/run/systemd/resolve', 0o755)
|
||||||
|
|
||||||
# Avoid "Failed to open /dev/tty" errors in containers.
|
# Avoid "Failed to open /dev/tty" errors in containers.
|
||||||
os.environ['SYSTEMD_LOG_TARGET'] = 'journal'
|
os.environ['SYSTEMD_LOG_TARGET'] = 'journal'
|
||||||
|
@ -60,6 +80,16 @@ def setUpModule():
|
||||||
subprocess.check_call(['adduser', '--system', '--no-create-home', 'systemd-network'])
|
subprocess.check_call(['adduser', '--system', '--no-create-home', 'systemd-network'])
|
||||||
|
|
||||||
|
|
||||||
|
def tearDownModule():
|
||||||
|
global tmpmounts
|
||||||
|
for d in tmpmounts:
|
||||||
|
subprocess.check_call(["umount", d])
|
||||||
|
for u in stopped_units:
|
||||||
|
subprocess.call(["systemctl", "stop", u])
|
||||||
|
for u in running_units:
|
||||||
|
subprocess.call(["systemctl", "restart", u])
|
||||||
|
|
||||||
|
|
||||||
class NetworkdTestingUtilities:
|
class NetworkdTestingUtilities:
|
||||||
"""Provide a set of utility functions to facilitate networkd tests.
|
"""Provide a set of utility functions to facilitate networkd tests.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue