[gnome-settings-daemon/benzea/ci-fixes: 17/19] tests: Move more logic into common test code




commit eba846d8dba3f352c792bef2b0bf95b96c341ab2
Author: Benjamin Berg <bberg redhat com>
Date:   Wed Jul 21 12:36:18 2021 +0200

    tests: Move more logic into common test code
    
    This also cleans up the startup logic and ensures we wait for the plugin
    to be ready after launching it.

 plugins/power/test.py     | 70 ++++++----------------------------------
 plugins/xsettings/test.py | 68 ++++++---------------------------------
 tests/gsdtestcase.py      | 81 +++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 97 insertions(+), 122 deletions(-)
---
diff --git a/plugins/power/test.py b/plugins/power/test.py
index a38ab193..d98efc0a 100755
--- a/plugins/power/test.py
+++ b/plugins/power/test.py
@@ -41,6 +41,9 @@ from gi.repository import UMockdev
 class PowerPluginBase(gsdtestcase.GSDTestCase):
     '''Test the power plugin'''
 
+    gsd_plugin = 'power'
+    gsd_plugin_case = 'Power'
+
     COMMON_SUSPEND_METHODS=['Suspend', 'Hibernate', 'SuspendThenHibernate']
 
     def setUp(self):
@@ -51,8 +54,6 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
         self.check_logind_gnome_session()
         self.start_logind()
         self.addCleanup(self.stop_logind)
-        self.daemon_death_expected = False
-
 
         # Setup umockdev testbed
         self.testbed = UMockdev.Testbed.new()
@@ -79,19 +80,8 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
             'gnome_screensaver')
         self.addCleanup(self.stop_process, self.screensaver)
 
-        self.session_log = OutputChecker()
-        self.session = subprocess.Popen(['gnome-session', '-f',
-                                         '-a', os.path.join(self.workdir, 'autostart'),
-                                         '--session=dummy', '--debug'],
-                                        stdout=self.session_log.fd,
-                                        stderr=subprocess.STDOUT)
-        self.session_log.writer_attached()
-        self.addCleanup(self.stop_process, self.screensaver)
-
-        # wait until the daemon is on the bus
-        self.wait_for_bus_object('org.gnome.SessionManager',
-                                 '/org/gnome/SessionManager',
-                                 timeout=100)
+        self.start_session()
+        self.addCleanup(self.stop_session)
 
         self.obj_session_mgr = self.session_bus_con.get_object(
             'org.gnome.SessionManager', '/org/gnome/SessionManager')
@@ -107,13 +97,14 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
         # ensure that our tests don't lock the screen when the screensaver
         # gets active
         self.settings_screensaver = Gio.Settings(schema_id='org.gnome.desktop.screensaver')
+        self.addCleanup(self.reset_settings, self.settings_screensaver)
         self.settings_screensaver['lock-enabled'] = False
 
         # Ensure we set up the external monitor state
         self.set_has_external_monitor(False)
 
         self.settings_gsd_power = Gio.Settings(schema_id='org.gnome.settings-daemon.plugins.power')
-        self.addCleanup(self.reset_settings)
+        self.addCleanup(self.reset_settings, self.settings_gsd_power)
 
         Gio.Settings.sync()
         # avoid painfully long delays of actions for tests
@@ -129,17 +120,8 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
             else:
                 env['LD_PRELOAD'] = env['POWER_LD_PRELOAD']
 
-        # We need to redirect stdout to grab the debug messages.
-        # stderr is not needed by the testing infrastructure but is useful to
-        # see warnings and errors.
-        self.plugin_log = OutputChecker()
-        self.daemon = subprocess.Popen(
-            [os.path.join(builddir, 'gsd-power'), '--verbose'],
-            stdout=self.plugin_log.fd,
-            stderr=subprocess.STDOUT,
-            env=env)
-        self.plugin_log.writer_attached()
-        self.addCleanup(self.stop_daemon)
+        self.start_plugin(env)
+        self.addCleanup(self.stop_plugin)
 
         # Store the early-init messages, some tests need them.
         self.plugin_startup_msgs = self.plugin_log.check_line(b'System inhibitor fd is', timeout=10)
@@ -149,47 +131,15 @@ class PowerPluginBase(gsdtestcase.GSDTestCase):
 
         self.p_notify_log.clear()
 
-    def stop_daemon(self):
-
-        daemon_running = self.daemon.poll() == None
-        if daemon_running:
-            self.stop_process(self.daemon)
-        self.plugin_log.assert_closed()
-
     def cleanup_testbed(self):
         del self.testbed
 
-    def reset_settings(self):
-        # reset all changed gsettings, so that tests are independent from each
-        # other
-        for schema in [self.settings_gsd_power, self.settings_session, self.settings_screensaver]:
-            for k in schema.list_keys():
-                schema.reset(k)
-        Gio.Settings.sync()
-
     def delete_external_monitor_file(self):
         try:
             os.unlink(self.mock_external_monitor_file)
         except OSError:
             pass
 
