mutter/src/tests/dbusmock-templates/meta-mocks-manager.py
Marco Trevisan (Treviño) bf54a16f92 tests: Add MetaOrientationManager tests via SensorsProxy mock
Create a test system bus and use it to run all the tests, add a mock
SensorsProxy (via dbusmock template) server that implements the
net.hadess.SensorProxy interface.

To make testing easier, the service is created on request of a proxy for
it, whose lifetime controls the mock service lifetime as well.
This is done using a further mock service that is used to manage the
others, using python-dbusmock to simplify the handling.

Add basic tests for the orientation manager.

As per the usage dbusmock, we're now launching all the tests under such
wrapper, so that local dbus environment won't ever considered, and
there's no risk that it may affect the tests results both locally and in
CI.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1233>
2021-09-04 10:04:00 +02:00

84 lines
2.4 KiB
Python

# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.
__author__ = 'Marco Trevisan'
__copyright__ = '(c) 2021 Canonical Ltd.'
import dbus
import fcntl
import os
import subprocess
from collections import OrderedDict
from dbusmock import DBusTestCase
BUS_NAME = 'org.gnome.Mutter.TestDBusMocksManager'
MAIN_OBJ = '/org/gnome/Mutter/TestDBusMocksManager'
MAIN_IFACE = 'org.gnome.Mutter.TestDBusMocksManager'
SYSTEM_BUS = True
def load(mock, parameters):
mock.mocks = OrderedDict()
DBusTestCase.setUpClass()
mock.dbus_mock = DBusTestCase()
mock.dbus_mock.setUp()
mock.templates_dir = parameters['templates-dir']
def set_nonblock(fd):
'''Set a file object to non-blocking'''
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
@dbus.service.method(MAIN_IFACE, in_signature='s')
def StartFromTemplate(self, template):
if template in self.mocks.keys():
raise KeyError('Template {} already started'.format(template))
mock_server, mock_obj = self.dbus_mock.spawn_server_template(template, {},
stdout=subprocess.PIPE)
set_nonblock(mock_server.stdout)
self.mocks[template] = (mock_server, mock_obj)
@dbus.service.method(MAIN_IFACE, in_signature='s')
def StartFromLocalTemplate(self, template):
path = os.path.join(self.templates_dir, template + '.py')
return self.StartFromTemplate(path)
@dbus.service.method(MAIN_IFACE, in_signature='s')
def StopTemplate(self, template):
(mock_server, mock_obj) = self.mocks.pop(template)
mock_server.terminate()
mock_server.wait()
@dbus.service.method(MAIN_IFACE, in_signature='s')
def StopLocalTemplate(self, template):
path = os.path.join(self.templates_dir, template + '.py')
return self.StopTemplate(path)
@dbus.service.method(MAIN_IFACE)
def Cleanup(self):
for (mock_server, mock_obj) in reversed(self.mocks.values()):
mock_server.terminate()
mock_server.wait()
self.dbus_mock.tearDown()
DBusTestCase.tearDownClass()
@dbus.service.method(MAIN_IFACE, out_signature='as')
def ListRunningTemplates(self):
return list(self.mocks.keys())