def get_models(metadata, mask_id=''): """ Given image metadata, returns model instances to represent each layer of the image defined by the unit_key :param metadata: a dictionary where keys are image IDs, and values are dictionaries with keys "parent" and "size", containing values for those two attributes as taken from the docker image metadata. :type metadata: dict :param mask_id: The ID of an image that should not be included in the returned models. This image and all of its ancestors will be excluded. :type mask_id: basestring :return: list of models.DockerImage instances :rtype: list """ images = [] existing_image_ids = set() leaf_image_ids = tarutils.get_youngest_children(metadata) for image_id in leaf_image_ids: while image_id: json_data = metadata[image_id] parent_id = json_data.get('parent') size = json_data['size'] if image_id not in existing_image_ids: # This will avoid adding multiple images with a same id, which can happen # in case of parents with multiple children. existing_image_ids.add(image_id) images.append(models.DockerImage(image_id, parent_id, size)) if parent_id == mask_id: break image_id = parent_id return images
def test_init_info(self): image = models.DockerImage('abc', 'xyz', 1024) self.assertEqual(image.image_id, 'abc') self.assertEqual(image.parent_id, 'xyz') self.assertEqual(image.size, 1024)
def test_metadata(self): image = models.DockerImage('abc', 'xyz', 1024) metadata = image.unit_metadata self.assertEqual(metadata.get('parent_id'), 'xyz') self.assertEqual(metadata.get('size'), 1024)
def test_relative_path(self): image = models.DockerImage('abc', 'xyz', 1024) self.assertEqual(image.relative_path, 'docker_image/abc')
def test_unit_key(self): image = models.DockerImage('abc', 'xyz', 1024) self.assertEqual(image.unit_key, {'image_id': 'abc'})