def test_should_fail_when_credentials_attribute_is_missing(self): self.resource.get_by_name.return_value = [] self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM_BY_NAME) StorageSystemModule().run() self.mock_ansible_module.fail_json.assert_called_once_with( msg=STORAGE_SYSTEM_CREDENTIALS_MANDATORY )
def test_should_fail_with_missing_required_attributes(self): self.mock_ansible_module.params = {"state": "present", "config": "config", "data": {"field": "invalid"}} StorageSystemModule().run() self.mock_ansible_module.fail_json.assert_called_once_with( msg=STORAGE_SYSTEM_MANDATORY_FIELDS_MISSING )
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 StorageSystemModule() 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_storage_system_not_exist(self): self.resource.get_by_ip_hostname.return_value = [] self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM_ABSENT) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( ansible_facts={}, changed=False, msg=STORAGE_SYSTEM_ALREADY_ABSENT )
def test_should_remove_storage_system(self): self.resource.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM_ABSENT) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( ansible_facts={}, changed=True, msg=STORAGE_SYSTEM_DELETED )
def test_should_not_update_when_data_is_equals(self): self.resource.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM self.resource.update.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=STORAGE_SYSTEM_ALREADY_UPDATED, ansible_facts=dict(storage_system=DICT_DEFAULT_STORAGE_SYSTEM) )
def test_should_not_update_when_data_is_equals_using_name(self): dict_by_name = yaml.load(YAML_STORAGE_SYSTEM_BY_NAME)["data"] self.resource.get_by_name.return_value = dict_by_name self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM_BY_NAME) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=False, msg=STORAGE_SYSTEM_ALREADY_UPDATED, ansible_facts=dict(storage_system=dict_by_name.copy()) )
def test_should_not_delete_when_oneview_exception(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = [DICT_DEFAULT_STORAGE_SYSTEM] mock_ov_instance.storage_systems.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_STORAGE_SYSTEM_ABSENT) mock_ansible_module.return_value = mock_ansible_instance self.assertRaises(Exception, StorageSystemModule().run()) mock_ansible_instance.fail_json.assert_called_once_with( msg=FAKE_MSG_ERROR )
def test_should_fail_when_credentials_attribute_is_missing(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_name.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_BY_NAME) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg=STORAGE_SYSTEM_CREDENTIALS_MANDATORY )
def test_should_create_new_storage_system(self): self.resource.get_by_ip_hostname.return_value = None self.resource.add.return_value = {"name": "name"} self.resource.update.return_value = {"name": "name"} self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=STORAGE_SYSTEM_ADDED, ansible_facts=dict(storage_system={"name": "name"}) )
def test_should_do_nothing_when_storage_system_not_exist(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = [] mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_ABSENT) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( ansible_facts={}, changed=False, msg=STORAGE_SYSTEM_ALREADY_ABSENT )
def test_should_remove_storage_system(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_ABSENT) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( ansible_facts={}, changed=True, msg=STORAGE_SYSTEM_DELETED )
def test_should_not_update_when_data_is_equals(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM mock_ov_instance.storage_systems.update.return_value = {"name": "name"} mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=STORAGE_SYSTEM_ALREADY_UPDATED, ansible_facts=dict(storage_system=DICT_DEFAULT_STORAGE_SYSTEM) )
def test_update_when_data_has_modified_attributes(self): data_merged = DICT_DEFAULT_STORAGE_SYSTEM.copy() data_merged['credentials']['newIp_hostname'] = '10.10.10.10' self.resource.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM self.resource.update.return_value = data_merged self.mock_ansible_module.params = yaml.load(YAML_STORAGE_SYSTEM_CHANGES) StorageSystemModule().run() self.mock_ansible_module.exit_json.assert_called_once_with( changed=True, msg=STORAGE_SYSTEM_UPDATED, ansible_facts=dict(storage_system=data_merged) )
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 StorageSystemModule().run() mock_ansible_instance.fail_json.assert_called_once_with( msg=STORAGE_SYSTEM_MANDATORY_FIELDS_MISSING )
def test_should_create_new_storage_system(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = None mock_ov_instance.storage_systems.add.return_value = {"name": "name"} mock_ov_instance.storage_systems.update.return_value = {"name": "name"} mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=STORAGE_SYSTEM_ADDED, ansible_facts=dict(storage_system={"name": "name"}) )
def test_should_not_update_when_data_is_equals_using_name(self, mock_ansible_module, mock_ov_client_from_json_file): mock_ov_instance = mock.Mock() dict_by_name = yaml.load(YAML_STORAGE_SYSTEM_BY_NAME)["data"] mock_ov_instance.storage_systems.get_by_name.return_value = dict_by_name mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_BY_NAME) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=False, msg=STORAGE_SYSTEM_ALREADY_UPDATED, ansible_facts=dict(storage_system=dict_by_name.copy()) )
def test_update_when_data_has_modified_attributes(self, mock_ansible_module, mock_ov_client_from_json_file): data_merged = DICT_DEFAULT_STORAGE_SYSTEM.copy() data_merged['credentials']['newIp_hostname'] = '10.10.10.10' # del data_merged['credentials']['password'] mock_ov_instance = mock.Mock() mock_ov_instance.storage_systems.get_by_ip_hostname.return_value = DICT_DEFAULT_STORAGE_SYSTEM mock_ov_instance.storage_systems.update.return_value = data_merged mock_ov_client_from_json_file.return_value = mock_ov_instance mock_ansible_instance = create_ansible_mock_yaml(YAML_STORAGE_SYSTEM_CHANGES) mock_ansible_module.return_value = mock_ansible_instance StorageSystemModule().run() mock_ansible_instance.exit_json.assert_called_once_with( changed=True, msg=STORAGE_SYSTEM_UPDATED, ansible_facts=dict(storage_system=data_merged) )