|
| 1 | +# Common helpers for LTFS integration tests. |
| 2 | +# |
| 3 | +# Tests run against the build tree (no installation needed) using the |
| 4 | +# file tape backend, which emulates a tape drive in a plain directory. |
| 5 | +# |
| 6 | +# A test script sources this file, calls ltfs_setup, performs its checks |
| 7 | +# under $MNT, then calls ltfs_finish (unmount + ltfsck). Cleanup of |
| 8 | +# mounts and temporary files is handled by an EXIT trap. |
| 9 | + |
| 10 | +set -eu |
| 11 | + |
| 12 | +SKIP=77 |
| 13 | + |
| 14 | +: "${top_builddir:?top_builddir must be set (run via make check)}" |
| 15 | +: "${top_srcdir:?top_srcdir must be set (run via make check)}" |
| 16 | + |
| 17 | +LTFS_BIN="$top_builddir/src/ltfs" |
| 18 | +MKLTFS_BIN="$top_builddir/src/utils/mkltfs" |
| 19 | +LTFSCK_BIN="$top_builddir/src/utils/ltfsck" |
| 20 | + |
| 21 | +# Extra mount options for the ltfs invocation; tests may override. |
| 22 | +LTFS_MOUNT_OPTS="${LTFS_MOUNT_OPTS:-}" |
| 23 | + |
| 24 | +WORK= |
| 25 | +LTFS_PID= |
| 26 | +MNT= |
| 27 | +TAPE= |
| 28 | + |
| 29 | +skip() { |
| 30 | + echo "SKIP: $*" |
| 31 | + exit "$SKIP" |
| 32 | +} |
| 33 | + |
| 34 | +fail() { |
| 35 | + echo "FAIL: $*" >&2 |
| 36 | + exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +ltfs_check_env() { |
| 40 | + [ "$(uname -s)" = "Linux" ] || skip "FUSE integration tests only run on Linux" |
| 41 | + [ -e /dev/fuse ] || skip "/dev/fuse is not available" |
| 42 | + command -v fusermount3 >/dev/null 2>&1 || command -v fusermount >/dev/null 2>&1 \ |
| 43 | + || [ "$(id -u)" = "0" ] || skip "fusermount is not available" |
| 44 | + [ -x "$LTFS_BIN" ] || fail "ltfs binary not found at $LTFS_BIN" |
| 45 | + [ -x "$MKLTFS_BIN" ] || fail "mkltfs binary not found at $MKLTFS_BIN" |
| 46 | + [ -x "$LTFSCK_BIN" ] || fail "ltfsck binary not found at $LTFSCK_BIN" |
| 47 | +} |
| 48 | + |
| 49 | +_fusermount() { |
| 50 | + if command -v fusermount3 >/dev/null 2>&1; then |
| 51 | + fusermount3 "$@" |
| 52 | + elif command -v fusermount >/dev/null 2>&1; then |
| 53 | + fusermount "$@" |
| 54 | + else |
| 55 | + # root can unmount directly |
| 56 | + shift # drop -u |
| 57 | + umount "$@" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +ltfs_cleanup() { |
| 62 | + status=$? |
| 63 | + set +e |
| 64 | + if [ -n "$MNT" ] && mountpoint -q "$MNT" 2>/dev/null; then |
| 65 | + _fusermount -u "$MNT" 2>/dev/null || umount "$MNT" 2>/dev/null |
| 66 | + fi |
| 67 | + if [ -n "$LTFS_PID" ] && kill -0 "$LTFS_PID" 2>/dev/null; then |
| 68 | + # Give the daemon time to flush the index after unmount. |
| 69 | + for _ in $(seq 50); do |
| 70 | + kill -0 "$LTFS_PID" 2>/dev/null || break |
| 71 | + sleep 0.2 |
| 72 | + done |
| 73 | + kill "$LTFS_PID" 2>/dev/null |
| 74 | + fi |
| 75 | + [ -n "$WORK" ] && rm -rf "$WORK" |
| 76 | + exit "$status" |
| 77 | +} |
| 78 | + |
| 79 | +# Locate a built plugin .so, accepting both the autotools (libtool .libs/) |
| 80 | +# and the CMake (plain subdir) layouts. |
| 81 | +_find_plugin() { |
| 82 | + subdir=$1 |
| 83 | + base=$2 |
| 84 | + for cand in \ |
| 85 | + "$top_builddir/src/$subdir/.libs/$base.so" \ |
| 86 | + "$top_builddir/src/$subdir/$base.so"; do |
| 87 | + if [ -f "$cand" ]; then |
| 88 | + echo "$cand" |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + done |
| 92 | + fail "plugin $base.so not found under $top_builddir/src/$subdir" |
| 93 | +} |
| 94 | + |
| 95 | +# Generate an ltfs.conf pointing at the plugins in the build tree. |
| 96 | +_write_config() { |
| 97 | + cat >"$WORK/ltfs.conf" <<EOF |
| 98 | +plugin tape file $(_find_plugin tape_drivers/generic/file libtape-file) |
| 99 | +plugin iosched unified $(_find_plugin iosched libiosched-unified) |
| 100 | +plugin iosched fcfs $(_find_plugin iosched libiosched-fcfs) |
| 101 | +plugin kmi flatfile $(_find_plugin kmi libkmi-flatfile) |
| 102 | +plugin kmi simple $(_find_plugin kmi libkmi-simple) |
| 103 | +default tape file |
| 104 | +default iosched unified |
| 105 | +default kmi none |
| 106 | +EOF |
| 107 | +} |
| 108 | + |
| 109 | +# ltfs_setup: format a file-backend tape and mount it on $MNT. |
| 110 | +ltfs_setup() { |
| 111 | + ltfs_check_env |
| 112 | + |
| 113 | + WORK=$(mktemp -d "${TMPDIR:-/tmp}/ltfstest.XXXXXX") |
| 114 | + trap ltfs_cleanup EXIT INT TERM |
| 115 | + MNT="$WORK/mnt" |
| 116 | + TAPE="$WORK/tape" |
| 117 | + mkdir -p "$MNT" "$TAPE" |
| 118 | + |
| 119 | + _write_config |
| 120 | + |
| 121 | + "$MKLTFS_BIN" -i "$WORK/ltfs.conf" -e file -d "$TAPE" -n testvol -f \ |
| 122 | + >"$WORK/mkltfs.log" 2>&1 || { |
| 123 | + cat "$WORK/mkltfs.log" >&2 |
| 124 | + fail "mkltfs failed" |
| 125 | + } |
| 126 | + |
| 127 | + # Run in the foreground so we keep the pid and the log. |
| 128 | + # shellcheck disable=SC2086 |
| 129 | + "$LTFS_BIN" "$MNT" -o config_file="$WORK/ltfs.conf" -o tape_backend=file \ |
| 130 | + -o devname="$TAPE" $LTFS_MOUNT_OPTS -f >"$WORK/ltfs.log" 2>&1 & |
| 131 | + LTFS_PID=$! |
| 132 | + |
| 133 | + for _ in $(seq 150); do |
| 134 | + mountpoint -q "$MNT" && return 0 |
| 135 | + kill -0 "$LTFS_PID" 2>/dev/null || break |
| 136 | + sleep 0.2 |
| 137 | + done |
| 138 | + cat "$WORK/ltfs.log" >&2 |
| 139 | + fail "ltfs did not mount within timeout" |
| 140 | +} |
| 141 | + |
| 142 | +# ltfs_umount: unmount and wait for the daemon to flush and exit. |
| 143 | +ltfs_umount() { |
| 144 | + _fusermount -u "$MNT" |
| 145 | + for _ in $(seq 150); do |
| 146 | + kill -0 "$LTFS_PID" 2>/dev/null || { LTFS_PID=; return 0; } |
| 147 | + sleep 0.2 |
| 148 | + done |
| 149 | + cat "$WORK/ltfs.log" >&2 |
| 150 | + fail "ltfs daemon did not exit after unmount" |
| 151 | +} |
| 152 | + |
| 153 | +# ltfs_fsck: verify the volume is consistent. |
| 154 | +# Exit codes follow fsck conventions: 0 = clean, 1 = corrected/treat as |
| 155 | +# success (e.g. MAM coherency update); anything else is a failure. |
| 156 | +ltfs_fsck() { |
| 157 | + rc=0 |
| 158 | + "$LTFSCK_BIN" -i "$WORK/ltfs.conf" -e file "$TAPE" >"$WORK/ltfsck.log" 2>&1 || rc=$? |
| 159 | + if [ "$rc" -gt 1 ]; then |
| 160 | + cat "$WORK/ltfsck.log" >&2 |
| 161 | + fail "ltfsck reported errors (exit $rc)" |
| 162 | + fi |
| 163 | + grep -q "Volume is consistent" "$WORK/ltfsck.log" || { |
| 164 | + cat "$WORK/ltfsck.log" >&2 |
| 165 | + fail "ltfsck did not report a consistent volume" |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +# ltfs_finish: standard end of test (unmount + consistency check). |
| 170 | +ltfs_finish() { |
| 171 | + ltfs_umount |
| 172 | + ltfs_fsck |
| 173 | +} |
| 174 | + |
| 175 | +# ltfs_is_fuse3: true when the ltfs binary is linked against libfuse 3. |
| 176 | +ltfs_is_fuse3() { |
| 177 | + { ldd "$LTFS_BIN" 2>/dev/null || ldd "$top_builddir/src/.libs/ltfs" 2>/dev/null; } \ |
| 178 | + | grep -q libfuse3 |
| 179 | +} |
| 180 | + |
| 181 | +# ltfs_remount: unmount and mount again (e.g. to verify persistence). |
| 182 | +ltfs_remount() { |
| 183 | + ltfs_umount |
| 184 | + # shellcheck disable=SC2086 |
| 185 | + "$LTFS_BIN" "$MNT" -o config_file="$WORK/ltfs.conf" -o tape_backend=file \ |
| 186 | + -o devname="$TAPE" $LTFS_MOUNT_OPTS -f >"$WORK/ltfs-remount.log" 2>&1 & |
| 187 | + LTFS_PID=$! |
| 188 | + for _ in $(seq 150); do |
| 189 | + mountpoint -q "$MNT" && return 0 |
| 190 | + kill -0 "$LTFS_PID" 2>/dev/null || break |
| 191 | + sleep 0.2 |
| 192 | + done |
| 193 | + cat "$WORK/ltfs-remount.log" >&2 |
| 194 | + fail "ltfs did not remount within timeout" |
| 195 | +} |
0 commit comments