Skip to content

Commit 7fd5952

Browse files
committed
Properly test oci/index.py
* Create new oci test directory * Move existing oci tests * Create dedicated test file for oci/index.py
1 parent 948ee3f commit 7fd5952

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

tests/oci/__init__.py

Whitespace-only changes.

tests/oci/test_index.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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)
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from click.testing import CliRunner
33
import sys
44
import json
5-
import os
6-
import logging
75

86
# Import reggie library correctly
97
from oras.provider import Registry
@@ -12,7 +10,7 @@
1210

1311
from gardenlinux.oci.__main__ import cli as gl_oci
1412

15-
from .constants import (
13+
from ..constants import (
1614
CONTAINER_NAME_ZOT_EXAMPLE,
1715
GARDENLINUX_ROOT_DIR_EXAMPLE,
1816
REGISTRY,

0 commit comments

Comments
 (0)