def test_power_status_not_installed(self, zboot, ipmi):
     instance = ZerobootIpmiHost(name="test", data=self._valid_data)
     with pytest.raises(
             StateCheckError,
             message=
             "power_status should be not be able to be called before install"
     ):
         instance.power_status()
    def test_power_status(self, zboot, ipmi):
        instance = ZerobootIpmiHost(name="test", data=self._valid_data)
        instance.state.set('actions', 'install', 'ok')
        ipmi.get().power_status = MagicMock(return_value="on")

        status = instance.power_status()

        assert status == True

        ipmi.get().power_status = MagicMock(return_value="off")
        status = instance.power_status()

        assert status == False
    def test_validate_no_ipmi_instance(self, zboot, ipmi):
        instance = ZerobootIpmiHost(name="test", data=self._valid_data)

        zboot.list = MagicMock(return_value=[self._valid_data['zerobootClient']])
        ipmi.list = MagicMock(return_value=[])
        instance.power_status = MagicMock(return_value=True)

        with pytest.raises(LookupError, message="ipmi instance should not be present") as excinfo:
            instance.validate()
        if "ipmi client" not in str(excinfo.value):
            pytest.fail("Received unexpected error message for missing ipmi instance: %s" % str(excinfo.value))
    def test_monitor_power_off(self):
        instance = ZerobootIpmiHost(name="test", data=self._valid_data)
        instance.state.set('actions', 'install', 'ok')
        instance.power_on = MagicMock()
        instance.power_off = MagicMock()
        instance.power_status = MagicMock(return_value=True)
        instance.data['powerState'] = False

        instance.monitor()

        # power state mismatched, power_off should have been called
        instance.power_on.assert_not_called()
        instance.power_off.assert_called_with()
    def test_monitor_matching_state(self):
        instance = ZerobootIpmiHost(name="test", data=self._valid_data)
        instance.state.set('actions', 'install', 'ok')
        instance.power_on = MagicMock()
        instance.power_off = MagicMock()
        instance.power_status = MagicMock(return_value=True)
        instance.data['powerState'] = True

        instance.monitor()

        # no power calls should be make
        instance.power_on.assert_not_called()
        instance.power_off.assert_not_called()