def test_unicode_parse_as_unicode(): parsed = parse_dockerfile(""" FROM someimage:latest MAINTAINER José Schorr <*****@*****.**> """.decode("utf-8")) assert parsed.get_image_and_tag() == ("someimage", "latest") assert parsed.get_base_image() == "someimage"
def test_basic_parse(): parsed = parse_dockerfile(""" FROM someimage:latest RUN dosomething """) assert parsed.get_image_and_tag() == ("someimage", "latest") assert parsed.get_base_image() == "someimage"
def test_two_from_lines(): parsed = parse_dockerfile(""" FROM someimage:latest FROM secondimage:second """) assert parsed.get_image_and_tag() == ("secondimage", "second") assert parsed.get_base_image() == "secondimage"
def test_parse_comments(): parsed = parse_dockerfile(""" # FROM someimage:latest FROM anotherimage:foobar # This is a comment RUN dosomething """) assert parsed.get_image_and_tag() == ("anotherimage", "foobar") assert parsed.get_base_image() == "anotherimage"
def analyze_trigger(self): # Load the contents of the Dockerfile. contents = self.handler.load_dockerfile_contents() if not contents: return self.analyze_view( self.namespace_name, None, "warning", message= "Specified Dockerfile path for the trigger was not found on the main " + "branch. This trigger may fail.", ) # Parse the contents of the Dockerfile. parsed = dockerfileparse.parse_dockerfile(contents) if not parsed: return self.analyze_view( self.namespace_name, None, "error", message="Could not parse the Dockerfile specified", ) # Check whether the dockerfile_path is correct if self.new_config_dict.get("context") and not is_parent( self.new_config_dict.get("context"), self.new_config_dict.get("dockerfile_path")): return self.analyze_view( self.namespace_name, None, "error", message="Dockerfile, %s, is not a child of the context, %s." % ( self.new_config_dict.get("context"), self.new_config_dict.get("dockerfile_path"), ), ) # Determine the base image (i.e. the FROM) for the Dockerfile. base_image = parsed.get_base_image() if not base_image: return self.analyze_view( self.namespace_name, None, "warning", message="No FROM line found in the Dockerfile") # Check to see if the base image lives in Quay. quay_registry_prefix = "%s/" % self.server_hostname if not base_image.startswith(quay_registry_prefix): return self.analyze_view(self.namespace_name, None, "publicbase") # Lookup the repository in Quay. result = str(base_image)[len(quay_registry_prefix):].split("/", 2) if len(result) != 2: msg = '"%s" is not a valid Quay repository path' % base_image return self.analyze_view(self.namespace_name, None, "warning", message=msg) (base_namespace, base_repository) = result found_repository = model.repository.get_repository( base_namespace, base_repository) if not found_repository: return self.analyze_view( self.namespace_name, None, "error", message= 'Repository "%s" referenced by the Dockerfile was not found' % base_image, ) # If the repository is private and the user cannot see that repo, then # mark it as not found. can_read = permissions.ReadRepositoryPermission( base_namespace, base_repository) if found_repository.visibility.name != "public" and not can_read: return self.analyze_view( self.namespace_name, None, "error", message= 'Repository "%s" referenced by the Dockerfile was not found' % base_image, ) if found_repository.visibility.name == "public": return self.analyze_view(base_namespace, base_repository, "publicbase") return self.analyze_view(base_namespace, base_repository, "requiresrobot")