def test_should_raise_exception_when_key_is_missing(self):
        self.resource.get_by.return_value = [STORAGE_VOLUME_TEMPLATE]
        self.resource.remove.side_effect = Exception(FAKE_MSG_ERROR)

        self.mock_ansible_module.params = PARAMS_FOR_MISSING_KEY

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=STORAGE_VOLUME_TEMPLATE_MANDATORY_FIELD_MISSING)
    def test_should_do_nothing_when_storage_volume_template_not_exist(self):
        self.resource.get_by.return_value = []

        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=False,
            msg=STORAGE_VOLUME_TEMPLATE_ALREADY_ABSENT)
    def test_should_remove_storage_volume_template(self):
        self.resource.get_by.return_value = [STORAGE_VOLUME_TEMPLATE]

        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=True,
            msg=STORAGE_VOLUME_TEMPLATE_DELETED)
    def test_should_update_the_storage_volume_template(self):
        self.resource.get_by.return_value = [STORAGE_VOLUME_TEMPLATE]
        self.resource.update.return_value = {"name": "name"}

        self.mock_ansible_module.params = PARAMS_WITH_CHANGES

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=STORAGE_VOLUME_TEMPLATE_UPDATED,
            ansible_facts=dict(storage_volume_template={"name": "name"}))
    def test_should_create_new_storage_volume_template(self):
        self.resource.get_by.return_value = []
        self.resource.create.return_value = {"name": "name"}

        self.mock_ansible_module.params = PARAMS_FOR_PRESENT

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=STORAGE_VOLUME_TEMPLATE_CREATED,
            ansible_facts=dict(storage_volume_template={"name": "name"}))
    def test_should_not_update_when_data_is_equals(self):
        self.resource.get_by.return_value = [STORAGE_VOLUME_TEMPLATE]

        self.mock_ansible_module.params = PARAMS_FOR_PRESENT

        StorageVolumeTemplateModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=STORAGE_VOLUME_TEMPLATE_ALREADY_UPDATED,
            ansible_facts=dict(
                storage_volume_template=STORAGE_VOLUME_TEMPLATE))
    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

        StorageVolumeTemplateModule()

        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_create_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = []
        mock_ov_instance.storage_volume_templates.create.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_VOLUME_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageVolumeTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
    def test_should_do_nothing_when_storage_volume_template_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_STORAGE_VOLUME_TEMPLATE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=False,
            msg=STORAGE_VOLUME_TEMPLATE_ALREADY_ABSENT)
    def test_should_raise_exception_when_key_is_missing(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [
            DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE
        ]
        mock_ov_instance.storage_volume_templates.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_VOLUME_TEMPLATE_MISSING_KEY)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, StorageVolumeTemplateModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=STORAGE_VOLUME_TEMPLATE_MANDATORY_FIELD_MISSING)
    def test_should_remove_storage_volume_template(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = [
            DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE
        ]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_STORAGE_VOLUME_TEMPLATE_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            ansible_facts={},
            changed=True,
            msg=STORAGE_VOLUME_TEMPLATE_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_volume_templates.get_by.return_value = [
            DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE
        ]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_STORAGE_VOLUME_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=STORAGE_VOLUME_TEMPLATE_ALREADY_UPDATED,
            ansible_facts=dict(
                storage_volume_template=DICT_DEFAULT_STORAGE_VOLUME_TEMPLATE))
    def test_should_create_new_storage_volume_template(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.storage_volume_templates.get_by.return_value = []
        mock_ov_instance.storage_volume_templates.create.return_value = {
            "name": "name"
        }

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock_yaml(
            YAML_STORAGE_VOLUME_TEMPLATE)
        mock_ansible_module.return_value = mock_ansible_instance

        StorageVolumeTemplateModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=STORAGE_VOLUME_TEMPLATE_CREATED,
            ansible_facts=dict(storage_volume_template={"name": "name"}))