#!/usr/bin/env bash set -euo pipefail # Runs bootstrap.sh against a stub `pct` (existing CT 501) and checks that the # host keeps no files and the CT keeps no installer files. Needs root (Linux/Docker). [[ $EUID == 0 ]] || { echo 'bootstrap test skipped: needs root (run it in a Debian container)'; exit 0; } repo=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) test_dir=$(mktemp -d) trap 'rm -rf -- "$test_dir"' EXIT mkdir "$test_dir/bin" "$test_dir/tmp" "$test_dir/ct" # Archive of the working tree, shaped like Gitea's (one top-level folder). tar -C "$repo/.." -czf "$test_dir/archive.tar.gz" --exclude=node_modules --exclude=.git "$(basename "$repo")/deploy" cat > "$test_dir/bin/pct" <<'STUB' #!/usr/bin/env bash log=$STUB_DIR/pct.log; ct=$STUB_DIR/ct echo "$*" >> "$log" case $1 in config) printf 'arch: amd64\nfeatures: nesting=1\nostype: debian\nunprivileged: 1\nlxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file\n' ;; status) echo 'status: running' ;; start|set|shutdown) ;; push) cp "$3" "$ct/$(basename "$4")" ;; exec) shift 3 if [[ $1 == dpkg ]]; then echo amd64 elif [[ $1 == bash && $2 == -c ]]; then # Run the real cleanup wrapper with /root mapped into the fake CT. script=${3//\/root\//$ct/}; script=${script//bash $ct\/shellbook-install.sh/echo installer-ran} bash -c "$script" fi ;; esac STUB printf '#!/usr/bin/env bash\necho amd64\n' > "$test_dir/bin/dpkg" chmod +x "$test_dir/bin/pct" "$test_dir/bin/dpkg" out=$(PATH="$test_dir/bin:$PATH" STUB_DIR="$test_dir" TMPDIR="$test_dir/tmp" CTID=501 SHELLBOOK_ARCHIVE_URL="file://$test_dir/archive.tar.gz" \ bash "$repo/deploy/proxmox/bootstrap.sh") [[ $out == *installer-ran* ]] || { echo "installer did not run: $out" >&2; exit 1; } [[ -z $(ls -A "$test_dir/tmp") ]] || { echo "host temp files left: $(ls -A "$test_dir/tmp")" >&2; exit 1; } [[ -z $(ls -A "$test_dir/ct") ]] || { echo "CT files left: $(ls -A "$test_dir/ct")" >&2; exit 1; } grep -q '^push 501 .*/install.sh /root/shellbook-install.sh' "$test_dir/pct.log" || { echo 'installer not pushed' >&2; exit 1; } ! grep -q -- '--dev0' "$test_dir/pct.log" || { echo 'dev0 added despite TUN mount' >&2; exit 1; } printf '5 bootstrap checks passed\n'