|
| 1 | +#!/usr/bin/env bats |
| 2 | +# backupstrategy-controller ships its strategy.backups.cozystack.io CRDs as |
| 3 | +# ordinary templates in the same release as the default Strategy CRs. Helm maps |
| 4 | +# every document of a release through the RESTMapper before it applies any, so |
| 5 | +# one Strategy CR whose CRD is not served yet fails the whole release with "no |
| 6 | +# matches for kind" and applies nothing, the CRDs included. That happens on the |
| 7 | +# upgrade that first ships a kind, and on the first render of an external-S3 |
| 8 | +# install, where the bucket gate is already open. Every default Strategy must |
| 9 | +# therefore also gate on its CRD being established (the crdEstablished helper). |
| 10 | +# |
| 11 | +# helm-unittest can only pin the templates someone remembered to list. These |
| 12 | +# cases discover the templates from the render itself, so a new |
| 13 | +# strategy-*-default.yaml without the CRD gate fails here without anyone |
| 14 | +# touching a test. |
| 15 | +# |
| 16 | +# Harness compatibility note: CI runs this file under Bats through |
| 17 | +# `make bats-unit-tests`. It also remains compatible with the narrower legacy |
| 18 | +# `hack/cozytest.sh` translator, so it avoids `run`, `$status`, `$output`, |
| 19 | +# `skip`, and setup()/teardown(). Paths stay repo-root-relative for both |
| 20 | +# runners. |
| 21 | +# |
| 22 | +# Run with: bats hack/backupstrategy-crd-gate.bats |
| 23 | + |
| 24 | +load test_helper |
| 25 | + |
| 26 | +CHART=packages/system/backupstrategy-controller |
| 27 | +STRATEGY_API='strategy.backups.cozystack.io/' |
| 28 | + |
| 29 | +# Every Strategy CR in the render under $1, one "<template> <kind>/<name>" line |
| 30 | +# each. --output-dir keeps one file per template, which is what names the |
| 31 | +# offender. |
| 32 | +strategy_crs() { |
| 33 | + find "$1" -name '*.yaml' | sort | while read -r f; do |
| 34 | + yq eval-all "select(.apiVersion // \"\" | test(\"^${STRATEGY_API}\")) | .kind + \"/\" + .metadata.name" "$f" \ |
| 35 | + | sed "s#^#$(basename "$f") #" |
| 36 | + done |
| 37 | +} |
| 38 | + |
| 39 | +@test "no Strategy CR renders while no strategy CRD is established, for any template" { |
| 40 | + tmp=$(mktemp -d) |
| 41 | + |
| 42 | + # provisionBucket=false resolves the bucket name from values, so every bucket |
| 43 | + # gate is open, while the render has no apiserver and every CRD lookup |
| 44 | + # answers "absent": the state of an upgrade that ships a new kind, and of the |
| 45 | + # first external-S3 install. |
| 46 | + helm template t "$CHART" -n cozy-backup-controller \ |
| 47 | + --set backupStorage.provisionBucket=false \ |
| 48 | + --set backupStorage.bucketName=ext-bucket \ |
| 49 | + --output-dir "$tmp/gated" >/dev/null |
| 50 | + strategy_crs "$tmp/gated" > "$tmp/gated.txt" |
| 51 | + |
| 52 | + # Positive control: the same render with only the CRD gate opened (the |
| 53 | + # offline bucketNameOverride short-circuits it) must produce Strategy CRs, |
| 54 | + # or the assertion above it would hold for an empty render. |
| 55 | + helm template t "$CHART" -n cozy-backup-controller \ |
| 56 | + --set backupStorage.provisionBucket=false \ |
| 57 | + --set backupStorage.bucketName=ext-bucket \ |
| 58 | + --set backupStorage.bucketNameOverride=ext-bucket \ |
| 59 | + --output-dir "$tmp/open" >/dev/null |
| 60 | + strategy_crs "$tmp/open" > "$tmp/open.txt" |
| 61 | + |
| 62 | + if [ -s "$tmp/gated.txt" ]; then |
| 63 | + echo "Strategy CRs rendered without an established CRD (missing crdEstablished gate):" >&2 |
| 64 | + cat "$tmp/gated.txt" >&2 |
| 65 | + false |
| 66 | + fi |
| 67 | + if [ ! -s "$tmp/open.txt" ]; then |
| 68 | + echo "control render produced no Strategy CR; the check above proves nothing" >&2 |
| 69 | + false |
| 70 | + fi |
| 71 | + # A strategy template that renders nothing by default (behind an opt-in |
| 72 | + # value) would pass the first check without its gate ever being exercised. |
| 73 | + # Every one of them has to show up in the control render; one that is off by |
| 74 | + # default needs its switch added to both renders above. |
| 75 | + missing="" |
| 76 | + for f in "$CHART"/templates/strategy-*.yaml; do |
| 77 | + f=$(basename "$f") |
| 78 | + if ! grep -q "^$f " "$tmp/open.txt"; then |
| 79 | + missing="$missing $f" |
| 80 | + fi |
| 81 | + done |
| 82 | + if [ -n "$missing" ]; then |
| 83 | + echo "strategy templates absent from the control render, so unchecked:$missing" >&2 |
| 84 | + false |
| 85 | + fi |
| 86 | + |
| 87 | + rm -rf "$tmp" |
| 88 | +} |
| 89 | + |
| 90 | +@test "every rendered Strategy CR gates on the CRD this chart ships for its kind" { |
| 91 | + tmp=$(mktemp -d) |
| 92 | + |
| 93 | + # kind -> CRD name, from the CRDs this chart actually renders. |
| 94 | + for f in "$CHART"/definitions/*.yaml; do |
| 95 | + yq eval '.spec.names.kind + " " + .metadata.name' "$f" |
| 96 | + done > "$tmp/crds.txt" |
| 97 | + if [ ! -s "$tmp/crds.txt" ]; then |
| 98 | + echo "no CRDs read from $CHART/definitions" >&2 |
| 99 | + false |
| 100 | + fi |
| 101 | + |
| 102 | + helm template t "$CHART" -n cozy-backup-controller \ |
| 103 | + --set backupStorage.bucketNameOverride=ext-bucket \ |
| 104 | + --output-dir "$tmp/open" >/dev/null |
| 105 | + strategy_crs "$tmp/open" > "$tmp/open.txt" |
| 106 | + if [ ! -s "$tmp/open.txt" ]; then |
| 107 | + echo "render produced no Strategy CR; nothing was checked" >&2 |
| 108 | + false |
| 109 | + fi |
| 110 | + |
| 111 | + # The gate passes on any established CRD it is given, so a gate naming the |
| 112 | + # wrong CRD (a typo, a copied sibling) leaves the CR out of every live render |
| 113 | + # while the case above stays green. The name has to be the one serving the |
| 114 | + # kind. |
| 115 | + bad="" |
| 116 | + while read -r tpl obj; do |
| 117 | + kind=${obj%%/*} |
| 118 | + crd=$(awk -v k="$kind" '$1 == k { print $2 }' "$tmp/crds.txt") |
| 119 | + if [ -z "$crd" ]; then |
| 120 | + bad="$bad |
| 121 | +$tpl: $obj has no CRD in $CHART/definitions" |
| 122 | + continue |
| 123 | + fi |
| 124 | + if ! grep -qF "\"crd\" \"$crd\"" "$CHART/templates/$tpl"; then |
| 125 | + bad="$bad |
| 126 | +$tpl: $obj does not gate on crdEstablished for $crd" |
| 127 | + fi |
| 128 | + done < "$tmp/open.txt" |
| 129 | + if [ -n "$bad" ]; then |
| 130 | + echo "Strategy CR templates with a wrong or missing CRD gate:$bad" >&2 |
| 131 | + false |
| 132 | + fi |
| 133 | + |
| 134 | + rm -rf "$tmp" |
| 135 | +} |
0 commit comments