Skip to content

Commit

Permalink
Added coverage of failed shutdown to tests.
Browse files Browse the repository at this point in the history
Refactored reboot test in package/changer.py
  • Loading branch information
mcw-work committed Dec 4, 2023
1 parent 1fbb9a1 commit b4eba59
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
6 changes: 6 additions & 0 deletions landscape/client/manager/tests/test_shutdownmanager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
16 changes: 9 additions & 7 deletions landscape/client/package/changer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
14 changes: 4 additions & 10 deletions landscape/client/package/tests/test_changer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit b4eba59

Please sign in to comment.