def test_is_docker_image_latest_tag_without_tag(): """ Given - A latest docker image has an empty tag When - The most updated docker image in docker-hub is '1.0.3' Then - If the docker image is numeric and the most update one, it is Valid - If the docker image is not numeric and labeled "latest", it is Invalid """ with mock.patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None): docker_image_validator = DockerImageValidator(None, None, None) docker_image_validator.yml_file = {} docker_image_validator.ignored_errors = {} docker_image_validator.file_path = "path" docker_image_validator.docker_image_latest_tag = '' docker_image_validator.docker_image_name = 'demisto/python' docker_image_validator.code_type = 'python' docker_image_validator.suppress_print = False docker_image_validator.is_latest_tag = True docker_image_validator.docker_image_tag = '1.0.2' docker_image_validator.is_valid = True assert docker_image_validator.is_docker_image_latest_tag() is False assert docker_image_validator.is_latest_tag is False assert docker_image_validator.is_docker_image_valid() is False
def test_is_docker_image_latest_tag_with_numeric_but_not_most_updated(): """ Given - A docker image with '1.0.2' as tag When - The most updated docker image in docker-hub is '1.0.3' Then - If the docker image is numeric and the most update one, it is Valid - If the docker image is not numeric and labeled "latest", it is Invalid - If the docker image is not the most updated one it is still valid (however, a warning will be printed) """ with mock.patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None): docker_image_validator = DockerImageValidator(None, None, None) docker_image_validator.yml_file = {} docker_image_validator.docker_image_latest_tag = '1.0.3' docker_image_validator.docker_image_name = 'demisto/python' docker_image_validator.is_latest_tag = True docker_image_validator.docker_image_tag = '1.0.2' docker_image_validator.is_valid = True assert docker_image_validator.is_docker_image_latest_tag() is True assert docker_image_validator.is_latest_tag is True assert docker_image_validator.is_docker_image_valid() is True
def test_is_docker_image_latest_tag_with_default_image(): """ Given - The default docker image - 'demisto/python:1.3-alpine' When - The most updated docker image in docker-hub is '1.0.3' Then - If the docker image is numeric and the most update one, it is Valid - If the docker image is not numeric and labeled "latest", it is Invalid """ with mock.patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None): docker_image_validator = DockerImageValidator(None, None, None) docker_image_validator.yml_file = {} docker_image_validator.file_path = "PATH" docker_image_validator.ignored_errors = {} docker_image_validator.code_type = 'python' docker_image_validator.checked_files = set() docker_image_validator.docker_image_latest_tag = '1.0.3' docker_image_validator.docker_image_name = 'demisto/python' docker_image_validator.suppress_print = False docker_image_validator.is_latest_tag = True docker_image_validator.is_modified_file = False docker_image_validator.docker_image_tag = '1.3-alpine' docker_image_validator.is_valid = True assert docker_image_validator.is_docker_image_latest_tag() is False assert docker_image_validator.is_latest_tag is False assert docker_image_validator.is_docker_image_valid() is False
def test_is_docker_image_latest_tag_with_numeric_but_not_most_updated(): """ Given - A docker image with '1.0.2' as tag When - The most updated docker image in docker-hub is '1.0.3' Then - If the docker image is numeric and the most update one, it is Valid - If the docker image is not numeric and labeled "latest", it is Invalid - If the docker image is not the most updated one it is invalid """ with mock.patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None): docker_image_validator = DockerImageValidator(None, None, None) docker_image_validator.yml_file = {} docker_image_validator.ignored_errors = {} docker_image_validator.file_path = "path" docker_image_validator.docker_image_latest_tag = '1.0.3' docker_image_validator.docker_image_name = 'demisto/python' docker_image_validator.code_type = 'python' docker_image_validator.checked_files = set() docker_image_validator.is_latest_tag = True docker_image_validator.docker_image_tag = '1.0.2' docker_image_validator.is_valid = True assert docker_image_validator.is_docker_image_latest_tag() is False assert docker_image_validator.is_latest_tag is False assert docker_image_validator.is_docker_image_valid() is False
def test_is_docker_image_latest_tag_with_tag_labeled_latest(): """ Given - A docker image with "latest" as tag When - The most updated docker image in docker-hub is '1.0.3' Then - If the docker image is numeric and the most update one, it is Valid - If the docker image is not numeric and labeled "latest", it is Invalid """ with mock.patch.object(DockerImageValidator, '__init__', lambda x, y, z, w: None): docker_image_validator = DockerImageValidator(None, None, None) docker_image_validator.yml_file = {} docker_image_validator.docker_image_latest_tag = 'latest' docker_image_validator.docker_image_name = 'demisto/python' docker_image_validator.is_latest_tag = True docker_image_validator.is_valid = True docker_image_validator.docker_image_tag = 'latest' assert docker_image_validator.is_docker_image_latest_tag() is False assert docker_image_validator.is_latest_tag is False assert docker_image_validator.is_docker_image_valid() is False
def is_docker_image_valid(self): # type: () -> bool docker_image_validator = DockerImageValidator(self.file_path, is_modified_file=True, is_integration=False) if docker_image_validator.is_docker_image_valid(): return True return False
def is_docker_image_valid(self): # type: () -> bool # dockers should not be checked when running on all files if self.skip_docker_check: return True docker_image_validator = DockerImageValidator(self.file_path, is_modified_file=True, is_integration=False, ignored_errors=self.ignored_errors, print_as_warnings=self.print_as_warnings) if docker_image_validator.is_docker_image_valid(): return True return False
def is_docker_image_valid(self): # type: () -> bool # dockers should not be checked when running on all files # dockers should not be checked when running on ApiModules scripts if self.skip_docker_check or API_MODULES_PACK in self.file_path: return True docker_image_validator = DockerImageValidator(self.file_path, is_modified_file=True, is_integration=False, ignored_errors=self.ignored_errors, print_as_warnings=self.print_as_warnings, suppress_print=self.suppress_print) if docker_image_validator.is_docker_image_valid(): return True return False
def is_docker_image_valid(self): # type: () -> bool # dockers should not be checked on master branch if self.branch_name == 'master': return True docker_image_validator = DockerImageValidator( self.file_path, is_modified_file=True, is_integration=False, ignored_errors=self.ignored_errors, print_as_warnings=self.print_as_warnings) if docker_image_validator.is_docker_image_valid(): return True return False
def test_non_existing_docker(self, integration, capsys, requests_mock, mocker): docker_image = 'demisto/nonexistingdocker:1.4.0' integration.yml.write_dict({ 'script': { 'subtype': 'python3', 'type': 'python', 'dockerimage': docker_image } }) error, code = Errors.non_existing_docker(docker_image) mocker.patch.object(DockerImageValidator, 'docker_auth', return_value='auth') requests_mock.get( "https://hub.docker.com/v2/repositories/demisto/nonexistingdocker/tags", json={'results': []}) validator = DockerImageValidator(integration.yml.path, True, True) assert validator.is_docker_image_valid() is False captured = capsys.readouterr() assert validator.is_valid is False assert error in captured.out assert code in captured.out