A sibling project is a working copy beside CoreDesktop's with a smoketest/project.conf (NAME, PARENT, URLS), as in miniserver-dev. `run`, `list`, `shell` and `images installed` take --with NAME to add it to the chain; its scenarios are named Project/file, and run along with CoreDesktop's. Images are layered: installed+HamPack is the installed image after `sibling add HamPack`, and rebuilding a layer deletes those on it. The installed image keeps its name, and one built before this (with installed.ref) is still used. A CoreDesktop whose scenario.sh predates sibling projects still builds its installed image. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
325 lines
12 KiB
Bash
325 lines
12 KiB
Bash
#!/usr/bin/env bats
|
|
|
|
load test_helper
|
|
|
|
# The driver with fake QEMU: "VMs" are sleep processes, and ssh runs its command
|
|
# right here. The working copy has a small fake smoketest of its own.
|
|
|
|
setup() {
|
|
common_setup
|
|
make_working_copy
|
|
|
|
export KVM_DEVICE="$BATS_TEST_TMPDIR/kvm"
|
|
touch "$KVM_DEVICE"
|
|
export SMOKETEST_DESKTOP_TIMEOUT=0 SMOKETEST_SSH_TIMEOUT=5
|
|
STATE="$HOME/.cache/coredesktop-dev/smoketest"
|
|
mkdir -p "$STATE/images"
|
|
touch "$STATE/images/base.qcow2" "$STATE/images/installed.qcow2"
|
|
git -C "$WC" rev-parse HEAD > "$STATE/images/installed.ref"
|
|
|
|
# A VM is a sleep that lets go of bats' output pipes
|
|
stub qemu-system-x86_64 'while [ $# -gt 0 ]; do [ "$1" = -pidfile ] && pidfile="$2"; shift; done
|
|
/usr/bin/sleep 300 > /dev/null 2>&1 3>&- &
|
|
echo $! > "$pidfile"'
|
|
stub qemu-img 'touch "${@: -1}"'
|
|
stub xorriso
|
|
stub curl
|
|
stub ss
|
|
stub ssh-keygen 'touch "${@: -1}" "${@: -1}.pub"'
|
|
stub systemctl
|
|
# Like a real SSH session, the command gets no controlling terminal: the
|
|
# driver runs bash -i, which would otherwise try to take over the one bats
|
|
# runs in, and be stopped for it
|
|
stub ssh 'while [ $# -gt 0 ]; do
|
|
case "$1" in -i | -o | -p) shift 2 ;; *@*) shift; break ;; *) shift ;; esac
|
|
done
|
|
[ $# -gt 0 ] || exit 0
|
|
cd "$HOME" && exec setsid -w bash -c "$*"'
|
|
stub scp 'dest="${*: -1}"; cp "${@: -2:1}" "${dest#*:}"'
|
|
|
|
SC="$WC/smoketest/scenarios"
|
|
mkdir -p "$SC" "$WC/smoketest/lib"
|
|
cat > "$WC/smoketest/lib/scenario.sh" << 'EOF'
|
|
SKIP_STATUS=3
|
|
smoketest_prepare() {
|
|
echo "prepare $IMAGE $SMOKETEST_REF $IMAGE_REF"
|
|
[ -z "${SMOKETEST_CHAIN_FILE:-}" ] || cp "$SMOKETEST_CHAIN_FILE" "$HOME/run-chain"
|
|
}
|
|
smoketest_build_layer() {
|
|
echo "build_layer $1 $2" >> "$HOME/layers"
|
|
cp "$SMOKETEST_CHAIN_FILE" "$HOME/layer-chain-$1"
|
|
}
|
|
fail() { echo "FAIL: $*"; exit 1; }
|
|
skip() { echo "SKIP: $*"; exit 3; }
|
|
EOF
|
|
scenario_file 10-passes pr installed 'vm_exec "echo in the vm"; echo "ref=$SMOKETEST_REF"'
|
|
scenario_file 20-fails pr base 'false; echo "still going"'
|
|
scenario_file 30-variants nightly base 'echo "variant $VARIANT"' 'VARIANTS=(one two)'
|
|
scenario_file 40-skips pr base 'skip "not today"'
|
|
}
|
|
|
|
# scenario_file NAME TIER IMAGE BODY [EXTRA]
|
|
scenario_file() {
|
|
printf '%s\n' "DESCRIPTION=\"$1 does things\"" "TIER=$2" "IMAGE=$3" "${5:-}" \
|
|
"scenario() {" "$4" "}" > "$SC/$1.sh"
|
|
}
|
|
|
|
last_run() {
|
|
ls -d "$STATE/runs/"* | tail -n 1
|
|
}
|
|
|
|
@test "list shows each scenario's tier, image and description" {
|
|
run cdd smoketest list
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"10-passes"*"pr"*"installed"*"10-passes does things"* ]]
|
|
[[ "$output" == *"30-variants"*"nightly"*"base"*"(2 variants)"* ]]
|
|
}
|
|
|
|
@test "run runs the pr scenarios against HEAD and reports each" {
|
|
run cdd smoketest run
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"PASS 10-passes"* ]]
|
|
[[ "$output" == *"FAIL 20-fails"* ]]
|
|
[[ "$output" == *"SKIP 40-skips"* ]]
|
|
[[ "$output" != *"30-variants"* ]]
|
|
[[ "$output" == *"in the vm"* ]]
|
|
[[ "$output" == *"ref=$(git -C "$WC" rev-parse HEAD)"* ]]
|
|
[ -f "$(last_run)/summary" ]
|
|
}
|
|
|
|
@test "a scenario stops at its first failing command" {
|
|
run cdd smoketest run 20
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" != *"still going"* ]]
|
|
}
|
|
|
|
@test "run picks scenarios by name or number" {
|
|
run cdd smoketest run 10 40-skips
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"PASS 10-passes"* ]]
|
|
[[ "$output" == *"SKIP 40-skips"* ]]
|
|
[[ "$output" != *"20-fails"* ]]
|
|
}
|
|
|
|
@test "run stops on an unknown scenario before starting any VM" {
|
|
run cdd smoketest run 10 nosuch
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"No scenario called nosuch"* ]]
|
|
not_called qemu-system-x86_64
|
|
}
|
|
|
|
@test "each variant runs in its own VM" {
|
|
run cdd smoketest run 30
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"PASS 30-variants @ one"* ]]
|
|
[[ "$output" == *"PASS 30-variants @ two"* ]]
|
|
[ "$(grep -c '^qemu-system-x86_64 ' "$STUB_LOG")" -eq 2 ]
|
|
grep -q "variant two" "$(last_run)/30-variants@two/scenario.log"
|
|
}
|
|
|
|
@test "--tier all runs every scenario" {
|
|
run cdd smoketest run --tier all
|
|
[[ "$output" == *"30-variants @ one"* ]]
|
|
[[ "$output" == *"10-passes"* ]]
|
|
}
|
|
|
|
@test "the scenario boots a copy of its image, and gets the installed image's commit" {
|
|
run cdd smoketest run 10
|
|
[ "$status" -eq 0 ]
|
|
grep -q "^qemu-img create -q -f qcow2 -b $STATE/images/installed.qcow2 " "$STUB_LOG"
|
|
[[ "$output" == *"prepare installed $(git -C "$WC" rev-parse HEAD) $(cat "$STATE/images/installed.ref")"* ]]
|
|
}
|
|
|
|
@test "the VM is stopped and its disk deleted after each scenario" {
|
|
run cdd smoketest run 10 20
|
|
for dir in "$(last_run)"/*/; do
|
|
[ ! -e "$dir/qemu.pid" ]
|
|
[ ! -e "$dir/disk.qcow2" ]
|
|
done
|
|
}
|
|
|
|
@test "--keep leaves a failed scenario's VM running, and stops the run" {
|
|
run cdd smoketest run --keep 20 10
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"The VM is still running"* ]]
|
|
[[ "$output" != *"PASS 10-passes"* ]]
|
|
pid="$(cat "$(last_run)/20-fails/qemu.pid")"
|
|
kill -0 "$pid"
|
|
kill "$pid"
|
|
}
|
|
|
|
@test "run --ref tests another commit" {
|
|
first="$(git -C "$WC" rev-parse HEAD)"
|
|
git -C "$WC" commit -q --allow-empty -m second
|
|
run cdd smoketest run --ref "$first" 10
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"ref=$first"* ]]
|
|
}
|
|
|
|
@test "run stops without KVM" {
|
|
rm "$KVM_DEVICE"
|
|
run cdd smoketest run 10
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"Can't use $KVM_DEVICE"* ]]
|
|
}
|
|
|
|
@test "smoketest needs a working copy with a smoketest folder" {
|
|
rm -rf "$WC/smoketest"
|
|
run cdd smoketest list
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"has no smoketest/scenarios"* ]]
|
|
}
|
|
|
|
@test "images shows what's built" {
|
|
rm "$STATE/images/installed.qcow2"
|
|
run cdd smoketest images
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"base built"* ]]
|
|
[[ "$output" == *"installed not built"* ]]
|
|
}
|
|
|
|
@test "long waits say what they're waiting for, then again once a minute" {
|
|
run bash -c 'source "$1"; wait_begin "waiting for X"; wait_tick
|
|
SECONDS=61; wait_tick; wait_tick; SECONDS=125; wait_tick; wait_done' _ "$PROJECT/lib/vm.sh"
|
|
[ "$status" -eq 0 ]
|
|
[ "${lines[0]}" = "waiting for X..." ]
|
|
[ "${lines[1]}" = " still waiting for X (1 min)" ]
|
|
[ "${lines[2]}" = " still waiting for X (2 min)" ]
|
|
[[ "${lines[3]}" == " done after "*s ]]
|
|
[ "${#lines[@]}" -eq 4 ]
|
|
}
|
|
|
|
# --- Sibling projects ---
|
|
|
|
# A sibling project, HamPack, beside the working copy, with scenarios 10 and 20
|
|
make_sibling() {
|
|
SIB="$HOME/Projects/HamPack"
|
|
mkdir -p "$SIB/smoketest/scenarios"
|
|
printf '%s\n' NAME=HamPack PARENT=CoreDesktop 'URLS=(https://example.com/HamPack.git)' \
|
|
> "$SIB/smoketest/project.conf"
|
|
printf '%s\n' 'DESCRIPTION="HamPack updates"' TIER=pr IMAGE=installed 'scenario() {' 'echo hampack scenario' '}' \
|
|
> "$SIB/smoketest/scenarios/20-update.sh"
|
|
printf '%s\n' 'DESCRIPTION="HamPack installs"' TIER=nightly IMAGE=base 'scenario() {' 'true' '}' \
|
|
> "$SIB/smoketest/scenarios/15-install.sh"
|
|
git -C "$SIB" init -q
|
|
git -C "$SIB" add -A
|
|
git -C "$SIB" commit -qm HamPack
|
|
SIB_SHA="$(git -C "$SIB" rev-parse HEAD)"
|
|
# Building an image shuts its VM down with sudo systemctl poweroff
|
|
stub sudo '[ "$*" = "systemctl poweroff" ] || exit 0
|
|
for f in "$HOME"/.cache/coredesktop-dev/smoketest/build-*/qemu.pid; do kill "$(cat "$f")"; done'
|
|
}
|
|
|
|
@test "list --with adds a sibling's scenarios, as Project/file" {
|
|
make_sibling
|
|
run cdd smoketest list
|
|
[[ "$output" != *HamPack* ]]
|
|
run cdd smoketest list --with hampack
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"10-passes"* ]]
|
|
[[ "$output" == *"HamPack/20-update"*"pr"*"installed"*"HamPack updates"* ]]
|
|
}
|
|
|
|
@test "run --with builds the sibling's image on the installed one, and runs every project's scenarios in it" {
|
|
make_sibling
|
|
root="$(cat "$STATE/images/installed.ref")"
|
|
run cdd smoketest run --with HamPack 10 HamPack/20
|
|
[ "$status" -eq 0 ]
|
|
[ "$(cat "$HOME/layers")" = "build_layer HamPack $SIB_SHA" ]
|
|
# The layer is built with CoreDesktop as it is in the installed image
|
|
[ "$(cat "$HOME/layer-chain-HamPack")" = "$(printf 'CoreDesktop\t%s\t%s\t%s\t\nHamPack\t%s\t%s\t\t%s' \
|
|
"$WC" "$root" "$root" "$SIB" "$SIB_SHA" https://example.com/HamPack.git)" ]
|
|
[ "$(cat "$STATE/images/installed+HamPack.refs")" = "$(printf 'CoreDesktop %s\nHamPack %s' "$root" "$SIB_SHA")" ]
|
|
[[ "$output" == *"PASS 10-passes"* ]]
|
|
[[ "$output" == *"PASS HamPack/20-update"* ]]
|
|
[[ "$output" == *"hampack scenario"* ]]
|
|
[ "$(grep -c "^qemu-img create -q -f qcow2 -b $STATE/images/installed+HamPack.qcow2 " "$STUB_LOG")" -eq 2 ]
|
|
# Scenarios get the chain, with each project's commit under test and in the image
|
|
[ "$(cat "$HOME/run-chain")" = "$(printf 'CoreDesktop\t%s\t%s\t%s\t\nHamPack\t%s\t%s\t%s\t%s' \
|
|
"$WC" "$(git -C "$WC" rev-parse HEAD)" "$root" "$SIB" "$SIB_SHA" "$SIB_SHA" https://example.com/HamPack.git)" ]
|
|
|
|
# The next run uses it
|
|
: > "$HOME/layers"
|
|
run cdd smoketest run --with HamPack HamPack/20
|
|
[ "$status" -eq 0 ]
|
|
[ ! -s "$HOME/layers" ]
|
|
}
|
|
|
|
@test "a scenario number two projects share must be named with its project" {
|
|
make_sibling
|
|
scenario_file 20-update pr installed true
|
|
run cdd smoketest run --with HamPack 20
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"20 could be any of:"*"20-update"*"HamPack/20-update"* ]]
|
|
[[ "$output" == *"Name it as Project/file, e.g. CoreDesktop/20."* ]]
|
|
not_called qemu-system-x86_64
|
|
|
|
run cdd smoketest run --with HamPack CoreDesktop/20
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"Name it by its whole file name."* ]]
|
|
|
|
run cdd smoketest run --with HamPack CoreDesktop/20-update hampack/20
|
|
[ "$status" -eq 0 ]
|
|
[[ "$output" == *"PASS 20-update"*"PASS HamPack/20-update"* ]]
|
|
}
|
|
|
|
@test "--with needs a sibling of a project already in the chain" {
|
|
make_sibling
|
|
run cdd smoketest list --with Nope
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"No sibling project called Nope"* ]]
|
|
|
|
sed -i 's/PARENT=CoreDesktop/PARENT=MiniServer/' "$SIB/smoketest/project.conf"
|
|
run cdd smoketest list --with HamPack
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"HamPack is a sibling of MiniServer: add --with MiniServer before it."* ]]
|
|
}
|
|
|
|
@test "rebuilding the installed image deletes the images built on it" {
|
|
make_sibling
|
|
cdd smoketest run --with HamPack HamPack/20
|
|
[ -f "$STATE/images/installed+HamPack.qcow2" ]
|
|
run cdd smoketest images installed
|
|
[ "$status" -eq 0 ]
|
|
[ "$(cat "$HOME/layers" | tail -n 1)" = "build_layer CoreDesktop $(git -C "$WC" rev-parse HEAD)" ]
|
|
[ ! -e "$STATE/images/installed+HamPack.qcow2" ]
|
|
[ ! -e "$STATE/images/installed.ref" ]
|
|
[ "$(cat "$STATE/images/installed.refs")" = "CoreDesktop $(git -C "$WC" rev-parse HEAD)" ]
|
|
}
|
|
|
|
@test "images installed builds siblings at the commits given" {
|
|
make_sibling
|
|
first="$SIB_SHA"
|
|
git -C "$SIB" commit -q --allow-empty -m second
|
|
run cdd smoketest images installed --with HamPack "HamPack=$first"
|
|
[ "$status" -eq 0 ]
|
|
[ "$(cat "$HOME/layers")" = "build_layer HamPack $first" ]
|
|
|
|
run cdd smoketest images
|
|
[[ "$output" == *"installed+HamPack built"*"HamPack ${first:0:7} HamPack"* ]]
|
|
|
|
run cdd smoketest images installed --with HamPack "CoreDesktop=nosuch"
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"CoreDesktop has no commit nosuch"* ]]
|
|
}
|
|
|
|
@test "a CoreDesktop from before sibling projects still builds its installed image, but not a sibling's" {
|
|
make_sibling
|
|
sed -i '/^smoketest_build_layer() {/,/^}/d' "$WC/smoketest/lib/scenario.sh"
|
|
printf '%s\n' 'smoketest_install_image() { echo "install_image $1" >> "$HOME/layers"; }' \
|
|
>> "$WC/smoketest/lib/scenario.sh"
|
|
stub ssh 'echo "ssh $*" >> "$HOME/ssh.log"; while [ $# -gt 0 ]; do
|
|
case "$1" in -i | -o | -p) shift 2 ;; *@*) shift; break ;; *) shift ;; esac
|
|
done
|
|
[ $# -gt 0 ] || exit 0
|
|
case "$*" in *boot_id*) echo "boot-$RANDOM" ; exit 0 ;; esac
|
|
cd "$HOME" && exec setsid -w bash -c "$*"'
|
|
run cdd smoketest images installed
|
|
[ "$status" -eq 0 ]
|
|
[ "$(cat "$HOME/layers")" = "install_image $(git -C "$WC" rev-parse HEAD)" ]
|
|
|
|
run cdd smoketest images installed --with HamPack
|
|
[ "$status" -eq 1 ]
|
|
[[ "$output" == *"can't add sibling projects"* ]]
|
|
}
|