]> arthur.barton.de Git - bup.git/blobdiff - t/test-meta.sh
Move tree comparison to t/compare-trees; compare content by default.
[bup.git] / t / test-meta.sh
index 3ce1d553113a3e7d66f656bd075d585584ac1896..b9c507f4a1404814df752f7daa9508a641c18b68 100755 (executable)
@@ -9,6 +9,11 @@ bup()
     "$TOP/bup" "$@"
 }
 
+hardlink-sets()
+{
+    "$TOP/t/hardlink-sets" "$@"
+}
+
 # Very simple metadata tests -- create a test tree then check that bup
 # meta can reproduce the metadata correctly (according to bup xstat)
 # via create, extract, start-extract, and finish-extract.  The current
@@ -44,22 +49,6 @@ force-delete()
     fi
 }
 
-compare-trees()
-{
-    (
-        set -e
-        set -o pipefail
-        tmpfile="$(mktemp)"
-        trap "rm -rf ${tmpfile}" EXIT
-        rsync -ni -aHAX "$1" "$2" > "${tmpfile}"
-        if test $(wc -l < "${tmpfile}") != 0; then
-            echo "ERROR: detected differences between $1 and $2"
-            cat "${tmpfile}"
-            false
-        fi
-    )
-}
-
 test-src-create-extract()
 {
     # Test bup meta create/extract for ./src -> ./src-restore.
@@ -104,15 +93,22 @@ test-src-save-restore()
         mkdir src-restore
         WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
         WVPASS test -d src-restore/src
-        WVPASS compare-trees src/ src-restore/src/
+        WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
         rm -rf src.bup
         set +x
     )
 }
 
-if actually-root; then
+universal-cleanup()
+{
+    if ! actually-root; then return 0; fi
+    cd "$TOP"
     umount "$TOP/bupmeta.tmp/testfs" || true
-fi
+    umount "$TOP/bupmeta.tmp/testfs-limited" || true
+}
+
+universal-cleanup
+trap universal-cleanup EXIT
 
 setup-test-tree()
 (
@@ -122,6 +118,15 @@ setup-test-tree()
     mkdir -p "$TOP/bupmeta.tmp/src"
     cp -pPR Documentation cmd lib t "$TOP/bupmeta.tmp"/src
 
+    # Add some hard links for the general tests.
+    (
+        cd "$TOP/bupmeta.tmp"/src
+        touch hardlink-target
+        ln hardlink-target hardlink-1
+        ln hardlink-target hardlink-2
+        ln hardlink-target hardlink-3
+    )
+
     # Regression test for metadata sort order.  Previously, these two
     # entries would sort in the wrong order because the metadata
     # entries were being sorted by mangled name, but the index isn't.
@@ -155,6 +160,125 @@ WVSTART 'metadata save/restore (general)'
     test-src-save-restore
 )
 
