Skip to content

Commit ae40522

Browse files
committed
Add tests for oci/layer.py
1 parent 8ff75ba commit ae40522

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

tests/oci/test_layer.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import builtins
2+
import pytest
3+
from pathlib import Path
4+
5+
import gardenlinux.oci.layer as gl_layer
6+
7+
8+
class DummyLayer:
9+
"""Minimal stub for oras.oci.Layer"""
10+
11+
def __init__(self, blob_path, media_type=None, is_dir=False):
12+
self._init_args = (blob_path, media_type, is_dir)
13+
14+
def to_dict(self):
15+
return {"dummy": True}
16+
17+
18+
@pytest.fixture(autouse=True)
19+
def patch__Layer(monkeypatch):
20+
"""Replace oras.oci.Layer with DummyLayer in Layer's module."""
21+
monkeypatch.setattr(gl_layer, "_Layer", DummyLayer)
22+
yield
23+
24+
25+
def test_dict_property_returns_with_annotations(tmp_path):
26+
"""dict property should merge _Layer.to_dict() with annotations."""
27+
# Arrange
28+
blob = tmp_path / "blob.txt"
29+
blob.write_text("data")
30+
31+
# Act
32+
l = gl_layer.Layer(blob)
33+
result = l.dict
34+
35+
# Assert
36+
assert result["dummy"] is True
37+
assert "annotations" in result
38+
assert result["annotations"]["org.opencontainers.image.title"] == "blob.txt"
39+
40+
41+
def test_getitem_and_delitem_annotations(tmp_path):
42+
"""getitem should return annotations, delitem should clear them."""
43+
# Arrange
44+
blob = tmp_path / "blob.txt"
45+
blob.write_text("data")
46+
l = gl_layer.Layer(blob)
47+
48+
# Act / Assert (__getitem__)
49+
ann = l["annotations"]
50+
assert isinstance(ann, dict)
51+
assert "org.opencontainers.image.title" in ann
52+
53+
# Act / Assert (__delitem__)
54+
l.__delitem__("annotations")
55+
assert l._annotations == {}
56+
57+
58+
def test_getitem_invalid_key_raises(tmp_path):
59+
"""getitem with unsupported key should raise KeyError."""
60+
# Arrange
61+
blob = tmp_path / "blob.txt"
62+
blob.write_text("data")
63+
l = gl_layer.Layer(blob)
64+
65+
# Act / Assert
66+
with pytest.raises(KeyError):
67+
_ = l["invalid"]
68+
69+
70+
def test_setitem_annotations(tmp_path):
71+
"""setitem with supported keys should set annotations"""
72+
# Arrange
73+
blob = tmp_path / "blob.txt"
74+
blob.write_text("data")
75+
l = gl_layer.Layer(blob)
76+
77+
# Act
78+
new_ann = {"x": "y"}
79+
l.__setitem__("annotations", new_ann)
80+
81+
# Assert
82+
assert l._annotations == new_ann
83+
84+
85+
def test_setitem_annotations_invalid_raises(tmp_path):
86+
# Arrange
87+
blob = tmp_path / "blob.txt"
88+
blob.write_text("data")
89+
l = gl_layer.Layer(blob)
90+
91+
# Act / Assert
92+
with pytest.raises(KeyError):
93+
_ = l["invalid"]
94+
95+
96+
def test_len_iter(tmp_path):
97+
# Arrange
98+
blob = tmp_path / "blob.txt"
99+
blob.write_text("data")
100+
l = gl_layer.Layer(blob)
101+
102+
# Act
103+
keys = list(iter(l))
104+
105+
# Assert
106+
assert keys == ["annotations"]
107+
assert len(keys) == 1
108+
109+
110+
def test_gen_metadata_from_file(tmp_path):
111+
# Arrange
112+
blob = tmp_path / "blob.tar"
113+
blob.write_text("data")
114+
l = gl_layer.Layer(blob)
115+
116+
# Act
117+
arch = "amd64"
118+
metadata = gl_layer.Layer.generate_metadata_from_file_name(blob, arch)
119+
120+
# Assert
121+
assert metadata["file_name"] == "blob.tar"
122+
assert "media_type" in metadata
123+
assert metadata["annotations"]["io.gardenlinux.image.layer.architecture"] == arch
124+
125+
126+
def test_lookup_media_type_for_file_name(tmp_path):
127+
# Arrange
128+
blob = tmp_path / "blob.tar"
129+
blob.write_text("data")
130+
131+
# Act
132+
media_type = gl_layer.Layer.lookup_media_type_for_file_name(blob)
133+
from gardenlinux.constants import GL_MEDIA_TYPE_LOOKUP
134+
135+
assert media_type == GL_MEDIA_TYPE_LOOKUP["tar"]
136+
137+
138+
def test_lookup_media_type_for_file_name_invalid_raises(tmp_path):
139+
# Arrange / Act / Assert
140+
with pytest.raises(ValueError):
141+
gl_layer.Layer.lookup_media_type_for_file_name(tmp_path / "unknown.xyz")

0 commit comments

Comments
 (0)