|
| 1 | +import io |
| 2 | +import json |
| 3 | +import pytest |
| 4 | + |
| 5 | +from gardenlinux.oci.index import Index |
| 6 | + |
| 7 | + |
| 8 | +def test_index_init_and_json(): |
| 9 | + """Ensure Index init works correctly""" |
| 10 | + # Arrange |
| 11 | + idx = Index() |
| 12 | + |
| 13 | + # Act |
| 14 | + json_bytes = idx.json |
| 15 | + decoded = json.loads(json_bytes.decode("utf-8")) |
| 16 | + |
| 17 | + # Assert |
| 18 | + assert "schemaVersion" in idx |
| 19 | + assert isinstance(json_bytes, bytes) |
| 20 | + assert decoded == idx |
| 21 | + |
| 22 | + |
| 23 | +def test_manifests_as_dict(): |
| 24 | + """Verify manifests_as_dict returns correct keys for cname and digest cases.""" |
| 25 | + # Arrange |
| 26 | + idx = Index() |
| 27 | + manifest_cname = {"digest": "sha256:abc", "annotations": {"cname": "foo"}} |
| 28 | + manifest_no_cname = {"digest": "sha256:def"} |
| 29 | + idx["manifests"] = [manifest_cname, manifest_no_cname] |
| 30 | + |
| 31 | + # Act |
| 32 | + result = idx.manifests_as_dict |
| 33 | + |
| 34 | + # Assert |
| 35 | + assert result["foo"] == manifest_cname |
| 36 | + assert result["sha256:def"] == manifest_no_cname |
| 37 | + |
| 38 | + |
| 39 | +def test_append_manifest_replace(): |
| 40 | + """Ensure append_manifest replaces existing manifest with same cname.""" |
| 41 | + # Arrange |
| 42 | + idx = Index() |
| 43 | + idx["manifests"] = [ |
| 44 | + {"annotations": {"cname": "old"}, "digest": "sha256:old"}, |
| 45 | + {"annotations": {"cname": "other"}, "digest": "sha256:other"}, |
| 46 | + ] |
| 47 | + new_manifest = {"annotations": {"cname": "old"}, "digest": "sha256:new"} |
| 48 | + |
| 49 | + # Act |
| 50 | + idx.append_manifest(new_manifest) |
| 51 | + |
| 52 | + # Assert |
| 53 | + cnames = [manifest["annotations"]["cname"] for manifest in idx["manifests"]] |
| 54 | + assert "old" in cnames |
| 55 | + assert any(manifest["digest"] == "sha256:new" for manifest in idx["manifests"]) |
| 56 | + |
| 57 | + |
| 58 | +def test_append_manifest_cname_not_found(): |
| 59 | + """Test appending new manifest if cname isn't found.""" |
| 60 | + # Arrange |
| 61 | + idx = Index() |
| 62 | + idx["manifests"] = [{"annotations": {"cname": "foo"}, "digest": "sha256:foo"}] |
| 63 | + new_manifest = {"annotations": {"cname": "bar"}, "digest": "sha256:bar"} |
| 64 | + |
| 65 | + # Act |
| 66 | + idx.append_manifest(new_manifest) |
| 67 | + |
| 68 | + # Assert |
| 69 | + cnames = [manifest["annotations"]["cname"] for manifest in idx["manifests"]] |
| 70 | + assert "bar" in cnames |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "bad_manifest", |
| 75 | + [ |
| 76 | + "not-a-dict", |
| 77 | + {"annotations": {}}, |
| 78 | + ], |
| 79 | +) |
| 80 | +def test_append_invalid_input_raises(bad_manifest): |
| 81 | + """Test proper error handling for invalid append_manifest input.""" |
| 82 | + # Arrange |
| 83 | + idx = Index() |
| 84 | + |
| 85 | + # Act / Assert |
| 86 | + with pytest.raises(RuntimeError): |
| 87 | + idx.append_manifest(bad_manifest) |
0 commit comments