def test_should_remove_server_hardware(self): self.resource.get_by.return_value = [{'name': 'name'}] self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_ABSENT) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( ansible_facts={}, changed=True, msg=SERVER_HARDWARE_DELETED)
def test_should_load_config_from_file(self, mock_ansible_module, mock_ov_client_from_env_vars, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock({'config': 'config.json'}) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule() mock_ov_client_from_json_file.assert_called_once_with('config.json') mock_ov_client_from_env_vars.not_been_called()
def test_should_fail_when_set_ilo_firmware_and_server_hardware_was_not_found( self): self.resource.get_by.return_value = [] self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_ILO_FIRMWARE) ServerHardwareModule().run() self.mock_ansible_module.fail_json.assert_called_once_with( msg=SERVER_HARDWARE_NOT_FOUND)
def test_should_do_nothing_when_server_hardware_not_exist(self): self.resource.get_by.return_value = [] self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_ABSENT) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( ansible_facts={}, changed=False, msg=SERVER_HARDWARE_ALREADY_ABSENT)
def test_should_not_add_when_it_already_exists(self): self.resource.get_by.return_value = [{"name": "name"}] self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_PRESENT) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=SERVER_HARDWARE_ALREADY_ADDED, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_fail_with_missing_name_attribute(self): self.mock_ansible_module.params = { "state": "absent", "config": "config", "data": { "field": "invalid" } } ServerHardwareModule().run() self.mock_ansible_module.fail_json.assert_called_once_with( msg=SERVER_HARDWARE_MANDATORY_FIELD_MISSING.format('data.name'))
def test_should_set_refresh_state(self): self.resource.get_by.return_value = [{"uri": "resourceuri"}] self.resource.refresh_state.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_REFRESH_STATE) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_REFRESH_STATE_UPDATED, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_add_new_server_hardware(self): self.resource.get_by.return_value = [] self.resource.add.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_PRESENT) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ADDED, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_set_ilo_firmware(self): self.resource.get_by.return_value = [{"uri": "resourceuri"}] self.resource.update_mp_firware_version.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_ILO_FIRMWARE) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_fail_when_the_server_hardware_was_not_found(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ILO_FIRMWARE) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg=SERVER_HARDWARE_NOT_FOUND )
def test_should_fail_when_remove_raises_exception(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{'name': 'name'}] mock_ov_instance.server_hardware.remove.side_effect = Exception(FAKE_MSG_ERROR) mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ABSENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, ServerHardwareModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR )
def test_should_do_nothing_when_server_hardware_not_exist(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ABSENT) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.exit_json.assert_called_once_with( ansible_facts={}, changed=False, msg=SERVER_HARDWARE_ALREADY_ABSENT )
def test_should_remove_server_hardware(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{'name': 'name'}] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ABSENT) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.exit_json.assert_called_once_with( ansible_facts={}, changed=True, msg=SERVER_HARDWARE_DELETED )
def test_should_not_add_when_it_already_exists(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{"name": "name"}] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=SERVER_HARDWARE_ALREADY_ADDED, ansible_facts=dict(server_hardware={"name": "name"}) )
def test_should_set_refresh_state(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{"uri": "resourceuri"}] mock_ov_instance.server_hardware.update_mp_firware_version.return_value = {"name": "name"} mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_ILO_FIRMWARE) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ILO_FIRMWARE_VERSION_UPDATED, ansible_facts=dict(server_hardware={"name": "name"}) )
def test_should_not_set_when_the_uid_state_is_already_off(self): server_hardware_uri = "resourceuri" server_hardware = {"uri": server_hardware_uri, "uidState": "Off"} self.resource.get_by.return_value = [server_hardware] self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_UID_STATE_OFF) ServerHardwareModule().run() self.resource.patch.assert_not_called() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=NOTHING_TO_DO, ansible_facts=dict(server_hardware=server_hardware))
def test_should_calibrate_max_power_server_hardware(self): self.resource.get_by.return_value = [{"name": "name", "uri": "uri"}] self.resource.update_environmental_configuration.return_value = { "name": "name" } self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_SET_CALIBRATED_MAX_POWER) ServerHardwareModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ENV_CONFIG_UPDATED, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_fail_with_missing_required_attributes(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = mock.Mock() mock_ansible_instance.params = {"state": "present", "config": "config", "data": {"field": "invalid"}} mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg=SERVER_HARDWARE_MANDATORY_FIELD_MISSING )
def test_should_calibrate_max_power_server_hardware(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_hardware.get_by.return_value = [{"name": "name", "uri": "uri"}] mock_ov_instance.server_hardware.update_environmental_configuration.return_value = {"name": "name"} mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_SERVER_HARDWARE_SET_CALIBRATED_MAX_POWER) mock_ansible_module.return_value = mock_ansible_instance ServerHardwareModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ENV_CONFIG_UPDATED, ansible_facts=dict(server_hardware={"name": "name", "uri": "uri"}) )
def test_should_reset_ilo_state(self): server_hardware_uri = "resourceuri" self.resource.get_by.return_value = [{"uri": server_hardware_uri}] self.resource.patch.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_ILO_STATE_RESET) ServerHardwareModule().run() patch_params = ServerHardwareModule.patch_params['ilo_state_reset'] self.resource.patch.assert_called_once_with( id_or_uri=server_hardware_uri, **patch_params) self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_ILO_STATE_RESET, ansible_facts=dict(server_hardware={"name": "name"}))
def test_should_set_off_the_uid_state(self): server_hardware_uri = "resourceuri" self.resource.get_by.return_value = [{ "uri": server_hardware_uri, "uidState": "On" }] self.resource.patch.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load( YAML_SERVER_HARDWARE_UID_STATE_OFF) ServerHardwareModule().run() patch_params = ServerHardwareModule.patch_params['uid_state_off'] self.resource.patch.assert_called_once_with( id_or_uri=server_hardware_uri, **patch_params) self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SERVER_HARDWARE_UID_STATE_CHANGED, ansible_facts=dict(server_hardware={"name": "name"}))