| new file mode 100755 |
| index 000000000..0b1f009f4 |
| --- /dev/null |
| +++ b/.amadan/opentofu.sh |
| @@ -0,0 +1,404 @@ |
| +#!/bin/sh |
| +# The OpenTofu module tests: every *.tftest.hcl under infra/, run by a |
| +# pinned tofu against mock providers, with no AWS credentials and no |
| +# public registry anywhere in sight. |
| +# |
| +# .amadan/opentofu.sh install DIR pinned tofu and providers into DIR |
| +# .amadan/opentofu.sh test every test file, tofu from PATH |
| +# .amadan/opentofu.sh both, in a scratch directory |
| +# |
| +# `make tofu-test` is the last form. CI runs the first two from |
| +# `10-gate`, beside `make test`, and the reason is wall time: amadan runs |
| +# the ci.d steps one after another, so a step of its own would add its |
| +# whole duration to every run. The gate's wall clock is `make test` |
| +# (about 220 s), which mostly waits rather than computes. This suite |
| +# finishes well inside it. Measured on docs-t1 on 2026-11-19, a busy |
| +# shared 16-core box: 170 s one file at a time, 78 s with eight workers, |
| +# and no file longer than 20 s alone (region-host's group.tftest.hcl). |
| +# So beside the gate it costs only the install, which comes before |
| +# `make test` (below): about 6 s on a warm cache, mostly tofu unzipping |
| +# the AWS provider. |
| +# |
| +# The install comes first because two Go tests in `make test` shell out |
| +# to tofu: internal/provision's TestRegionGroupUserDataOpenTofu (and, |
| +# when the media-conversion branch lands, internal/mediaaws' |
| +# TestModuleRenderIsCurrent). They skip without tofu on a laptop and |
| +# fail without it in CI, so the gate puts the pinned tofu on PATH, and |
| +# TF_CLI_CONFIG_FILE at the mirror below, before `make test` starts. |
| +# |
| +# WHAT RUNS. Every *.tftest.hcl under infra/, found by walking the tree, |
| +# so a new module or test directory is covered without touching this |
| +# file. A test file's module is the nearest directory at or above it |
| +# that holds a .tf or .tofu file; the path between them is the test |
| +# directory, passed as -test-directory unless it is the default `tests` |
| +# (infra/modules/region-host/tests/managed_fleets is the one that needs |
| +# it today). Each (module, test directory) is initialised once with |
| +# `tofu init -backend=false`, then every file runs as its own |
| +# `tofu test -filter=<file>`, eight at a time (OPENTOFU_JOBS). |
| +# |
| +# WHAT TURNS IT RED. A file passes only when tofu exits 0, prints the |
| +# file's own "... pass" line, and ends with "Success! N passed, 0 |
| +# failed." for some N above zero. So a file that a filter typo, a rename |
| +# or a skipped run leaves unexercised fails: `tofu test` reports a |
| +# filter that matches nothing as "Success! 0 passed", and a run skipped |
| +# after an earlier failure as "N skipped". |
| +# |
| +# NO AWS. The tests use mock_provider "aws" only. The AWS credential and |
| +# profile variables are unset, the shared config and credentials files |
| +# are /dev/null, and instance metadata is off, so a test that lost its |
| +# mock could not reach an account even if a runner had one. |
| +# |
| +# THE PINS. OpenTofu 1.13.9, the newest release on 2026-11-19 (its 1.13 |
| +# series is supported until 2027-08-01). 1.13.0 is the floor: |
| +# `tofu test` takes instance keys in override_data and |
| +# override_resource targets (data.aws_eip.group["eu-west-1a"]) from 1.13 |
| +# on (opentofu#4067). Nine region-host test files use them, and 1.12.x, |
| +# up to 1.12.12, refuses to load the module's tests at all. |
| +# |
| +# The providers are those the modules require: hashicorp/aws (>= 6.0; |
| +# box-fence-writer says >= 5.0) and hashicorp/archive (>= 2.4, the |
| +# media-engine module on the media-conversion branch). Pinned to the |
| +# newest of each on 2026-11-19: aws 6.73.0, archive 2.8.1. The modules' |
| +# own constraints stay open-ended; this pin is what CI tests them with. |
| +# |
| +# THE SOURCE. Never a public registry at run time. Every file comes from |
| +# our own mirror (the bucket 30-minio-contract uses, under opentofu/), |
| +# is checked against the sha256 below after download and again on every |
| +# cache hit, and a mismatch refuses to run. The CLI config this writes |
| +# names one filesystem mirror and no direct installation, so tofu cannot |
| +# reach a registry even for a provider nobody pinned: it fails instead. |
| +# |
| +# The digests are upstream's. tofu: the release's tofu_1.13.9_SHA256SUMS |
| +# from the opentofu/opentofu GitHub release. Providers: the packages |
| +# registry.opentofu.org serves (opentofu/terraform-provider-* release |
| +# assets), which `tofu providers mirror` fetched and verified against |
| +# the registry's signing key, and whose digests match the registry's |
| +# published shasum. HashiCorp's own zips of the same versions are |
| +# packed differently and have other digests; these are not those. |
| +# |
| +# Bump a pin by fetching the new upstream files, checking them against |
| +# upstream's sums, uploading them to the mirror, and re-deriving the |
| +# digests here. |
| +# |
| +# THE CACHE. As in 30-minio-contract, an untracked dot-directory in the |
| +# checkout: .amadan-cache/opentofu-<arch>/, kept between jobs, never |
| +# trusted without its digests. Each run unpacks into its own scratch |
| +# directory: the tofu binary, and the providers unzipped once by tofu |
| +# itself (a bootstrap `tofu init` into a plugin cache). That plugin |
| +# cache then serves as an unpacked filesystem mirror, which tofu links |
| +# rather than copies, so the 800 MB AWS provider exists once per run, not |
| +# once per module. |
| +# |
| +# NOTHING WRITTEN UNDER infra/. tofu init writes a .terraform.lock.hcl |
| +# beside each module, and internal/provision's census walks every regular |
| +# file under infra/, dotfiles included, while `make test` runs beside |
| +# this. So the tests run in a scratch tree: a copy of infra/ (less any |
| +# .terraform directories and lock files a developer has), with every |
| +# other top-level entry of the checkout symlinked beside it, so a test's |
| +# file("../../../internal/...") still finds the real file. The |
| +# .terraform directories go to scratch too (TF_DATA_DIR). |
| +set -eu |
| + |
| +OPENTOFU_VERSION=1.13.9 |
| +AWS_VERSION=6.73.0 |
| +ARCHIVE_VERSION=2.8.1 |
| + |
| +# OPENTOFU_MIRROR exists so the step can be checked against a local copy |
| +# of the mirror (file://...) before anything is uploaded. The digests |
| +# hold whatever the source. |
| +MIRROR=${OPENTOFU_MIRROR:-https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu} |
| + |
| +root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd) |
| + |
| +case "$(uname -m)" in |
| +x86_64 | amd64) |
| + arch=amd64 |
| + tofu_sha=b34b327cc1ef07178207b6f2cd1f5276537d3e5b7be316500f6dac50d4c0a857 |
| + aws_sha=a61f8e114e5233e63b8d2b034b1ad59a220e532fa34aaaa63eec6bad73a0ca21 |
| + archive_sha=167dcb2f3dfef941d300e4899c6b1852592e98cd5e20782289369bf0c81512f0 |
| + ;; |
| +aarch64 | arm64) |
| + arch=arm64 |
| + tofu_sha=5dc196dde75b667e4806d358310ec96742774cd0993c54c1237efee5644b69b8 |
| + aws_sha=ba50cb9245a579285169565995e20bebb410e7f4d4181dc7de1205877fe04728 |
| + archive_sha=a77922d3edcc1288d52e5d225e0b98d6ae8b3ef8ef4f27a40a6c11136489b886 |
| + ;; |
| +*) |
| + echo "no pinned OpenTofu build for $(uname -m): add one above, do not guess" >&2 |
| + exit 1 |
| + ;; |
| +esac |
| + |
| +fetch_pinned() { |
| + fp_name=$1 |
| + fp_url=$2 |
| + fp_want=$3 |
| + fp_dest=$4 |
| + |
| + if [ -f "$fp_dest" ] && [ "$(sha256sum <"$fp_dest" | cut -d' ' -f1)" = "$fp_want" ]; then |
| + echo " $fp_name: cached, digest verified" |
| + return 0 |
| + fi |
| + |
| + echo " $fp_name: downloading $fp_url" |
| + mkdir -p "$(dirname "$fp_dest")" |
| + curl -sfL --retry 3 -o "$fp_dest.part" "$fp_url" |
| + |
| + fp_got=$(sha256sum <"$fp_dest.part" | cut -d' ' -f1) |
| + if [ "$fp_got" != "$fp_want" ]; then |
| + rm -f "$fp_dest.part" |
| + echo "$fp_name digest mismatch: refusing to run" >&2 |
| + echo " want $fp_want" >&2 |
| + echo " got $fp_got" >&2 |
| + echo " from $fp_url" >&2 |
| + exit 1 |
| + fi |
| + mv "$fp_dest.part" "$fp_dest" |
| + echo " $fp_name: downloaded, digest verified" |
| +} |
| + |
| +# install DIR: DIR/bin/tofu, DIR/plugins (an unpacked provider mirror) |
| +# and DIR/tofurc (a CLI config naming that mirror and nothing else). |
| +install() { |
| + dir=$1 |
| + for tool in curl sha256sum tar; do |
| + command -v "$tool" >/dev/null 2>&1 || { |
| + echo "$tool is required by this step but was not found on PATH" >&2 |
| + exit 1 |
| + } |
| + done |
| + echo "opentofu: $arch, tofu $OPENTOFU_VERSION, aws $AWS_VERSION, archive $ARCHIVE_VERSION" |
| + |
| + cache=$root/.amadan-cache/opentofu-$arch |
| + tarball=tofu_${OPENTOFU_VERSION}_linux_$arch.tar.gz |
| + aws_zip=terraform-provider-aws_${AWS_VERSION}_linux_$arch.zip |
| + archive_zip=terraform-provider-archive_${ARCHIVE_VERSION}_linux_$arch.zip |
| + packed=$cache/providers/registry.opentofu.org/hashicorp |
| + fetch_pinned tofu "$MIRROR/$tarball" "$tofu_sha" "$cache/$tarball" |
| + fetch_pinned aws "$MIRROR/$aws_zip" "$aws_sha" "$packed/aws/$aws_zip" |
| + fetch_pinned archive "$MIRROR/$archive_zip" "$archive_sha" "$packed/archive/$archive_zip" |
| + |
| + mkdir -p "$dir/bin" "$dir/plugins" "$dir/bootstrap" |
| + tar -xzf "$cache/$tarball" -C "$dir/bin" tofu |
| + got=$("$dir/bin/tofu" version | head -n 1) |
| + if [ "$got" != "OpenTofu v$OPENTOFU_VERSION" ]; then |
| + echo "the pinned tarball says \"$got\", not OpenTofu v$OPENTOFU_VERSION" >&2 |
| + exit 1 |
| + fi |
| + |
| + # The bootstrap: one init against the packed zips, which tofu unpacks |
| + # into its plugin cache. Its own lock file lands in DIR/bootstrap. |
| + cat >"$dir/bootstrap.tofurc" <<-EOF |
| + provider_installation { |
| + filesystem_mirror { |
| + path = "$cache/providers" |
| + } |
| + } |
| + plugin_cache_dir = "$dir/plugins" |
| + EOF |
| + cat >"$dir/bootstrap/main.tf" <<-EOF |
| + terraform { |
| + required_providers { |
| + aws = { source = "hashicorp/aws", version = "$AWS_VERSION" } |
| + archive = { source = "hashicorp/archive", version = "$ARCHIVE_VERSION" } |
| + } |
| + } |
| + EOF |
| + if ! (cd "$dir/bootstrap" && |
| + TF_CLI_CONFIG_FILE="$dir/bootstrap.tofurc" TF_DATA_DIR="$dir/bootstrap/.terraform" \ |
| + "$dir/bin/tofu" init -backend=false -input=false -no-color >"$dir/bootstrap.log" 2>&1); then |
| + cat "$dir/bootstrap.log" >&2 |
| + echo "unpacking the pinned providers failed" >&2 |
| + exit 1 |
| + fi |
| + |
| + cat >"$dir/tofurc" <<-EOF |
| + provider_installation { |
| + filesystem_mirror { |
| + path = "$dir/plugins" |
| + } |
| + } |
| + EOF |
| + echo " installed in $dir" |
| +} |
| + |
| +# The module a test file belongs to: the nearest directory at or above |
| +# it holding a .tf or .tofu file. |
| +module_of() { |
| + mo_dir=$(dirname "$1") |
| + while [ "$mo_dir" != . ] && [ "$mo_dir" != / ]; do |
| + for mo_f in "$mo_dir"/*.tf "$mo_dir"/*.tofu; do |
| + if [ -f "$mo_f" ]; then |
| + echo "$mo_dir" |
| + return 0 |
| + fi |
| + done |
| + mo_dir=$(dirname "$mo_dir") |
| + done |
| + echo "no module above $1" >&2 |
| + return 1 |
| +} |
| + |
| +test_all() { |
| + command -v tofu >/dev/null 2>&1 || { |
| + echo "tofu is not on PATH: run '.amadan/opentofu.sh install DIR' and put DIR/bin first" >&2 |
| + exit 1 |
| + } |
| + : "${TF_CLI_CONFIG_FILE:?set TF_CLI_CONFIG_FILE to the tofurc that install wrote}" |
| + got=$(tofu version | head -n 1) |
| + if [ "$got" != "OpenTofu v$OPENTOFU_VERSION" ]; then |
| + echo "tofu on PATH is \"$got\"; this step pins OpenTofu v$OPENTOFU_VERSION" >&2 |
| + exit 1 |
| + fi |
| + |
| + unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN \ |
| + AWS_SECURITY_TOKEN AWS_PROFILE AWS_DEFAULT_PROFILE AWS_ROLE_ARN \ |
| + AWS_WEB_IDENTITY_TOKEN_FILE AWS_CONTAINER_CREDENTIALS_RELATIVE_URI \ |
| + AWS_CONTAINER_CREDENTIALS_FULL_URI |
| + AWS_CONFIG_FILE=/dev/null |
| + AWS_SHARED_CREDENTIALS_FILE=/dev/null |
| + AWS_EC2_METADATA_DISABLED=true |
| + TF_IN_AUTOMATION=1 |
| + CHECKPOINT_DISABLE=1 |
| + export AWS_CONFIG_FILE AWS_SHARED_CREDENTIALS_FILE AWS_EC2_METADATA_DISABLED \ |
| + TF_IN_AUTOMATION CHECKPOINT_DISABLE |
| + |
| + run=$(mktemp -d "${TMPDIR:-/tmp}/carlos-opentofu-test-XXXXXX") |
| + trap 'rm -rf "$run"' EXIT |
| + trap 'exit 1' INT TERM |
| + tree=$run/tree |
| + mkdir -p "$run/data" "$run/logs" "$tree" |
| + |
| + # The scratch tree: infra/ copied, everything else linked. |
| + for entry in "$root"/*; do |
| + [ "$(basename "$entry")" = infra ] || ln -s "$entry" "$tree/" |
| + done |
| + (cd "$root" && tar --exclude=.terraform --exclude=.terraform.lock.hcl -cf - infra) | |
| + (cd "$tree" && tar -xf -) |
| + |
| + cd "$tree" |
| + find infra -name '*.tftest.hcl' | LC_ALL=C sort >"$run/files" |
| + if [ ! -s "$run/files" ]; then |
| + echo "no *.tftest.hcl under infra/: the walk is broken, or the tests moved" >&2 |
| + exit 1 |
| + fi |
| + |
| + # jobs: one line per test file. suites: one per (module, test dir). |
| + : >"$run/jobs" |
| + : >"$run/suites" |
| + while IFS= read -r file; do |
| + module=$(module_of "$file") |
| + rel=${file#"$module"/} |
| + testdir=$(dirname "$rel") |
| + [ "$testdir" = . ] && testdir=tests |
| + key="$module $testdir" |
| + suite=$(grep -n -x -F "$key" "$run/suites" | cut -d: -f1 || true) |
| + if [ -z "$suite" ]; then |
| + echo "$key" >>"$run/suites" |
| + suite=$(wc -l <"$run/suites" | tr -d ' ') |
| + fi |
| + printf '%s %s %s %s\n' "$suite" "$module" "$testdir" "$rel" >>"$run/jobs" |
| + done <"$run/files" |
| + nfiles=$(wc -l <"$run/jobs" | tr -d ' ') |
| + nsuites=$(wc -l <"$run/suites" | tr -d ' ') |
| + echo "opentofu: $nfiles test files in $nsuites suites, $(tofu version | head -n 1)" |
| + |
| + start=$(date +%s) |
| + n=0 |
| + while read -r module testdir; do |
| + n=$((n + 1)) |
| + flag= |
| + [ "$testdir" = tests ] || flag=-test-directory=$testdir |
| + # shellcheck disable=SC2086 # $flag is empty or one word |
| + if ! (cd "$module" && TF_DATA_DIR="$run/data/$n" \ |
| + tofu init -backend=false -input=false -no-color $flag >"$run/logs/init-$n.log" 2>&1); then |
| + cat "$run/logs/init-$n.log" |
| + echo "opentofu: tofu init failed in $module ($testdir)" >&2 |
| + exit 1 |
| + fi |
| + done <"$run/suites" |
| + echo " initialised in $(($(date +%s) - start)) s" |
| + |
| + # Each file in its own process, OPENTOFU_JOBS at a time. |
| + OPENTOFU_RUN=$run |
| + export OPENTOFU_RUN |
| + awk '{ print NR }' "$run/jobs" | |
| + xargs -P "${OPENTOFU_JOBS:-8}" -n 1 "$root/.amadan/opentofu.sh" _one || true |
| + |
| + failed=0 |
| + n=0 |
| + while read -r suite module testdir rel; do |
| + n=$((n + 1)) |
| + log=$run/logs/$n.log |
| + rc=$(cat "$run/logs/$n.rc" 2>/dev/null || echo missing) |
| + secs=$(cat "$run/logs/$n.secs" 2>/dev/null || echo '?') |
| + summary=$(grep -E '^(Success|Failure)!' "$log" 2>/dev/null | tail -n 1 || true) |
| + if [ "$rc" = 0 ] && |
| + grep -q -x -F "$rel... pass" "$log" && |
| + echo "$summary" | grep -q -E '^Success! [1-9][0-9]* passed, 0 failed\.$'; then |
| + printf ' ok %4s s %s/%s %s\n' "$secs" "$module" "$rel" "$summary" |
| + else |
| + failed=$((failed + 1)) |
| + printf ' FAIL %4s s %s/%s exit %s %s\n' "$secs" "$module" "$rel" "$rc" "$summary" |
| + sed 's/^/ /' "$log" 2>/dev/null || true |
| + fi |
| + done <"$run/jobs" |
| + |
| + echo "opentofu: $nfiles files in $(($(date +%s) - start)) s" |
| + if [ "$failed" -ne 0 ]; then |
| + echo "opentofu: $failed of $nfiles test files did not pass (a file passes only with its own" >&2 |
| + echo "\"... pass\" line and \"Success! N passed, 0 failed.\", N > 0, so a skip fails too)" >&2 |
| + exit 1 |
| + fi |
| + echo "opentofu: PASS" |
| +} |
| + |
| +# One test file: the Nth line of the job list. |
| +one() { |
| + run=$OPENTOFU_RUN |
| + line=$(sed -n "${1}p" "$run/jobs") |
| + n=$1 |
| + # shellcheck disable=SC2086 # four space-separated fields, no spaces inside |
| + set -- $line |
| + suite=$1 module=$2 testdir=$3 rel=$4 |
| + flag= |
| + [ "$testdir" = tests ] || flag=-test-directory=$testdir |
| + t0=$(date +%s) |
| + rc=0 |
| + # shellcheck disable=SC2086 # $flag is empty or one word |
| + (cd "$run/tree/$module" && TF_DATA_DIR="$run/data/$suite" \ |
| + tofu test -no-color $flag -filter="$rel" >"$run/logs/$n.log" 2>&1) || rc=$? |
| + echo "$(($(date +%s) - t0))" >"$run/logs/$n.secs" |
| + echo "$rc" >"$run/logs/$n.rc" |
| +} |
| + |
| +case "${1:-}" in |
| +install) |
| + [ $# -eq 2 ] || { |
| + echo "usage: $0 install DIR" >&2 |
| + exit 2 |
| + } |
| + install "$2" |
| + ;; |
| +test) |
| + test_all |
| + ;; |
| +_one) |
| + one "$2" |
| + ;; |
| +'') |
| + dir=$(mktemp -d "${TMPDIR:-/tmp}/carlos-opentofu-XXXXXX") |
| + # test_all sets its own EXIT trap; the subshell keeps this one. |
| + trap 'rm -rf "$dir"' EXIT |
| + trap 'exit 1' INT TERM |
| + install "$dir" |
| + PATH=$dir/bin:$PATH |
| + TF_CLI_CONFIG_FILE=$dir/tofurc |
| + export PATH TF_CLI_CONFIG_FILE |
| + (test_all) |
| + ;; |
| +*) |
| + echo "usage: $0 [install DIR | test]" >&2 |
| + exit 2 |
| + ;; |
| +esac |