Skip to content

Commit df8647d

Browse files
tests: move sbom tests to unit tests
1 parent 480357a commit df8647d

File tree

4 files changed

+84
-78
lines changed

4 files changed

+84
-78
lines changed

scripts/sbom/sbom.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ _sbom_edit_component() {
117117
fi
118118

119119
yq -e -o json ".components[] | select(.name == \"${component_name}\" and .version == \"${component_version}\") | .${key}" "${sbom_file}" >"${tmp_change}"
120-
"${EDITOR}" "${tmp_change}"
120+
"${EDITOR:-}" "${tmp_change}"
121121

122122
query="with(.components[] | select(.name == \"${component_name}\" and .version == \"${component_version}\"); .${key} = $(jq -c '.' "${tmp_change}"))"
123123

tests/common/bats/gpg.bash

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ gpg.setup() {
4949
export CK8S_PGP_FP
5050
}
5151

52+
gpg.setup_one() {
53+
GNUPGHOME="$(mktemp --directory)"
54+
export GNUPGHOME
55+
56+
gpg.auto_generate_key_retry "Key one"
57+
58+
CK8S_PGP_FP="$(gpg --list-secret-keys --with-colons | grep -A1 '^sec' | grep '^fpr' | awk -F: '{print $10}' | paste -sd "," -)"
59+
export CK8S_PGP_FP
60+
}
61+
5262
# Deletes the temporary gpg home
5363
gpg.teardown() {
5464
rm -rf "${GNUPGHOME}"

tests/end-to-end/general/bin-sbom.bats

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env bats
2+
3+
# bats file_tags=scripts-sbom
4+
5+
sbom_backup=$(mktemp --suffix=-sbom.json)
6+
7+
setup() {
8+
load "../../bats.lib.bash"
9+
load_assert
10+
load_file
11+
12+
cp "${ROOT}/docs/sbom.json" "${sbom_backup}"
13+
14+
PATH=:${ROOT}/scripts/sbom:${PATH}
15+
16+
export CK8S_AUTO_APPROVE=true
17+
VELERO_CHART="${ROOT}/helmfile.d/upstream/vmware-tanzu/velero/Chart.yaml"
18+
velero_version=$(yq '.version' "${VELERO_CHART}")
19+
}
20+
21+
teardown() {
22+
mv "${sbom_backup}" "${ROOT}/docs/sbom.json"
23+
}
24+
25+
@test "sbom script should show usage if no command is given" {
26+
run sbom.bash
27+
assert_failure
28+
assert_output --partial "COMMANDS:"
29+
}
30+
31+
@test "sbom script get existing component" {
32+
run sbom.bash get velero
33+
assert_success
34+
assert_output --partial '"name": "velero"'
35+
}
36+
37+
@test "sbom script get non-existing component" {
38+
run sbom.bash get non-existing
39+
assert_failure
40+
}
41+
42+
@test "sbom script add component with unsupported key" {
43+
run sbom.bash add velero "${velero_version}" unsupported-key '{"name": "test", "value": "test"}'
44+
assert_failure
45+
assert_output --partial 'unsupported key'
46+
}
47+
48+
@test "sbom script add component properties with correct object format" {
49+
run sbom.bash add velero "${velero_version}" properties '{"name": "test", "value": "test"}'
50+
assert_success
51+
assert_output --partial 'Updated properties'
52+
}
53+
54+
@test "sbom script add component properties with incorrect object format should fail cyclonedx validation" {
55+
export CK8S_AUTO_APPROVE=false
56+
export CK8S_SKIP_VALIDATION=false
57+
run sbom.bash add velero "${velero_version}" properties '{"unsupported-key-name": "test", "unsupported-value": "test"}' <<< n
58+
assert_output --partial 'Validation failed:'
59+
assert_output --regexp 'Required properties .* are not present'
60+
}
61+
62+
# TODO: change this if generate command is changed to not require GITHUB_TOKEN
63+
@test "sbom script generate requires GITHUB_TOKEN" {
64+
GITHUB_TOKEN="" run sbom.bash generate
65+
assert_failure
66+
assert_output --partial "Missing GITHUB_TOKEN"
67+
}
68+
69+
@test "sbom script validate should be successful" {
70+
run sbom.bash validate
71+
assert_success
72+
assert_output --partial "BOM validated successfully."
73+
}

0 commit comments

Comments
 (0)