Beispiel #1
0
def test_get_manifest_default(
    expected_manifest,
    manifest_type,
    accept_types,
    default_should_raise,
    requests_mock,
    fake_home,
):
    service = "registry.docker.io"
    image = "repo/sitory"
    expected_tag = "latest"
    expected_digest = "sha256:dummy_digest"
    # return manifest type depending on Content-Type header

    headers = {"docker-content-digest": expected_digest}
    if manifest_type:
        headers["content-type"] = manifest_type
    request_headers = {}
    if accept_types:
        request_headers = {"Accept": ",".join(accept_types)}
    requests_mock.get(
        "https://%s/v2/%s/manifests/%s" % (service, image, expected_tag),
        status_code=200,
        headers=headers,
        request_headers=request_headers,
        json=expected_manifest,
    )

    # test default
    if default_should_raise:
        with pytest.raises(NoMockAddress):
            _, actual_digest, actual_manifest = get_manifest(
                "https://%s" % service, image, expected_tag)
    else:
        _, actual_digest, actual_manifest = get_manifest(
            "https://%s" % service, image, expected_tag)
        assert expected_digest == actual_digest
        assert expected_manifest == actual_manifest

    # test required
    _, actual_digest, actual_manifest = get_manifest(
        "https://%s" % service,
        image,
        expected_tag,
        manifest_types=accept_types)
    assert expected_digest == actual_digest
    assert expected_manifest == actual_manifest
Beispiel #2
0
def test_get_manifest_raise_on_500(requests_mock, fake_home):
    tag = "latest"
    image = "repo/sitory"
    service = "registry.docker.io"
    requests_mock.register_uri("GET",
                               "https://%s/v2/%s/manifests/%s" %
                               (service, image, tag),
                               status_code=500)

    with pytest.raises(requests.exceptions.HTTPError,
                       match="500 Server Error: None"):
        _, digest, manifest = get_manifest("https://registry.docker.io", image,
                                           tag)
Beispiel #3
0
def test_get_manifest_raise_on_404(requests_mock, fake_home):
    # when there is no API endpoint found, regular 404 should be raised.
    # It is the difference to correct API-returned 404 which contains
    # json data about errors
    # (test_get_manifest_raise_on_UNKNOWN_MANIFEST)
    tag = "latest"
    image = "repo/sitory"
    service = "registry.docker.io"

    requests_mock.register_uri("GET",
                               "https://%s/v2/%s/manifests/%s" %
                               (service, image, tag),
                               status_code=404)

    with pytest.raises(requests.exceptions.HTTPError,
                       match="404 Client Error: None"):
        _, digest, manifest = get_manifest("https://%s" % service, image, tag)
Beispiel #4
0
def test_get_manifest_raise_on_UNKNOWN_MANIFEST(requests_mock, fake_home):
    tag = "latest"
    image = "repo/sitory"
    service = "registry.docker.io"

    error_message = re.escape(
        "\"Failed to get the manifest for image 'repo/sitory' [latest]\"")
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/%s/manifests/%s" % (service, image, tag),
        status_code=404,
        json={"errors": [{
            "code": "MANIFEST_UNKNOWN"
        }]},
    )

    with pytest.raises(KeyError, match=error_message):
        digest, manifest = get_manifest("https://%s" % service, image, tag)
Beispiel #5
0
def test_get_manifest(requests_mock, fake_home):
    service = "registry.docker.io"
    image = "repo/sitory"
    expected_tag = "latest"
    expected_digest = (
        "sha256:25a01ec9bfd13d405583ef21cdc9ba3182684578ee46248559e75eb59cd73f36"
    )
    expected_manifest = {
        "architecture":
        "amd64",
        "fsLayers": [
            {
                "blobSum":
                "sha256:b4a41a81fce32bd7fb00ad10e6c73285f937ce2110a619d306ec09f487b40cca"
            },
            {
                "blobSum":
                "sha256:16dc1f96e3a1bb628be2e00518fec2bb97bd5933859de592a00e2eb7774b6ecf"
            },
        ],
        "name":
        "twaugh/buildroot",
        "schemaVersion":
        1,
        "signatures": [{
            "header": {
                "alg": "ES256",
                "jwk": {
                    "crv": "P-256",
                    "kid":
                    "IT3U:4H7H:YPAL:CSU4:CAC2:MFNW:IWV5:YQNM:547A:FMN4:A4D3:UZAG",
                    "kty": "EC",
                    "x": "WjjIX9YwUSO64Zc5iNMODC4mh9vB-mqgt1uEcE7gLfE",
                    "y": "6fhZlalfi9_cvQd-AwBBwGnidIZKIYVzLsSV6HBR8jA",
                },
            },
            "protected":
            "eyJmb3JtYXRMZW5ndGgiOjIzMjAsImZvcm1hdFRhaWwiOiJDbjAiLCJ0aW1lIjoiMjAxNi0wNy0xM1QwOToxNjoxMFoifQ",
            "signature":
            "-dqrUzJ8IbSURO__gkbG2vdzQEbjX32Qv2DjWG3mazTGsRXXgYofr-6VY7lMDUwiOERTD9Te9wyyrALMB7Yt1A",
        }],
        "tag":
        expected_tag,
    }

    headers = {
        "docker-content-digest": expected_digest,
        "content-type": "application/vnd.docker.distribution.manifest.v1+json",
    }
    requests_mock.register_uri(
        "GET",
        "https://%s/v2/%s/manifests/%s" % (service, image, expected_tag),
        status_code=200,
        headers=headers,
        json=expected_manifest,
    )

    _, actual_digest, actual_manifest = get_manifest("https://%s" % service,
                                                     image, expected_tag)
    assert expected_digest == actual_digest
    assert expected_manifest == actual_manifest