Skip to content

Commit

Permalink
Add testcases for execute_reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
vvolam committed Sep 15, 2024
1 parent e88db57 commit 49caaca
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/host_modules/systemd_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,59 @@ def test_service_stop_empty(self, MockInit, MockBusName, MockSystemBus):
ret, msg = systemd_service_stub.stop_service(service)
assert ret == 1
assert "stop_service called with no service specified" in msg

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
@mock.patch("dbus.service.Object.__init__")
def test_execute_reboot_cold(self, MockInit, MockBusName, MockSystemBus):
# Mock subprocess.run
with mock.patch("subprocess.run") as mock_run:
# Mock the result of subprocess.run
res_mock = mock.Mock()
test_ret = 0
test_msg = b"Succeeded"
res_mock.configure_mock(returncode=test_ret, stderr=test_msg)
mock_run.return_value = res_mock

# Define reboot method and instantiate the service
method = 1 # Method 1 corresponds to cold reboot
systemd_service_stub = systemd_service.SystemdService(systemd_service.MOD_NAME)

# Execute the reboot method
ret, msg = systemd_service_stub.execute_reboot(method)

# Assert the correct command was called
call_args = mock_run.call_args[0][0]
assert "reboot" in call_args, "Expected reboot command in subprocess call"

# Assert the return values are correct
assert ret == test_ret, f"Expected return code {test_ret}, got {ret}"
assert msg == "", f"Expected return message '', got {msg}"

@mock.patch("dbus.SystemBus")
@mock.patch("dbus.service.BusName")
@mock.patch("dbus.service.Object.__init__")
def test_execute_reboot_halt(self, MockInit, MockBusName, MockSystemBus):
# Mock subprocess.run
with mock.patch("subprocess.run") as mock_run:
# Mock the result of subprocess.run
res_mock = mock.Mock()
test_ret = 0
test_msg = b"Succeeded"
res_mock.configure_mock(returncode=test_ret, stderr=test_msg)
mock_run.return_value = res_mock

# Define reboot method and instantiate the service
method = 3 # Method 3 corresponds to halt reboot
systemd_service_stub = systemd_service.SystemdService(systemd_service.MOD_NAME)

# Execute the reboot method
ret, msg = systemd_service_stub.execute_reboot(method)

# Assert the correct command was called
call_args = mock_run.call_args[0][0]
assert "sudo reboot" in call_args, "Expected reboot command in subprocess call"

# Assert the return values are correct
assert ret == test_ret, f"Expected return code {test_ret}, got {ret}"
assert msg == "", f"Expected return message '', got {msg}"

0 comments on commit 49caaca

Please sign in to comment.