-
CAUTION
-------
-The programs here destroy data on your disks!
+RUN THESE TESTS IN A SCRATCH MACHINE.
+
+The tests in this directory only work when run as the root user.
+
+They operate
+- on a scratch BTRFS filesystem (`/testsuite`)
+- created in a loopback-mounted file (`/testsuite.img`),
+- using a scratch Snapper configuration (`testsuite`).
+
+However bugs do happen so it is recommended that you
+run these tests in a scratch machine. A bug may cause
+LOSS OR CORRUPTION OF YOUR OWN BTRFS SNAPSHOTS AND DATA.
+See `setup-and-run-all` for the scratch setup and `run-all` for the actual
+test set.
+++ /dev/null
-
-To run testsuite:
-
-- read the CAUTION file
-
-- create subvolume /testsuite
-
- btrfs subvolume create /testsuite
-
-- create snapper config for testsuite
-
- snapper --config=testsuite create-config /testsuite
-
-- run test
-
- ./run-all
-
--- /dev/null
+#!/bin/bash
+set -eu
+set -x
+if [ "$(id -u)" != "0" ]; then
+ set +x
+ echo "These tests only work when run as root." >&2
+ echo "Yes. See also the CAUTION file." >&2
+ exit 1
+fi
+
+CFG=testsuite
+MNT=/"$CFG"
+#CFG=scratch2
+#MNT=~/"$CFG"-btrfs
+IMG="$MNT".img
+
+forget_config() {
+ sed -i -e "/^SNAPPER_CONFIGS=/s/\b${CFG}\b//" /etc/sysconfig/snapper
+}
+
+setup() {
+ if [ -f "$IMG" ]; then
+ set +x
+ echo >&2 "The image '$IMG' already exists, aborting setup."
+ echo >&2 "Probably a previous test run failed."
+ echo >&2 "After you inspect the artifacts, run '$0 teardown' to clean up."
+ exit 1
+ fi
+ dd if=/dev/zero of="$IMG" bs=1M count=110
+ LOOP=$(losetup --find)
+ losetup "$LOOP" "$IMG"
+ mkfs.btrfs "$LOOP"
+ mkdir -p "$MNT"
+ mount "$IMG" "$MNT"
+ forget_config
+ snapper --no-dbus --config="$CFG" create-config "$MNT"
+}
+
+main() {
+ # read -p "Enter to CONTINUE, Ctrl-C to exit:"
+ ./run-all
+}
+
+teardown() {
+ snapper --no-dbus --config="$CFG" delete-config || forget_config
+ if grep "$MNT" /proc/mounts >/dev/null; then
+ umount "$MNT"
+ fi
+ if [ -d "$MNT" ]; then
+ rmdir "$MNT"
+ fi
+ # mapfile is better than VAR=($(cmd))
+ # https://github.com/koalaman/shellcheck/wiki/SC2207
+ mapfile -t LOOPS < <(losetup --associated "$IMG" | cut -f1 -d:)
+ if [ ${#LOOPS[@]} != 0 ]; then
+ losetup --detach "${LOOPS[@]}"
+ fi
+ rm -f "$IMG"
+}
+
+all() {
+ setup
+ main
+ teardown
+}
+
+ACTION=${1-all}
+$ACTION