]> arthur.barton.de Git - bup.git/blobdiff - t/test-meta.sh
test-meta.sh: move mount points to t/mnt
[bup.git] / t / test-meta.sh
index b247e7ad7be2c3d3d26074a82dc996cbb0289e3e..f4481634dbdbd5ecf142fad44b01db3f9e6dc682 100755 (executable)
@@ -1,11 +1,24 @@
 #!/usr/bin/env bash
-. wvtest.sh
-. t/lib.sh
+. wvtest-bup.sh || exit $?
+. t/lib.sh || exit $?
 
 set -o pipefail
 
+root_status="$(t/root-status)" || exit $?
+
 TOP="$(WVPASS pwd)" || exit $?
-export BUP_DIR="$TOP/buptest.tmp"
+tmpdir="$(WVPASS wvmktempdir)" || exit $?
+
+export BUP_DIR="$tmpdir/bup"
+
+WVPASS force-delete "$TOP/bupmeta.tmp"
+timestamp_resolutions="$(t/ns-timestamp-resolutions "$TOP/bupmeta.tmp")" \
+    || exit $?
+WVPASS rm "$TOP/bupmeta.tmp"
+atime_resolution="$(echo $timestamp_resolutions | WVPASS cut -d' ' -f 1)" \
+    || exit $?
+mtime_resolution="$(echo $timestamp_resolutions | WVPASS cut -d' ' -f 2)" \
+    || exit $?
 
 bup()
 {
@@ -34,7 +47,9 @@ genstat()
         export PATH="$TOP:$PATH" # pick up bup
         # Skip atime (test elsewhere) to avoid the observer effect.
         WVPASS find . | WVPASS sort \
-            | WVPASS xargs bup xstat --exclude-fields ctime,atime,size
+            | WVPASS xargs bup xstat \
+            --mtime-resolution "$mtime_resolution"ns \
+            --exclude-fields ctime,atime,size
     )
 }
 
@@ -72,7 +87,7 @@ test-src-save-restore()
     (
         WVPASS rm -rf src.bup
         WVPASS mkdir src.bup
-        export BUP_DIR=$(pwd)/src.bup
+        export BUP_DIR=$(pwd)/src.bup || exit $?
         WVPASS bup init
         WVPASS bup index src
         WVPASS bup save -t -n src src
@@ -86,16 +101,6 @@ test-src-save-restore()
     )
 }
 
