Пример #1
0
def test_get_manifest_bad_v2(responses, caplog):
    """Couldn't get a v2 manifest."""
    caplog.set_level(logging.DEBUG, logger="charmcraft")
    ocireg = OCIRegistry("fakereg.com", "test-orga", "test-image")

    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest"}
    response_content = {"schemaVersion": 1}
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)

    # second response with a bad manifest
    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest-for-real"}
    response_content = {"sadly broken": ":("}
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)

    # try it
    with pytest.raises(CommandError) as cm:
        ocireg.get_manifest("test-reference")
    assert str(cm.value) == "Manifest v2 not found for 'test-reference'."
    expected = (
        "Got something else when asking for a v2 manifest: {'sadly broken': ':('}"
    )
    assert expected in [rec.message for rec in caplog.records]
Пример #2
0
def test_get_manifest_simple_multiple(responses):
    """Straightforward download of a multiple manifest."""
    ocireg = OCIRegistry("fakereg.com", "test-orga", "test-image")
    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest"}
    lot_of_manifests = [
        {
            "manifest1": "stuff"
        },
        {
            "manifest2": "morestuff",
            "foo": "bar"
        },
    ]
    response_content = {"manifests": lot_of_manifests}
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)

    # try it
    sublist, digest, raw_manifest = ocireg.get_manifest("test-reference")
    assert sublist == lot_of_manifests
    assert digest == "test-digest"
    assert raw_manifest == responses.calls[0].response.text  # exact
Пример #3
0
def test_get_manifest_simple_v2(responses, caplog):
    """Straightforward download of a v2 manifest."""
    caplog.set_level(logging.DEBUG, logger="charmcraft")

    ocireg = OCIRegistry("fakereg.com", "test-orga", "test-image")
    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest"}
    response_content = {
        "schemaVersion": 2,
        "foo": "bar",
        "unicodecontent": "moño"
    }
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)

    # try it
    sublist, digest, raw_manifest = ocireg.get_manifest("test-reference")
    assert sublist is None
    assert digest == "test-digest"
    assert raw_manifest == responses.calls[
        0].response.text  # must be exactly the same

    log_lines = [rec.message for rec in caplog.records]
    assert "Getting manifests list for test-reference" in log_lines
    assert "Got the manifest directly, schema 2" in log_lines

    assert responses.calls[0].request.headers["Accept"] == MANIFEST_LISTS
Пример #4
0
def test_get_manifest_v1_and_redownload(responses, caplog):
    """Get a v2 manifest after initially getting a v1."""
    caplog.set_level(logging.DEBUG, logger="charmcraft")

    ocireg = OCIRegistry("fakereg.com", "test-orga", "test-image")
    # first response with v1 manifest
    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest"}
    response_content = {"schemaVersion": 1}
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)
    # second response with v2 manifest, note the changed digest!
    url = "https://fakereg.com/v2/test-orga/test-image/manifests/test-reference"
    response_headers = {"Docker-Content-Digest": "test-digest-for-real"}
    response_content = {"schemaVersion": 2}
    responses.add(responses.GET,
                  url,
                  status=200,
                  headers=response_headers,
                  json=response_content)

    # try it
    sublist, digest, raw_manifest = ocireg.get_manifest("test-reference")
    assert sublist is None
    assert digest == "test-digest-for-real"
    assert raw_manifest == responses.calls[1].response.text  # the second one

    log_lines = [rec.message for rec in caplog.records]
    assert "Getting manifests list for test-reference" in log_lines
    assert "Got the manifest directly, schema 1" in log_lines
    assert "Got the v2 manifest ok" in log_lines

    assert responses.calls[0].request.headers["Accept"] == MANIFEST_LISTS
    assert responses.calls[1].request.headers["Accept"] == MANIFEST_V2_MIMETYPE