def test_download_should_fail(self):
        self.resource.get_by_name.return_value = None
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_DOWNLOAD)

        ArtifactBundleModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            exception=mock.ANY, msg=ArtifactBundleModule.MSG_REQUIRED)
    def test_backup_extract_should_fail(self):
        self.resource.get_all_backups.return_value = []
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_BACKUP_EXTRACT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            exception=mock.ANY, msg=ArtifactBundleModule.MSG_BACKUP_REQUIRED)
    def test_should_do_nothing_when_resource_already_absent(self):
        self.resource.get_by_name.return_value = None
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_ABSENT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=ArtifactBundleModule.MSG_ALREADY_ABSENT,
        )
    def test_should_not_update_when_existing_data_is_equals(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_PRESENT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=ArtifactBundleModule.MSG_ALREADY_PRESENT,
            ansible_facts=dict(artifact_bundle=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_extract(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.extract.return_value = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_EXTRACT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_EXTRACTED,
            ansible_facts=dict(artifact_bundle=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_download(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.download.return_value = True
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_DOWNLOAD)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=ArtifactBundleModule.MSG_DOWNLOADED,
            ansible_facts=dict())
    def test_should_delete_when_resource_exist(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.delete.return_value = True
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_ABSENT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_DELETED,
        )
    def test_should_upload_backup(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.upload_backup_bundle_from_file.return_value = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_BACKUP_UPLOAD)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_BACKUP_UPLOADED,
            ansible_facts=dict(
                artifact_bundle_deployment_group=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_upload_when_data_has_destination_path(self):
        self.resource.get_by_name.return_value = None
        self.resource.upload_bundle_from_file.return_value = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_UPLOAD)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_UPLOADED,
            ansible_facts=dict(artifact_bundle=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_create_when_resource_not_exist(self):
        self.resource.get_by_name.return_value = None
        self.resource.create.return_value = self.resource
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_PRESENT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_CREATED,
            ansible_facts=dict(artifact_bundle=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_update_when_data_has_modified_attributes(self):
        data_merged = DICT_DEFAULT_ARTIFACT_BUNDLE.copy()
        data_merged['newName'] = 'New Name'

        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_RENAME)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_UPDATED,
            ansible_facts=dict(artifact_bundle=DICT_DEFAULT_ARTIFACT_BUNDLE))
    def test_should_extract_backup(self):
        self.resource.data = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.resource.get_all_backups.return_value = [
            DICT_DEFAULT_ARTIFACT_BUNDLE
        ]
        self.resource.get_backup.return_value = self.resource
        self.resource.extract_backup.return_value = DICT_DEFAULT_ARTIFACT_BUNDLE
        self.mock_ansible_module.params = yaml.load(
            YAML_ARTIFACT_BUNDLE_BACKUP_EXTRACT)

        ArtifactBundleModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=ArtifactBundleModule.MSG_BACKUP_EXTRACTED,
            ansible_facts=dict(
                artifact_bundle_deployment_group=DICT_DEFAULT_ARTIFACT_BUNDLE))