|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -u -o pipefail |
| 3 | + |
| 4 | +cvetool="./cvetool" |
| 5 | +failures=0 |
| 6 | + |
| 7 | +fail() { |
| 8 | + echo "" |
| 9 | + echo "==========================================" |
| 10 | + echo "FAIL: $1" |
| 11 | + echo "==========================================" |
| 12 | + if [ -n "${2:-}" ]; then |
| 13 | + echo "--- output ---" |
| 14 | + echo "$2" |
| 15 | + echo "--- end output ---" |
| 16 | + fi |
| 17 | + echo "" |
| 18 | + failures=$((failures + 1)) |
| 19 | +} |
| 20 | + |
| 21 | +# Test: `cvetool --version` exits successfully |
| 22 | +echo "Test: cvetool --version exits successfully..." |
| 23 | +version_output=$("$cvetool" --version 2>&1) || { |
| 24 | + fail "cvetool --version exited non-zero" "${version_output:-}" |
| 25 | +} |
| 26 | +if [ -z "${version_output:-}" ]; then |
| 27 | + fail "cvetool --version produced no output" |
| 28 | +else |
| 29 | + echo "PASS: cvetool --version output: $version_output" |
| 30 | +fi |
| 31 | + |
| 32 | +# Test: `cvetool update` should not produce any warnings |
| 33 | +echo "Test: cvetool update produces no warnings..." |
| 34 | +tmpdb=$(mktemp) |
| 35 | +unwritable_dir="" |
| 36 | +cleanup() { |
| 37 | + [ -n "$unwritable_dir" ] && chmod 0755 "$unwritable_dir" 2>/dev/null && rm -rf "$unwritable_dir" |
| 38 | + rm -f "$tmpdb" |
| 39 | +} |
| 40 | +trap cleanup EXIT |
| 41 | +update_ok=true |
| 42 | +update_output=$("$cvetool" -l debug update --db-path "$tmpdb" 2>&1) || { |
| 43 | + fail "cvetool update exited non-zero" "${update_output:-}" |
| 44 | + update_ok=false |
| 45 | +} |
| 46 | +if echo "${update_output:-}" | grep -qi "WARN"; then |
| 47 | + fail "cvetool update produced warnings" "$update_output" |
| 48 | +else |
| 49 | + echo "PASS: no warnings in cvetool update output" |
| 50 | +fi |
| 51 | + |
| 52 | +if [ "$update_ok" = true ]; then |
| 53 | + # Test: `cvetool scan` exits successfully (warnings are acceptable) |
| 54 | + echo "Test: cvetool scan exits successfully..." |
| 55 | + scan_output=$("$cvetool" -l debug scan --db-path "$tmpdb" 2>&1) || { |
| 56 | + fail "cvetool scan exited non-zero" "${scan_output:-}" |
| 57 | + } |
| 58 | + if echo "${scan_output:-}" | grep -qi "ERR"; then |
| 59 | + fail "cvetool scan produced errors" "$scan_output" |
| 60 | + else |
| 61 | + echo "PASS: cvetool scan exited successfully" |
| 62 | + fi |
| 63 | + |
| 64 | + # Test: `cvetool scan --format sarif` produces valid JSON |
| 65 | + echo "Test: cvetool scan --format sarif produces valid JSON..." |
| 66 | + json_output=$("$cvetool" -l debug scan --db-path "$tmpdb" --format sarif 2>/dev/null) || { |
| 67 | + fail "cvetool scan --format sarif exited non-zero" "${json_output:-}" |
| 68 | + } |
| 69 | + if ! echo "${json_output:-}" | jq . >/dev/null 2>&1; then |
| 70 | + fail "cvetool scan --format sarif produced invalid JSON" "$json_output" |
| 71 | + else |
| 72 | + echo "PASS: cvetool scan --format sarif output is valid JSON" |
| 73 | + fi |
| 74 | + |
| 75 | + # Test: `cvetool scan --format quay` produces valid JSON |
| 76 | + echo "Test: cvetool scan --format quay produces valid JSON..." |
| 77 | + json_output=$("$cvetool" -l debug scan --db-path "$tmpdb" --format quay 2>/dev/null) || { |
| 78 | + fail "cvetool scan --format quay exited non-zero" "${json_output:-}" |
| 79 | + } |
| 80 | + if ! echo "${json_output:-}" | jq . >/dev/null 2>&1; then |
| 81 | + fail "cvetool scan --format quay produced invalid JSON" "$json_output" |
| 82 | + else |
| 83 | + echo "PASS: cvetool scan --format quay output is valid JSON" |
| 84 | + fi |
| 85 | +else |
| 86 | + echo "SKIP: scan tests skipped because cvetool update failed" |
| 87 | +fi |
| 88 | + |
| 89 | +# Test: `cvetool scan` with a bad db path should fail |
| 90 | +echo "Test: cvetool scan with bad db path fails..." |
| 91 | +if bad_db_output=$("$cvetool" scan --db-path /nonexistent/bad.db 2>&1); then |
| 92 | + fail "cvetool scan with bad db path exited 0 (expected failure)" "$bad_db_output" |
| 93 | +else |
| 94 | + echo "PASS: cvetool scan with bad db path exits non-zero" |
| 95 | +fi |
| 96 | + |
| 97 | +# Test: `cvetool update` with an unwritable db path should fail |
| 98 | +echo "Test: cvetool update with unwritable db path fails..." |
| 99 | +unwritable_dir=$(mktemp -d) |
| 100 | +chmod 0000 "$unwritable_dir" |
| 101 | +if [ "$(id -u)" -eq 0 ]; then |
| 102 | + if unwritable_output=$(runuser -u nobody -- "$cvetool" update --db-path "$unwritable_dir/db" 2>&1); then |
| 103 | + fail "cvetool update with unwritable db path exited 0 (expected failure)" "${unwritable_output:-}" |
| 104 | + else |
| 105 | + echo "PASS: cvetool update with unwritable db path exits non-zero" |
| 106 | + fi |
| 107 | +else |
| 108 | + if unwritable_output=$("$cvetool" update --db-path "$unwritable_dir/db" 2>&1); then |
| 109 | + fail "cvetool update with unwritable db path exited 0 (expected failure)" "${unwritable_output:-}" |
| 110 | + else |
| 111 | + echo "PASS: cvetool update with unwritable db path exits non-zero" |
| 112 | + fi |
| 113 | +fi |
| 114 | + |
| 115 | +# Summary |
| 116 | +echo "" |
| 117 | +echo "==========================================" |
| 118 | +if [ $failures -gt 0 ]; then |
| 119 | + echo "DONE: $failures test(s) FAILED" |
| 120 | + echo "==========================================" |
| 121 | + exit 1 |
| 122 | +else |
| 123 | + echo "DONE: all tests passed" |
| 124 | + echo "==========================================" |
| 125 | +fi |
0 commit comments