Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import unittest |
| 2 | import re |
| 3 | from oeqa.oetest import oeRuntimeTest, skipModule |
| 4 | from oeqa.utils.decorators import * |
| 5 | |
| 6 | def setUpModule(): |
| 7 | if not oeRuntimeTest.hasFeature("systemd"): |
| 8 | skipModule("target doesn't have systemd in DISTRO_FEATURES") |
| 9 | if "systemd" != oeRuntimeTest.tc.d.getVar("VIRTUAL-RUNTIME_init_manager", True): |
| 10 | skipModule("systemd is not the init manager for this image") |
| 11 | |
| 12 | |
| 13 | class SystemdTest(oeRuntimeTest): |
| 14 | |
| 15 | def systemctl(self, action = '', target = '', expected = 0, verbose = False): |
| 16 | command = 'systemctl %s %s' % (action, target) |
| 17 | status, output = self.target.run(command) |
| 18 | message = '\n'.join([command, output]) |
| 19 | if status != expected and verbose: |
| 20 | message += self.target.run('systemctl status --full %s' % target)[1] |
| 21 | self.assertEqual(status, expected, message) |
| 22 | return output |
| 23 | |
| 24 | |
| 25 | class SystemdBasicTests(SystemdTest): |
| 26 | |
| 27 | @skipUnlessPassed('test_ssh') |
| 28 | def test_systemd_basic(self): |
| 29 | self.systemctl('--version') |
| 30 | |
| 31 | @testcase(551) |
| 32 | @skipUnlessPassed('test_system_basic') |
| 33 | def test_systemd_list(self): |
| 34 | self.systemctl('list-unit-files') |
| 35 | |
| 36 | def settle(self): |
| 37 | """ |
| 38 | Block until systemd has finished activating any units being activated, |
| 39 | or until two minutes has elapsed. |
| 40 | |
| 41 | Returns a tuple, either (True, '') if all units have finished |
| 42 | activating, or (False, message string) if there are still units |
| 43 | activating (generally, failing units that restart). |
| 44 | """ |
| 45 | import time |
| 46 | endtime = time.time() + (60 * 2) |
| 47 | while True: |
| 48 | status, output = self.target.run('systemctl --state=activating') |
| 49 | if "0 loaded units listed" in output: |
| 50 | return (True, '') |
| 51 | if time.time() >= endtime: |
| 52 | return (False, output) |
| 53 | time.sleep(10) |
| 54 | |
| 55 | @testcase(550) |
| 56 | @skipUnlessPassed('test_systemd_basic') |
| 57 | def test_systemd_failed(self): |
| 58 | settled, output = self.settle() |
| 59 | self.assertTrue(settled, msg="Timed out waiting for systemd to settle:\n" + output) |
| 60 | |
| 61 | output = self.systemctl('list-units', '--failed') |
| 62 | match = re.search("0 loaded units listed", output) |
| 63 | if not match: |
| 64 | output += self.systemctl('status --full --failed') |
| 65 | self.assertTrue(match, msg="Some systemd units failed:\n%s" % output) |
| 66 | |
| 67 | |
| 68 | class SystemdServiceTests(SystemdTest): |
| 69 | |
| 70 | def check_for_avahi(self): |
| 71 | if not self.hasPackage('avahi-daemon'): |
| 72 | raise unittest.SkipTest("Testcase dependency not met: need avahi-daemon installed on target") |
| 73 | |
| 74 | @skipUnlessPassed('test_systemd_basic') |
| 75 | def test_systemd_status(self): |
| 76 | self.check_for_avahi() |
| 77 | self.systemctl('status --full', 'avahi-daemon.service') |
| 78 | |
| 79 | @testcase(695) |
| 80 | @skipUnlessPassed('test_systemd_status') |
| 81 | def test_systemd_stop_start(self): |
| 82 | self.check_for_avahi() |
| 83 | self.systemctl('stop', 'avahi-daemon.service') |
| 84 | self.systemctl('is-active', 'avahi-daemon.service', expected=3, verbose=True) |
| 85 | self.systemctl('start','avahi-daemon.service') |
| 86 | self.systemctl('is-active', 'avahi-daemon.service', verbose=True) |
| 87 | |
| 88 | @testcase(696) |
| 89 | @skipUnlessPassed('test_systemd_basic') |
| 90 | def test_systemd_disable_enable(self): |
| 91 | self.check_for_avahi() |
| 92 | self.systemctl('disable', 'avahi-daemon.service') |
| 93 | self.systemctl('is-enabled', 'avahi-daemon.service', expected=1) |
| 94 | self.systemctl('enable', 'avahi-daemon.service') |
| 95 | self.systemctl('is-enabled', 'avahi-daemon.service') |
| 96 | |
| 97 | class SystemdJournalTests(SystemdTest): |
| 98 | @skipUnlessPassed('test_ssh') |
| 99 | def test_systemd_journal(self): |
| 100 | (status, output) = self.target.run('journalctl') |
| 101 | self.assertEqual(status, 0, output) |