Skip to content

Commit

Permalink
Update shutdown/restart unit tests
Browse files Browse the repository at this point in the history
assert calls on dbus object
added clock advance for shutdown delay
  • Loading branch information
mcw-work committed Nov 22, 2023
1 parent 3deeefb commit c113436
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
5 changes: 4 additions & 1 deletion landscape/client/manager/shutdownmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class ShutdownManager(ManagerPlugin):
This is usually sufficent.
"""

callLater = reactor.callLater
shutdown_delay = 120

def register(self, registry):
super().register(registry)
self.config = registry.config
Expand Down Expand Up @@ -75,7 +78,7 @@ def _respond_reboot_success(self, data, operation_id):

def _respond_shutdown_success(self, data, operation_id):
deferred = self._respond(SUCCEEDED, data, operation_id)
reactor.callLater(120, self._Shutdown)
self.callLater(self.shutdown_delay, self._Shutdown)
deferred.addErrback(self._respond_fail)
return deferred

Expand Down
39 changes: 24 additions & 15 deletions landscape/client/manager/tests/test_shutdownmanager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import patch
from unittest.mock import patch, Mock
from twisted.internet import task

from landscape.client.manager.shutdownmanager import ShutdownManager
Expand All @@ -20,27 +20,36 @@ def setUp(self):

self.clock = task.Clock()
self.plugin = ShutdownManager()
self.plugin.reactor = self.clock

self.manager.add(self.plugin)
self.plugin.callLater = self.clock.callLater

self.dbus_mock = patch(
"landscape.client.manager.shutdownmanager.dbus").start()

def test_reboot(self):
bus_object = Mock()
self.dbus_mock.SystemBus.return_value = bus_object

@patch('landscape.client.manager.shutdownmanager.ShutdownManager._Reboot')
def test_reboot(self, mock_reboot):
message = {"type": "shutdown", "reboot": True, "operation-id": 100}
deferred = self.plugin._handle_shutdown(message)

mock_reboot.assert_called_once()
def check(_):
bus_object.get_object.assert_called_once()
bus_object.Reboot.assert_called_once()

Check warning on line 38 in landscape/client/manager/tests/test_shutdownmanager.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_shutdownmanager.py#L37-L38

Added lines #L37 - L38 were not covered by tests

return deferred

@patch('landscape.client.manager.shutdownmanager.reactor')
def test_shutdown(self, mock_reactor):
def test_shutdown(self):
bus_object = Mock()
self.dbus_mock.SystemBus.return_value = bus_object

message = {"type": "shutdown", "reboot": False, "operation-id": 101}
self.plugin._handle_shutdown(message)
message = {"type": "shutdown", "reboot": False, "operation-id": 100}
deferred = self.plugin._handle_shutdown(message)

self.clock.advance(self.plugin.shutdown_delay)

mock_reactor.callLater.assert_called_once()
def check(_):
bus_object.get_object.assert_called_once()
bus_object.PowerOff.assert_called_once()

Check warning on line 53 in landscape/client/manager/tests/test_shutdownmanager.py

View check run for this annotation

Codecov / codecov/patch

landscape/client/manager/tests/test_shutdownmanager.py#L52-L53

Added lines #L52 - L53 were not covered by tests

# check it was the shutdown method requested
arg = mock_reactor.callLater.call_args.args[1]
name = getattr(arg, "__name__", str(arg))
self.assertEqual(name, "_Shutdown")
return deferred

0 comments on commit c113436

Please sign in to comment.