def test_create_new_golden_image(self):
        self.i3s.golden_images.get_by.return_value = []
        self.i3s.golden_images.create.return_value = {"name": "name"}
        self.i3s.os_volumes.get_by_name.return_value = {
            'uri': '/rest/os-volumes/1'
        }
        self.i3s.build_plans.get_by.return_value = [{
            'uri':
            '/rest/build-plans/1'
        }]

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_CREATE

        GoldenImageModule().run()

        self.i3s.golden_images.create.assert_called_once_with({
            'osVolumeURI':
            '/rest/os-volumes/1',
            'description':
            'Test Description',
            'buildPlanUri':
            '/rest/build-plans/1',
            'name':
            'Demo Golden Image creation',
            'imageCapture':
            'true'
        })

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=GOLDEN_IMAGE_CREATED,
            ansible_facts=dict(golden_image={"name": "name"}))
    def test_golden_image_archive_download_nonexistent(self):
        self.i3s.golden_images.get_by.return_value = []
        self.mock_ansible_module.params = self.GOLDEN_IMAGE_ARCHIVE_DOWNLOAD

        GoldenImageModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=GOLDEN_IMAGE_WAS_NOT_FOUND, )
    def test_should_do_nothing_when_deleting_a_non_existent_golden_image(self):
        self.i3s.golden_images.get_by.return_value = []

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_DELETE

        GoldenImageModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            ansible_facts={}, changed=False, msg=GOLDEN_IMAGE_ALREADY_ABSENT)
    def test_should_fail_when_build_plan_not_found(self):
        self.i3s.golden_images.get_by.return_value = []
        self.i3s.build_plans.get_by.return_value = None

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_CREATE

        GoldenImageModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=I3S_BUILD_PLAN_WAS_NOT_FOUND)
    def test_should_fail_when_os_volume_not_found(self):
        self.i3s.golden_images.get_by.return_value = []

        self.i3s.os_volumes.get_by_name.return_value = None

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_CREATE

        GoldenImageModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=I3S_OS_VOLUME_WAS_NOT_FOUND)
    def test_should_fail_when_mandatory_attributes_are_missing(self):
        self.i3s.golden_images.get_by.return_value = []

        del self.GOLDEN_IMAGE_CREATE['data']['osVolumeName']

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_CREATE

        GoldenImageModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=I3S_MISSING_MANDATORY_ATTRIBUTES)
    def test_delete_golden_image(self):
        self.i3s.golden_images.get_by.return_value = [
            self.GOLDEN_IMAGE_CREATE['data']
        ]

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_DELETE

        GoldenImageModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            ansible_facts={}, changed=True, msg=GOLDEN_IMAGE_DELETED)
    def test_should_fail_when_present_is_incosistent(self):
        self.i3s.golden_images.get_by.return_value = []
        self.i3s.os_volumes.get_by_name.return_value = {
            'uri': '/rest/os-volumes/1'
        }

        self.GOLDEN_IMAGE_CREATE['data']['localImageFilePath'] = 'filename'

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_CREATE

        GoldenImageModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=I3S_CANT_CREATE_AND_UPLOAD)
    def test_update_golden_image(self):
        self.i3s.golden_images.get_by.return_value = [
            self.GOLDEN_IMAGE_CREATE['data']
        ]
        self.i3s.golden_images.update.return_value = {"name": "name"}

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_UPDATE

        GoldenImageModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=GOLDEN_IMAGE_UPDATED,
            ansible_facts=dict(golden_image={"name": "name"}))
    def test_should_not_update_when_data_is_equals(self):
        self.i3s.golden_images.get_by.return_value = [
            self.GOLDEN_IMAGE_UPDATE['data']
        ]

        del self.GOLDEN_IMAGE_UPDATE['data']['newName']

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_UPDATE

        GoldenImageModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=GOLDEN_IMAGE_ALREADY_UPDATED,
            ansible_facts=dict(golden_image=self.GOLDEN_IMAGE_UPDATE['data']))
    def test_golden_image_download(self):
        golden_image = self.GOLDEN_IMAGE_CREATE['data']
        golden_image['uri'] = '/rest/golden-images/1'

        self.i3s.golden_images.get_by.return_value = [golden_image]
        self.mock_ansible_module.params = self.GOLDEN_IMAGE_DOWNLOAD

        GoldenImageModule().run()

        download_file = self.GOLDEN_IMAGE_DOWNLOAD['data'][
            'destination_file_path']
        self.i3s.golden_images.download.assert_called_once_with(
            '/rest/golden-images/1', download_file)

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True, msg=GOLDEN_IMAGE_DOWNLOADED, ansible_facts={})
    def test_upload_a_golden_image(self):
        self.i3s.golden_images.get_by.return_value = []
        self.i3s.golden_images.upload.return_value = {"name": "name"}

        self.mock_ansible_module.params = self.GOLDEN_IMAGE_UPLOAD

        file_path = self.GOLDEN_IMAGE_UPLOAD['data']['localImageFilePath']

        GoldenImageModule().run()

        self.i3s.golden_images.upload.assert_called_once_with(
            file_path, self.GOLDEN_IMAGE_UPLOAD['data'])

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=GOLDEN_IMAGE_UPLOADED,
            ansible_facts=dict(golden_image={"name": "name"}))