]> arthur.barton.de Git - bup.git/commitdiff
Test file size changes during save
authorJohannes Berg <johannes@sipsolutions.net>
Thu, 31 Dec 2020 22:10:05 +0000 (23:10 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sun, 30 May 2021 16:24:40 +0000 (11:24 -0500)
Test the changes in "save: fix race in data vs. metadata size".

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
[rlb@defaultvalue.org: adjust commit message; rework to use injection]
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/cmd/save.py
test/ext/test-save-data-race [new file with mode: 0755]

index 21a49ad28b300d39d2ba533e3e49da2130b6eece..3e5696ce4c8c5abb6eb6950062acfe5512d9ff9c 100755 (executable)
@@ -37,6 +37,10 @@ graft=     a graft point *old_path*=*new_path* (can be used more than once)
 #,compress=  set compression level to # (0-9, 9 is highest) [1]
 """
 
+# Test hook
+def before_saving_regular_file(name):
+    return
+
 def main(argv):
 
     # Hack around lack of nonlocal vars in python 2
@@ -445,6 +449,7 @@ def main(argv):
                     def new_blob(data):
                         meta.size += len(data)
                         return w.new_blob(data)
+                    before_saving_regular_file(ent.name)
                     with hashsplit.open_noatime(ent.name) as f:
                         (mode, id) = hashsplit.split_to_blob_or_tree(
                                                 new_blob, w.new_tree, [f],
diff --git a/test/ext/test-save-data-race b/test/ext/test-save-data-race
new file mode 100755 (executable)
index 0000000..cf7a9d4
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/env bash
+. wvtest.sh
+. wvtest-bup.sh
+. dev/lib.sh
+
+set -o pipefail
+
+top="$(WVPASS pwd)" || exit $?
+tmpdir="$(WVPASS wvmktempdir)" || exit $?
+export BUP_DIR="$tmpdir/bup"
+
+bup() { "$top/bup" "$@"; }
+
+# Inject code to coordinate test
+
+WVPASS rm -rf "$tmpdir/mod"
+WVPASS mkdir -p "$tmpdir/mod"
+cat > "$tmpdir/mod/pause_file_save.py" << EOF
+
+import os, time
+import bup.cmd.save
+
+def test_save_data_race_pause_save(name):
+    if name == b'$tmpdir/save/data':
+        with open('$tmpdir/waiting-to-save', 'w') as f:
+             pass
+        while os.path.exists('$tmpdir/block-save'):
+           time.sleep(0.01)
+
+bup.cmd.save.before_saving_regular_file = test_save_data_race_pause_save
+
+EOF
+
+instrumented-bup()
+{
+    PYTHONPATH="$tmpdir/mod" bup --import-py-module pause_file_save "$@"
+}
+
+WVPASS cd "$tmpdir"
+WVPASS bup init
+WVPASS mkdir "$tmpdir/save"
+WVPASS echo "some random file content" > "$tmpdir/save/data"
+WVPASS bup index "$tmpdir/save"
+WVPASS touch "$tmpdir/block-save"
+
+(
+    set -e
+    while ! test -e "$tmpdir/waiting-to-save"; do
+        "$top/dev/python" -c 'import time; time.sleep(0.01)'
+    done
+    echo 'truncated' > "$tmpdir/save/data"
+    rm "$tmpdir/block-save"
+) &
+truncator=$!
+trap "kill $truncator" EXIT
+
+WVPASS instrumented-bup save -n test "$tmpdir/save"
+
+meta_size=$(WVPASS bup ls -nl "test/latest/$tmpdir/save/data" |
+                sed 's/[^ ]* [^ ]* *\([^ ]*\).*/\1/')
+data_size=$(git -C "$BUP_DIR" show $(WVPASS bup ls -ls "test/latest/$tmpdir/save/data" |
+                                         sed 's/ .*//') | wc -c)
+WVPASSEQ 10 $meta_size
+WVPASSEQ 10 $data_size
+
+WVPASS rm -rf "$tmpdir"