Skip to content

Commit 71069d4

Browse files
author
Lukas Fischer
committed
#1838 Add unit tests for parser-cyclonedx
Add a small and very simple test for the CycloneDX parser, after all it doesn't do much. More annoying is again the Makefile structure, since the project is not set up for multiple parsers for a scanner, it also does not accommodate that for the tests. To circumvent this, the added target for the parser-cyclonedx tests needs to manually run the install-deps-js target with a different module name, so that all the dependencies get installed correctly. Signed-off-by: Lukas Fischer <lukas.fischer@iteratec.com>
1 parent 033a502 commit 71069d4

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

scanners/trivy/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ scanner = trivy
1818
docker-build: | docker-build-parser-sbom
1919
docker-export: | docker-export-parser-sbom
2020
kind-import: | kind-import-parser-sbom
21+
unit-tests: unit-tests-parser-sbom
2122

2223
include ../../scanners.mk
2324

@@ -57,3 +58,12 @@ deploy-without-scanner:
5758
--set="cyclonedxParser.image.tag=$(IMG_TAG)" \
5859
--set="parser.env[0].name=CRASH_ON_FAILED_VALIDATION" \
5960
--set-string="parser.env[0].value=true"
61+
62+
# The unit tests for the cyclonedx-parser cannot reuse the unit-test-js target, because it requires install-deps-js,
63+
# which then tries to run npm ci in ../../${module}-sdk/nodejs and module is set to "parser-cyclonedx", but
64+
# install-deps-js still needs to be executed, in case this target runs before the normal parser tests
65+
.PHONY: unit-tests-parser-sbom
66+
unit-tests-parser-sbom:
67+
@$(MAKE) -s install-deps-js module=$(parser-prefix)
68+
@echo ".: 🧪 Starting unit-tests for '$(name)' parser-cyclonedx."
69+
npm run test:unit -- ${name}/parser-cyclonedx/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-FileCopyrightText: the secureCodeBox authors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
const { parse } = require("./parser");
6+
const {
7+
validateParser,
8+
} = require("@securecodebox/parser-sdk-nodejs/parser-utils");
9+
10+
let scan;
11+
12+
beforeEach(() => {
13+
scan = {
14+
metadata: {
15+
name: "my-cyclonedx-sbom-scan",
16+
namespace: "default",
17+
},
18+
spec: {
19+
scanType: "trivy-image-sbom",
20+
parameters: ["hello-world:latest"],
21+
},
22+
status: {
23+
rawResultDownloadLink: "https://s3.example.com/sbom-cyclonedx.json",
24+
},
25+
};
26+
});
27+
28+
test("should create finding correctly", async () => {
29+
const result = {
30+
bomFormat: "CycloneDX",
31+
metadata: {
32+
component: {
33+
name: "hello-world:latest"
34+
}
35+
}
36+
};
37+
38+
const findings = await parse(JSON.stringify(result), scan);
39+
await expect(validateParser(findings)).resolves.toBeUndefined();
40+
expect(findings).toMatchInlineSnapshot(`
41+
[
42+
{
43+
"attributes": {
44+
"downloadLink": "https://s3.example.com/sbom-cyclonedx.json",
45+
},
46+
"category": "SBOM",
47+
"description": "Generated an SBOM for: 'hello-world:latest'",
48+
"name": "SBOM for hello-world:latest",
49+
"osi_layer": "APPLICATION",
50+
"severity": "INFORMATIONAL",
51+
},
52+
]
53+
`);
54+
});

0 commit comments

Comments
 (0)