def test_issue_ipmi_command_issues_power_on(self): context = make_context() ipmi_chassis_config_command = make_ipmi_chassis_config_command( **context, tmp_config_name=ANY) ipmipower_command = make_ipmipower_command(**context) ipmipower_command += ("--cycle", "--on-if-off") ipmi_power_driver = IPMIPowerDriver() run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.side_effect = [ ProcessResult(), ProcessResult(stdout="on"), ] result = ipmi_power_driver._issue_ipmi_command("on", **context) run_command_mock.assert_has_calls( [call(*ipmi_chassis_config_command), call(*ipmipower_command)]) self.expectThat(result, Equals("on"))
def test_issue_ipmi_command_issues_power_query(self): context = make_context() ipmipower_command = make_ipmipower_command(**context) ipmipower_command += ("--stat", ) ipmi_power_driver = IPMIPowerDriver() run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.return_value = ProcessResult(stdout="other") result = ipmi_power_driver._issue_ipmi_command("query", **context) run_command_mock.assert_called_once_with(*ipmipower_command) self.expectThat(result, Equals("other"))
def test_issue_ipmipower_command_raises_unknown_error(self): run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.return_value = ProcessResult(stderr="error", returncode=1) self.assertRaises( PowerError, IPMIPowerDriver._issue_ipmipower_command, factory.make_name("command"), factory.make_name("power_change"), factory.make_name("power_address"), )
def test_issue_ipmipower_command_does_not_mistake_host_for_status(self): run_command_mock = self.patch(ipmi_module.shell, "run_command") # "cameron" contains the string "on", but the machine is off. run_command_mock.return_value = ProcessResult(stdout="cameron: off") self.assertThat( IPMIPowerDriver._issue_ipmipower_command( factory.make_name("command"), "query", factory.make_name("address"), ), Equals("off"), )
def test_issue_ipmipower_command_raises_error(self): for error, error_info in IPMI_ERRORS.items(): run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.return_value = ProcessResult(stdout=error, returncode=1) self.assertRaises( error_info.get("exception"), IPMIPowerDriver._issue_ipmipower_command, factory.make_name("command"), factory.make_name("power_change"), factory.make_name("power_address"), )
def patch_run_command(self, stdout=b"", stderr=b"", returncode=0, decode=False): mock_run_command = self.patch(amt_module.shell, "run_command") if decode: stdout = stdout.decode() stderr = stderr.decode() mock_run_command.return_value = ProcessResult(stdout=stdout, stderr=stderr, returncode=returncode) return mock_run_command
def test_issue_ipmi_chassis_config_command_raises_power_auth_error(self): ipmi_errors = { key: IPMI_ERRORS[key] for key in IPMI_ERRORS if IPMI_ERRORS[key]["exception"] == PowerAuthError } for error, error_info in ipmi_errors.items(): run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.return_value = ProcessResult(stderr=error) self.assertRaises( error_info.get("exception"), IPMIPowerDriver._issue_ipmi_chassis_config_command, factory.make_name("command"), factory.make_name("power_change"), factory.make_name("power_address"), )
def test_issue_ipmi_chassis_config_command_logs_maaslog_warning(self): power_address = factory.make_name("power_address") stderr = factory.make_name("stderr") run_command_mock = self.patch(ipmi_module.shell, "run_command") run_command_mock.return_value = ProcessResult(stderr=stderr, returncode=1) maaslog = self.patch(ipmi_module, "maaslog") IPMIPowerDriver._issue_ipmi_chassis_config_command( [factory.make_name("command")], factory.make_name("power_change"), power_address, ) self.assertThat( maaslog.warning, MockCalledOnceWith( "Failed to change the boot order to PXE %s: %s" % (power_address, stderr)), )
def patch_run_command(self, stdout="", stderr="", returncode=0): mock_run_command = self.patch(apc_module.shell, "run_command") mock_run_command.return_value = ProcessResult(stdout=stdout, stderr=stderr, returncode=returncode) return mock_run_command