test(gitops): define argocd offline render contract
This commit is contained in:
1 parent
ff761a3e90
commit
5aa91ede4a
2 files changed
+167
No files matched your search
Executable
+160
@@ -0,0 +1,160 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
INVENTORY_FILE="$SCRIPT_DIR/tests/expected-workloads.txt"
|
||||
|
||||
fail() {
|
||||
echo "ERROR: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ $# -eq 1 ]] || fail "usage: $0 <rendered-manifest>"
|
||||
|
||||
rendered_manifest="$1"
|
||||
[[ -s "$rendered_manifest" ]] || fail "rendered manifest is missing or empty: $rendered_manifest"
|
||||
[[ -s "$INVENTORY_FILE" ]] || fail "workload inventory is missing or empty: $INVENTORY_FILE"
|
||||
|
||||
expected_images=(
|
||||
"192.168.1.119:8888/argoproj/argocd:v3.4.4"
|
||||
"192.168.1.119:8888/dexidp/dex:v2.45.0"
|
||||
"192.168.1.119:8888/library/redis:8.2.3-alpine"
|
||||
)
|
||||
|
||||
for expected_image in "${expected_images[@]}"; do
|
||||
grep -Fq "image: $expected_image" "$rendered_manifest" \
|
||||
|| fail "expected image is missing: $expected_image"
|
||||
done
|
||||
|
||||
while IFS= read -r actual_image; do
|
||||
case "$actual_image" in
|
||||
"${expected_images[0]}"|"${expected_images[1]}"|"${expected_images[2]}") ;;
|
||||
*) fail "unexpected workload image in render: $actual_image" ;;
|
||||
esac
|
||||
done < <(awk '$1 == "image:" {gsub(/\"/, "", $2); print $2}' "$rendered_manifest" | sort -u)
|
||||
|
||||
if grep -Eq 'image:[[:space:]]+(quay\.io|ghcr\.io|public\.ecr\.aws)/' "$rendered_manifest"; then
|
||||
fail "upstream image reference remains in render"
|
||||
fi
|
||||
|
||||
if grep -Fq 'imagePullPolicy: Always' "$rendered_manifest"; then
|
||||
fail "imagePullPolicy Always remains in render"
|
||||
fi
|
||||
|
||||
verify_workload() {
|
||||
local resource="$1"
|
||||
shift
|
||||
local expected_containers="$*"
|
||||
local expected_kind="${resource%%/*}"
|
||||
local expected_name="${resource#*/}"
|
||||
|
||||
awk \
|
||||
-v expected_kind="$expected_kind" \
|
||||
-v expected_name="$expected_name" \
|
||||
-v expected_containers="$expected_containers" '
|
||||
function reset_document() {
|
||||
delete lines
|
||||
line_count = 0
|
||||
}
|
||||
|
||||
function validate_document( i, kind, name, in_metadata, in_containers,
|
||||
policy_count, container_count, field_name,
|
||||
field_value, expected_count, expected,
|
||||
container_name) {
|
||||
kind = ""
|
||||
name = ""
|
||||
in_metadata = 0
|
||||
in_containers = 0
|
||||
policy_count = 0
|
||||
container_count = 0
|
||||
delete seen_containers
|
||||
|
||||
for (i = 1; i <= line_count; i++) {
|
||||
if (lines[i] ~ /^kind: /) {
|
||||
kind = lines[i]
|
||||
sub(/^kind: /, "", kind)
|
||||
} else if (lines[i] == "metadata:") {
|
||||
in_metadata = 1
|
||||
} else if (in_metadata && lines[i] ~ /^ name: /) {
|
||||
name = lines[i]
|
||||
sub(/^ name: /, "", name)
|
||||
in_metadata = 0
|
||||
}
|
||||
}
|
||||
|
||||
if (kind != expected_kind || name != expected_name) {
|
||||
return
|
||||
}
|
||||
|
||||
found_workload = 1
|
||||
for (i = 1; i <= line_count; i++) {
|
||||
if (lines[i] ~ /^ (containers|initContainers):$/) {
|
||||
in_containers = 1
|
||||
continue
|
||||
}
|
||||
if (in_containers && lines[i] ~ /^ [[:alnum:]][[:alnum:]-]*:/) {
|
||||
in_containers = 0
|
||||
}
|
||||
if (!in_containers) {
|
||||
continue
|
||||
}
|
||||
if (lines[i] ~ /^ name: /) {
|
||||
container_name = lines[i]
|
||||
sub(/^ name: /, "", container_name)
|
||||
seen_containers[container_name]++
|
||||
container_count++
|
||||
} else if (lines[i] ~ /^ imagePullPolicy: /) {
|
||||
field_value = lines[i]
|
||||
sub(/^ imagePullPolicy: /, "", field_value)
|
||||
if (field_value != "IfNotPresent") {
|
||||
printf "ERROR: %s/%s has non-offline pull policy: %s\n", kind, name, field_value > "/dev/stderr"
|
||||
errors++
|
||||
}
|
||||
policy_count++
|
||||
}
|
||||
}
|
||||
|
||||
expected_count = split(expected_containers, expected, /[[:space:]]+/)
|
||||
for (i = 1; i <= expected_count; i++) {
|
||||
if (seen_containers[expected[i]] != 1) {
|
||||
printf "ERROR: %s/%s expected container exactly once: %s\n", kind, name, expected[i] > "/dev/stderr"
|
||||
errors++
|
||||
}
|
||||
}
|
||||
if (container_count != expected_count) {
|
||||
printf "ERROR: %s/%s container inventory mismatch: expected %d, found %d\n", kind, name, expected_count, container_count > "/dev/stderr"
|
||||
errors++
|
||||
}
|
||||
if (policy_count != container_count) {
|
||||
printf "ERROR: %s/%s requires explicit IfNotPresent on every container: %d containers, %d policies\n", kind, name, container_count, policy_count > "/dev/stderr"
|
||||
errors++
|
||||
}
|
||||
}
|
||||
|
||||
/^---$/ {
|
||||
validate_document()
|
||||
reset_document()
|
||||
next
|
||||
}
|
||||
|
||||
{ lines[++line_count] = $0 }
|
||||
|
||||
END {
|
||||
validate_document()
|
||||
if (!found_workload) {
|
||||
printf "ERROR: expected workload is missing: %s/%s\n", expected_kind, expected_name > "/dev/stderr"
|
||||
errors++
|
||||
}
|
||||
exit(errors > 0 ? 1 : 0)
|
||||
}
|
||||
' "$rendered_manifest"
|
||||
}
|
||||
|
||||
while read -r resource containers; do
|
||||
[[ -n "$resource" && -n "$containers" ]] || fail "invalid workload inventory entry"
|
||||
# shellcheck disable=SC2086
|
||||
verify_workload "$resource" $containers
|
||||
done < "$INVENTORY_FILE"
|
||||
|
||||
echo "Argo CD offline render verification passed."
|
||||
Reference in new issue
Block a user