Skip to content

Commit 71bb4c5

Browse files
committed
Add tests
1 parent f2fc2d5 commit 71bb4c5

File tree

5 files changed

+120
-60
lines changed

5 files changed

+120
-60
lines changed

src/confcom/azext_confcom/lib/cose.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ def cose_get_properties(file_path: Path):
6161
cose_print_output = cose_print(file_path)
6262
return {
6363
"iss": re.search(r"^iss:\s*(.*)$", cose_print_output, re.MULTILINE).group(1),
64-
"feed": re.search(r"^feed:\s*(.*)$", cose_print_output, re.MULTILINE).group(1),
64+
"feed": re.search(r"^feed:[ \t]*([^\r\n]*)", cose_print_output, re.MULTILINE).group(1),
6565
"payload": re.search(r"^payload:\s*(.*)", cose_print_output, re.MULTILINE | re.DOTALL).group(1),
6666
}

src/confcom/azext_confcom/lib/fragment_references_from_image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6+
import re
67
import tempfile
78

89
from pathlib import Path
@@ -17,12 +18,13 @@ def fragment_references_from_image(image: str, minimum_svn: Optional[str]):
1718

1819
for signed_fragment in get_fragments_from_image(image):
1920

20-
package_name = signed_fragment.name.split(".")[0]
2121
cose_properties = cose_get_properties(signed_fragment)
2222

2323
with tempfile.NamedTemporaryFile("w+b") as payload:
2424
payload.write(cose_properties["payload"].encode("utf-8"))
2525
payload.flush()
26+
27+
package_name = re.search(r"^package\s*(.*)$", cose_properties["payload"], re.MULTILINE).group(1)
2628
fragment_properties = opa_eval(
2729
Path(payload.name),
2830
f"data.{package_name}",

src/confcom/azext_confcom/tests/conftest.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55

66
import fcntl
77
import importlib
8+
import json
89
import os
910
import subprocess
1011
import tempfile
1112
import psutil
1213
import pytest
1314
import sys
1415
import shutil
16+
import zipfile
1517

1618
from pathlib import Path
17-
import zipfile
19+
20+
21+
CONFCOM_DIR = Path(__file__).parent.parent.parent
22+
SAMPLES_DIR = CONFCOM_DIR / "samples"
1823

1924

2025
# This fixture ensures tests are run against final built wheels of the extension
@@ -89,3 +94,55 @@ def run_on_wheel(request):
8994
importlib.import_module(module.__name__)
9095

9196
yield
97+
98+
99+
@pytest.fixture()
100+
def docker_image():
101+
102+
registry_id = subprocess.run(
103+
["docker", "run", "-d", "-p", "0:5000", "registry:2"],
104+
stdout=subprocess.PIPE,
105+
text=True,
106+
).stdout
107+
108+
registry_port = subprocess.run(
109+
["docker", "port", registry_id],
110+
stdout=subprocess.PIPE,
111+
text=True,
112+
).stdout.split(":")[-1].strip()
113+
114+
test_container_ref = f"localhost:{registry_port}/hello-world:latest"
115+
subprocess.run(["docker", "pull", "hello-world"])
116+
subprocess.run(["docker", "tag", "hello-world", test_container_ref])
117+
subprocess.run(["docker", "push", test_container_ref])
118+
119+
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", delete=True) as temp_file:
120+
json.dump({
121+
"version": "1.0.0",
122+
"containers": [
123+
{
124+
"name": "hello-world",
125+
"properties": {
126+
"image": test_container_ref,
127+
},
128+
}
129+
]
130+
}, temp_file)
131+
temp_file.flush()
132+
133+
yield test_container_ref, temp_file.name
134+
135+
subprocess.run(["docker", "stop", registry_id])
136+
137+
138+
@pytest.fixture(scope="session")
139+
def cert_chain():
140+
with tempfile.TemporaryDirectory() as temp_dir:
141+
subprocess.run(
142+
[
143+
(SAMPLES_DIR / "certs" / "create_certchain.sh").as_posix(),
144+
temp_dir
145+
],
146+
check=True,
147+
)
148+
yield temp_dir

src/confcom/azext_confcom/tests/latest/test_confcom_acifragmentgen.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,14 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
import contextlib
76
import io
87
import json
98
import os
109
import subprocess
1110
import tempfile
12-
import pytest
1311

1412
from azext_confcom.custom import acifragmentgen_confcom, fragment_push, fragment_attach
1513

16-
TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), ".."))
17-
SAMPLES_DIR = os.path.abspath(os.path.join(TEST_DIR, "..", "..", "..", "samples"))
18-
19-
20-
@pytest.fixture()
21-
def docker_image():
22-
23-
registry_id = subprocess.run(
24-
["docker", "run", "-d", "-p", "0:5000", "registry:2"],
25-
stdout=subprocess.PIPE,
26-
text=True,
27-
).stdout
28-
29-
registry_port = subprocess.run(
30-
["docker", "port", registry_id],
31-
stdout=subprocess.PIPE,
32-
text=True,
33-
).stdout.split(":")[-1].strip()
34-
35-
test_container_ref = f"localhost:{registry_port}/hello-world:latest"
36-
subprocess.run(["docker", "pull", "hello-world"])
37-
subprocess.run(["docker", "tag", "hello-world", test_container_ref])
38-
subprocess.run(["docker", "push", test_container_ref])
39-
40-
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf-8", delete=True) as temp_file:
41-
json.dump({
42-
"version": "1.0.0",
43-
"containers": [
44-
{
45-
"name": "hello-world",
46-
"properties": {
47-
"image": test_container_ref,
48-
},
49-
}
50-
]
51-
}, temp_file)
52-
temp_file.flush()
53-
54-
yield test_container_ref, temp_file.name
55-
56-
subprocess.run(["docker", "stop", registry_id])
57-
58-
59-
@pytest.fixture(scope="session")
60-
def cert_chain():
61-
with tempfile.TemporaryDirectory() as temp_dir:
62-
subprocess.run(
63-
[
64-
os.path.join(SAMPLES_DIR, "certs", "create_certchain.sh"),
65-
temp_dir
66-
],
67-
check=True,
68-
)
69-
yield temp_dir
70-
7114

