Skip to content

Commit 0ac8a7c

Browse files
committed
Add some tests to verify outputs
1 parent f21f0dc commit 0ac8a7c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

tests/full-filters.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# Filters used for triggering k8s deployments
3+
4+
k8s/network:
5+
- 'k8s/network/**'
6+
Sandbox|k8s/network:
7+
- 'k8s/network/**'
8+
Production|k8s/network:
9+
- 'k8s/network/**'
10+
11+
# Environments (change in `base` will deploy everywhere)
12+
Development|k8s/environments/development:
13+
- 'k8s/environments/development/**'
14+
- 'k8s/base/**'
15+
QA|k8s/environments/qa:
16+
- 'k8s/environments/qa/**'
17+
- 'k8s/base/**'
18+
Staging|k8s/environments/staging:
19+
- 'k8s/environments/staging/**'
20+
- 'k8s/base/**'
21+
Sandbox|k8s/environments/sandbox:
22+
- 'k8s/environments/sandbox/**'
23+
- 'k8s/base/**'
24+
Production|k8s/environments/production:
25+
- 'k8s/environments/production/**'
26+
- 'k8s/base/**'
27+
28+
# Shared services
29+
k8s/shared/signoz-otel-collector:
30+
- 'k8s/shared/signoz-otel-collector/**'
31+
Sandbox|k8s/shared/signoz-otel-collector:
32+
- 'k8s/shared/signoz-otel-collector/**'
33+
Production|k8s/shared/signoz-otel-collector:
34+
- 'k8s/shared/signoz-otel-collector/**'

tests/run-tests.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

tests/simple-filters.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# Filters used for triggering k8s deployments
3+
4+
k8s/network:
5+
- 'k8s/network/**'
6+
# Environments (change in `base` will deploy everywhere)
7+
k8s/environments/development:
8+
- 'k8s/environments/development/**'
9+
- 'k8s/base/**'
10+
k8s/environments/qa:
11+
- 'k8s/environments/qa/**'
12+
- 'k8s/base/**'
13+
k8s/environments/staging:
14+
- 'k8s/environments/staging/**'
15+
- 'k8s/base/**'
16+
k8s/environments/sandbox:
17+
- 'k8s/environments/sandbox/**'
18+
- 'k8s/base/**'
19+
k8s/environments/production:
20+
- 'k8s/environments/production/**'
21+
- 'k8s/base/**'
22+
# Shared services
23+
k8s/shared/signoz-otel-collector:
24+
- 'k8s/shared/signoz-otel-collector/**'

0 commit comments

Comments
 (0)