Skip to content

Commit e721ecb

Browse files
add SPDX generation test
This is to ensure that both the new and old versions of the SPDX writers satisfy the same tests. This uses an Image instance that was generated during the call of "tern report -i golang:1.12-alpine" Signed-off-by: Armin Tänzer <[email protected]>
1 parent 9f80484 commit e721ecb

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

tern/formats/spdx_new/layer_helpers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def get_layer_dict(layer_obj: ImageLayer) -> Tuple[SpdxPackage, List[Relationshi
109109
The analyzed files will go in a separate part of the document."""
110110

111111
comment = get_layer_package_comment(layer_obj)
112-
verification_code = get_layer_verification_code(layer_obj)
112+
verification_code = get_layer_verification_code(layer_obj) if layer_obj.files_analyzed else None
113113

114114
layer_licenses = get_layer_licenses(layer_obj)
115115
license_info_from_files = []
@@ -128,7 +128,7 @@ def get_layer_dict(layer_obj: ImageLayer) -> Tuple[SpdxPackage, List[Relationshi
128128
file_name=layer_obj.tar_file,
129129
download_location=SpdxNone(),
130130
files_analyzed=bool(layer_obj.files_analyzed),
131-
verification_code=verification_code if bool(layer_obj.files_analyzed) else None,
131+
verification_code=verification_code,
132132
checksums=[get_layer_checksum(layer_obj)],
133133
license_concluded=SpdxNoAssertion(),
134134
license_declared=SpdxNoAssertion(),

tern/formats/spdx_new/make_spdx_model.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from tern.formats.spdx_new.constants import DOCUMENT_ID, DOCUMENT_NAME, SPDX_VERSION, DATA_LICENSE, DOCUMENT_COMMENT, \
1818
LICENSE_LIST_VERSION, CREATOR_NAME, DOCUMENT_NAME_SNAPSHOT, DOCUMENT_NAMESPACE_SNAPSHOT
1919
from tern.formats.spdx_new.file_helpers import get_layer_files_list
20-
from tern.formats.spdx_new.general_helpers import get_current_timestamp, get_uuid, get_image_spdxref
20+
from tern.formats.spdx_new.general_helpers import get_current_timestamp, get_uuid
2121
from tern.classes.image import Image
2222
from tern.formats.spdx.spdx import SPDX
2323
from tern.formats.spdx_new.file_helpers import get_files_list
@@ -51,7 +51,6 @@ def make_spdx_model(image_obj_list: List[Image]) -> Document:
5151
data_license=DATA_LICENSE,
5252
document_comment=DOCUMENT_COMMENT,
5353
)
54-
describes_relationship = Relationship(DOCUMENT_ID, RelationshipType.DESCRIBES, get_image_spdxref(image_obj))
5554
packages = [get_image_dict(image_obj, template)]
5655
image_layer_relationships = get_image_layer_relationships(image_obj)
5756

@@ -69,7 +68,7 @@ def make_spdx_model(image_obj_list: List[Image]) -> Document:
6968
creation_info=creation_info,
7069
packages=packages,
7170
files=files,
72-
relationships=[describes_relationship] + image_layer_relationships + layer_file_relationships,
71+
relationships=image_layer_relationships + layer_file_relationships,
7372
extracted_licensing_info=extracted_licensing_info
7473
)
7574

tests/golang_test_image.pkl

2.19 MB
Binary file not shown.

tests/test_spdx_generation.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import json
2+
import os
3+
import pickle
4+
import unittest
5+
6+
from tern.formats.spdx.spdxjson.generator import SpdxJSON
7+
8+
9+
class TestSPDXGeneration(unittest.TestCase):
10+
test_package = {
11+
"name": "alpine-keys",
12+
"SPDXID": "SPDXRef-alpine-keys-2.1-r2",
13+
"versionInfo": "2.1-r2",
14+
"supplier": "Organization: Alpine Linux",
15+
"downloadLocation": "NOASSERTION",
16+
"filesAnalyzed": False,
17+
"licenseConcluded": "NOASSERTION",
18+
"licenseDeclared": "MIT",
19+
"copyrightText": "NONE",
20+
"externalRefs": [
21+
{
22+
"referenceCategory": "PACKAGE-MANAGER",
23+
"referenceLocator": "pkg:apk/alpine/[email protected]?arch=x86_64",
24+
"referenceType": "purl"
25+
}
26+
],
27+
"comment": "alpine-keys:\n\twarning: No metadata for key: copyright\n\twarning: No metadata for key: download_url\n\twarning: No metadata for key: checksum\n\twarning: No metadata for key: pkg_licenses\n\twarning: No metadata for key: pkg_format\n\twarning: No metadata for key: src_name\n\twarning: No metadata for key: src_version\n"
28+
}
29+
30+
test_describes_relationship = {
31+
"spdxElementId": "SPDXRef-DOCUMENT",
32+
"relatedSpdxElement": "SPDXRef-golang-1.12-alpine",
33+
"relationshipType": "DESCRIBES"
34+
}
35+
36+
test_contains_relationship = {
37+
"spdxElementId": "SPDXRef-5216338b40",
38+
"relatedSpdxElement": "SPDXRef-alpine-keys-2.1-r2",
39+
"relationshipType": "CONTAINS"
40+
}
41+
42+
test_has_prerequisite_relationship = {
43+
"spdxElementId": "SPDXRef-3957f7032f",
44+
"relatedSpdxElement": "SPDXRef-7306dca01e",
45+
"relationshipType": "HAS_PREREQUISITE"
46+
}
47+
48+
test_extracted_licensing_info = {
49+
"extractedText": "MPL-2.0 GPL-2.0-or-later",
50+
"licenseId": "LicenseRef-f30c02b"
51+
}
52+
53+
def test_spdx_generation_from_pickled_image(self):
54+
json_file_path = "spdx_test.json"
55+
test_image_file_path = "golang_test_image.pkl" # generated during "tern report -i golang:1.12-alpine"
56+
with open(test_image_file_path, "rb") as f:
57+
image = pickle.load(f)
58+
image_list = [image]
59+
60+
json_as_string = SpdxJSON().generate(image_list)
61+
with open(json_file_path, "w") as f:
62+
f.write(json_as_string)
63+
64+
with open(json_file_path, "r") as f:
65+
json_dict = json.load(f)
66+
assert json_dict["SPDXID"] == "SPDXRef-DOCUMENT"
67+
assert json_dict["spdxVersion"] == "SPDX-2.2"
68+
assert len(json_dict["packages"]) == 21
69+
assert self.test_package in json_dict["packages"]
70+
assert len(json_dict["relationships"]) == 25
71+
assert self.test_describes_relationship in json_dict["relationships"]
72+
assert self.test_contains_relationship in json_dict["relationships"]
73+
assert self.test_has_prerequisite_relationship in json_dict["relationships"]
74+
assert len(json_dict["hasExtractedLicensingInfos"]) == 4
75+
assert self.test_extracted_licensing_info in json_dict["hasExtractedLicensingInfos"]
76+
77+
os.remove(json_file_path)

0 commit comments

Comments
 (0)