+setup-hardlink-test()
+{
+    (
+        cd "$TOP/bupmeta.tmp"
+        rm -rf src src.bup
+        mkdir src src.bup
+        WVPASS bup init
+    )
+}
+
+hardlink-test-run-restore()
+{
+    force-delete src-restore
+    mkdir src-restore
+    WVPASS bup restore -C src-restore "/src/latest$(pwd)/"
+    WVPASS test -d src-restore/src
+}
+
+# Test hardlinks more carefully.
+WVSTART 'metadata save/restore (hardlinks)'
+(
+    set -e
+    set -x
+    export BUP_DIR="$TOP/bupmeta.tmp/src.bup"
+    force-delete "$TOP/bupmeta.tmp"
+    mkdir -p "$TOP/bupmeta.tmp"
+
+    cd "$TOP/bupmeta.tmp"
+
+    # Test trivial case - single hardlink.
+    setup-hardlink-test
+    (
+        cd "$TOP/bupmeta.tmp"/src
+        touch hardlink-target
+        ln hardlink-target hardlink-1
+    )
+    WVPASS bup index src
+    WVPASS bup save -t -n src src
+    hardlink-test-run-restore
+    WVPASS "$TOP/t/compare-trees" -c src/ src-restore/src/
+
+    # Test hardlink changes between index runs.
+    #
+    setup-hardlink-test
+    cd "$TOP/bupmeta.tmp"/src
+    touch hardlink-target-a
+    touch hardlink-target-b
+    ln hardlink-target-a hardlink-b-1
+    ln hardlink-target-a hardlink-a-1
+    cd ..
+    WVPASS bup index -vv src
+    rm src/hardlink-b-1
+    ln src/hardlink-target-b src/hardlink-b-1
+    WVPASS bup index -vv src
+    WVPASS bup save -t -n src src
+    hardlink-test-run-restore
+    echo ./src/hardlink-a-1 > hardlink-sets.expected
+    echo ./src/hardlink-target-a >> hardlink-sets.expected
+    echo >> hardlink-sets.expected
+    echo ./src/hardlink-b-1 >> hardlink-sets.expected
+    echo ./src/hardlink-target-b >> hardlink-sets.expected
+    (cd src-restore && hardlink-sets .) > hardlink-sets.restored
+    WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+    # Test hardlink changes between index and save -- hardlink set [a
+    # b c d] changes to [a b] [c d].  At least right now bup should
+    # notice and recreate the latter.
+    setup-hardlink-test
+    cd "$TOP/bupmeta.tmp"/src
+    touch a
+    ln a b
+    ln a c
+    ln a d
+    cd ..
+    WVPASS bup index -vv src
+    rm src/c src/d
+    touch src/c
+    ln src/c src/d
+    WVPASS bup save -t -n src src
+    hardlink-test-run-restore
+    echo ./src/a > hardlink-sets.expected
+    echo ./src/b >> hardlink-sets.expected
+    echo >> hardlink-sets.expected
+    echo ./src/c >> hardlink-sets.expected
+    echo ./src/d >> hardlink-sets.expected
+    (cd src-restore && hardlink-sets .) > hardlink-sets.restored
+    WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+    # Test that we don't link outside restore tree.
+    setup-hardlink-test
+    cd "$TOP/bupmeta.tmp"
+    mkdir src/a src/b
+    touch src/a/1
+    ln src/a/1 src/b/1
+    WVPASS bup index -vv src
+    WVPASS bup save -t -n src src
+    force-delete src-restore
+    mkdir src-restore
+    WVPASS bup restore -C src-restore "/src/latest$(pwd)/src/a/"
+    WVPASS test -e src-restore/1
+    echo -n > hardlink-sets.expected
+    (cd src-restore && hardlink-sets .) > hardlink-sets.restored
+    WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+
+    # Test that we do link within separate sub-trees.
+    setup-hardlink-test
+    cd "$TOP/bupmeta.tmp"
+    mkdir src/a src/b
+    touch src/a/1
+    ln src/a/1 src/b/1
+    WVPASS bup index -vv src/a src/b
+    WVPASS bup save -t -n src src/a src/b
+    hardlink-test-run-restore
+    echo ./src/a/1 > hardlink-sets.expected
+    echo ./src/b/1 >> hardlink-sets.expected
+    (cd src-restore && hardlink-sets .) > hardlink-sets.restored
+    WVPASS diff -u hardlink-sets.expected hardlink-sets.restored
+)
+
 WVSTART 'meta --edit'
 (
     force-delete "$TOP/bupmeta.tmp"
@@ -220,8 +344,8 @@ WVSTART 'meta --edit'
     WVPASS bup xstat src | grep -qvE '^user: root'
 
     # Make sure we can restore one of the user's groups.
-    user_groups="$(groups)"
-    last_group="$(echo ${user_groups/* /})"
+    last_group="$(python -c 'import os,grp; \
+      print grp.getgrgid(os.getgroups()[0])[0]')"
     rm -rf src
     WVPASS bup meta --edit --set-group "$last_group" ../src.meta \
         | WVPASS bup meta -x
@@ -273,6 +397,7 @@ WVSTART 'meta --edit'
     mkdir "$TOP/bupmeta.tmp"
     cd "$TOP/bupmeta.tmp"
     touch src
+    chown 0:0 src # In case the parent dir is sgid, etc.
     WVPASS bup meta -cf src.meta src
 
     mkdir dest
@@ -354,18 +479,12 @@ WVSTART 'meta --edit'
 # Linux attr, Linux xattr, etc.
 if actually-root; then
     (
+        set -e
+        # Some cleanup handled in universal-cleanup() above.
         # These tests are only likely to work under Linux for now
         # (patches welcome).
         [[ $(uname) =~ Linux ]] || exit 0
 
-        cleanup_at_exit()
-        {
-            cd "$TOP"
-            umount "$TOP/bupmeta.tmp/testfs" || true
-        }
-
-        trap cleanup_at_exit EXIT
-
         WVSTART 'meta - general (as root)'
         setup-test-tree
         cd "$TOP/bupmeta.tmp"
@@ -379,6 +498,13 @@ if actually-root; then
         chown root:root testfs
         chmod 0700 testfs
 
+        umount testfs-limited || true
+        dd if=/dev/zero of=testfs-limited.img bs=1M count=32
+        mkfs -t vfat testfs-limited.img
+        mkdir testfs-limited
+        mount -o loop,uid=root,gid=root,umask=0077 \
+            testfs-limited.img testfs-limited
+
         #cp -a src testfs/src
         cp -pPR src testfs/src
         (cd testfs && test-src-create-extract)
@@ -422,6 +548,18 @@ if actually-root; then
             chattr +acdeijstuADST testfs/src/foo
             chattr +acdeijstuADST testfs/src/bar
             (cd testfs && test-src-create-extract)
+            # Test restoration to a limited filesystem (vfat).
+            (
+                WVPASS bup meta --create --recurse --file testfs/src.meta \
+                    testfs/src
+                force-delete testfs-limited/src-restore
+                mkdir testfs-limited/src-restore
+                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()) == 2)'
+            )
         )
 
         WVSTART 'meta - Linux xattr (as root)'
@@ -433,6 +571,19 @@ if actually-root; then
             attr -s foo -V bar testfs/src/foo
             attr -s foo -V bar testfs/src/bar
             (cd testfs && test-src-create-extract)
+
+            # Test restoration to a limited filesystem (vfat).
+            (
+                WVPASS bup meta --create --recurse --file testfs/src.meta \
+                    testfs/src
+                force-delete testfs-limited/src-restore
+                mkdir testfs-limited/src-restore
+                cd testfs-limited/src-restore
+                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)'
+            )
         )
 
         WVSTART 'meta - POSIX.1e ACLs (as root)'
@@ -444,6 +595,19 @@ if actually-root; then
             setfacl -m u:root:r testfs/src/foo
             setfacl -m u:root:r testfs/src/bar
             (cd testfs && test-src-create-extract)
+
+            # Test restoration to a limited filesystem (vfat).
+            (
+                WVPASS bup meta --create --recurse --file testfs/src.meta \
+                    testfs/src
+                force-delete testfs-limited/src-restore
+                mkdir testfs-limited/src-restore
+                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)'
+            )
         )
     )
 fi