def test_should_do_nothing_when_templates_not_exists(self): self.resource.get_by_name.return_value = None self.mock_ansible_module.params = PARAMS_FOR_ABSENT ServerProfileTemplateModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=SRV_PROFILE_TEMPLATE_ALREADY_ABSENT )
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 ServerProfileTemplateModule() mock_ov_client_from_json_file.assert_called_once_with('config.json') mock_ov_client_from_env_vars.not_been_called()
def test_should_do_nothing_when_templates_not_exists( self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.return_value = None mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=TEMPLATE_ALREADY_ABSENT)
def test_should_delete_when_template_exists(self): self.resource.get_by_name.return_value = CREATED_BASIC_TEMPLATE self.mock_ansible_module.params = PARAMS_FOR_ABSENT ServerProfileTemplateModule().run() self.resource.delete.assert_called_once_with(CREATED_BASIC_TEMPLATE) self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SRV_PROFILE_TEMPLATE_DELETED )
def test_should_fail_when_enclosure_group_not_found(self): self.resource.get_by_name.return_value = CREATED_BASIC_TEMPLATE self.resource.update.return_value = CREATED_BASIC_TEMPLATE self.mock_ov_client.enclosure_groups.get_by.return_value = [] self.mock_ov_client.server_hardware_types.get_by.return_value = [{'uri': SHT_URI}] self.mock_ansible_module.params = deepcopy(PARAMS_FOR_UPDATE_WITH_NAME) ServerProfileTemplateModule().run() self.mock_ansible_module.fail_json.assert_called_once_with( msg=SRV_PROFILE_TEMPLATE_ENCLOSURE_GROUP_NOT_FOUND + 'EG Name' )
def test_should_not_modify_when_template_already_exists(self): self.resource.get_by_name.return_value = CREATED_BASIC_TEMPLATE self.resource.create.return_value = CREATED_BASIC_TEMPLATE self.mock_ansible_module.params = PARAMS_FOR_PRESENT ServerProfileTemplateModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=SRV_PROFILE_TEMPLATE_ALREADY_EXIST, ansible_facts=dict(server_profile_template=CREATED_BASIC_TEMPLATE) )
def test_should_fail_when_oneview_client_raises_exception( self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.side_effect = Exception( FAKE_MSG_ERROR) mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR)
def test_should_delete_when_template_exists(self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.return_value = CREATED_BASIC_TEMPLATE mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() mock_ov_instance.server_profile_templates.delete.assert_called_once_with( CREATED_BASIC_TEMPLATE) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=TEMPLATE_DELETED)
def test_should_not_modify_when_template_already_exists( self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.return_value = CREATED_BASIC_TEMPLATE mock_ov_instance.server_profile_templates.create.return_value = CREATED_BASIC_TEMPLATE mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=TEMPLATE_ALREADY_EXIST, ansible_facts=dict(server_profile_template=CREATED_BASIC_TEMPLATE))
def test_update_using_names_for_dependecies(self): self.resource.get_by_name.return_value = CREATED_BASIC_TEMPLATE self.resource.update.return_value = CREATED_BASIC_TEMPLATE self.mock_ov_client.enclosure_groups.get_by.return_value = [{'uri': ENCLOSURE_GROUP_URI}] self.mock_ov_client.server_hardware_types.get_by.return_value = [{'uri': SHT_URI}] self.mock_ansible_module.params = deepcopy(PARAMS_FOR_UPDATE_WITH_NAME) ServerProfileTemplateModule().run() expected = CREATED_BASIC_TEMPLATE.copy() expected.update(BASIC_TEMPLATE_MODIFIED) self.resource.update.assert_called_once_with(resource=expected, id_or_uri=expected["uri"]) self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SRV_PROFILE_TEMPLATE_UPDATED, ansible_facts=dict(server_profile_template=CREATED_BASIC_TEMPLATE) )
def test_should_update_when_data_has_modified_attributes(self): self.resource.get_by_name.return_value = CREATED_BASIC_TEMPLATE self.resource.update.return_value = CREATED_BASIC_TEMPLATE self.mock_ansible_module.params = PARAMS_FOR_UPDATE ServerProfileTemplateModule().run() expected = CREATED_BASIC_TEMPLATE.copy() expected.update(BASIC_TEMPLATE_MODIFIED) self.resource.update.assert_called_once_with( resource=expected, id_or_uri=expected["uri"] ) self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=SRV_PROFILE_TEMPLATE_UPDATED, ansible_facts=dict(server_profile_template=CREATED_BASIC_TEMPLATE) )
def test_should_update_when_data_has_modified_attributes( self, mock_ansible_module, mock_ov_from_file): mock_ov_instance = mock.Mock() mock_ov_instance.server_profile_templates.get_by_name.return_value = CREATED_BASIC_TEMPLATE mock_ov_instance.server_profile_templates.update.return_value = CREATED_BASIC_TEMPLATE mock_ov_from_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE) mock_ansible_module.return_value = mock_ansible_instance ServerProfileTemplateModule().run() expected = CREATED_BASIC_TEMPLATE.copy() expected.update(BASIC_TEMPLATE_MODIFIED) mock_ov_instance.server_profile_templates.update.assert_called_once_with( resource=expected, id_or_uri=expected["uri"]) mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=TEMPLATE_UPDATED, ansible_facts=dict(server_profile_template=CREATED_BASIC_TEMPLATE))