import json import pytest from image.docker.schema1 import MalformedSchema1Manifest, DockerSchema1Manifest @pytest.mark.parametrize('json_data', [ '', '{}', """ { "unknown": "key" } """, ]) def test_malformed_manifests(json_data): with pytest.raises(MalformedSchema1Manifest): DockerSchema1Manifest(json_data) @pytest.mark.parametrize('namespace', [ '', 'somenamespace', ]) def test_valid_manifest(namespace): manifest_bytes = json.dumps({ "name": namespace + "/hello-world" if namespace else 'hello-world', "tag": "latest", "architecture": "amd64", "fsLayers": [ { "blobSum": "sha256:cc8567d70002e957612902a8e985ea129d831ebe04057d88fb644857caa45d11" }, { "blobSum": "sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef" } ], "history": [ { "v1Compatibility": "{\"id\":\"someid\", \"parent\": \"anotherid\"}" }, { "v1Compatibility": "{\"id\":\"anotherid\"}" }, ], "schemaVersion": 1, "signatures": [ { "header": { "jwk": { "crv": "P-256", "kid": "OD6I:6DRK:JXEJ:KBM4:255X:NSAA:MUSF:E4VM:ZI6W:CUN2:L4Z6:LSF4", "kty": "EC", "x": "3gAwX48IQ5oaYQAYSxor6rYYc_6yjuLCjtQ9LUakg4A", "y": "t72ge6kIA1XOjqjVoEOiPPAURltJFBMGDSQvEGVB010" }, "alg": "ES256" }, "signature": "XREm0L8WNn27Ga_iE_vRnTxVMhhYY0Zst_FfkKopg6gWSoTOZTuW4rK0fg_IqnKkEKlbD83tD46LKEGi5aIVFg", "protected": "eyJmb3JtYXRMZW5ndGgiOjY2MjgsImZvcm1hdFRhaWwiOiJDbjAiLCJ0aW1lIjoiMjAxNS0wNC0wOFQxODo1Mjo1OVoifQ" } ] }) manifest = DockerSchema1Manifest(manifest_bytes, validate=False) assert len(manifest.signatures) == 1 assert manifest.namespace == namespace assert manifest.repo_name == 'hello-world' assert manifest.tag == 'latest' assert manifest.image_ids == {'someid', 'anotherid'} assert manifest.parent_image_ids == {'anotherid'} assert len(manifest.layers) == 2 assert manifest.layers[0].v1_metadata.image_id == 'anotherid' assert manifest.layers[0].v1_metadata.parent_image_id is None assert manifest.layers[1].v1_metadata.image_id == 'someid' assert manifest.layers[1].v1_metadata.parent_image_id == 'anotherid' assert manifest.leaf_layer == manifest.layers[1] assert manifest.digest