|
| 1 | +#!/usr/bin/env bats |
| 2 | +# ----------------------------------------------------------------------------- |
| 3 | +# Execution-level regression test for the worker TalosConfigTemplate heredoc |
| 4 | +# (cozystack/cozystack#3513, #3575 review). |
| 5 | +# |
| 6 | +# The reconcile Job applies the TalosConfigTemplate via an UNQUOTED |
| 7 | +# `cat <<EOF | kubectl apply -f -` heredoc, so every line of its body is subject |
| 8 | +# to shell parameter expansion and command substitution at Job runtime. The |
| 9 | +# `talos.registryMirrors` knob renders free-form tenant-facing input into that |
| 10 | +# heredoc. A helm-unittest string `matchRegex` cannot catch a heredoc that the |
| 11 | +# shell refuses to emit (e.g. an unbalanced backtick from an un-escaped value OR |
| 12 | +# from a stray literal comment): it never runs the shell. This test does. |
| 13 | +# |
| 14 | +# It renders the Job with a HOSTILE mirror endpoint (`$(...)` + a backtick), |
| 15 | +# extracts the `cat <<EOF ... EOF` block, runs it through a real shell, and |
| 16 | +# asserts the heredoc still emits the TalosConfigTemplate (non-empty) and that |
| 17 | +# the hostile value rendered as a LITERAL (never command-substituted). On the |
| 18 | +# pre-fix code (unescaped value, or the explanatory comment carrying raw |
| 19 | +# backticks) the heredoc dies with "bad substitution: no closing backtick" and |
| 20 | +# `cat` emits zero bytes, which this test catches; a `matchRegex` did not. |
| 21 | +# |
| 22 | +# Needs `helm` + `yq`; cozytest.sh runs from the repo root. |
| 23 | +# Run with: hack/cozytest.sh hack/talos-reconcile-heredoc_test.bats |
| 24 | +# ----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +CHART=packages/apps/kubernetes |
| 27 | + |
| 28 | +@test "worker TalosConfigTemplate heredoc emits with a hostile registryMirrors value (no shell break, value stays literal)" { |
| 29 | + work=$(mktemp -d) |
| 30 | + # Hostile endpoint: a command substitution and a backtick. Single-quoted so |
| 31 | + # THIS shell does not expand it before helm sees it. |
| 32 | + helm template t "$CHART" -n tenant-test -f "$CHART/tests/values/common.yaml" \ |
| 33 | + --set 'talos.registryMirrors.ghcr\.io.endpoints[0]=http://m$(id)`x`y' \ |
| 34 | + --show-only templates/talos/talos-reconcile-job.yaml \ |
| 35 | + | yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' \ |
| 36 | + > "$work/cmd.sh" |
| 37 | + [ -s "$work/cmd.sh" ] || { echo "render produced no Job command" >&2; rm -rf "$work"; exit 1; } |
| 38 | + |
| 39 | + # Extract just the TalosConfigTemplate heredoc, dropping the kubectl pipe so we |
| 40 | + # exercise the shell's handling of the body without applying anything. The |
| 41 | + # rendered script dedents the heredoc to column 0, so the terminator is `EOF`. |
| 42 | + awk ' |
| 43 | + /^cat <<EOF \| kubectl apply/ { print "cat <<EOF"; inblock=1; next } |
| 44 | + inblock && /^EOF$/ { print "EOF"; inblock=0; next } |
| 45 | + inblock { print } |
| 46 | + ' "$work/cmd.sh" > "$work/heredoc.sh" |
| 47 | + grep -q '^cat <<EOF$' "$work/heredoc.sh" || { echo "could not extract the heredoc" >&2; rm -rf "$work"; exit 1; } |
| 48 | + |
| 49 | + # Run the heredoc through a real shell. Unset ${RELEASE}/${CLUSTER_ID}/... just |
| 50 | + # expand to empty; the point is that the shell can emit the body at all. |
| 51 | + out=$(sh "$work/heredoc.sh" 2>"$work/err") || { echo "heredoc shell exited non-zero" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; } |
| 52 | + |
| 53 | + # (a) Non-empty: on the pre-fix break `cat` emitted zero bytes. |
| 54 | + [ -n "$out" ] || { echo "heredoc emitted no output (shell refused the body)" >&2; cat "$work/err" >&2; rm -rf "$work"; exit 1; } |
| 55 | + # (b) It really is the worker config. |
| 56 | + printf '%s' "$out" | grep -q 'kind: TalosConfigTemplate' || { echo "heredoc output is not the TalosConfigTemplate" >&2; rm -rf "$work"; exit 1; } |
| 57 | + # (c) The hostile value rendered LITERALLY: the command substitution was NOT |
| 58 | + # executed (its literal text survives) and no `id` output leaked in. |
| 59 | + printf '%s' "$out" | grep -qF 'http://m$(id)`x`y' || { echo "hostile endpoint was not preserved literally (shell expansion leaked)" >&2; printf '%s\n' "$out" | grep -i ghcr >&2; rm -rf "$work"; exit 1; } |
| 60 | + |
| 61 | + rm -rf "$work" |
| 62 | +} |
| 63 | + |
| 64 | +@test "worker TalosConfigTemplate heredoc emits with the default (no registryMirrors)" { |
| 65 | + work=$(mktemp -d) |
| 66 | + helm template t "$CHART" -n tenant-test -f "$CHART/tests/values/common.yaml" \ |
| 67 | + --show-only templates/talos/talos-reconcile-job.yaml \ |
| 68 | + | yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' \ |
| 69 | + > "$work/cmd.sh" |
| 70 | + awk ' |
| 71 | + /^cat <<EOF \| kubectl apply/ { print "cat <<EOF"; inblock=1; next } |
| 72 | + inblock && /^EOF$/ { print "EOF"; inblock=0; next } |
| 73 | + inblock { print } |
| 74 | + ' "$work/cmd.sh" > "$work/heredoc.sh" |
| 75 | + out=$(sh "$work/heredoc.sh" 2>/dev/null) || { echo "default heredoc shell exited non-zero" >&2; rm -rf "$work"; exit 1; } |
| 76 | + [ -n "$out" ] || { echo "default heredoc emitted no output" >&2; rm -rf "$work"; exit 1; } |
| 77 | + printf '%s' "$out" | grep -q 'kind: TalosConfigTemplate' || { echo "default heredoc output is not the TalosConfigTemplate" >&2; rm -rf "$work"; exit 1; } |
| 78 | + # Default renders no machine-level registries mirrors block. |
| 79 | + printf '%s' "$out" | grep -q 'mirrors:' && { echo "default unexpectedly rendered a mirrors block" >&2; rm -rf "$work"; exit 1; } |
| 80 | + rm -rf "$work" |
| 81 | +} |
0 commit comments