-    def tearDown(self):
-        # we check this at the end so that the other cleanup always happens
-        daemon_running = self.daemon.poll() == None
-        self.assertTrue(daemon_running or self.daemon_death_expected, 'daemon died during the test')
-
-    def stop_session(self):
-        '''Stop GNOME session'''
-
-        assert self.session
-        self.stop_process(self.session)
-        # dummyapp.desktop survives the session. This keeps the FD open in the
-        # CI environment when gnome-session fails to redirect the child output
-        # to journald.
-        # Though, gnome-session should probably kill the child anyway.
-        #self.session_log.assert_closed()
-        self.session_log.force_close()
-
     def check_logind_gnome_session(self):
         '''Check that gnome-session is built with logind support'''
 
@@ -981,7 +931,7 @@ class PowerPluginTest6(PowerPluginBase):
     def test_forced_logout(self):
         '''Test forced logout'''
 
-        self.daemon_death_expected = True
+        self.plugin_death_expected = True
         idle_delay = round(gsdpowerconstants.MINIMUM_IDLE_DIM_DELAY / 
gsdpowerconstants.IDLE_DELAY_TO_IDLE_DIM_MULTIPLIER)
 
         self.settings_session['idle-delay'] = idle_delay
diff --git a/plugins/xsettings/test.py b/plugins/xsettings/test.py
index 79d5a87f..7b25169a 100755
--- a/plugins/xsettings/test.py
+++ b/plugins/xsettings/test.py
@@ -30,34 +30,21 @@ from gi.repository import GLib
 class XsettingsPluginTest(gsdtestcase.GSDTestCase):
     '''Test the xsettings plugin'''
 
+    gsd_plugin = 'xsettings'
+    gsd_plugin_case = 'XSettings'
+
     def setUp(self):
         self.start_logind()
+        self.addCleanup(self.stop_logind)
 
-        self.daemon_death_expected = False
-        self.session_log_write = open(os.path.join(self.workdir, 'gnome-session.log'), 'wb')
-        self.session = subprocess.Popen(['gnome-session', '-f',
-                                         '-a', os.path.join(self.workdir, 'autostart'),
-                                         '--session=dummy', '--debug'],
-                                        stdout=self.session_log_write,
-                                        stderr=subprocess.STDOUT)
-
-        # wait until the daemon is on the bus
-        try:
-            self.wait_for_bus_object('org.gnome.SessionManager',
-                                     '/org/gnome/SessionManager',
-                                     timeout=100)
-        except:
-            # on failure, print log
-            with open(self.session_log_write.name) as f:
-                print('----- session log -----\n%s\n------' % f.read())
-            raise
-
-        self.session_log = open(self.session_log_write.name)
+        self.start_session()
+        self.addCleanup(self.stop_session)
 
         self.obj_session_mgr = self.session_bus_con.get_object(
             'org.gnome.SessionManager', '/org/gnome/SessionManager')
 
         self.start_mutter()
+        self.addCleanup(self.stop_mutter)
 
         Gio.Settings.sync()
         os.environ['GSD_ignore_llvmpipe'] = '1'
@@ -85,17 +72,11 @@ class XsettingsPluginTest(gsdtestcase.GSDTestCase):
                 os.path.join(modules_dir, 'pk-gtk-module.desktop'))
 
         self.settings_sound = Gio.Settings.new('org.gnome.desktop.sound')
+        self.addCleanup(self.reset_settings, self.settings_sound)
         Gio.Settings.sync()
 
         env = os.environ.copy()
-        self.plugin_log = OutputChecker()
-        self.daemon = subprocess.Popen(
-            [os.path.join(builddir, 'gsd-xsettings'), '--verbose'],
-            # comment out this line if you want to see the logs in real time
-            stdout=self.plugin_log.fd,
-            stderr=subprocess.STDOUT,
-            env=env)
-        self.plugin_log.writer_attached()
+        self.start_plugin(os.environ.copy())
 
         # flush notification log
         self.p_notify_log.clear()
@@ -106,37 +87,6 @@ class XsettingsPluginTest(gsdtestcase.GSDTestCase):
             'org.gtk.Settings', '/org/gtk/Settings')
         self.obj_xsettings_props = dbus.Interface(obj_xsettings, dbus.PROPERTIES_IFACE)
 
