Exemplo n.º 1
0
    def test_update_docker_format(self, tmpdir, mocker):
        """Test that script and integration formatter update docker image tag
        """
        test_tag = '1.0.0-test-tag'
        mocker.patch.object(DockerImageValidator, 'get_docker_image_latest_tag_request', return_value=test_tag)
        schema_dir = f'{GIT_ROOT}/demisto_sdk/commands/common/schemas'
        test_files_dir = f'{GIT_ROOT}/demisto_sdk/tests/test_files/update-docker'
        dest = str(tmpdir.join('docker-res.yml'))

        # test example script file with version before 5.0.0
        src_file = f'{test_files_dir}/SlackAsk.yml'
        with open(src_file) as f:
            data = yaml.safe_load(f)
        org_docker = data['dockerimage']
        assert data['fromversion'] < '5.0.0'
        assert not data.get(
            'dockerimage45')  # make sure for the test that dockerimage45 is not set (so we can verify that we set it in format)
        format_obj = ScriptYMLFormat(src_file, output=dest, path=f'{schema_dir}/script.yml', no_validate=True,
                                     update_docker=True)
        assert format_obj.run_format() == 0
        with open(dest) as f:
            data = yaml.safe_load(f)
        assert data['dockerimage'].endswith(f':{test_tag}')
        assert data['dockerimage45'] == org_docker

        # test integration file
        src_file = f'{test_files_dir}/Slack.yml'
        format_obj = IntegrationYMLFormat(src_file, output=dest, path=f'{schema_dir}/integration.yml', no_validate=True,
                                          update_docker=True)
        assert format_obj.run_format() == 0
        with open(dest) as f:
            data = yaml.safe_load(f)
        assert data['script']['dockerimage'].endswith(f':{test_tag}')
        assert not data['script'].get('dockerimage45')
Exemplo n.º 2
0
    def test_update_docker_format_with_invalid_dockerimage(self, requests_mock, mocker, tmp_path, docker_image, ):
        """
            Given
                - An integration yml file.
            When
                - Run format on the integration
            Then
                - Ensure format runs successfully
                - Verify the docker image is not modified
            """

        auth_token = 'token'
        mocker.patch.object(DockerImageValidator, 'docker_auth', return_value=auth_token)

        requests_mock.get('https://hub.docker.com/v2/repositories/error/tags', json={"detail": "Object not found"},
                          status_code=404)
        requests_mock.get('https://registry-1.docker.io/v2/error/tags/list', json={'error': 'not found'},
                          status_code=401)
        requests_mock.get('https://hub.docker.com/v2/repositories/demisto/error/tags', json={"count": 0,
                                                                                             "next": 'null',
                                                                                             "previous": 'null',
                                                                                             "results": []},
                          status_code=200)
        integration_yml_file_1 = tmp_path / 'Integration1.yml'
        integration_obj = {'dockerimage': docker_image,
                           'fromversion': '5.0.0'}
        ryaml.dump(integration_obj, integration_yml_file_1.open('w'))

        format_obj = ScriptYMLFormat(str(integration_yml_file_1), update_docker=True)
        format_obj.update_docker_image()
        with open(str(integration_yml_file_1)) as f:
            data = yaml.safe_load(f)
        assert data.get('dockerimage') == docker_image
Exemplo n.º 3
0
def test_pwsh_format(tmpdir, yml_file, yml_type):
    schema_path = os.path.normpath(
        os.path.join(__file__, "..", "..", "..", "common", "schemas", '{}.yml'.format(yml_type)))
    dest = str(tmpdir.join('pwsh_format_res.yml'))
    src_file = f'{GIT_ROOT}/demisto_sdk/tests/test_files/{yml_file}'
    if yml_type == 'script':
        format_obj = ScriptYMLFormat(src_file, output=dest, path=schema_path)
    else:
        format_obj = IntegrationYMLFormat(src_file, output=dest, path=schema_path)
    assert format_obj.run_format() == 0
    with open(dest) as f:
        data = yaml.safe_load(f)
    assert data['fromversion'] == '5.5.0'
    assert data['commonfields']['version'] == -1
Exemplo n.º 4
0
 def update_docker_image(self):
     if self.update_docker:
         ScriptYMLFormat.update_docker_image_in_script(
             self.data['script'], self.source_file,
             self.data.get(self.from_version_key))