-universal-cleanup()
-{
-    if [ $(t/root-status) != root ]; then return 0; fi
-    umount "$TOP/bupmeta.tmp/testfs" || true
-    umount "$TOP/bupmeta.tmp/testfs-limited" || true
-}
-
-WVPASS universal-cleanup
-trap universal-cleanup EXIT
-
 setup-test-tree()
 {
     WVPASS force-delete "$BUP_DIR"
@@ -158,7 +163,7 @@ WVSTART 'metadata save/restore (general)'
     WVPASS test -d src/lib/bup
     WVPASS rm -rf src.bup
     WVPASS mkdir src.bup
-    export BUP_DIR=$(pwd)/src.bup
+    export BUP_DIR=$(pwd)/src.bup || exit $?
     WVPASS bup init
     WVPASS touch -t 201111111111 src-restore # Make sure the top won't match.
     WVPASS bup index src
@@ -193,7 +198,7 @@ WVSTART 'metadata save/restore (using index metadata)'
 
     WVPASS rm -rf src.bup
     WVPASS mkdir src.bup
-    export BUP_DIR=$(pwd)/src.bup
+    export BUP_DIR=$(pwd)/src.bup || exit $?
     WVPASS bup init
     WVPASS bup index src
     WVPASS bup save -t -n src src
@@ -414,7 +419,7 @@ src/foo/3"
 
 # Test ownership restoration (when not root or fakeroot).
 (
-    if [ $(t/root-status) != none ]; then
+    if [ "$root_status" != none ]; then
         exit 0
     fi
 
@@ -422,6 +427,7 @@ src/foo/3"
       print grp.getgrgid(os.getgroups()[0])[0]')" || exit $?
     last_group="$(python -c 'import os,grp; \
       print grp.getgrgid(os.getgroups()[-1])[0]')" || exit $?
+    last_group_erx="$(escape-erx "$last_group")"
 
     WVSTART 'metadata (restoration of ownership)'
     WVPASS force-delete "$TOP/bupmeta.tmp"
@@ -448,11 +454,11 @@ src/foo/3"
     WVPASS rm -rf src
     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
         | WVPASS bup meta -x
-    WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group"
+    WVPASS bup xstat src | WVPASS grep -qE "^group: $last_group_erx"
 
     # Make sure we can restore one of the user's gids.
-    user_gids="$(id -G)"
-    last_gid="$(echo ${user_gids/* /})"
+    user_gids="$(id -G)" || exit $?
+    last_gid="$(echo ${user_gids/* /})" || exit $?
     WVPASS rm -rf src
     WVPASS bup meta --edit --unset-group --set-gid "$last_gid" ../src.meta \
         | WVPASS bup meta -x
@@ -460,54 +466,57 @@ src/foo/3"
 
     # Test --numeric-ids (gid).
     WVPASS rm -rf src
-    current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
+    current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
         | WVPASS bup meta -x --numeric-ids
-    new_gidx=$(bup xstat src | grep -e '^gid:')
+    new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
     WVPASSEQ "$current_gidx" "$new_gidx"
 
     # Test that restoring an unknown user works.
-    unknown_user=$("$TOP"/t/unknown-owner --user)
+    unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
     WVPASS rm -rf src
-    current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:')
+    current_uidx=$(bup meta -tvvf ../src.meta | grep -e '^uid:') || exit $?
     WVPASS bup meta --edit --set-user "$unknown_user" ../src.meta \
         | WVPASS bup meta -x
-    new_uidx=$(bup xstat src | grep -e '^uid:')
+    new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
     WVPASSEQ "$current_uidx" "$new_uidx"
 
     # Test that restoring an unknown group works.
-    unknown_group=$("$TOP"/t/unknown-owner --group)
+    unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
     WVPASS rm -rf src
-    current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:')
+    current_gidx=$(bup meta -tvvf ../src.meta | grep -e '^gid:') || exit $?
     WVPASS bup meta --edit --set-group "$unknown_group" ../src.meta \
         | WVPASS bup meta -x
-    new_gidx=$(bup xstat src | grep -e '^gid:')
+    new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
     WVPASSEQ "$current_gidx" "$new_gidx"
 ) || exit $?
 
 # Test ownership restoration (when root or fakeroot).
 (
-    if [ $(t/root-status) = none ]; then
+    if [ "$root_status" = none ]; then
         exit 0
     fi
 
+    uid=$(WVPASS id -un) || exit $?
+    gid=$(WVPASS id -gn) || exit $?
+
     WVSTART 'metadata (restoration of ownership as root)'
     WVPASS force-delete "$TOP/bupmeta.tmp"
     WVPASS mkdir "$TOP/bupmeta.tmp"
     WVPASS cd "$TOP/bupmeta.tmp"
     WVPASS touch src
-    WVPASS chown 0:0 src # In case the parent dir is sgid, etc.
+    WVPASS chown "$uid:$gid" src # In case the parent dir is sgid, etc.
     WVPASS bup meta -cf src.meta src
 
     WVPASS mkdir dest
     WVPASS chmod 700 dest # so we can't accidentally do something insecure
     WVPASS cd dest
 
-    other_uinfo="$(id-other-than --user "$(id -un)")"
+    other_uinfo="$(id-other-than --user "$uid")" || exit $?
     other_user="${other_uinfo%%:*}"
     other_uid="${other_uinfo##*:}"
 
-    other_ginfo="$(id-other-than --group "$(id -gn)")"
+    other_ginfo="$(id-other-than --group "$gid")" || exit $?
     other_group="${other_ginfo%%:*}"
     other_gid="${other_ginfo##*:}"
 
@@ -521,25 +530,27 @@ src/foo/3"
         | WVPASS bup meta -x
     WVPASS bup xstat src | WVPASS grep -qE "^gid: $other_gid"
 
-    other_uinfo2="$(id-other-than --user "$(id -un)" "$other_user")"
+    other_uinfo2="$(id-other-than --user "$(id -un)" "$other_user")" || exit $?
     other_user2="${other_uinfo2%%:*}"
+    other_user2_erx="$(escape-erx "$other_user2")" || exit $?
     other_uid2="${other_uinfo2##*:}"
 
-    other_ginfo2="$(id-other-than --group "$(id -gn)" "$other_group")"
+    other_ginfo2="$(id-other-than --group "$(id -gn)" "$other_group")" || exit $?
     other_group2="${other_ginfo2%%:*}"
+    other_group2_erx="$(escape-erx "$other_group2")" || exit $?
     other_gid2="${other_ginfo2##*:}"
 
     # Try to restore a user (and see that user trumps uid when uid is not 0).
     WVPASS bup meta --edit \
-        --set-uid "$other_uid2" --set-user "$some_user" ../src.meta \
+        --set-uid "$other_uid" --set-user "$other_user2" ../src.meta \
         | WVPASS bup meta -x
-    WVPASS bup xstat src | WVPASS grep -qE "^user: $some_user"
+    WVPASS bup xstat src | WVPASS grep -qE "^user: $other_user2_erx"
 
     # Try to restore a group (and see that group trumps gid when gid is not 0).
     WVPASS bup meta --edit \
-        --set-gid "$other_gid2" --set-group "$some_group" ../src.meta \
+        --set-gid "$other_gid" --set-group "$other_group2" ../src.meta \
         | WVPASS bup meta -x
-    WVPASS bup xstat src | WVPASS grep -qE "^group: $some_user"
+    WVPASS bup xstat src | WVPASS grep -qE "^group: $other_group2_erx"
 
     # Test --numeric-ids (uid).  Note the name 'root' is not handled
     # specially, so we use that here as the test user name.  We assume
@@ -547,7 +558,7 @@ src/foo/3"
     WVPASS rm -rf src
     WVPASS bup meta --edit --set-user root --set-uid "$other_uid" ../src.meta \
         | WVPASS bup meta -x --numeric-ids
-    new_uidx=$(bup xstat src | grep -e '^uid:')
+    new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
     WVPASSEQ "$new_uidx" "uid: $other_uid"
 
     # Test --numeric-ids (gid).  Note the name 'root' is not handled
@@ -556,25 +567,25 @@ src/foo/3"
     WVPASS rm -rf src
     WVPASS bup meta --edit --set-group root --set-gid "$other_gid" ../src.meta \
         | WVPASS bup meta -x --numeric-ids
-    new_gidx=$(bup xstat src | grep -e '^gid:')
+    new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
     WVPASSEQ "$new_gidx" "gid: $other_gid"
 
     # Test that restoring an unknown user works.
-    unknown_user=$("$TOP"/t/unknown-owners --user)
+    unknown_user=$("$TOP"/t/unknown-owner --user) || exit $?
     WVPASS rm -rf src
     WVPASS bup meta --edit \
         --set-uid "$other_uid" --set-user "$unknown_user" ../src.meta \
         | WVPASS bup meta -x
-    new_uidx=$(bup xstat src | grep -e '^uid:')
+    new_uidx=$(bup xstat src | grep -e '^uid:') || exit $?
     WVPASSEQ "$new_uidx" "uid: $other_uid"
 
     # Test that restoring an unknown group works.
-    unknown_group=$("$TOP"/t/unknown-owners --group)
+    unknown_group=$("$TOP"/t/unknown-owner --group) || exit $?
     WVPASS rm -rf src
     WVPASS bup meta --edit \
         --set-gid "$other_gid" --set-group "$unknown_group" ../src.meta \
         | WVPASS bup meta -x
-    new_gidx=$(bup xstat src | grep -e '^gid:')
+    new_gidx=$(bup xstat src | grep -e '^gid:') || exit $?
     WVPASSEQ "$new_gidx" "gid: $other_gid"
 
     if ! [[ $(uname) =~ CYGWIN ]]; then
@@ -582,15 +593,15 @@ src/foo/3"
         # restoring an unknown uid/gid.
 
         # Make sure a uid of 0 trumps a non-root user.
-        WVPASS bup meta --edit --set-user "$some_user" ../src.meta \
+        WVPASS bup meta --edit --set-user "$other_user2" ../src.meta \
             | WVPASS bup meta -x
-        WVPASS bup xstat src | WVPASS grep -qvE "^user: $some_user"
+        WVPASS bup xstat src | WVPASS grep -qvE "^user: $other_user2_erx"
         WVPASS bup xstat src | WVPASS grep -qE "^uid: 0"
 
         # Make sure a gid of 0 trumps a non-root group.
-        WVPASS bup meta --edit --set-group "$some_user" ../src.meta \
+        WVPASS bup meta --edit --set-group "$other_group2" ../src.meta \
             | WVPASS bup meta -x
-        WVPASS bup xstat src | WVPASS grep -qvE "^group: $some_group"
+        WVPASS bup xstat src | WVPASS grep -qvE "^group: $other_group2_erx"
         WVPASS bup xstat src | WVPASS grep -qE "^gid: 0"
     fi
 ) || exit $?
@@ -598,18 +609,26 @@ src/foo/3"
 
 # Root-only tests that require an FS with all the trimmings: ACLs,
 # Linux attr, Linux xattr, etc.
-if [ $(t/root-status) = root ]; then
+if [ "$root_status" = root ]; then
     (
         # Some cleanup handled in universal-cleanup() above.
         # These tests are only likely to work under Linux for now
         # (patches welcome).
         [[ $(uname) =~ Linux ]] || exit 0
 
+        if ! modprobe loop; then
+            echo 'Unable to load loopback module; skipping dependent tests.' 1>&2
+            exit 0
+        fi
+
+        testfs="$(WVPASS wvmkmountpt)" || exit $?
+        testfs_limited="$(WVPASS wvmkmountpt)" || exit $?
+
         WVSTART 'meta - general (as root)'
         WVPASS setup-test-tree
         WVPASS cd "$TOP/bupmeta.tmp"
 
-        umount testfs
+        umount "$testfs"
         WVPASS dd if=/dev/zero of=testfs.img bs=1M count=32
         # Make sure we have all the options the chattr test needs
         # (i.e. create a "normal" ext4 filesystem).
@@ -617,34 +636,32 @@ if [ $(t/root-status) = root ]; then
             -I 256 \
             -O has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize \
             testfs.img
-        WVPASS mkdir testfs
-        WVPASS mount -o loop,acl,user_xattr testfs.img testfs
+        WVPASS mount -o loop,acl,user_xattr testfs.img "$testfs"
         # Hide, so that tests can't create risks.
-        WVPASS chown root:root testfs
-        WVPASS chmod 0700 testfs
+        WVPASS chown root:root "$testfs"
+        WVPASS chmod 0700 "$testfs"
 
-        umount testfs-limited
+        umount "$testfs_limited"
         WVPASS dd if=/dev/zero of=testfs-limited.img bs=1M count=32
         WVPASS mkfs -t vfat testfs-limited.img
-        WVPASS mkdir testfs-limited
         WVPASS mount -o loop,uid=root,gid=root,umask=0077 \
-            testfs-limited.img testfs-limited
+            testfs-limited.img "$testfs_limited"
 
-        WVPASS cp -pPR src testfs/src
-        (WVPASS cd testfs; WVPASS test-src-create-extract) || exit $?
+        WVPASS cp -pPR src "$testfs"/src
+        (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
 
         WVSTART 'meta - atime (as root)'
-        WVPASS force-delete testfs/src
-        WVPASS mkdir testfs/src
+        WVPASS force-delete "$testfs"/src
+        WVPASS mkdir "$testfs"/src
         (
-            WVPASS mkdir testfs/src/foo
-            WVPASS touch testfs/src/bar
+            WVPASS mkdir "$testfs"/src/foo
+            WVPASS touch "$testfs"/src/bar
             PYTHONPATH="$TOP/lib" \
                 WVPASS python -c "from bup import xstat; \
                 x = xstat.timespec_to_nsecs((42, 0));\
-                   xstat.utime('testfs/src/foo', (x, x));\
-                   xstat.utime('testfs/src/bar', (x, x));"
-            WVPASS cd testfs
+                   xstat.utime('$testfs/src/foo', (x, x));\
+                   xstat.utime('$testfs/src/bar', (x, x));"
+            WVPASS cd "$testfs"
             WVPASS bup meta -v --create --recurse --file src.meta src
             WVPASS bup meta -tvf src.meta
             # Test extract.
@@ -664,22 +681,22 @@ if [ $(t/root-status) = root ]; then
         ) || exit $?
 
         WVSTART 'meta - Linux attr (as root)'
-        WVPASS force-delete testfs/src
-        WVPASS mkdir testfs/src
+        WVPASS force-delete "$testfs"/src
+        WVPASS mkdir "$testfs"/src
         (
-            WVPASS touch testfs/src/foo
-            WVPASS mkdir testfs/src/bar
-            WVPASS chattr +acdeijstuADST testfs/src/foo
-            WVPASS chattr +acdeijstuADST testfs/src/bar
-            (WVPASS cd testfs; WVPASS test-src-create-extract) || exit $?
+            WVPASS touch "$testfs"/src/foo
+            WVPASS mkdir "$testfs"/src/bar
+            WVPASS chattr +acdeijstuADST "$testfs"/src/foo
+            WVPASS chattr +acdeijstuADST "$testfs"/src/bar
+            (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
             # Test restoration to a limited filesystem (vfat).
             (
-                WVPASS bup meta --create --recurse --file testfs/src.meta \
-                    testfs/src
-                WVPASS force-delete testfs-limited/src-restore
-                WVPASS mkdir testfs-limited/src-restore
-                WVPASS cd testfs-limited/src-restore
-                WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
+                WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
+                    "$testfs"/src
+                WVPASS force-delete "$testfs_limited"/src-restore
+                WVPASS mkdir "$testfs_limited"/src-restore
+                WVPASS cd "$testfs_limited"/src-restore
+                WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
                     | WVPASS grep -e '^Linux chattr:' \
                     | WVPASS python -c \
                     'import sys; exit(not len(sys.stdin.readlines()) == 3)'
@@ -687,48 +704,55 @@ if [ $(t/root-status) = root ]; then
         ) || exit $?
 
         WVSTART 'meta - Linux xattr (as root)'
-        WVPASS force-delete testfs/src
-        WVPASS mkdir testfs/src
-        WVPASS touch testfs/src/foo
-        WVPASS mkdir testfs/src/bar
-        WVPASS attr -s foo -V bar testfs/src/foo
-        WVPASS attr -s foo -V bar testfs/src/bar
-        (WVPASS cd testfs; WVPASS test-src-create-extract) || exit $?
+        WVPASS force-delete "$testfs"/src
+        WVPASS mkdir "$testfs"/src
+        WVPASS touch "$testfs"/src/foo
+        WVPASS mkdir "$testfs"/src/bar
+        WVPASS attr -s foo -V bar "$testfs"/src/foo
+        WVPASS attr -s foo -V bar "$testfs"/src/bar
+        (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
 
         # Test restoration to a limited filesystem (vfat).
         (
-            WVPASS bup meta --create --recurse --file testfs/src.meta \
-                testfs/src
-            WVPASS force-delete testfs-limited/src-restore
-            WVPASS mkdir testfs-limited/src-restore
-            WVPASS cd testfs-limited/src-restore
-            WVFAIL bup meta --extract --file ../../testfs/src.meta
-            WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
-                | WVPASS grep -e '^xattr\.set:' \
+            WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
+                "$testfs"/src
+            WVPASS force-delete "$testfs_limited"/src-restore
+            WVPASS mkdir "$testfs_limited"/src-restore
+            WVPASS cd "$testfs_limited"/src-restore
+            WVFAIL bup meta --extract --file "$testfs"/src.meta
+            WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
+                | WVPASS grep -e "^xattr\.set '" \
                 | WVPASS python -c \
                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
         ) || exit $?
 
         WVSTART 'meta - POSIX.1e ACLs (as root)'
-        WVPASS force-delete testfs/src
-        WVPASS mkdir testfs/src
-        WVPASS touch testfs/src/foo
-        WVPASS mkdir testfs/src/bar
-        WVPASS setfacl -m u:root:r testfs/src/foo
-        WVPASS setfacl -m u:root:r testfs/src/bar
-        (WVPASS cd testfs; WVPASS test-src-create-extract) || exit $?
+        WVPASS force-delete "$testfs"/src
+        WVPASS mkdir "$testfs"/src
+        WVPASS touch "$testfs"/src/foo
+        WVPASS mkdir "$testfs"/src/bar
+        WVPASS setfacl -m u:root:r "$testfs"/src/foo
+        WVPASS setfacl -m u:root:r "$testfs"/src/bar
+        (WVPASS cd "$testfs"; WVPASS test-src-create-extract) || exit $?
 
         # Test restoration to a limited filesystem (vfat).
         (
-            WVPASS bup meta --create --recurse --file testfs/src.meta \
-                testfs/src
-            WVPASS force-delete testfs-limited/src-restore
-            WVPASS mkdir testfs-limited/src-restore
-            WVPASS cd testfs-limited/src-restore
-            WVFAIL bup meta --extract --file ../../testfs/src.meta 2>&1 \
+            WVPASS bup meta --create --recurse --file "$testfs"/src.meta \
+                "$testfs"/src
+            WVPASS force-delete "$testfs_limited"/src-restore
+            WVPASS mkdir "$testfs_limited"/src-restore
+            WVPASS cd "$testfs_limited"/src-restore
+            WVFAIL bup meta --extract --file "$testfs"/src.meta 2>&1 \
                 | WVPASS grep -e '^POSIX1e ACL applyto:' \
                 | WVPASS python -c \
                 'import sys; exit(not len(sys.stdin.readlines()) == 2)'
         ) || exit $?
+
+        WVPASS umount "$testfs"
+        WVPASS umount "$testfs_limited"
+        WVPASS rm -r "$testfs" "$testfs_limited"
+
     ) || exit $?
 fi
+
+WVPASS rm -r "$tmpdir"