1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
#!/usr/bin/python3
# autopkgtest check: Boot with systemd and check critical desktop services
# (C) 2014 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
import platform
import sys
import os
import unittest
import subprocess
import tempfile
import shutil
import time
import re
from glob import glob
is_container = subprocess.call(['systemd-detect-virt', '--container']) == 0
def wait_unit_stop(unit, timeout=10):
'''Wait until given unit is not running any more
Raise RuntimeError on timeout.
'''
for i in range(timeout):
if subprocess.call(['systemctl', 'is-active', '--quiet', unit]) != 0:
return
time.sleep(1)
raise RuntimeError('Timed out waiting for %s to stop' % unit)
class ServicesTest(unittest.TestCase):
'''Check that expected services are running'''
def test_0_init(self):
'''Verify that init is systemd'''
self.assertIn('systemd', os.readlink('/proc/1/exe'))
@unittest.skipUnless(shutil.which('gdm3') is not None, 'gdm3 not found')
def test_gdm3(self):
if 'lxc' in subprocess.run(['systemd-detect-virt', '--container'], check=False, stdout=subprocess.PIPE).stdout.decode():
# this test often fails in tests-in-lxd
self.skipTest('gdm3 is flaky under lxd')
subprocess.check_call(['pgrep', '-af', '/gdm[-3]'])
self.active_unit('gdm')
def test_dbus(self):
out = subprocess.check_output(
['dbus-send', '--print-reply', '--system',
'--dest=org.freedesktop.DBus', '/', 'org.freedesktop.DBus.GetId'])
self.assertIn(b'string "', out)
self.active_unit('dbus')
def test_network_manager(self):
# 0.9.10 changed the command name
_help = subprocess.check_output(['nmcli', '--help'],
stderr=subprocess.STDOUT)
if b' g[eneral]' in _help:
out = subprocess.check_output(['nmcli', 'general'])
else:
out = subprocess.check_output(['nmcli', 'nm'])
self.assertIn(b'enabled', out)
self.active_unit('NetworkManager')
def test_cron(self):
pid = subprocess.check_output(['pidof', 'cron'], universal_newlines=True).strip()
out = subprocess.check_output(['ps', 'u', pid], universal_newlines=True)
self.assertIn('root', out)
self.active_unit('cron')
def test_logind(self):
out = subprocess.check_output(['loginctl'])
self.assertNotEqual(b'', out)
self.active_unit('systemd-logind')
@unittest.skipIf('pkg.systemd.upstream' in os.environ.get('DEB_BUILD_PROFILES', ''),
'Forwarding to rsyslog is a Debian patch')
def test_rsyslog(self):
pid = subprocess.check_output(['pidof', 'rsyslogd'], universal_newlines=True).strip()
out = subprocess.check_output(['ps', 'u', pid], universal_newlines=True)
self.assertIn('bin/rsyslogd', out)
self.active_unit('rsyslog')
subprocess.check_call(
['systemd-run',
'--quiet',
'--wait',
'--unit',
'test-boot-and-services-rsyslog.service',
'--',
'echo',
'hello rsyslog'
]
)
subprocess.check_call(['journalctl', '--sync'])
with open('/var/log/syslog') as f:
log = f.read()
if not is_container:
# has kernel messages
self.assertRegex(log, 'kernel:.*')
# has init messages
self.assertRegex(log, 'systemd.*Reached target')
# has other services
self.assertRegex(log, 'echo.*: hello rsyslog')
self.assertRegex(log, 'test-boot-and-services-rsyslog.service.*:')
@unittest.skipIf(is_container, 'udev does not work in containers')
def test_udev(self):
out = subprocess.check_output(['udevadm', 'info', '--export-db'])
self.assertIn(b'\nP: /devices/', out)
self.active_unit('systemd-udevd')
@unittest.skipIf('pkg.systemd.upstream' in os.environ.get('DEB_BUILD_PROFILES', ''),
'Debian specific configuration, N/A for upstream')
def test_tmp_cleanup(self):
# autopkgtest overrides tmp.mount with empty /etc/systemd/system/tmp.mount
# as a workaround for issues where /tmp is filled up too easily. LP: #2069834
try:
if os.stat('/etc/systemd/system/tmp.mount').st_size == 0:
self.skipTest('autopkgtest environment has overridden tmp.mount')
except FileNotFoundError:
pass
# systemd-tmpfiles-clean.timer only runs 15 mins after boot, shortcut
# it
self.assertEqual(subprocess.call(
['systemctl', 'status', 'systemd-tmpfiles-clean.timer'],
stdout=subprocess.PIPE), 0)
subprocess.check_call(['systemctl', 'start', 'systemd-tmpfiles-clean'])
if not is_container:
# all files in /tmp/ should get cleaned up on boot
self.assertFalse(os.path.exists('/tmp/oldfile.test'))
self.assertFalse(os.path.exists('/tmp/newfile.test'))
# files in /var/tmp/ older than 30d should get cleaned up, unless legacy
# compat tmpfiles.d is installed
if not is_container and not os.path.exists('/etc/tmpfiles.d/tmp.conf'):
self.assertFalse(os.path.exists('/var/tmp/oldfile.test'))
self.assertTrue(os.path.exists('/var/tmp/newfile.test'))
# next run should leave the recent ones
os.close(os.open('/tmp/newfile.test',
os.O_CREAT | os.O_EXCL | os.O_WRONLY))
subprocess.check_call(['systemctl', 'start', 'systemd-tmpfiles-clean'])
self.assertTrue(os.path.exists('/tmp/newfile.test'))
# Helper methods
def active_unit(self, unit):
'''Check that given unit is active'''
out = subprocess.check_output(['systemctl', 'status', unit])
self.assertIn(b'active (running)', out)
class JournalTest(unittest.TestCase):
'''Check journal functionality'''
def test_no_options(self):
out = subprocess.check_output(['journalctl'])
if not is_container:
# has kernel messages
self.assertRegex(out, b'kernel:.*')
# has init messages
self.assertRegex(out, b'systemd.*Reached target(?: graphical.target -)? Graphical Interface')
# has other services
self.assertRegex(out, b'NetworkManager.*:.*starting')
def test_log_for_service(self):
out = subprocess.check_output(
['journalctl', '_SYSTEMD_UNIT=NetworkManager.service'])
self.assertRegex(out, b'NetworkManager.*:.*starting')
self.assertNotIn(b'kernel:', out)
self.assertNotIn(b'systemd:', out)
@unittest.skipIf(is_container, 'nspawn does not work in most containers')
class NspawnTest(unittest.TestCase):
'''Check nspawn'''
@classmethod
def setUpClass(kls):
'''Build a bootable busybox mini-container'''
kls.td_c_busybox = tempfile.TemporaryDirectory(prefix='c_busybox.')
kls.c_busybox = kls.td_c_busybox.name
for d in ['etc/init.d', 'bin', 'sbin']:
os.makedirs(os.path.join(kls.c_busybox, d))
shutil.copy('/bin/busybox', os.path.join(kls.c_busybox, 'bin'))
shutil.copy('/etc/os-release', os.path.join(kls.c_busybox, 'etc'))
os.symlink('busybox', os.path.join(kls.c_busybox, 'bin', 'sh'))
os.symlink('../bin/busybox', os.path.join(kls.c_busybox, 'sbin/init'))
with open(os.path.join(kls.c_busybox, 'etc/init.d/rcS'), 'w') as f:
f.write('''#!/bin/sh
echo fake container started
ps aux
poweroff\n''')
os.fchmod(f.fileno(), 0o755)
subprocess.check_call(['systemd-machine-id-setup', '--root',
kls.c_busybox], stderr=subprocess.PIPE)
def setUp(self):
self.workdir = tempfile.TemporaryDirectory()
def test_boot(self):
cont = os.path.join(self.workdir.name, 'c1')
shutil.copytree(self.c_busybox, cont, symlinks=True)
os.sync()
nspawn = subprocess.Popen(['systemd-nspawn', '-D', cont, '-b'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out = nspawn.communicate(timeout=60)[0]
self.assertIn(b'Spawning container c1', out)
self.assertIn(b'fake container started', out)
self.assertRegex(out, rb'\n\s+1\s+0\s+init[\r\n]')
self.assertRegex(out, rb'\n\s+2+\s+0\s.*rcS[\r\n]')
self.assertRegex(out, b'Container c1.*shut down')
self.assertEqual(nspawn.returncode, 0)
def test_service(self):
self.assertTrue(os.path.isdir('/var/lib/machines'))
cont = '/var/lib/machines/c1'
shutil.copytree(self.c_busybox, cont, symlinks=True)
self.addCleanup(shutil.rmtree, cont)
os.sync()
subprocess.check_call(['systemctl', 'start', 'systemd-nspawn@c1'])
wait_unit_stop('systemd-nspawn@c1')
subprocess.call(['journalctl', '--sync'])
systemctl = subprocess.Popen(
['systemctl', 'status', '-overbose', '-l', 'systemd-nspawn@c1'],
stdout=subprocess.PIPE)
out = systemctl.communicate()[0].decode('UTF-8', 'replace')
self.assertEqual(systemctl.returncode, 3, out)
self.assertNotIn('failed', out)
@unittest.skipUnless(os.path.exists('/sys/kernel/security/apparmor'),
'AppArmor not enabled')
@unittest.skipIf(is_container and platform.machine().startswith('arm'), 'fails on armhf testbeds, see LP: #1842352')
class AppArmorTest(unittest.TestCase):
def test_profile(self):
'''AppArmor confined unit'''
# create AppArmor profile
aa_profile = tempfile.NamedTemporaryFile(prefix='aa_violator.')
aa_profile.write(b'''#include <tunables/global>
profile "violator-test" {
#include <abstractions/base>
/{usr/,}bin/** rix,
/etc/machine-id r,
}
''')
aa_profile.flush()
subprocess.check_call(['apparmor_parser', '-r', '-v', aa_profile.name])
# create confined unit
with open('/run/systemd/system/violator.service', 'w') as f:
f.write('''[Unit]
Description=AppArmor test
[Service]
ExecStart=/bin/sh -euc 'echo CP1; cat /etc/machine-id; echo CP2; if cat /etc/passwd; then exit 1; fi; echo CP3'
AppArmorProfile=violator-test
''')
self.addCleanup(os.unlink, '/run/systemd/system/violator.service')
# launch
subprocess.check_call(['systemctl', 'daemon-reload'])
subprocess.check_call(['systemctl', 'start', 'violator.service'])
wait_unit_stop('violator.service')
# check status
st = subprocess.Popen(['systemctl', 'status', '-l',
'violator.service'], stdout=subprocess.PIPE,
universal_newlines=True)
out = st.communicate()[0]
# unit should be stopped
self.assertEqual(st.returncode, 3)
self.assertIn('inactive', out)
self.assertIn('CP1', out)
self.assertIn('CP2', out)
self.assertIn('CP3', out)
with open('/etc/machine-id') as f:
self.assertIn(f.read().strip(), out)
self.assertNotIn('root:x', out, 'unit can read /etc/passwd')
@unittest.skipIf(os.path.exists('/sys/fs/cgroup/cgroup.controllers'),
'test needs to be reworked on unified cgroup hierarchy')
class CgroupsTest(unittest.TestCase):
'''Check cgroup setup'''
@classmethod
def setUpClass(kls):
kls.controllers = []
for controller in glob('/sys/fs/cgroup/*'):
if not os.path.islink(controller):
kls.controllers.append(controller)
def setUp(self):
self.service = 'testsrv.service'
self.service_file = '/run/systemd/system/' + self.service
def tearDown(self):
subprocess.call(['systemctl', 'stop', self.service],
stderr=subprocess.PIPE)
try:
os.unlink(self.service_file)
except OSError:
pass
subprocess.check_call(['systemctl', 'daemon-reload'])
def create_service(self, extra_service=''):
'''Create test service unit'''
with open(self.service_file, 'w') as f:
f.write('''[Unit]
Description=test service
[Service]
ExecStart=/bin/sleep 500
%s
''' % extra_service)
subprocess.check_call(['systemctl', 'daemon-reload'])
def assertNoControllers(self):
'''Assert that no cgroup controllers exist for test service'''
cs = glob('/sys/fs/cgroup/*/system.slice/%s' % self.service)
self.assertEqual(cs, [])
def assertController(self, name):
'''Assert that cgroup controller exists for test service'''
c = '/sys/fs/cgroup/%s/system.slice/%s' % (name, self.service)
self.assertTrue(os.path.isdir(c))
def assertNoController(self, name):
'''Assert that cgroup controller does not exist for test service'''
c = '/sys/fs/cgroup/%s/system.slice/%s' % (name, self.service)
self.assertFalse(os.path.isdir(c))
def test_simple(self):
'''simple service'''
self.create_service()
self.assertNoControllers()
subprocess.check_call(['systemctl', 'start', self.service])
self.assertController('systemd')
subprocess.check_call(['systemctl', 'stop', self.service])
self.assertNoControllers()
def test_cpushares(self):
'''service with CPUShares'''
self.create_service('CPUShares=1000')
self.assertNoControllers()
subprocess.check_call(['systemctl', 'start', self.service])
self.assertController('systemd')
self.assertController('cpu,cpuacct')
subprocess.check_call(['systemctl', 'stop', self.service])
self.assertNoControllers()
class SeccompTest(unittest.TestCase):
'''Check seccomp syscall filtering'''
def test_failing(self):
with open('/run/systemd/system/scfail.service', 'w') as f:
f.write('''[Unit]
Description=seccomp test
[Service]
ExecStart=/bin/cat /etc/machine-id
SystemCallFilter=access
''')
self.addCleanup(os.unlink, '/run/systemd/system/scfail.service')
# launch
subprocess.check_call(['systemctl', 'daemon-reload'])
subprocess.check_call(['systemctl', 'start', 'scfail.service'])
wait_unit_stop('scfail.service')
# check status
st = subprocess.Popen(['systemctl', 'status', '-l',
'scfail.service'], stdout=subprocess.PIPE)
out = st.communicate()[0]
# unit should be stopped
self.assertEqual(st.returncode, 3)
subprocess.check_call(['systemctl', 'reset-failed', 'scfail.service'])
self.assertIn(b'failed', out)
self.assertRegex(out, b'code=(killed|dumped), signal=SYS')
with open('/etc/machine-id') as f:
self.assertNotIn(f.read().strip().encode('ASCII'), out)
@unittest.skipIf(is_container, 'systemd-coredump does not work in containers')
class CoredumpTest(unittest.TestCase):
'''Check systemd-coredump'''
def test_bash_crash(self):
subprocess.call("ulimit -c unlimited; bash -c 'kill -SEGV $$'", shell=True,
cwd='/tmp', stderr=subprocess.DEVNULL)
# with systemd-coredump installed we should get the core dumps in
# systemd's dir
for timeout in range(50):
cores = glob('/var/lib/systemd/coredump/core.bash.*')
if cores:
break
time.sleep(1)
self.assertNotEqual(cores, [])
self.assertEqual(glob('/tmp/core*'), [])
# we should also get a message and stack trace in journal
for timeout in range(10):
subprocess.call(['journalctl', '--sync'])
journal = subprocess.check_output(['journalctl', '-t', 'systemd-coredump'])
if re.search(b'Process.*bash.*dumped core', journal):
break
time.sleep(1)
self.assertRegex(journal, b'Process.*bash.*dumped core')
self.assertIn(b'Stack trace', journal)
class CLITest(unittest.TestCase):
def setUp(self):
self.programs = []
for line in subprocess.check_output(['dpkg', '-L', 'systemd', 'systemd-container', 'systemd-coredump', 'udev'],
universal_newlines=True).splitlines():
if '/bin/' in line:
self.programs.append(line.strip())
def test_help(self):
'--help works and succeeds'''
for program in self.programs:
p = subprocess.Popen([program, '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
(out, err) = p.communicate()
try:
self.assertEqual(err, '')
self.assertEqual(p.returncode, 0)
self.assertIn(os.path.basename(program), out)
self.assertTrue('--help' in out or 'Usage' in out, out)
except AssertionError:
print('Failed program: %s' % program)
raise
def test_version(self):
'--version works and succeeds'''
version = subprocess.check_output(['pkg-config', '--modversion', 'systemd'],
universal_newlines=True).strip()
for program in self.programs:
# known to not respond to --version
if os.path.basename(program) in ['kernel-install', 'systemd-ask-password', 'systemd-stdio-bridge']:
continue
p = subprocess.Popen([program, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
(out, err) = p.communicate()
try:
self.assertEqual(err, '')
self.assertEqual(p.returncode, 0)
self.assertIn(version, out)
except AssertionError:
print('Failed program: %s' % program)
raise
def test_invalid_option(self):
'''Calling with invalid option fails'''
for program in self.programs:
p = subprocess.Popen([program, '--invalid-option'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
(out, err) = p.communicate()
try:
# kernel-install is an unique snowflake
if not program.endswith('/kernel-install'):
self.assertIn('--invalid-option', err)
self.assertNotEqual(p.returncode, 0)
except AssertionError:
print('Failed program: %s' % program)
raise
def pre_boot_setup():
'''Test setup before rebooting testbed'''
subprocess.check_call(['systemctl', 'set-default', 'graphical.target'],
stderr=subprocess.STDOUT)
# This test installs network-manager, which seems to cause
# systemd-networkd-wait-online to be stuck as they conflict,
# so systemctl start network-online.target ran by autopkgtest
# gets stuck, at least in Debian Bullseye images.
# https://salsa.debian.org/ci-team/autopkgtest/-/blob/debian/5.21/virt/autopkgtest-virt-lxc#L131
subprocess.check_call(['systemctl', 'disable', 'systemd-networkd.service'],
stderr=subprocess.STDOUT)
# create a few temporary files to ensure that they get cleaned up on boot
os.close(os.open('/tmp/newfile.test',
os.O_CREAT | os.O_EXCL | os.O_WRONLY))
os.close(os.open('/var/tmp/newfile.test',
os.O_CREAT | os.O_EXCL | os.O_WRONLY))
# we can't use utime() here, as systemd looks for ctime
if not is_container:
cur_time = time.clock_gettime(time.CLOCK_REALTIME)
time.clock_settime(time.CLOCK_REALTIME, cur_time - 2 * 30 * 86400)
try:
os.close(os.open('/tmp/oldfile.test',
os.O_CREAT | os.O_EXCL | os.O_WRONLY))
os.close(os.open('/var/tmp/oldfile.test',
os.O_CREAT | os.O_EXCL | os.O_WRONLY))
finally:
time.clock_settime(time.CLOCK_REALTIME, cur_time)
# allow X to start even on headless machines
os.makedirs('/etc/X11/xorg.conf.d/', exist_ok=True)
with open('/etc/X11/xorg.conf.d/dummy.conf', 'w') as f:
f.write('''Section "Device"
Identifier "test"
Driver "dummy"
EndSection''')
# accounts-daemon.service fails if /usr/share/accountsservice/interfaces does not exist.
# FIXME: remove this workaround again when
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1038137 is fixed
os.makedirs('/usr/share/accountsservice/interfaces', exist_ok=True)
if __name__ == '__main__':
if not os.getenv('AUTOPKGTEST_REBOOT_MARK'):
pre_boot_setup()
print('Rebooting...')
subprocess.check_call(['/tmp/autopkgtest-reboot', 'boot1'])
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
verbosity=2))
|