Patchwork lib/oeqa/runtime: rewrite the systemd test module

login
register
mail settings
Submitter Stanacar, StefanX
Date Dec. 18, 2013, 7:02 p.m.
Message ID <1387393325-12705-1-git-send-email-stefanx.stanacar@intel.com>
Download mbox | patch
Permalink /patch/63553/
State Accepted
Commit b24f2172a5e1ac93ea1c378b6989f8f573ccd154
Headers show

Comments

Stanacar, StefanX - Dec. 18, 2013, 7:02 p.m.
They are basically the same tests but:
- they look cleaner, using one single method / assert
- output from unittest will be cleaner (and includes a verbose status when needed)
- they are better grouped and use a real, active, enabled service
  (machineid will be dropped and hostnamed was a static service)

Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
---
 meta/lib/oeqa/runtime/systemd.py | 93 +++++++++++++++++++---------------------
 1 file changed, 45 insertions(+), 48 deletions(-)

Patch

diff --git a/meta/lib/oeqa/runtime/systemd.py b/meta/lib/oeqa/runtime/systemd.py
index 31007df..eed29d3 100644
--- a/meta/lib/oeqa/runtime/systemd.py
+++ b/meta/lib/oeqa/runtime/systemd.py
@@ -1,4 +1,5 @@ 
 import unittest
+import re
 from oeqa.oetest import oeRuntimeTest, skipModule
 from oeqa.utils.decorators import *
 
@@ -9,57 +10,53 @@  def setUpModule():
             skipModule("systemd is not the init manager for this image")
 
 
-class SystemdBasicTest(oeRuntimeTest):
+class SystemdTest(oeRuntimeTest):
 
-    @skipUnlessPassed('test_ssh')
-    def test_systemd_version(self):
-        (status, output) = self.target.run('systemctl --version')
-        self.assertEqual(status, 0, msg="status and output: %s and %s" % (status,output))
-
-class SystemdTests(oeRuntimeTest):
-
-    @skipUnlessPassed('test_systemd_version')
-    def test_systemd_failed(self):
-        (status, output) = self.target.run('systemctl --failed | grep "0 loaded units listed"')
-        if status != 0:
-            print self.target.run('systemctl status --failed -l')[1]
-            self.fail("Some systemd units failed.")
-
-    @skipUnlessPassed('test_systemd_version')
-    def test_systemd_service(self):
-        (status, output) = self.target.run('systemctl list-unit-files | grep "systemd-hostnamed.service"')
-        self.assertEqual(status, 0, msg="systemd-hostnamed.service service is not available.")
+    def systemctl(self, action = '', target = '', expected = 0, verbose = False):
+        command = 'systemctl %s %s' % (action, target)
+        status, output = self.target.run(command)
+        message = '\n'.join([command, output])
+        if status != expected and verbose:
+            message += self.target.run('systemctl status --full %s' % target)[1]
+        self.assertEqual(status, expected, message)
+        return output
 
-    @skipUnlessPassed('test_systemd_service')
-    def test_systemd_stop(self):
-        self.target.run('systemctl stop systemd-hostnamed.service')
-        (status, output) = self.target.run('systemctl show systemd-hostnamed.service | grep "ActiveState" | grep "=inactive"')
-        if status != 0:
-            (status, output) = self.target.run('systemctl status -l systemd-hostnamed.service')
-            self.fail(msg="systemd-hostnamed.service service could not be stopped. Status:\n" + output)
 
-    @skipUnlessPassed('test_systemd_stop')
-    @skipUnlessPassed('test_systemd_version')
-    def test_systemd_start(self):
-        self.target.run('systemctl start systemd-hostnamed.service')
-        (status, output) = self.target.run('systemctl is-active systemd-hostnamed.service')
-        if status != 0:
-            (status, output) = self.target.run('systemctl status -l systemd-hostnamed.service')
-            self.fail(msg="systemd-hostnamed.service service could not be started. Status:\n" + output)
+class SystemdBasicTests(SystemdTest):
 
-    @skipUnlessPassed('test_systemd_version')
-    def test_systemd_enable(self):
-        self.target.run('systemctl enable machineid.service')
-        (status, output) = self.target.run('systemctl is-enabled machineid.service')
-        self.assertEqual(output, 'enabled', msg="machineid.service service could not be enabled. Status and output: %s and %s" % (status, output))
-
-    @skipUnlessPassed('test_systemd_enable')
-    def test_systemd_disable(self):
-        self.target.run('systemctl disable machineid.service')
-        (status, output) = self.target.run('systemctl is-enabled machineid.service')
-        self.assertEqual(output, 'disabled', msg="machineid.service service could not be disabled. Status and output: %s and %s" % (status, output))
+    @skipUnlessPassed('test_ssh')
+    def test_systemd_basic(self):
+        self.systemctl('--version')
 
-    @skipUnlessPassed('test_systemd_version')
+    @skipUnlessPassed('test_system_basic')
     def test_systemd_list(self):
-        (status, output) = self.target.run('systemctl list-unit-files')
-        self.assertEqual(status, 0, msg="systemctl list-unit-files command failed. Status:  %s" % status)
+        self.systemctl('list-unit-files')
+
+    @skipUnlessPassed('test_systemd_basic')
+    def test_systemd_failed(self):
+        output = self.systemctl('list-units', '--failed')
+        match = re.search("0 loaded units listed", output)
+        if not match:
+            output += self.systemctl('status --full --failed')
+        self.assertTrue(match, msg="Some systemd units failed:\n%s" % output)
+
+
+class SystemdServiceTests(SystemdTest):
+
+    @skipUnlessPassed('test_systemd_basic')
+    def test_systemd_status(self):
+        self.systemctl('status --full', 'avahi-daemon.service')
+
+    @skipUnlessPassed('test_systemd_status')
+    def test_systemd_stop_start(self):
+        self.systemctl('stop', 'avahi-daemon.service')
+        self.systemctl('is-active', 'avahi-daemon.service', expected=3, verbose=True)
+        self.systemctl('start','avahi-daemon.service')
+        self.systemctl('is-active', 'avahi-daemon.service', verbose=True)
+
+    @skipUnlessPassed('test_systemd_basic')
+    def test_systemd_disable_enable(self):
+        self.systemctl('disable', 'avahi-daemon.service')
+        self.systemctl('is-enabled', 'avahi-daemon.service', expected=1)
+        self.systemctl('enable', 'avahi-daemon.service')
+        self.systemctl('is-enabled', 'avahi-daemon.service')