Example #1
0
    def _write_test_image(self, url, manifest):
        image, tag = image_uploader.BaseImageUploader._image_tag_from_url(
            url)
        blob_dir = os.path.join(
            image_export.IMAGE_EXPORT_DIR, 'v2', image[1:], 'blobs')
        image_export.make_dir(blob_dir)

        if manifest.get('schemaVersion', 2) == 1:
            config_str = None
            manifest_type = image_uploader.MEDIA_MANIFEST_V1
            layers = list(reversed([x['blobSum']
                                    for x in manifest['fsLayers']]))
        else:
            config_str = '{"config": {}}'
            manifest_type = image_uploader.MEDIA_MANIFEST_V2
            layers = [x['digest'] for x in manifest['layers']]
        manifest_str = json.dumps(manifest)
        calc_digest = hashlib.sha256()
        calc_digest.update(manifest_str.encode('utf-8'))
        manifest_digest = 'sha256:%s' % calc_digest.hexdigest()

        image_export.export_manifest_config(
            url, manifest_str, manifest_type, config_str
        )
        for digest in layers:
            blob_path = os.path.join(blob_dir, '%s.gz' % digest)

            with open(blob_path, 'w+') as f:
                f.write('The Blob')
        return manifest_digest
    def _write_test_image(self, url, manifest):
        image, tag = image_uploader.BaseImageUploader._image_tag_from_url(
            url)
        blob_dir = os.path.join(
            image_export.IMAGE_EXPORT_DIR, 'v2', image[1:], 'blobs')
        image_export.make_dir(blob_dir)

        if manifest.get('schemaVersion', 2) == 1:
            config_str = None
            manifest_type = image_uploader.MEDIA_MANIFEST_V1
            layers = list(reversed([l['blobSum']
                                    for l in manifest['fsLayers']]))
        else:
            config_str = '{"config": {}}'
            manifest_type = image_uploader.MEDIA_MANIFEST_V2
            layers = [l['digest'] for l in manifest['layers']]
        manifest_str = json.dumps(manifest)
        calc_digest = hashlib.sha256()
        calc_digest.update(manifest_str.encode('utf-8'))
        manifest_digest = 'sha256:%s' % calc_digest.hexdigest()

        image_export.export_manifest_config(
            url, manifest_str, manifest_type, config_str
        )
        for digest in layers:
            blob_path = os.path.join(blob_dir, '%s.gz' % digest)

            with open(blob_path, 'w+') as f:
                f.write('The Blob')
        return manifest_digest
    def test_export_manifest_config(self):
        target_url = urlparse('docker://localhost:8787/t/nova-api:latest')
        config_str = '{"config": {}}'
        config_digest = 'sha256:1234'
        manifest = {
            'config': {
                'digest': config_digest,
                'size': 2,
                'mediaType': 'application/vnd.docker.container.image.v1+json'
            },
            'layers': [
                {
                    'digest': 'sha256:aeb786'
                },
                {
                    'digest': 'sha256:4dc536'
                },
            ],
            'mediaType': 'application/vnd.docker.'
            'distribution.manifest.v2+json',
        }
        catalog = {'repositories': ['t/nova-api']}

        manifest_str = json.dumps(manifest)
        calc_digest = hashlib.sha256()
        calc_digest.update(manifest_str.encode('utf-8'))
        manifest_digest = 'sha256:%s' % calc_digest.hexdigest()

        image_export.export_manifest_config(target_url, manifest_str,
                                            image_uploader.MEDIA_MANIFEST_V2,
                                            config_str)

        catalog_path = os.path.join(image_export.IMAGE_EXPORT_DIR,
                                    'v2/_catalog')
        config_path = os.path.join(image_export.IMAGE_EXPORT_DIR,
                                   'v2/t/nova-api/blobs/sha256:1234')
        manifest_path = os.path.join(image_export.IMAGE_EXPORT_DIR,
                                     'v2/t/nova-api/manifests',
                                     manifest_digest, 'index.json')
        manifest_htaccess_path = os.path.join(image_export.IMAGE_EXPORT_DIR,
                                              'v2/t/nova-api/manifests',
                                              manifest_digest, '.htaccess')
        expected_htaccess = '''Header set Content-Type "%s"
Header set Docker-Content-Digest "%s"
Header set ETag "%s"
''' % ('application/vnd.docker.distribution.manifest.v2+json', manifest_digest,
        manifest_digest)

        with open(catalog_path, 'r') as f:
            self.assertEqual(catalog, json.load(f))
        with open(config_path, 'r') as f:
            self.assertEqual(config_str, f.read())
        with open(manifest_path, 'r') as f:
            self.assertEqual(manifest_str, f.read())
        with open(manifest_htaccess_path, 'r') as f:
            self.assertEqual(expected_htaccess, f.read())
    def _write_test_image(self, url, manifest):
        image, tag = image_uploader.BaseImageUploader._image_tag_from_url(url)
        blob_dir = os.path.join(image_export.IMAGE_EXPORT_DIR, 'v2', image[1:],
                                'blobs')
        image_export.make_dir(blob_dir)

        config_str = '{"config": {}}'
        manifest_str = json.dumps(manifest)
        calc_digest = hashlib.sha256()
        calc_digest.update(manifest_str.encode('utf-8'))
        manifest_digest = 'sha256:%s' % calc_digest.hexdigest()

        image_export.export_manifest_config(url, manifest_str,
                                            image_uploader.MEDIA_MANIFEST_V2,
                                            config_str)
        for layer in manifest['layers']:
            blob_path = os.path.join(blob_dir, '%s.gz' % layer['digest'])

            with open(blob_path, 'w+') as f:
                f.write('The Blob')
        return manifest_digest
    def test_export_manifest_config(self):
        target_url = urlparse('docker://localhost:8787/t/nova-api:latest')
        config_str = '{"config": {}}'
        config_digest = 'sha256:1234'
        manifest = {
            'config': {
                'digest': config_digest,
                'size': 2,
                'mediaType': 'application/vnd.docker.container.image.v1+json'
            },
            'layers': [
                {'digest': 'sha256:aeb786'},
                {'digest': 'sha256:4dc536'},
            ],
            'mediaType': 'application/vnd.docker.'
                         'distribution.manifest.v2+json',
        }
        catalog = {'repositories': ['t/nova-api']}

        manifest_str = json.dumps(manifest)
        calc_digest = hashlib.sha256()
        calc_digest.update(manifest_str.encode('utf-8'))
        manifest_digest = 'sha256:%s' % calc_digest.hexdigest()

        image_export.export_manifest_config(
            target_url, manifest_str,
            image_uploader.MEDIA_MANIFEST_V2, config_str
        )

        catalog_path = os.path.join(
            image_export.IMAGE_EXPORT_DIR,
            'v2/_catalog'
        )
        config_path = os.path.join(
            image_export.IMAGE_EXPORT_DIR,
            'v2/t/nova-api/blobs/sha256:1234'
        )
        manifest_path = os.path.join(
            image_export.IMAGE_EXPORT_DIR,
            'v2/t/nova-api/manifests',
            manifest_digest,
            'index.json'
        )
        manifest_htaccess_path = os.path.join(
            image_export.IMAGE_EXPORT_DIR,
            'v2/t/nova-api/manifests',
            manifest_digest,
            '.htaccess'
        )
        expected_htaccess = '''Header set Content-Type "%s"
Header set Docker-Content-Digest "%s"
Header set ETag "%s"
''' % (
            'application/vnd.docker.distribution.manifest.v2+json',
            manifest_digest,
            manifest_digest
        )

        with open(catalog_path, 'r') as f:
            self.assertEqual(catalog, json.load(f))
        with open(config_path, 'r') as f:
            self.assertEqual(config_str, f.read())
        with open(manifest_path, 'r') as f:
            self.assertEqual(manifest_str, f.read())
        with open(manifest_htaccess_path, 'r') as f:
            self.assertEqual(expected_htaccess, f.read())