#!/usr/bin/env bats load test_helper # smoketest/lib/scenario.sh with a "VM" that is just a local shell in the # test's HOME, and a small repo of its own standing in for CoreDesktop setup() { common_setup export GIT_AUTHOR_NAME=Test GIT_AUTHOR_EMAIL=test@localhost export GIT_COMMITTER_NAME=Test GIT_COMMITTER_EMAIL=test@localhost # Two commits: install.sh writes which one ran, and what it read from stdin export SMOKETEST_REPO="$BATS_TEST_TMPDIR/repo" git init -q "$SMOKETEST_REPO" cd "$SMOKETEST_REPO" printf '%s\n' 'echo v1 > ~/installed' 'read -r line; echo "read: $line" >> ~/installed' > install.sh git add install.sh git commit -qm v1 V1="$(git rev-parse HEAD)" sed -i 's/v1/v2/' install.sh git commit -qam v2 V2="$(git rev-parse HEAD)" cd "$BATS_TEST_TMPDIR" export SMOKETEST_VM_DIR="$BATS_TEST_TMPDIR/vm" mkdir -p "$SMOKETEST_VM_DIR" export SMOKETEST_RESULTS="$BATS_TEST_TMPDIR/results" export SMOKETEST_SHARED="$BATS_TEST_TMPDIR/shared" mkdir -p "$SMOKETEST_RESULTS" "$SMOKETEST_SHARED" export SMOKETEST_REF="$V2" vm_exec() { bash -c "$1" < /dev/null; } vm_put() { cp "$1" "$2"; } source "$REPO/smoketest/lib/scenario.sh" } upstream_main() { git --git-dir="$UPSTREAM" rev-parse main } @test "prepare makes the fake upstream, at the commit under test" { smoketest_prepare [ "$(upstream_main)" = "$V2" ] } @test "pushing to the VM skips the working copy's pre-push hook" { printf '%s\n' '#!/bin/bash' 'echo hook ran; exit 1' > "$SMOKETEST_REPO/.git/hooks/pre-push" chmod +x "$SMOKETEST_REPO/.git/hooks/pre-push" smoketest_prepare [ "$(upstream_main)" = "$V2" ] } @test "prepare sends CoreDesktop's URLs to the fake upstream" { smoketest_prepare for url in "${COREDESKTOP_URLS[@]}"; do git clone -q "$url" "$BATS_TEST_TMPDIR/clone" [ "$(git -C "$BATS_TEST_TMPDIR/clone" rev-parse HEAD)" = "$V2" ] rm -rf "$BATS_TEST_TMPDIR/clone" done } @test "every URL install.sh has ever cloned from goes to the fake upstream" { local repo="$BATS_TEST_DIRNAME/.." url [ "$(git -C "$repo" rev-parse --is-shallow-repository)" = false ] \ || skip "needs install.sh's full history" while read -r url; do printf '%s\n' "${COREDESKTOP_URLS[@]}" | grep -qxF "$url" \ || { echo "install.sh has cloned from $url, which isn't in COREDESKTOP_URLS"; return 1; } done < <(git -C "$repo" log -p -- install.sh | grep -oE 'https?://[^ "]*coredesktop\.git' | sort -u) } @test "prepare can run twice" { smoketest_prepare smoketest_prepare [ "$(git config --global --get-all "url.$UPSTREAM.insteadOf" | wc -l)" -eq "${#COREDESKTOP_URLS[@]}" ] } @test "prepare on the installed image updates it to the commit under test" { export IMAGE=installed IMAGE_REF="$V1" mkdir -p "$HOME/.local/bin" printf '%s\n' '#!/bin/bash' 'echo "update ran with $(git --git-dir="$UPSTREAM" rev-parse main)"' \ > "$HOME/.local/bin/update" chmod +x "$HOME/.local/bin/update" export UPSTREAM run smoketest_prepare [ "$status" -eq 0 ] grep -qx "update ran with $V2" "$SMOKETEST_RESULTS/01-update.log" } @test "prepare on the installed image fails if the image isn't behind the commit under test" { export IMAGE=installed IMAGE_REF="$V2" SMOKETEST_REF="$V1" run smoketest_prepare [ "$status" -eq 1 ] [[ "$output" == *"isn't in the history of the commit under test"* ]] } @test "upstream_set moves main back and forth" { smoketest_prepare upstream_set "$V1" [ "$(upstream_main)" = "$V1" ] upstream_set "$V2" [ "$(upstream_main)" = "$V2" ] } @test "upstream_set fails on an unknown commit" { smoketest_prepare run upstream_set nosuchcommit [ "$status" -eq 1 ] [[ "$output" == *"no such commit: nosuchcommit"* ]] } @test "upstream_commit releases a commit made by its script" { smoketest_prepare upstream_commit "Add a file" 'echo hello > new-file' [ "$(git --git-dir="$UPSTREAM" rev-parse main^)" = "$V2" ] [ "$(git --git-dir="$UPSTREAM" show main:new-file)" = hello ] [ "$(git --git-dir="$UPSTREAM" log -1 --format=%s main)" = "Add a file" ] } @test "upstream_commit fails the scenario if its script fails" { smoketest_prepare run upstream_commit "Broken" 'false' [ "$status" -eq 1 ] [ "$(upstream_main)" = "$V2" ] } @test "cd_install runs main's install.sh, with nothing on stdin" { smoketest_prepare upstream_set "$V1" cd_install [ "$STATUS" -eq 0 ] [ "$(cat "$HOME/installed")" = "$(printf 'v1\nread: ')" ] } @test "run_in_vm keeps the output, status and a numbered log, and doesn't fail" { run_in_vm first 'echo one; exit 3' [ "$STATUS" -eq 3 ] [ "$OUTPUT" = one ] [ "$(cat "$SMOKETEST_RESULTS/01-first.log")" = one ] run_in_vm second 'echo two >&2' [ "$STATUS" -eq 0 ] [ "$OUTPUT" = two ] [ -f "$SMOKETEST_RESULTS/02-second.log" ] } @test "run_in_vm says once a tick that it's still running, with the log's latest line" { export SMOKETEST_TICK=1 run run_in_vm slow 'echo working; sleep 2.5' [ "$status" -eq 0 ] [[ "$output" == *" still running slow (0 min): working"* ]] [[ "$output" == *"== slow exited 0 (log: "* ]] } @test "run_in_vm writes its log while the script runs" { run_in_vm slow 'echo early; cat "$SMOKETEST_RESULTS/01-slow.log" > "$HOME/seen"' [ "$STATUS" -eq 0 ] [ "$(cat "$HOME/seen")" = early ] } @test "cd_update passes its variables to update" { mkdir -p "$HOME/.local/bin" printf '%s\n' '#!/bin/bash' 'echo "force=$UPDATE_FORCE"' > "$HOME/.local/bin/update" chmod +x "$HOME/.local/bin/update" cd_update UPDATE_FORCE=1 [ "$STATUS" -eq 0 ] [ "$OUTPUT" = "force=1" ] } @test "assert_status and assert_output fail with the log's path" { run_in_vm thing 'echo hello; exit 1' run assert_status 0 [ "$status" -eq 1 ] [[ "$output" == *"thing exited 1, expected 0"*"01-thing.log"* ]] run assert_output goodbye [ "$status" -eq 1 ] run assert_output hello [ "$status" -eq 0 ] run refute_output hello [ "$status" -eq 1 ] } @test "assert_vm and refute_vm" { run assert_vm "true passes" true [ "$status" -eq 0 ] run assert_vm "false fails" 'echo why; false' [ "$status" -eq 1 ] [[ "$output" == *why*"FAIL: false fails"* ]] run refute_vm "false passes" false [ "$status" -eq 0 ] run refute_vm "true fails" true [ "$status" -eq 1 ] } @test "assert_same_fingerprint passes on a match, and saves the diff when they differ" { printf 'a\nb\n' > "$BATS_TEST_TMPDIR/one" printf 'a\nb\n' > "$BATS_TEST_TMPDIR/two" printf 'a\nc\n' > "$BATS_TEST_TMPDIR/three" run assert_same_fingerprint "$BATS_TEST_TMPDIR/one" "$BATS_TEST_TMPDIR/two" [ "$status" -eq 0 ] run assert_same_fingerprint "$BATS_TEST_TMPDIR/one" "$BATS_TEST_TMPDIR/three" [ "$status" -eq 1 ] grep -qx -- '-b' "$SMOKETEST_RESULTS/fingerprint.diff" grep -qx -- '+c' "$SMOKETEST_RESULTS/fingerprint.diff" } @test "skip exits with the skip status" { run skip "not today" [ "$status" -eq "$SKIP_STATUS" ] [[ "$output" == *"SKIP: not today"* ]] } @test "every scenario sets a description, image and tier, and defines scenario" { for file in "$REPO"/smoketest/scenarios/*.sh; do ( unset DESCRIPTION IMAGE TIER unset -f scenario source "$file" [ -n "$DESCRIPTION" ] || { echo "$file: no DESCRIPTION"; exit 1; } [[ "$IMAGE" == base || "$IMAGE" == installed ]] || { echo "$file: IMAGE is '$IMAGE'"; exit 1; } [[ "$TIER" == pr || "$TIER" == nightly ]] || { echo "$file: TIER is '$TIER'"; exit 1; } declare -f scenario > /dev/null || { echo "$file: no scenario function"; exit 1; } ) done } @test "every baseline is a commit in this repo" { sed 's/#.*//' "$REPO/smoketest/baselines" | awk 'NF { print $1 }' | while read -r ref; do git -C "$REPO" rev-parse --verify --quiet "$ref^{commit}" > /dev/null || { echo "unknown: $ref"; exit 1; } done } # --- Sibling projects --- # A sibling project, HamPack, with two commits, and a chain file with # CoreDesktop and it: CoreDesktop at V1 in the image and V2 under test, # HamPack likewise at S1 and S2 chain_with_sibling() { SIB="$BATS_TEST_TMPDIR/HamPack" git init -q "$SIB" echo s1 > "$SIB/version" git -C "$SIB" add version git -C "$SIB" commit -qm s1 S1="$(git -C "$SIB" rev-parse HEAD)" echo s2 > "$SIB/version" git -C "$SIB" commit -qam s2 S2="$(git -C "$SIB" rev-parse HEAD)" export SMOKETEST_CHAIN_FILE="$BATS_TEST_TMPDIR/chain" printf 'CoreDesktop\t%s\t%s\t%s\t\n' "$SMOKETEST_REPO" "$V2" "$V1" > "$SMOKETEST_CHAIN_FILE" printf 'HamPack\t%s\t%s\t%s\t%s\n' "$SIB" "$S2" "$S1" \ "https://example.com/david/HamPack.git https://example.org/HamPack.git" >> "$SMOKETEST_CHAIN_FILE" SIB_UPSTREAM="$SMOKETEST_VM_DIR/HamPack.git" # A fake `sibling add`, which clones like the real one, and update and reboot mkdir -p "$HOME/.local/bin" printf '%s\n' '#!/bin/bash' 'echo "sibling $*" >> ~/vm.log' \ 'git clone -q "$2" ~/.local/share/siblings/"$(basename "${2%.git}")"' > "$HOME/.local/bin/sibling" printf '%s\n' '#!/bin/bash' 'echo update >> ~/vm.log' > "$HOME/.local/bin/update" chmod +x "$HOME/.local/bin/sibling" "$HOME/.local/bin/update" vm_reboot() { echo reboot >> "$HOME/vm.log"; } } @test "without a chain file, the chain is CoreDesktop at the commit under test" { [ "$(chain_names)" = CoreDesktop ] [ "$(project_sha CoreDesktop)" = "$V2" ] [ -z "$(chain_siblings)" ] } @test "prepare makes a fake upstream for each project, and sends its URLs there" { chain_with_sibling smoketest_prepare [ "$(upstream_main)" = "$V2" ] [ "$(git --git-dir="$SIB_UPSTREAM" rev-parse main)" = "$S2" ] for url in https://example.com/david/HamPack.git https://example.com/david/HamPack https://example.org/HamPack.git; do git clone -q "$url" "$BATS_TEST_TMPDIR/clone" [ "$(cat "$BATS_TEST_TMPDIR/clone/version")" = s2 ] rm -rf "$BATS_TEST_TMPDIR/clone" done } @test "prepare on the installed image checks every project's image commit, then updates once" { chain_with_sibling export IMAGE=installed run smoketest_prepare [ "$status" -eq 0 ] [ "$(cat "$HOME/vm.log")" = update ] printf 'HamPack\t%s\t%s\t%s\t%s\n' "$SIB" "$S1" "$S2" https://example.com/HamPack.git \ > "$BATS_TEST_TMPDIR/hampack-line" sed -i '2d' "$SMOKETEST_CHAIN_FILE" cat "$BATS_TEST_TMPDIR/hampack-line" >> "$SMOKETEST_CHAIN_FILE" run smoketest_prepare [ "$status" -eq 1 ] [[ "$output" == *"the installed image has HamPack at $S2, which isn't in the history"* ]] } @test "upstream_set and upstream_commit take a project's name" { chain_with_sibling smoketest_prepare upstream_set HamPack "$S1" [ "$(git --git-dir="$SIB_UPSTREAM" rev-parse main)" = "$S1" ] [ "$(upstream_main)" = "$V2" ] upstream_commit HamPack "Bump" 'echo s3 > version' [ "$(git --git-dir="$SIB_UPSTREAM" show main:version)" = s3 ] [ "$(upstream_main)" = "$V2" ] run upstream_set HamPack nosuchcommit [[ "$output" == *"no such commit in HamPack: nosuchcommit"* ]] run upstream_commit Nope "x" true [ "$status" -eq 1 ] [[ "$output" == *"Nope isn't in this run's chain"* ]] } @test "building a sibling's layer adds it at its commit, over the others at their image's, and reboots" { chain_with_sibling smoketest_build_layer HamPack "$S2" [ "$(upstream_main)" = "$V1" ] [ "$(cat "$HOME/.local/share/siblings/HamPack/version")" = s2 ] [ "$(cat "$HOME/vm.log")" = "$(printf 'sibling add https://example.com/david/HamPack.git\nreboot')" ] } @test "building CoreDesktop's layer runs install.sh at its commit" { vm_reboot() { echo reboot >> "$HOME/vm.log"; } smoketest_build_layer CoreDesktop "$V1" [ "$(cat "$HOME/vm.log")" = reboot ] [ "$(head -n 1 "$HOME/installed")" = v1 ] } @test "install_chain installs CoreDesktop, reboots, adds each sibling and reboots again" { chain_with_sibling smoketest_prepare install_chain [ "$(cat "$HOME/vm.log")" = "$(printf 'reboot\nsibling add https://example.com/david/HamPack.git\nreboot')" ] [ "$(head -n 1 "$HOME/installed")" = v2 ] } @test "a project's setup script runs in the VM before the scenario" { chain_with_sibling mkdir -p "$SIB/smoketest" echo 'echo set up > ~/setup-ran' > "$SIB/smoketest/setup" smoketest_prepare [ "$(cat "$HOME/setup-ran")" = "set up" ] echo 'exit 1' > "$SIB/smoketest/setup" run smoketest_prepare [ "$status" -eq 1 ] [[ "$output" == *"setup-HamPack exited 1"* ]] } @test "health checks CoreDesktop, and each sibling that's installed and has health checks" { chain_with_sibling mkdir -p "$SIB/smoketest" echo 'echo hampack healthy' > "$SIB/smoketest/health" SMOKETEST_DIR="$BATS_TEST_TMPDIR/cd-smoketest" mkdir -p "$SMOKETEST_DIR" echo 'echo coredesktop healthy' > "$SMOKETEST_DIR/health" run health [ "$status" -eq 0 ] [[ "$output" == *"coredesktop healthy"* ]] [[ "$output" != *"hampack healthy"* ]] mkdir -p "$HOME/.local/share/siblings/HamPack" run health [[ "$output" == *"coredesktop healthy"*"hampack healthy"* ]] echo 'exit 1' > "$SIB/smoketest/health" run health [ "$status" -eq 1 ] [[ "$output" == *"the health checks failed for: HamPack"* ]] run health CoreDesktop [ "$status" -eq 0 ] }