]> arthur.barton.de Git - bup.git/blobdiff - t/test-meta.sh
Add tests for index --no-check-device, and support for t/mnt.
[bup.git] / t / test-meta.sh
index aea9b77146330a8ebfd7efdb1bfae57ddb510a70..29c182813a35d38bb8b28a29c7faab6210c9b881 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env bash
 . wvtest.sh
+. t/lib.sh
 
 TOP="$(pwd)"
 export BUP_DIR="$TOP/buptest.tmp"
@@ -9,6 +10,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
@@ -24,42 +30,6 @@ genstat()
     )
 }
 
-actually-root()
-{
-    test "$(whoami)" == root -a -z "$FAKEROOTKEY"
-}
-
-force-delete()
-{
-    if ! actually-root; then
-        rm -rf "$@"
-    else
-        # Go to greater lengths to deal with any test detritus.
-        for f in "$@"; do
-            test -e "$@" || continue
-            chattr -fR = "$@" || true
-            setfacl -Rb "$@"
-            rm -r "$@"
-        done
-    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,7 +74,7 @@ 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
     )
@@ -129,6 +99,22 @@ 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
+    )
+
+    # Add some trivial files for the index, modify, save tests.
+    (
+        cd "$TOP/bupmeta.tmp"/src
+        mkdir volatile
+        touch volatile/{1,2,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.
@@ -162,6 +148,189 @@ WVSTART 'metadata save/restore (general)'
     test-src-save-restore
 )
 
+# Test that we pull the index (not filesystem) metadata for any
+# unchanged files whenever we're saving other files in a given
+# directory.
+WVSTART 'metadata save/restore (using index metadata)'
+(
+    setup-test-tree
+    cd "$TOP/bupmeta.tmp"
+
+    # ...for now -- might be a problem with hardlink restores that was
+    # causing noise wrt this test.
+    rm -rf src/hardlink*
+
+    # Pause here to keep the filesystem changes far enough away from
+    # the first index run that bup won't cap their index timestamps
+    # (see "bup help index" for more information).  Without this
+    # sleep, the compare-trees test below "Bup should *not* pick up
+    # these metadata..." may fail.
+    sleep 1
+
+    set -x
+    rm -rf src.bup
+    mkdir src.bup
+    export BUP_DIR=$(pwd)/src.bup
+    WVPASS bup init
+    WVPASS bup index src
+    WVPASS bup save -t -n src src
+
+    force-delete src-restore-1
+    mkdir src-restore-1
+    WVPASS bup restore -C src-restore-1 "/src/latest$(pwd)/"
+    WVPASS test -d src-restore-1/src
+    WVPASS "$TOP/t/compare-trees" -c src/ src-restore-1/src/
+
+    echo "blarg" > src/volatile/1
+    cp -a src/volatile/1 src-restore-1/src/volatile/
+    WVPASS bup index src
+
+    # Bup should *not* pick up these metadata changes.
+    touch src/volatile/2
+
+    WVPASS bup save -t -n src src
+
+    force-delete src-restore-2
+    mkdir src-restore-2
+    WVPASS bup restore -C src-restore-2 "/src/latest$(pwd)/"
+    WVPASS test -d src-restore-2/src
+    WVPASS "$TOP/t/compare-trees" -c src-restore-1/src/ src-restore-2/src/
+
+    rm -rf src.bup
+    set +x
+)
+
+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 the case where the hardlink hasn't changed, but the tree
+    # needs to be saved again. i.e. the save-cmd.py "if hashvalid:"
+    # case.
+    (
+        cd "$TOP/bupmeta.tmp"/src
+        echo whatever > something-new
+    )
+    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"