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