"""The machine's power state and what AMT accepts, from the recorded NUC9.""" import pytest from napalm_amt.parsers import POWER_REQUESTS, accepted_actions, parse_power_state from ..conftest import instances def test_power_state_of_a_running_machine(): (service,) = instances("CIM_AssociatedPowerManagementService.pull.xml") assert parse_power_state(service) == { "state": "On", "available": [10, 8, 5, 11, 4, 7, 14, 12], } @pytest.mark.parametrize( ("code", "state"), [ ("2", "On"), ("8", "Off"), ("6", "Off"), ("13", "Off"), # Asleep or hibernating: the OS is not running, On wakes it. ("3", "Off"), ("4", "Off"), ("7", "Off"), ("99", ""), ("", ""), ], ) def test_power_state_codes(code, state): assert parse_power_state({"PowerState": code})["state"] == state def test_a_single_available_state_is_a_list(): assert parse_power_state({"PowerState": "8", "AvailableRequestedPowerStates": "2"})[ "available" ] == [2] def test_running_machine_accepts_what_amt_offers_except_on(): assert accepted_actions("On", [10, 8, 5, 11, 4, 7, 14, 12]) == [ "GracefulShutdown", "ForceOff", "GracefulRestart", "ForceRestart", "PowerCycle", ] def test_without_graceful_states_amt_offers_no_graceful_actions(): # AMT lists the graceful states only where the host can honour them. assert accepted_actions("On", [10, 8, 5]) == ["ForceOff", "ForceRestart", "PowerCycle"] def test_machine_that_is_off_accepts_on_only(): assert accepted_actions("Off", [2]) == ["On"] assert accepted_actions("Off", []) == ["On"] def test_unknown_state_accepts_nothing(): assert accepted_actions("", [2, 8, 10]) == [] def test_power_requests_cover_every_role_action(): from napalm_device_types.models import POWER_ACTIONS assert set(POWER_REQUESTS) == set(POWER_ACTIONS) assert POWER_REQUESTS["On"] == 2 assert POWER_REQUESTS["ForceOff"] == 8 assert POWER_REQUESTS["ForceRestart"] == 10