|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Fail on first error |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# Verify tools installed |
| 7 | +if ! [ -x "$(command -v jq)" ]; then |
| 8 | + echo >&2 'Error: jq is not installed.' |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | +if ! [ -x "$(command -v yq)" ]; then |
| 12 | + echo >&2 'Error: jq is not installed.' |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# |
| 17 | +# Helpers |
| 18 | +# |
| 19 | + |
| 20 | +function test_cmd() { |
| 21 | + NAME=$1 |
| 22 | + EXPECTED=$2 |
| 23 | + shift; shift |
| 24 | + |
| 25 | + echo -n "### Testing '$NAME'… " |
| 26 | + OUTPUT=`$@` |
| 27 | + |
| 28 | + if [ "$OUTPUT" = "$EXPECTED" ]; then |
| 29 | + echo "Success!" |
| 30 | + else |
| 31 | + echo "Failed!" |
| 32 | + cat << EOF |
| 33 | +- Output: |
| 34 | +$OUTPUT |
| 35 | +- Expected: |
| 36 | +$EXPECTED |
| 37 | +EOF |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | +} |
| 41 | + |
| 42 | +function test_cmd_with_diff() { |
| 43 | + NAME=$1 |
| 44 | + EXPECTED=$2 |
| 45 | + shift; shift |
| 46 | + |
| 47 | + echo -n "### Testing '$NAME'… " |
| 48 | + tmpfile=$(mktemp /tmp/devops-ga-changed-paths-filter.XXXXXX) |
| 49 | + $@ $tmpfile |
| 50 | + |
| 51 | + if diff_output=$(diff $tmpfile $EXPECTED); then |
| 52 | + echo "Success!" |
| 53 | + rm $tmpfile |
| 54 | + else |
| 55 | + echo "Error!" |
| 56 | + echo "$diff_output" |
| 57 | + rm $tmpfile |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +# |
| 62 | +# Tests |
| 63 | +# |
| 64 | + |
| 65 | +# Simplify filter file |
| 66 | +test_cmd_with_diff \ |
| 67 | + 'Simplify filter file' \ |
| 68 | + ./tests/simple-filters.yaml \ |
| 69 | + ./scripts/simplify-filter-file.sh tests/full-filters.yaml |
| 70 | + |
| 71 | +# Generate all |
| 72 | +test_cmd \ |
| 73 | + 'Generate all' \ |
| 74 | + '["k8s/network","k8s/environments/development","k8s/environments/qa","k8s/environments/staging","k8s/environments/sandbox","k8s/environments/production","k8s/shared/signoz-otel-collector"]' \ |
| 75 | + ./scripts/generate-all-keys.sh tests/simple-filters.yaml |
| 76 | + |
| 77 | +# Process manual keys |
| 78 | +test_cmd \ |
| 79 | + 'Process manual keys' \ |
| 80 | + '["hello/world","foo","bar/baz"]' \ |
| 81 | + ./scripts/process-manual-keys.sh "hello/world,foo,bar/baz" |
| 82 | + |
| 83 | +# Verify matrix generation (all) |
| 84 | +CHANGED_PATHS='["k8s/network","k8s/environments/development","k8s/environments/qa","k8s/environments/staging","k8s/environments/sandbox","k8s/environments/production","k8s/shared/signoz-otel-collector"]' |
| 85 | +test_cmd \ |
| 86 | + 'Matrix generation (all)' \ |
| 87 | + '[{"environment":"Default","path":"k8s/network"},{"environment":"Sandbox","path":"k8s/network"},{"environment":"Production","path":"k8s/network"},{"environment":"Development","path":"k8s/environments/development"},{"environment":"QA","path":"k8s/environments/qa"},{"environment":"Staging","path":"k8s/environments/staging"},{"environment":"Sandbox","path":"k8s/environments/sandbox"},{"environment":"Production","path":"k8s/environments/production"},{"environment":"Default","path":"k8s/shared/signoz-otel-collector"},{"environment":"Sandbox","path":"k8s/shared/signoz-otel-collector"},{"environment":"Production","path":"k8s/shared/signoz-otel-collector"}]' \ |
| 88 | + ./scripts/generate-matrix.sh <(echo $CHANGED_PATHS) tests/full-filters.yaml environment Default |
| 89 | + |
| 90 | +# Verify matrix generation (partial) |
| 91 | +CHANGED_PATHS='["k8s/network","k8s/environments/development"]' |
| 92 | +test_cmd \ |
| 93 | + 'Matrix generation (partial)' \ |
| 94 | + '[{"environment":"Default","path":"k8s/network"},{"environment":"Sandbox","path":"k8s/network"},{"environment":"Production","path":"k8s/network"},{"environment":"Development","path":"k8s/environments/development"}]' \ |
| 95 | + ./scripts/generate-matrix.sh <(echo $CHANGED_PATHS) tests/full-filters.yaml environment Default |
0 commit comments