-    def tearDown(self):
-
-        daemon_running = self.daemon.poll() == None
-        if daemon_running:
-            self.stop_process(self.daemon)
-        self.plugin_log.assert_closed()
-
-        self.stop_session()
-        self.stop_mutter()
-        self.stop_logind()
-
-        # reset all changed gsettings, so that tests are independent from each
-        # other
-        for schema in [self.settings_sound]:
-            for k in schema.list_keys():
-                schema.reset(k)
-        Gio.Settings.sync()
-
-        # we check this at the end so that the other cleanup always happens
-        self.assertTrue(daemon_running or self.daemon_death_expected, 'daemon died during the test')
-
-    def stop_session(self):
-        '''Stop GNOME session'''
-
-        assert self.session
-        self.stop_process(self.session)
-
-        self.session_log_write.flush()
-        self.session_log_write.close()
-        self.session_log.close()
-
     def check_plugin_log(self, needle, timeout=0, failmsg=None):
         '''Check that needle is found in the log within the given timeout.
         Returns immediately when found.
diff --git a/tests/gsdtestcase.py b/tests/gsdtestcase.py
index 3a6ff9ca..88e71493 100644
--- a/tests/gsdtestcase.py
+++ b/tests/gsdtestcase.py
@@ -100,7 +100,7 @@ class GSDTestCase(X11SessionTestCase):
         klass.p_notify_log.writer_attached()
         klass.addClassCleanup(klass.stop_process, klass.p_notify)
 
-        klass.start_session()
+        klass.configure_session()
         klass.start_monitor()
         klass.addClassCleanup(klass.stop_monitor)
 
@@ -111,6 +111,11 @@ class GSDTestCase(X11SessionTestCase):
             raise KeyboardInterrupt()
         signal.signal(signal.SIGTERM, r)
 
+    def tearDown(self):
+        # we check this at the end so that the other cleanup always happens
+        daemon_running = self.daemon.poll() == None
+        self.assertTrue(daemon_running or self.daemon_death_expected, 'daemon died during the test')
+
     def run(self, result=None):
         '''Show log files on failed tests
 
@@ -131,8 +136,8 @@ class GSDTestCase(X11SessionTestCase):
                               % (log_file, f.read()))
 
     @classmethod
-    def start_session(klass):
-        '''Start minimal GNOME session'''
+    def configure_session(klass):
+        '''Configure minimal GNOME session'''
 
         # create dummy session type and component
         d = os.path.join(klass.workdir, 'config', 'gnome-session', 'sessions')
@@ -145,6 +150,34 @@ class GSDTestCase(X11SessionTestCase):
             os.makedirs(d)
         shutil.copy(os.path.join(os.path.dirname(__file__), 'dummyapp.desktop'), d)
 
+    def start_session(self):
+        self.session_log = OutputChecker()
+        self.session = subprocess.Popen(['gnome-session', '-f',
+                                         '-a', os.path.join(self.workdir, 'autostart'),
+                                         '--session=dummy', '--debug'],
+                                        stdout=self.session_log.fd,
+                                        stderr=subprocess.STDOUT)
+        self.session_log.writer_attached()
+
+        # wait until the daemon is on the bus
+        self.wait_for_bus_object('org.gnome.SessionManager',
+                                 '/org/gnome/SessionManager',
+                                 timeout=100)
+
+        self.session_log.check_line(b'fill: *** Done adding required components')
+
+    def stop_session(self):
+        '''Stop GNOME session'''
+
+        assert self.session
+        self.stop_process(self.session)
+        # dummyapp.desktop survives the session. This keeps the FD open in the
+        # CI environment when gnome-session fails to redirect the child output
+        # to journald.
+        # Though, gnome-session should probably kill the child anyway.
+        #self.session_log.assert_closed()
+        self.session_log.force_close()
+
     @classmethod
     def start_monitor(klass):
         '''Start dbus-monitor'''
@@ -190,6 +223,7 @@ class GSDTestCase(X11SessionTestCase):
         self.stop_process(self.logind)
         self.logind_log.assert_closed()
 
+    @classmethod
     def start_mutter(klass):
         ''' start mutter '''
 
@@ -200,12 +234,53 @@ class GSDTestCase(X11SessionTestCase):
                                  '/org/gnome/Mutter/IdleMonitor/Core',
                                  timeout=100)
 
+    @classmethod
     def stop_mutter(klass):
         '''stop mutter'''
 
         assert klass.monitor
         klass.stop_process(klass.mutter, timeout=2)
 
+    def start_plugin(self, env):
+        self.plugin_death_expected = False
+
+        # We need to redirect stdout to grab the debug messages.
+        # stderr is not needed by the testing infrastructure but is useful to
+        # see warnings and errors.
+        self.plugin_log = OutputChecker()
+        self.daemon = subprocess.Popen(
+            [os.path.join(top_builddir, 'plugins', self.gsd_plugin, 'gsd-' + self.gsd_plugin), '--verbose'],
+            stdout=self.plugin_log.fd,
+            stderr=subprocess.STDOUT,
+            env=env)
+        self.plugin_log.writer_attached()
+
+
+        bus = self.get_dbus(False)
+
+        timeout = 100
+        while timeout > 0:
+            if bus.name_has_owner('org.gnome.SettingsDaemon.' + self.gsd_plugin_case):
+                break
+
+            timeout -= 1
+            time.sleep(0.1)
+        if timeout <= 0:
+            assert timeout > 0, 'timed out waiting for plugin startup: %s' % (self.gsd_plugin_case)
+
+    def stop_plugin(self):
+        daemon_running = self.daemon.poll() == None
+        if daemon_running:
+            self.stop_process(self.daemon)
+        self.plugin_log.assert_closed()
+
+    def reset_settings(self, schema):
+        # reset all changed gsettings, so that tests are independent from each
+        # other
+        for k in schema.list_keys():
+            schema.reset(k)
+        Gio.Settings.sync()
+
     @classmethod
     def stop_process(cls, proc, timeout=1):
         proc.terminate()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]