#!/bin/bash # # Purpose : Helpers and assertions for smoketest scenarios (see ../README.md). # Sourced by the driver after it defines vm_exec, vm_put and # vm_reboot. # # A run tests a chain of projects: CoreDesktop, then any sibling projects # (HamPack, say) the driver was asked for. The driver describes the chain in # the file SMOKETEST_CHAIN_FILE, one line per project, tab-separated: # # name working copy commit under test commit in the image URLs (by spaces) # # CoreDesktop comes first, with no URLs: it has COREDESKTOP_URLS. Without a # chain file, the chain is CoreDesktop alone, at SMOKETEST_REF, in an image at # IMAGE_REF, as drivers before sibling projects described it. SMOKETEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # The CoreDesktop repo whose commits are tested. Overridable for the tests. SMOKETEST_REPO="${SMOKETEST_REPO:-$(cd "$SMOKETEST_DIR/.." && pwd)}" # In the VM: the fake upstreams, the scratch clone upstream_commit works in, and # where the fingerprint, health and setup scripts are copied to SMOKETEST_VM_DIR="${SMOKETEST_VM_DIR:-/srv/smoketest}" UPSTREAM="$SMOKETEST_VM_DIR/coredesktop.git" WORK="$SMOKETEST_VM_DIR/work" # The chain's first project ROOT_PROJECT=CoreDesktop # Every URL CoreDesktop has been cloned from, so install.sh at any baseline # clones the fake upstream instead COREDESKTOP_URLS=( https://gitea.federated.computer/Federated_Computer/coredesktop.git https://gitea.federated.computer/david/coredesktop.git https://gitea.young.computer/david/coredesktop.git ) # Exit status of a scenario that skipped itself SKIP_STATUS=3 # Output, exit status, name and log file of the last run_in_vm OUTPUT="" STATUS=0 LAST_RUN="" LAST_LOG="" RUN_COUNT=0 q() { printf '%q' "$1" } fail() { echo "FAIL: $*" >&2 exit 1 } skip() { echo "SKIP: $*" exit "$SKIP_STATUS" } # --- The chain --------------------------------------------------------------- # The chain file's lines, or CoreDesktop's alone without one chain_lines() { if [ -n "${SMOKETEST_CHAIN_FILE:-}" ]; then cat "$SMOKETEST_CHAIN_FILE" else printf '%s\t%s\t%s\t%s\t\n' "$ROOT_PROJECT" "$SMOKETEST_REPO" "${SMOKETEST_REF:-}" "${IMAGE_REF:-}" fi } # The projects in the chain, CoreDesktop first, one per line chain_names() { chain_lines | cut -f1 } # The sibling projects in the chain, one per line chain_siblings() { chain_names | tail -n +2 } # chain_field NAME COLUMN: 2 working copy, 3 commit under test, 4 commit in # the image, 5 URLs chain_field() { chain_lines | awk -F'\t' -v n="$1" -v c="$2" '$1 == n { print $c }' } project_dir() { chain_field "$1" 2; } project_sha() { chain_field "$1" 3; } image_sha() { chain_field "$1" 4; } # project_urls NAME: the URLs project NAME is cloned from, one per line project_urls() { if [ "$1" = "$ROOT_PROJECT" ]; then printf '%s\n' "${COREDESKTOP_URLS[@]}" else chain_field "$1" 5 | tr ' ' '\n' | grep . fi } # The URL a sibling project is added from (its first) project_url() { project_urls "$1" | head -n 1 } # True if project NAME is in this run's chain in_run() { chain_names | grep -qxF "$1" } # upstream NAME: the project's fake upstream in the VM upstream() { if [ "$1" = "$ROOT_PROJECT" ]; then echo "$UPSTREAM" else echo "$SMOKETEST_VM_DIR/$1.git" fi } # sibling_name NAME: the folder in ~/.local/share/siblings that `sibling add` # clones project NAME into: its URL's last part, without .git sibling_name() { local name name="$(project_url "$1")" name="${name%/}" name="${name##*/}" echo "${name%.git}" } # True if sibling project NAME is installed in the VM sibling_installed() { vm_exec "test -d ~/.local/share/siblings/$(q "$(sibling_name "$1")")" > /dev/null 2>&1 } # --- Running things in the VM ------------------------------------------------ # run_in_vm NAME SCRIPT: run SCRIPT in the VM, keeping its output in $OUTPUT, # its exit status in $STATUS, and both in NN-NAME.log. Doesn't fail on its own: # check with assert_status. The log is written as SCRIPT runs, so a long one # (install.sh takes an hour or more) can be followed with tail -f, and once a # minute run_in_vm says it's still running, with the log's latest line. run_in_vm() { local ticker RUN_COUNT=$((RUN_COUNT + 1)) LAST_RUN="$1" LAST_LOG="$SMOKETEST_RESULTS/$(printf '%02d' "$RUN_COUNT")-$1.log" echo "== $1 (to follow it: tail -f $LAST_LOG)" : > "$LAST_LOG" progress_ticker "$1" "$LAST_LOG" & ticker=$! vm_exec "$2" > "$LAST_LOG" 2>&1 && STATUS=0 || STATUS=$? kill "$ticker" 2> /dev/null wait "$ticker" 2> /dev/null || true OUTPUT="$(cat "$LAST_LOG")" echo "== $1 exited $STATUS (log: $LAST_LOG)" } # progress_ticker NAME LOG: every SMOKETEST_TICK seconds (60), say NAME is # still running, for how long, and LOG's latest line. run_in_vm kills it. # sleep gets no stdout, so a killed ticker leaves nothing holding the driver's # pipe to tee open. progress_ticker() { local start=$SECONDS tick="${SMOKETEST_TICK:-60}" last while sleep "$tick" > /dev/null 2>&1 3>&-; do last="$(tail -c 4096 "$2" | tr '\r' '\n' | grep -v '^[[:space:]]*$' | tail -n 1 | cut -c 1-100)" echo " still running $1 ($(( (SECONDS - start) / 60 )) min)${last:+: $last}" done } # --- Fake upstreams ---------------------------------------------------------- # Make a bare repo in the VM for each project in the chain, and have git send # the project's real URLs there, so install.sh, sibling add and update run # unchanged setup_upstreams() { local script="set -e git config --global user.name 'CoreDesktop Smoketest' git config --global user.email smoketest@localhost" name url up while read -r name; do up="$(q "$(upstream "$name")")" script+=" [ -d $up ] || git init -q --bare --initial-branch=main $up git config --global --unset-all url.$up.insteadOf || true" while read -r url; do script+=" git config --global --add url.$up.insteadOf $(q "$url")" # A sibling may be added by its URL with or without .git; each is matched # as a prefix, and the longer wins if [ "$name" != "$ROOT_PROJECT" ] && [[ "$url" == *.git ]]; then script+=" git config --global --add url.$up.insteadOf $(q "${url%.git}")" fi done < <(project_urls "$name") done < <(chain_names) vm_exec "$script" || fail "couldn't set up the fake upstreams in the VM" } # upstream_set [NAME] REF: point project NAME's (default CoreDesktop's) fake # upstream main at REF, a commit in its working copy upstream_set() { local name="$ROOT_PROJECT" dir sha if (( $# > 1 )); then name="$1" shift fi dir="$(project_dir "$name")" [ -n "$dir" ] || fail "$name isn't in this run's chain" if ! sha="$(git -C "$dir" rev-parse --verify --quiet "$1^{commit}")"; then [ "$name" = "$ROOT_PROJECT" ] && fail "no such commit: $1" fail "no such commit in $name: $1" fi # --no-verify: a pre-push hook in the working copy (coredesktop-dev's runs # make check) is for pushes to the real upstream, not to the VM GIT_SSH_COMMAND="${VM_GIT_SSH:-ssh}" git -C "$dir" push --quiet --force --no-verify \ "${VM_GIT_URL:-}$(upstream "$name")" "$sha:refs/heads/main" || fail "couldn't push $name $1 to the VM" } # upstream_commit [NAME] MESSAGE SCRIPT: release a new commit of project NAME # (default CoreDesktop), made by running SCRIPT (in the VM) in a clone of its # fake upstream's main upstream_commit() { local name="$ROOT_PROJECT" if (( $# > 2 )); then name="$1" shift fi in_run "$name" || fail "$name isn't in this run's chain" run_in_vm commit "set -e rm -rf $(q "$WORK") git clone -q $(q "$(upstream "$name")") $(q "$WORK") cd $(q "$WORK") $2 git add -A git commit -qm $(q "$1") git push -q origin HEAD:main" assert_status 0 } # Run each project's smoketest/setup in the VM, if it has one: what a project # needs set up on any machine it's tested on (HamPack's turns off its Windows # installers, which wait for clicks no one makes) run_setup_scripts() { local name script while read -r name; do script="$(project_dir "$name")/smoketest/setup" [ -f "$script" ] || continue vm_put "$script" "$SMOKETEST_VM_DIR/setup-$name" || fail "couldn't copy $name's setup script to the VM" run_in_vm "setup-$name" "bash $(q "$SMOKETEST_VM_DIR/setup-$name")" assert_status 0 done < <(chain_names) } # --- Called by the driver ---------------------------------------------------- # Once the VM is up, before the scenario: point each fake upstream at the # commit under test. On the installed image, update to it. smoketest_prepare() { local name image setup_upstreams run_setup_scripts while read -r name; do if [ "${IMAGE:-}" = installed ]; then image="$(image_sha "$name")" git -C "$(project_dir "$name")" merge-base --is-ancestor "$image" "$(project_sha "$name")" \ || fail "the installed image has $name at $image, which isn't in the history of the" \ "commit under test. Rebuild it: coredesktop-dev smoketest images installed" \ "(with the same --with options)" fi upstream_set "$name" "$(project_sha "$name")" done < <(chain_names) if [ "${IMAGE:-}" = installed ]; then cd_update assert_status 0 fi } # To build an image layer: install project NAME at SHA on a machine that has # the projects before it in the chain, then reboot, as the READMEs say to smoketest_build_layer() { local name="$1" sha="$2" other setup_upstreams run_setup_scripts while read -r other; do upstream_set "$other" "$(image_sha "$other")" done < <(chain_names | grep -vxF "$name") upstream_set "$name" "$sha" if [ "$name" = "$ROOT_PROJECT" ]; then cd_install else sibling_add "$name" fi assert_status 0 vm_reboot } # For drivers from before smoketest_build_layer: the installed image from the # base image smoketest_install_image() { smoketest_prepare upstream_set "$1" cd_install assert_status 0 } # --- What a person would run ------------------------------------------------- # Run install.sh the way the README's wget ... | bash does: main's install.sh, # with no terminal to ask questions on cd_install() { run_in_vm install "git --git-dir=$(q "$UPSTREAM") show main:install.sh > ~/install.sh \ && cd && TERM=dumb bash ~/install.sh" } # cd_update [VAR=value...]: run update, with those variables set # Scenarios pass the variables, e.g. 44-low-disk.sh # shellcheck disable=SC2120 cd_update() { local vars="" var for var in "$@"; do vars+=" $(q "$var")" done run_in_vm update "TERM=dumb env$vars ~/.local/bin/update" } # sibling_add NAME: add sibling project NAME from its (real) URL, as its README # says to sibling_add() { in_run "$1" || fail "$1 isn't in this run's chain" run_in_vm "sibling-add-$1" "TERM=dumb ~/.local/bin/sibling add $(q "$(project_url "$1")")" } # Install CoreDesktop and every sibling in the chain at the commits under test, # as a person would on a fresh machine, rebooting where the READMEs say to install_chain() { local name added=0 cd_install assert_status 0 vm_reboot while read -r name; do sibling_add "$name" assert_status 0 added=1 done < <(chain_siblings) (( added == 0 )) || vm_reboot } # fingerprint FILE: save the machine's fingerprint to FILE fingerprint() { vm_put "$SMOKETEST_DIR/fingerprint" "$SMOKETEST_VM_DIR/fingerprint" \ || fail "couldn't copy the fingerprint script to the VM" vm_exec "bash $(q "$SMOKETEST_VM_DIR/fingerprint")" > "$1" || fail "fingerprint failed (see $1)" } # Where 10-fresh-install saves the fingerprint of a fresh install of the # commit under test reference_fingerprint() { echo "$SMOKETEST_SHARED/fresh-install.fingerprint" } # health [NAME...]: run the health checks of the projects named (default: # CoreDesktop, and each sibling in the chain that's installed in the VM and has # a smoketest/health); fail the scenario if any fails health() { local names=("$@") name script dest log failed=() if (( ${#names[@]} == 0 )); then names=("$ROOT_PROJECT") while read -r name; do [ -f "$(project_dir "$name")/smoketest/health" ] && sibling_installed "$name" && names+=("$name") done < <(chain_siblings) fi for name in "${names[@]}"; do if [ "$name" = "$ROOT_PROJECT" ]; then script="$SMOKETEST_DIR/health" dest="$SMOKETEST_VM_DIR/health" log=health else script="$(project_dir "$name")/smoketest/health" dest="$SMOKETEST_VM_DIR/health-$name" log="health-$name" [ -f "$script" ] || fail "$name has no smoketest/health" fi vm_put "$script" "$dest" || fail "couldn't copy $name's health script to the VM" run_in_vm "$log" "bash $(q "$dest")" printf '%s\n' "$OUTPUT" [ "$STATUS" -eq 0 ] || failed+=("$name") done (( ${#failed[@]} == 0 )) || fail "the health checks failed for: ${failed[*]}" } assert_status() { [ "$STATUS" -eq "$1" ] || fail "$LAST_RUN exited $STATUS, expected $1 (see $LAST_LOG)" } assert_output() { [[ "$OUTPUT" == *"$1"* ]] || fail "$LAST_RUN's output doesn't contain \"$1\" (see $LAST_LOG)" } refute_output() { [[ "$OUTPUT" != *"$1"* ]] || fail "$LAST_RUN's output contains \"$1\" (see $LAST_LOG)" } # assert_vm DESCRIPTION SCRIPT: SCRIPT must succeed in the VM assert_vm() { local out out="$(vm_exec "$2" 2>&1)" || { printf '%s\n' "$out"; fail "$1"; } echo "ok: $1" } # refute_vm DESCRIPTION SCRIPT: SCRIPT must fail in the VM refute_vm() { local out if out="$(vm_exec "$2" 2>&1)"; then printf '%s\n' "$out" fail "$1" fi echo "ok: $1" } # vm_value SCRIPT: print SCRIPT's output, failing the scenario if SCRIPT fails vm_value() { vm_exec "$1" || fail "couldn't run in the VM: $1" } # assert_same_fingerprint EXPECTED ACTUAL: the fingerprint files must match assert_same_fingerprint() { local diff="$SMOKETEST_RESULTS/fingerprint.diff" if ! diff -u --label expected --label actual "$1" "$2" > "$diff"; then cat "$diff" fail "the machine differs from $1 (diff: $diff)" fi echo "ok: same fingerprint as $1" }