def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' set_mount_dir() create_top_dir() check_docker_setup() if not check_image('vmware/tern@sha256:20b32a9a20752aa1ad' '7582c667704fda9f004cc4bfd8601fac7f2656c7567bb4'): try: container.pull_image('vmware/tern@sha256:20b32a9a20' '752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') except subprocess.CalledProcessError as error: print(error.output) self.image = DockerImage('vmware/tern@sha256:20b32' 'a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') # constants for this image self.image_id = ('acb194ad84d0f9734e794fbbdbb65fb' '7db6eda83f33e9e817bcc75b1bdd99f5e') self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ')
def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' create_working_dir() rootfs.set_working_dir() # this should check if the docker image extraction is successful dump_docker_image('vmware/tern@sha256:20b32a9a20752aa1ad7582c667704f' 'da9f004cc4bfd8601fac7f2656c7567bb4') self.image = DockerImage('vmware/tern@sha256:20b32a9a20752aa1ad7582c6' '67704fda9f004cc4bfd8601fac7f2656c7567bb4') # constants for this image self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ') self.file_info = [('file2.txt', 'documents/test/file2.txt', '9710f003d924890c7677b4dd91fd753f6ed71cc57d4f' '9482261b6786d81957fa', 'sha256'), ('file2.txt', 'documents/test/test2/file2.txt', '885000512dee8ac814641bbf6a7c887012ec23a2fb3e' '3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/test2/file1.txt', '885000512dee8ac814641bbf6a7c887012ec' '23a2fb3e3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/file1.txt', 'a3cccbc52486d50a86ff0bc1e6ea0e0b701ac' '4bb139f8713fa136ef9ec68e97e', 'sha256')]
def load_full_image(image_tag_string): '''Create image object from image name and tag and return the object''' test_image = DockerImage(image_tag_string) failure_origin = formats.image_load_failure.format( testimage=test_image.repotag) try: test_image.load_image() except (NameError, subprocess.CalledProcessError, IOError, ValueError, EOFError) as error: logger.warning('Error in loading image: %s', str(error)) test_image.origins.add_notice_to_origins(failure_origin, Notice(str(error), 'error')) return test_image
def testInstance(self): self.assertEqual( self.image.repotag, 'vmware/tern@sha256:20b32a9a2' '0752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') self.assertEqual(self.image.name, 'vmware/tern') self.assertEqual(self.image.tag, '') self.assertTrue(self.image.checksum_type, 'sha256') self.assertTrue( self.image.checksum, '20b32a9a20752aa1ad7582c66' '7704fda9f004cc4bfd8601fac7' 'f2656c7567bb4') self.assertFalse(self.image.manifest) self.assertFalse(self.image.repotags) self.assertFalse(self.image.config) self.assertFalse(self.image.layers) self.assertFalse(self.image.history) # test instantiating with a tag if not check_image('vmware/tern:testimage'): try: container.pull_image('vmware/tern:testimage') except subprocess.CalledProcessError as error: print(error.output) d = DockerImage('vmware/tern:testimage') self.assertEqual(d.name, 'vmware/tern') self.assertEqual(d.tag, 'testimage') self.assertEqual(d.checksum_type, 'sha256') self.assertEqual( d.checksum, '20b32a9a20752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4')
def get_dockerfile_base(): '''Get the base image object from the dockerfile base instructions 1. get the instructions around FROM 2. get the base image and tag 3. Make notes based on what the image and tag rules are 4. Return an image object and the base instructions string NOTE: Potential ARG values in the Dockerfile object have already been expanded at this point. However, Dockerfile rules say that if no --build-arg is passed during docker build and ARG has no default, the build will fail. We assume for now that we will not be passing build arguments in which case if there is no default ARG, we will raise an exception indicating that since the build arguments are determined by the user we will not be able to determine what the user wanted''' try: # Get the base image tag. # NOTE: ARG values have already been expanded. base_image_string, from_line = get_base_image_tag(docker_commands) # check for scratch if base_image_string == 'scratch': # there is no base image to pull raise ValueError("Cannot pull 'scratch' base image.") # there should be some image object here base_image = DockerImage(base_image_string) base_image.origins.add_notice_origin(from_line) base_image.name = base_image_string.split(':')[0] # check if there is a tag if not check_image_string(base_image_string): message_string = errors.dockerfile_no_tag.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( docker_commands, Notice(message_string, 'warning')) base_image.tag = 'latest' else: base_image.tag = base_image_string.split(':')[1] # check if the tag is 'latest' if base_image.tag == 'latest': message_string = errors.dockerfile_using_latest.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( docker_commands, Notice(message_string, 'warning')) return base_image, from_line except ValueError as e: logger.fatal( "%s", errors.cannot_parse_base_image.format(dockerfile=dockerfile_global, error_msg=e)) sys.exit(1)
def load_full_image(image_tag_string): '''Create image object from image name and tag and return the object''' test_image = DockerImage(image_tag_string) failure_origin = formats.image_load_failure.format( testimage=test_image.repotag) try: test_image.load_image() except NameError as error: test_image.origins.add_notice_to_origins(failure_origin, Notice(str(error), 'error')) except subprocess.CalledProcessError as error: test_image.origins.add_notice_to_origins( failure_origin, Notice(str(error.output, 'utf-8'), 'error')) except IOError as error: test_image.origins.add_notice_to_origins(failure_origin, Notice(str(error), 'error')) return test_image
def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' set_mount_dir() create_top_dir() check_docker_setup() if not check_image('vmware/tern@sha256:20b32a9a20752aa1ad' '7582c667704fda9f004cc4bfd8601fac7f2656c7567bb4'): try: container.pull_image('vmware/tern@sha256:20b32a9a20' '752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') except subprocess.CalledProcessError as error: print(error.output) self.image = DockerImage('vmware/tern@sha256:20b32' 'a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') # constants for this image self.image_id = ('acb194ad84d0f9734e794fbbdbb65fb' '7db6eda83f33e9e817bcc75b1bdd99f5e') self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ') self.file_info = [ ('file2.txt', 'documents/test/file2.txt', '9710f003d924890c7677b4dd91fd753f6ed71cc57d4f' '9482261b6786d81957fa', 'sha256'), ('file2.txt', 'documents/test/test2/file2.txt', '885000512dee8ac814641bbf6a7c887012ec23a2fb3e' '3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/test2/file1.txt', '885000512dee8ac814641bbf6a7c887012ec' '23a2fb3e3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/file1.txt', 'a3cccbc52486d50a86ff0bc1e6ea0e0b701ac' '4bb139f8713fa136ef9ec68e97e', 'sha256') ]
def get_dockerfile_base(): '''Get the base image object from the dockerfile base instructions 1. get the instructions around FROM 2. get the base image and tag 3. Make notes based on what the image and tag rules are 4. Return an image object and the base instructions string''' try: base_instructions = dockerfile.get_base_instructions(docker_commands) base_image_tag = dockerfile.get_base_image_tag(base_instructions) dockerfile_lines = print_dockerfile_base(base_instructions) # check for scratch if base_image_tag[0] == 'scratch': # there is no base image - return no image object return None # there should be some image object here repotag = base_image_tag[0] + dockerfile.tag_separator + \ base_image_tag[1] from_line = 'FROM ' + repotag base_image = DockerImage(repotag) base_image.origins.add_notice_origin(dockerfile_lines) base_image.name = base_image_tag[0] # check if there is a tag if not base_image_tag[1]: message_string = errors.dockerfile_no_tag.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( dockerfile_lines, Notice(message_string, 'warning')) base_image.tag = 'latest' else: base_image.tag = base_image_tag[1] # check if the tag is 'latest' if base_image_tag[1] == 'latest': message_string = errors.dockerfile_using_latest.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( dockerfile_lines, Notice(message_string, 'warning')) return base_image, dockerfile_lines except ValueError as e: logger.warning( "%s", errors.cannot_parse_base_image.format(dockerfile=dockerfile_global, error_msg=e)) return None
def testInstance(self): self.assertEqual( self.image.repotag, 'vmware/tern@sha256:20b32a9a2' '0752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') self.assertEqual(self.image.name, 'vmware/tern') self.assertEqual(self.image.tag, '') self.assertTrue(self.image.checksum_type, 'sha256') self.assertTrue( self.image.checksum, '20b32a9a20752aa1ad7582c66' '7704fda9f004cc4bfd8601fac7' 'f2656c7567bb4') self.assertFalse(self.image.manifest) self.assertFalse(self.image.repotags) self.assertFalse(self.image.config) self.assertFalse(self.image.layers) self.assertFalse(self.image.history) # test instantiating with a tag d = DockerImage('vmware/tern:testimage') self.assertEqual(d.name, 'vmware/tern') self.assertEqual(d.tag, 'testimage') self.assertFalse(d.checksum_type) self.assertFalse(d.checksum)
class TestClassDockerImage(unittest.TestCase): def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' create_working_dir() rootfs.set_working_dir() # this should check if the docker image extraction is successful dump_docker_image('vmware/tern@sha256:20b32a9a20752aa1ad7582c667704f' 'da9f004cc4bfd8601fac7f2656c7567bb4') self.image = DockerImage('vmware/tern@sha256:20b32a9a20752aa1ad7582c6' '67704fda9f004cc4bfd8601fac7f2656c7567bb4') # constants for this image self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ') self.file_info = [('file2.txt', 'documents/test/file2.txt', '9710f003d924890c7677b4dd91fd753f6ed71cc57d4f' '9482261b6786d81957fa', 'sha256'), ('file2.txt', 'documents/test/test2/file2.txt', '885000512dee8ac814641bbf6a7c887012ec23a2fb3e' '3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/test2/file1.txt', '885000512dee8ac814641bbf6a7c887012ec' '23a2fb3e3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/file1.txt', 'a3cccbc52486d50a86ff0bc1e6ea0e0b701ac' '4bb139f8713fa136ef9ec68e97e', 'sha256')] def tearDown(self): del self.image remove_working_dir() def testInstance(self): self.assertEqual( self.image.repotag, 'vmware/tern@sha256:20b32a9a2' '0752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') self.assertEqual(self.image.name, 'vmware/tern') self.assertEqual(self.image.tag, '') self.assertTrue(self.image.checksum_type, 'sha256') self.assertTrue( self.image.checksum, '20b32a9a20752aa1ad7582c66' '7704fda9f004cc4bfd8601fac7' 'f2656c7567bb4') self.assertFalse(self.image.manifest) self.assertFalse(self.image.repotags) self.assertFalse(self.image.config) self.assertFalse(self.image.layers) self.assertFalse(self.image.history) # test instantiating with a tag d = DockerImage('vmware/tern:testimage') self.assertEqual(d.name, 'vmware/tern') self.assertEqual(d.tag, 'testimage') self.assertFalse(d.checksum_type) self.assertFalse(d.checksum) def testLoadImage(self): self.image.load_image() self.assertEqual(self.image.layers[0].diff_id, self.layer) self.assertEqual(len(self.image.layers), self.no_layers) self.assertEqual(self.image.layers[0].created_by, self.created_by) self.assertEqual(self.image.layers[0].checksum_type, 'sha256') self.assertEqual(self.image.layers[0].checksum, self.layer) def testGetLayerDiffIds(self): self.image.load_image() self.assertEqual(len(self.image.get_layer_diff_ids()), self.no_layers) self.assertEqual(self.image.get_layer_diff_ids()[0], self.layer) def testLayerFiles(self): self.image.load_image() self.assertFalse(self.image.layers[0].files) print(self.image.layers[0].image_layout) self.image.layers[0].add_files() for file in self.image.layers[0].files: self.assertTrue((file.name, file.path, file.checksum, file.checksum_type) in self.file_info)
def get_dockerfile_base(): '''Get the base image object from the dockerfile base instructions 1. get the instructions around FROM 2. get the base image and tag 3. Make notes based on what the image and tag rules are 4. Return an image object and the base instructions string NOTE: Potential ARG values in the Dockerfile object have already been expanded at this point. However, Dockerfile rules say that if no --build-arg is passed during docker build and ARG has no default, the build will fail. We assume for now that we will not be passing build arguments in which case if there is no default ARG, we will raise an exception indicating that since the build arguments are determined by the user we will not be able to determine what the user wanted''' try: dockerfile_lines = docker_commands base_image_string = '' from_line = '' # Get the base image tag. # NOTE: ARG values have already been expanded. for i, cmd_dict in enumerate(dockerfile_lines): if cmd_dict['instruction'] == 'FROM': # Account for "as" keyword in FROM line base_image_string = re.split(" as", cmd_dict['value'], flags=re.IGNORECASE)[0] from_line = 'FROM' + base_image_string # Check that potential ARG values has default if i != 0 and dockerfile_lines[i - 1]['instruction'] == 'ARG': if len(dockerfile_lines[i - 1]['value'].split('=')) == 1: raise ValueError('No ARG default value to pass to ' 'FROM command in Dockerfile.') break # check for scratch if base_image_string == 'scratch': # there is no base image to pull raise ValueError("Cannot pull 'scratch' base image.") # there should be some image object here base_image = DockerImage(base_image_string) base_image.origins.add_notice_origin(from_line) base_image.name = base_image_string.split(':')[0] # check if there is a tag if not check_image_string(base_image_string): message_string = errors.dockerfile_no_tag.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( dockerfile_lines, Notice(message_string, 'warning')) base_image.tag = 'latest' else: base_image.tag = base_image_string.split(':')[1] # check if the tag is 'latest' if base_image.tag == 'latest': message_string = errors.dockerfile_using_latest.format( dockerfile_line=from_line) base_image.origins.add_notice_to_origins( dockerfile_lines, Notice(message_string, 'warning')) return base_image, from_line except ValueError as e: logger.fatal( "%s", errors.cannot_parse_base_image.format(dockerfile=dockerfile_global, error_msg=e)) sys.exit(1)
class TestClassDockerImage(unittest.TestCase): def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' set_mount_dir() create_top_dir() check_docker_setup() if not check_image('vmware/tern@sha256:20b32a9a20752aa1ad' '7582c667704fda9f004cc4bfd8601fac7f2656c7567bb4'): try: container.pull_image('vmware/tern@sha256:20b32a9a20' '752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') except subprocess.CalledProcessError as error: print(error.output) self.image = DockerImage('vmware/tern@sha256:20b32' 'a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') # constants for this image self.image_id = ('acb194ad84d0f9734e794fbbdbb65fb' '7db6eda83f33e9e817bcc75b1bdd99f5e') self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ') def tearDown(self): container.close_client() del self.image def testInstance(self): self.assertEqual( self.image.repotag, 'vmware/tern@sha256:20b32a9a2' '0752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') self.assertEqual(self.image.name, 'vmware/tern@sha256') self.assertEqual( self.image.tag, '20b32a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') self.assertFalse(self.image.image_id) self.assertFalse(self.image.manifest) self.assertFalse(self.image.repotags) self.assertFalse(self.image.config) self.assertFalse(self.image.layers) self.assertFalse(self.image.history) def testLoadImage(self): self.image.load_image() self.assertEqual(self.image.image_id, self.image_id) self.assertEqual(self.image.layers[0].diff_id, self.layer) self.assertEqual(len(self.image.layers), self.no_layers) self.assertEqual(self.image.layers[0].created_by, self.created_by) def testGetImageOption(self): self.assertEqual(self.image.get_image_option(), self.image.repotag) def testGetLayerDiffIds(self): self.image.load_image() self.assertEqual(len(self.image.get_layer_diff_ids()), self.no_layers) self.assertEqual(self.image.get_layer_diff_ids()[0], self.layer)
class TestClassDockerImage(unittest.TestCase): def setUp(self): '''Using a specific image here. If this test fails due to the image not being found anymore, pick a different image to test against For now use Docker to pull the image from Dockerhub''' set_mount_dir() create_top_dir() check_docker_setup() if not check_image('vmware/tern@sha256:20b32a9a20752aa1ad' '7582c667704fda9f004cc4bfd8601fac7f2656c7567bb4'): try: container.pull_image('vmware/tern@sha256:20b32a9a20' '752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') except subprocess.CalledProcessError as error: print(error.output) self.image = DockerImage('vmware/tern@sha256:20b32' 'a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') # constants for this image self.image_id = ('acb194ad84d0f9734e794fbbdbb65fb' '7db6eda83f33e9e817bcc75b1bdd99f5e') self.layer = ('c1c3a87012e7ff5791b31e94515b661' 'cdf06f6d5dc2f9a6245eda8774d257a13') self.no_layers = 1 self.created_by = ('/bin/sh -c #(nop) ADD ' 'file:92137e724f46c720d8083a11290c67' 'd9daa387e523336b1757a0e3c4f5867cd5 ' 'in / ') self.file_info = [('file2.txt', 'documents/test/file2.txt', '9710f003d924890c7677b4dd91fd753f6ed71cc57d4f' '9482261b6786d81957fa', 'sha256'), ('file2.txt', 'documents/test/test2/file2.txt', '885000512dee8ac814641bbf6a7c887012ec23a2fb3e' '3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/test2/file1.txt', '885000512dee8ac814641bbf6a7c887012ec' '23a2fb3e3b2cff583c45a611317d', 'sha256'), ('file1.txt', 'documents/test/file1.txt', 'a3cccbc52486d50a86ff0bc1e6ea0e0b701ac' '4bb139f8713fa136ef9ec68e97e', 'sha256')] def tearDown(self): container.close_client() del self.image def testInstance(self): self.assertEqual( self.image.repotag, 'vmware/tern@sha256:20b32a9a2' '0752aa1ad7582c667704fda9f004cc4' 'bfd8601fac7f2656c7567bb4') self.assertEqual(self.image.name, 'vmware/tern@sha256') self.assertEqual( self.image.tag, '20b32a9a20752aa1ad7582c667704fda9f00' '4cc4bfd8601fac7f2656c7567bb4') self.assertFalse(self.image.image_id) self.assertFalse(self.image.manifest) self.assertFalse(self.image.repotags) self.assertFalse(self.image.config) self.assertFalse(self.image.layers) self.assertFalse(self.image.history) def testLoadImage(self): self.image.load_image() self.assertEqual(self.image.image_id, self.image_id) self.assertEqual(self.image.layers[0].diff_id, self.layer) self.assertEqual(len(self.image.layers), self.no_layers) self.assertEqual(self.image.layers[0].created_by, self.created_by) self.assertEqual(self.image.layers[0].checksum_type, 'sha256') self.assertEqual(self.image.layers[0].checksum, self.layer) def testGetImageOption(self): self.assertEqual(self.image.get_image_option(), self.image.repotag) def testGetLayerDiffIds(self): self.image.load_image() self.assertEqual(len(self.image.get_layer_diff_ids()), self.no_layers) self.assertEqual(self.image.get_layer_diff_ids()[0], self.layer) def testLayerFiles(self): self.image.load_image() self.assertFalse(self.image.layers[0].files) self.image.layers[0].add_files() for file in self.image.layers[0].files: self.assertTrue((file.name, file.path, file.checksum, file.checksum_type) in self.file_info)