Skip to content

Commit be2cebd

Browse files
authored
Merge pull request #312 from komish/check-tool-version-in-report
Ensure the chart's verifier-version matches the verifier version subcommand output
2 parents 5c0b3f1 + 42979d8 commit be2cebd

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea/
22
/out/
33
**/reports/
4+
__pycache__

tests/tests/functional/chart_test.py

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@ def public_key_location(location,public_key):
5353
def image_type(image_type):
5454
return image_type
5555

56+
@given('The chart verifier version value',target_fixture='verifier_version')
57+
def verifier_version(image_type):
58+
"""Get the version of the chart verifier tool used to produce and verify reports.
59+
60+
This output comes directly from the output of `chart-verifier version`, which
61+
is the normalized to match what we would expect to find in a report.
62+
63+
Parameters:
64+
image_type (string): How chart verifier will run. Options: tarball, podman, docker
65+
66+
Returns:
67+
string: a normalized semantic version, like 0.0.0
68+
"""
69+
if image_type == "tarball":
70+
tarball_name = os.environ.get("VERIFIER_TARBALL_NAME")
71+
print(f"\nRun version using tarbal {tarball_name}")
72+
return run_version_tarball_image(tarball_name)
73+
elif image_type == "podman":
74+
image_tag = os.environ.get("PODMAN_IMAGE_TAG")
75+
if not image_tag:
76+
image_tag = "main"
77+
image_name = "quay.io/redhat-certification/chart-verifier"
78+
print(f"\nRun version using podman image {image_name}:{image_tag}")
79+
return run_version_podman_image(image_name,image_tag)
80+
else: # Fallback to Docker.
81+
image_tag = os.environ.get("VERIFIER_IMAGE_TAG")
82+
if not image_tag:
83+
image_tag = "main"
84+
image_name = "quay.io/redhat-certification/chart-verifier"
85+
print(f"\nRun version using docker image {image_name}:{image_tag}")
86+
return run_version_docker_image(image_name, image_tag)
87+
5688
@when(parsers.parse("I run the chart-verifier verify command against the chart to generate a report"),target_fixture="run_verify")
5789
def run_verify(image_type, profile_type, chart_location):
5890
print(f"\nrun {image_type} verifier verify with profile : {profile_type}, and chart: {chart_location}")
@@ -191,6 +223,48 @@ def run_report_docker_image(verifier_image_name,verifier_image_tag,profile_type,
191223

192224
return output.decode("utf-8")
193225

226+
def run_version_tarball_image(tarball_name):
227+
tar = tarfile.open(tarball_name, "r:gz")
228+
tar.extractall(path="./test_verifier")
229+
out = subprocess.run(["./test_verifier/chart-verifier","version"],capture_output=True)
230+
return normalize_version(out.stdout.decode("utf-8"))
231+
232+
def normalize_version(version):
233+
"""Trim trailing newlines and leading v from semantic versions.
234+
235+
Parameters:
236+
version (string): a semver string like v0.0.0\n
237+
238+
Returns:
239+
string: a normalized semver like 0.0.0.
240+
"""
241+
print(f'version input to normalize_version function is: {version}')
242+
return version.rstrip().lstrip('v')
243+
244+
def run_version_docker_image(verifier_image_name,verifier_image_tag):
245+
"""Run chart verifier's version command using the Docker image."""
246+
verifier_image = f"{verifier_image_name}:{verifier_image_tag}"
247+
os.environ["VERIFIER_IMAGE"] = verifier_image
248+
try:
249+
client = docker.from_env()
250+
output = client.containers.run(verifier_image,"version",stdin_open=True,tty=True,stdout=True,remove=True)
251+
except docker.errors.ContainerError as exc:
252+
return f"FAIL: docker.errors.ContainerError: {exc.args}"
253+
except docker.errors.ImageNotFound as exc:
254+
return f"FAIL: docker.errors.ImageNotFound: {exc.args}"
255+
except docker.errors.APIError as exc:
256+
return f"FAIL: docker.errors.APIError: {exc.args}"
257+
258+
if not output:
259+
return f"FAIL: did not receive output from the chart verifier version subcommand."
260+
261+
return normalize_version(output.decode("utf-8"))
262+
263+
def run_version_podman_image(verifier_image_name,verifier_image_tag):
264+
"""Run chart verifier's version command in Podman."""
265+
out = subprocess.run(["podman", "run", "--rm", f"{verifier_image_name}:{verifier_image_tag}", "version"], capture_output=True)
266+
return normalize_version(out.stdout.decode("utf-8"))
267+
194268
def run_verify_tarball_image(tarball_name,profile_type, chart_location,pgp_key_location=None):
195269
print(f"Run tarball image from {tarball_name}")
196270

@@ -255,15 +329,15 @@ def run_report_podman_image(verifier_image_name,verifier_image_tag,profile_type,
255329
return out.stdout.decode("utf-8")
256330

257331
@then("I should see the report-info from the report for the signed chart matching the expected report-info")
258-
def signed_chart_report(run_signed_verify, profile_type, report_info_location, image_type):
259-
check_report(run_signed_verify, profile_type, report_info_location, image_type)
332+
def signed_chart_report(run_signed_verify, profile_type, report_info_location, image_type, verifier_version):
333+
check_report(run_signed_verify, profile_type, report_info_location, image_type, verifier_version)
260334

261335

262336
@then("I should see the report-info from the generated report matching the expected report-info")
263-
def chart_report(run_verify, profile_type, report_info_location, image_type):
264-
check_report(run_verify, profile_type, report_info_location, image_type)
337+
def chart_report(run_verify, profile_type, report_info_location, image_type, verifier_version):
338+
check_report(run_verify, profile_type, report_info_location, image_type, verifier_version)
265339

266-
def check_report(verify_result, profile_type, report_info_location, image_type):
340+
def check_report(verify_result, profile_type, report_info_location, image_type, verifier_version):
267341

268342
if verify_result.startswith("FAIL"):
269343
pytest.fail(f'FAIL some tests failed: {verify_result}')
@@ -272,8 +346,14 @@ def check_report(verify_result, profile_type, report_info_location, image_type):
272346

273347
report_data = yaml.load(verify_result, Loader=Loader)
274348

349+
275350
test_passed = True
276351

352+
report_verifier_version = report_data['metadata']['tool']['verifier-version']
353+
if report_verifier_version != verifier_version:
354+
print(f"FAIL: verifier-version found in report does not match tool version. Expected {verifier_version}, but report has {report_verifier_version}")
355+
test_passed = False
356+
277357
report_vendor_type = report_data["metadata"]["tool"]["profile"]["VendorType"]
278358
if report_vendor_type != profile_type:
279359
print(f"FAIL: profiles do not match. Expected {profile_type}, but report has {report_vendor_type}")

tests/tests/functional/features/chart_good.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Feature: Chart verification
1313
Given I will provide a <location> of a <helm_chart>
1414
Given I will provide a <location> of an expected <report_info>
1515
Given I will use the chart verifier <image_type> image
16+
Given The chart verifier version value
1617
When I run the chart-verifier verify command against the chart to generate a report
1718
Then I should see the report-info from the generated report matching the expected report-info
1819

@@ -31,6 +32,7 @@ Feature: Chart verification
3132
Given I will provide a <location> of an expected <report_info>
3233
Given I will use the chart verifier <image_type> image
3334
Given I will provide a <location> of a <public_key> to verify the signature
35+
Given The chart verifier version value
3436
When I run the chart-verifier verify command against the signed chart to generate a report
3537
Then I should see the report-info from the report for the signed chart matching the expected report-info
3638

0 commit comments

Comments
 (0)