7215
def test_acifragmentgen_fragment_gen(docker_image):
7316

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
import json
7+
import subprocess
8+
9+
from contextlib import redirect_stdout
10+
from io import StringIO
11+
from pathlib import Path
12+
13+
from azext_confcom.command.fragment_references_from_image import fragment_references_from_image
14+
15+
16+
CONFCOM_DIR = Path(__file__).parent.parent.parent.parent
17+
SAMPLES_DIR = CONFCOM_DIR / "samples"
18+
19+
20+
def test_fragment_reference_from_image(docker_image):
21+
22+
image_ref, spec_file_path = docker_image
23+
signed_fragment_path = SAMPLES_DIR / "fragments" / "fragment.rego.cose"
24+
25+
# Attach a signed fragment to the image
26+
subprocess.run(
27+
[
28+
"oras",
29+
"attach",
30+
"--artifact-type",
31+
"application/x-ms-ccepolicy-frag",
32+
image_ref,
33+
signed_fragment_path.name,
34+
],
35+
check=True,
36+
timeout=120,
37+
cwd=signed_fragment_path.parent.as_posix(),
38+
)
39+
40+
# Generate the fragment reference
41+
buffer = StringIO()
42+
with redirect_stdout(buffer):
43+
fragment_references_from_image(
44+
image=image_ref,
45+
minimum_svn=None,
46+
)
47+
48+
fragment_references = json.loads(buffer.getvalue())
49+
50+
# Check the reference looks as expected
51+
assert fragment_references == [
52+
{
53+
'feed': '',
54+
'includes': ['containers'],
55+
'issuer': 'did:x509:0:sha256:q2YUkwrO2Ufcq66-CXKS9CA-XZMqFMbFom99GjaR2eI::subject:CN:Contoso',
56+
'minimum_svn': '1'
57+
}
58+
]

0 commit comments

Comments
 (0)