From d787b37bd3d0b20d353734dffae7d52360554be6 Mon Sep 17 00:00:00 2001 From: Matyas Horky Date: Mon, 5 Dec 2022 12:24:39 +0100 Subject: [PATCH 1/2] Show locals in pytest output Cherry-picked from f6362515af213d5fb184395b715eec269be78839 --- jenkins/python3-tests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jenkins/python3-tests.sh b/jenkins/python3-tests.sh index ec50c5015f..54c24230ff 100644 --- a/jenkins/python3-tests.sh +++ b/jenkins/python3-tests.sh @@ -23,7 +23,7 @@ python3 setup.py build python3 setup.py build_ext --inplace # make sure we have a dbus session for the dbus tests -dbus-run-session coverage run +dbus-run-session coverage run -m pytest -vvl coverage report coverage xml From 23248d993fc518ea802ab75944918b02010d8396 Mon Sep 17 00:00:00 2001 From: Matyas Horky Date: Mon, 5 Dec 2022 12:42:50 +0100 Subject: [PATCH 2/2] Fix failures of D-Bus' Register tests Our test that checks if it is possible to connect to newly created socket started failing with 'Permission denied' when the code tried to use it. Pytest cannot write into '/run', moving it to '/tmp' solves the issue. Additionally, as a result of relatively recent D-Bus implementation changes, 'tmpdir' and 'dir' behave the same and create regular socket, not an abstract socket. Connecting with "\0" prefix (which signifies the abstract one) now fails and thus has been removed from the tests. Cherry-picked from bae7ce3d034f0f7b5f5e530aa4d72dfc7ceff3ad --- src/rhsmlib/dbus/server.py | 14 +++++++++++++- test/rhsmlib_test/test_register.py | 24 ++++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/rhsmlib/dbus/server.py b/src/rhsmlib/dbus/server.py index 1b83fdf0fe..81c4d002ae 100644 --- a/src/rhsmlib/dbus/server.py +++ b/src/rhsmlib/dbus/server.py @@ -254,6 +254,18 @@ class DomainSocketServer(object): session bus since those aren't really locked down. The work-around is the client asks our service to open another server on a domain socket, gets socket information back, and then connects and sends the register command (with the credentials) to the server on the domain socket.""" + + # FIXME Use `dir` instead. + # `tmpdir` behaves differently from `dir` on old versions of dbus + # (earlier than 1.12.24 and 1.14.4). + # In newer versions we are not getting abstract socket anymore. + _server_socket_iface: str = "unix:tmpdir=" + _server_socket_path: str = "/run" + + @property + def _server_socket(self): + return self._server_socket_iface + self._server_socket_path + @staticmethod def connection_added(domain_socket_server, service_class, object_list, conn): object_list.append(service_class(conn=conn)) @@ -305,7 +317,7 @@ def shutdown(self): def run(self): try: - self._server = dbus.server.Server("unix:tmpdir=/run") + self._server = dbus.server.Server(self._server_socket) for clazz in self.object_classes: self._server.on_connection_added.append( diff --git a/test/rhsmlib_test/test_register.py b/test/rhsmlib_test/test_register.py index d56b1899db..246985f243 100644 --- a/test/rhsmlib_test/test_register.py +++ b/test/rhsmlib_test/test_register.py @@ -22,6 +22,8 @@ import dbus.connection import socket import six +import tempfile +from typing import Optional import subscription_manager.injection as inj @@ -38,6 +40,7 @@ from rhsm import connection from rhsmlib.dbus import constants +from rhsmlib.dbus.server import DomainSocketServer from rhsmlib.dbus.objects import RegisterDBusObject from rhsmlib.services import register, exceptions @@ -743,6 +746,8 @@ def test_requires_basic_auth_for_normal_registration(self): @subman_marker_dbus class DomainSocketRegisterDBusObjectTest(DBusObjectTest, InjectionMockingTest): + socket_dir: Optional[tempfile.TemporaryDirectory] = None + def dbus_objects(self): return [RegisterDBusObject] @@ -788,6 +793,16 @@ def setUp(self): self.mock_ent_cert_service_invoker = ent_cert_service_patcher.start() self.addCleanup(ent_cert_service_patcher.stop) + self.socket_dir = tempfile.TemporaryDirectory() + self.addCleanup(self.socket_dir.cleanup) + socket_path_patch = mock.patch.object(DomainSocketServer, "_server_socket_path", self.socket_dir.name) + socket_path_patch.start() + # `tmpdir` behaves differently from `dir` on old versions of dbus + # (earlier than 1.12.24 and 1.14.4). + # In newer versions we are not getting abstract socket anymore. + socket_iface_patch = mock.patch.object(DomainSocketServer, "_server_socket_iface", "unix:dir=") + socket_iface_patch.start() + def injection_definitions(self, *args, **kwargs): if args[0] == inj.IDENTITY: return self.mock_identity @@ -801,7 +816,7 @@ def test_open_domain_socket(self): def assertions(*args): result = args[0] - six.assertRegex(self, result, r'/run/dbus.*') + six.assertRegex(self, result, self.socket_dir.name + "/dbus.*") self.dbus_request(assertions, self.interface.Start, dbus_method_args) @@ -812,7 +827,7 @@ def assertions(*args): # Assign the result as an attribute to this function. # See http://stackoverflow.com/a/27910553/6124862 assertions.result = args[0] - six.assertRegex(self, assertions.result, r'/run/dbus.*') + six.assertRegex(self, assertions.result, self.socket_dir.name + "/dbus.*") self.dbus_request(assertions, self.interface.Start, dbus_method_args) @@ -844,10 +859,7 @@ def get_address(*args): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: - # The socket returned for connection is an abstract socket so we have - # to begin the name with a NUL byte to get into that namespace. See - # http://blog.eduardofleury.com/archives/2007/09/13 - sock.connect('\0' + get_address.address) + sock.connect(get_address.address) finally: sock.close()