From b4eba59915097c0a817350c4fb13ec3865d3ee76 Mon Sep 17 00:00:00 2001 From: mcw-work Date: Mon, 4 Dec 2023 10:58:22 +0000 Subject: [PATCH] Added coverage of failed shutdown to tests. Refactored reboot test in package/changer.py --- .../client/manager/tests/test_shutdownmanager.py | 6 ++++++ landscape/client/package/changer.py | 16 +++++++++------- landscape/client/package/tests/test_changer.py | 14 ++++---------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/landscape/client/manager/tests/test_shutdownmanager.py b/landscape/client/manager/tests/test_shutdownmanager.py index ee57bcf3e..796710720 100644 --- a/landscape/client/manager/tests/test_shutdownmanager.py +++ b/landscape/client/manager/tests/test_shutdownmanager.py @@ -1,5 +1,7 @@ from unittest.mock import Mock +import twisted.internet.defer + from landscape.client.manager.shutdownmanager import ShutdownManager from landscape.client.tests.helpers import LandscapeTest from landscape.client.tests.helpers import ManagerHelper @@ -44,3 +46,7 @@ def check(_): self.plugin.shutdown_deferred.addCallback(check) return deferred + + def test_shutdown_failed(self): + deferred = self.plugin._respond_fail("", 100) + self.assertIsInstance(deferred, twisted.internet.defer.Deferred) diff --git a/landscape/client/package/changer.py b/landscape/client/package/changer.py index 6ce521e66..3bd0cdaa2 100644 --- a/landscape/client/package/changer.py +++ b/landscape/client/package/changer.py @@ -3,8 +3,8 @@ import os import pwd import time -import dbus +import dbus from twisted.internet import reactor from twisted.internet.defer import maybeDeferred from twisted.internet.defer import succeed @@ -366,17 +366,19 @@ def handle_change_packages(self, message): def _reboot_later(self, result): self._landscape_reactor.call_later(5, self._run_reboot) - def _run_reboot(self): + def _run_reboot(self, bus=dbus.SystemBus()): """ Fire a dbus system shutdown """ - bus = dbus.SystemBus() - bus_object = bus.get_object( + self.bus = bus + self.bus_object = self.bus.get_object( "org.freedesktop.login1", - "/org/freedesktop/login1") - bus_object.Reboot( + "/org/freedesktop/login1", + ) + self.bus_object.Reboot( True, - dbus_interface="org.freedesktop.login1.Manager") + dbus_interface="org.freedesktop.login1.Manager", + ) def _log_reboot(self, result, minutes): """Log the reboot.""" diff --git a/landscape/client/package/tests/test_changer.py b/landscape/client/package/tests/test_changer.py index a8281fffc..30de8a89c 100644 --- a/landscape/client/package/tests/test_changer.py +++ b/landscape/client/package/tests/test_changer.py @@ -1626,14 +1626,8 @@ def assert_session_id(ignored): return result.addCallback(assert_session_id) def test_reboot_flag(self): - self.dbus_mock = patch( - "landscape.client.package.changer.dbus").start() - bus_object = Mock() + self.dbus_mock = Mock() - self.dbus_mock.SystemBus.return_value = bus_object - - self.changer._run_reboot() - - def check(_): - bus_object.get_object.assert_called_once() - bus_object.Reboot.assert_called_once() + self.changer._run_reboot(bus=self.dbus_mock) + self.changer.bus.get_object.assert_called_once() + self.changer.bus_object.Reboot.assert_called_once()