|
| 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