forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-template-snapshot.sh
executable file
·101 lines (88 loc) · 2.23 KB
/
helm-template-snapshot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -euo pipefail
# helm-template-snapshot.sh
#
# SUMMARY
#
# Manages the Helm template snapshots.
# See usage function in the code or run without arguments.
cd "$(dirname "${BASH_SOURCE[0]}")/.."
CONFIGURATIONS_DIR="tests/helm-snapshots"
generate() {
local RELEASE_NAME="$1"
local CHART="$2"
local VALUES_FILE="$3"
# Print header.
cat <<EOF
# Do not edit!
# This file is generated
# - by "scripts/helm-snapshot-tests.sh"
# - for the chart at "$CHART"
# - with the values from "$VALUES_FILE"
EOF
# Generate template.
# TODO: use app-version when https://github.com/helm/helm/issues/8670 is solved
helm template \
"$RELEASE_NAME" \
"$CHART" \
--namespace vector \
--create-namespace \
--values "$VALUES_FILE" \
--version master \
--debug
}
list-config-files() {
CONFIG_FILES=()
while IFS= read -r -d $'\0'; do
CONFIG_FILES+=("$REPLY")
done < <(find "$CONFIGURATIONS_DIR" -name "config.sh" -print0)
}
update() {
list-config-files
for CONFIG_FILE in "${CONFIG_FILES[@]}"; do
VALUES_FILE="$(dirname "$CONFIG_FILE")/values.yaml"
TARGET_FILE="$(dirname "$CONFIG_FILE")/snapshot.yaml"
(
# shellcheck disable=SC1090
source "$CONFIG_FILE"
generate "$RELEASE_NAME" "$CHART" "$VALUES_FILE" >"$TARGET_FILE"
)
done
}
check() {
list-config-files
for CONFIG_FILE in "${CONFIG_FILES[@]}"; do
VALUES_FILE="$(dirname "$CONFIG_FILE")/values.yaml"
TARGET_FILE="$(dirname "$CONFIG_FILE")/snapshot.yaml"
(
# shellcheck disable=SC1090
source "$CONFIG_FILE"
GENERATED="$(generate "$RELEASE_NAME" "$CHART" "$VALUES_FILE")"
FILE="$(cat "$TARGET_FILE")"
if [[ "$GENERATED" != "$FILE" ]]; then
echo "Error: snapshot ($TARGET_FILE) does not match the generated version" >&2
diff "$TARGET_FILE" - <<<"$GENERATED"
exit 1
fi
)
done
}
usage() {
cat >&2 <<-EOF
Usage: $0 MODE
Modes:
check - run the tests, compare the generated outputs with snapshots and
exit with non-zero exit code if the outputs do not match
update - run the tests and update the snapshots from the generated output
EOF
exit 1
}
MODE="${1:-}"
case "$MODE" in
update | check)
"$MODE"
;;
*)
usage
;;
esac