diff mbox series

[2/2] btrfs-progs: tests: Test backup root retention logic

Message ID 20191015154249.21615-3-nborisov@suse.com (mailing list archive)
State New, archived
Headers show
Series Support patches for btrfs backup root rework | expand

Commit Message

Nikolay Borisov Oct. 15, 2019, 3:42 p.m. UTC
This tests ensures that the kernel correctly persists backup roots in
case the filesystem has been mounted from a backup root.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
 .../misc-tests/038-backup-root-corruption/test.sh  | 50 ++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100755 tests/misc-tests/038-backup-root-corruption/test.sh

Comments

David Sterba Nov. 1, 2019, 11:48 a.m. UTC | #1
On Tue, Oct 15, 2019 at 06:42:49PM +0300, Nikolay Borisov wrote:
> This tests ensures that the kernel correctly persists backup roots in
> case the filesystem has been mounted from a backup root.

The test does not work very well under a non-root user.

> Signed-off-by: Nikolay Borisov <nborisov@suse.com>
> ---
>  .../misc-tests/038-backup-root-corruption/test.sh  | 50 ++++++++++++++++++++++
>  1 file changed, 50 insertions(+)
>  create mode 100755 tests/misc-tests/038-backup-root-corruption/test.sh
> 
> diff --git a/tests/misc-tests/038-backup-root-corruption/test.sh b/tests/misc-tests/038-backup-root-corruption/test.sh
> new file mode 100755
> index 000000000000..2fb117b3a928
> --- /dev/null
> +++ b/tests/misc-tests/038-backup-root-corruption/test.sh
> @@ -0,0 +1,50 @@
> +#!/bin/bash
> +# Test that a corrupted filesystem will correctly handle writing of 
> +# backup root
> +
> +source "$TEST_TOP/common"
> +
> +check_prereq mkfs.btrfs
> +check_prereq btrfs
> +check_prereq btrfs-corrupt-block
> +

setup_root_helper

> +setup_loopdevs 1
> +prepare_loopdevs
> +dev=${loopdevs[1]}

You can use TEST_DEV and then all the common mkfs/mount/umount helpers
will work

> +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$dev"
> +
> +# Create a file and unmount to commit some backup roots
> +run_check $SUDO_HELPER mount "$dev" "$TEST_MNT"
> +run_check touch "$TEST_MNT/file" && sync

'touch' is on the mounted fs, so it needs $SUDO_HELPER too

> +run_check $SUDO_HELPER umount "$TEST_MNT"
> +
> +# Ensure currently active backup slot is the expected one (slot 3)
> +backup2_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
> +	| grep -A1 "backup 2" | grep backup_tree_root | awk '{print $2}')
> +
> +main_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
> +	| grep root | head -n1 | awk '{print $2}')
> +
> +[[ "$backup2_root_ptr" -eq "$main_root_ptr" ]] || _fail "Backup slot 2 is not in use"
> +
> +run_check "$TOP/btrfs-corrupt-block" -m $main_root_ptr -f generation "$dev"
> +
> +## should fail because the root is corrupted

double ##

> +run_mustfail "Unexpected successful mount" $SUDO_HELPER mount "$dev" "$TEST_MNT"
> +
> +# Cycle mount with the backup to force rewrite of slot 3 
> +run_check $SUDO_HELPER mount -ousebackuproot "$dev" "$TEST_MNT"

run_check_mount_test_dev -o usebackuproot

> +run_check $SUDO_HELPER umount "$TEST_MNT"

run_check_umount_test_dev

> +
> +

two empty lines

> +# Since we've used backup 1 as the usable root, then backup 2 should have been 
> +# overwritten 

trailing whitespace (here and in several above as well)

> +main_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
> +	| grep root | head -n1 | awk '{print $2}')
> +backup2_new_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
> +	| grep -A1 "backup 2" | grep backup_tree_root | awk '{print $2}')
> +
> +[[ "$backup2_root_ptr" -ne "$backup2_new_root_ptr" ]] || _fail "Backup 2 not overwritten"
> +
> +cleanup_loopdevs
> -- 
> 2.7.4
David Sterba Nov. 1, 2019, 12:21 p.m. UTC | #2
On Tue, Oct 15, 2019 at 06:42:49PM +0300, Nikolay Borisov wrote:
> This tests ensures that the kernel correctly persists backup roots in
> case the filesystem has been mounted from a backup root.
> 
> Signed-off-by: Nikolay Borisov <nborisov@suse.com>
> ---
>  .../misc-tests/038-backup-root-corruption/test.sh  | 50 ++++++++++++++++++++++
>  1 file changed, 50 insertions(+)
>  create mode 100755 tests/misc-tests/038-backup-root-corruption/test.sh
> 
> diff --git a/tests/misc-tests/038-backup-root-corruption/test.sh b/tests/misc-tests/038-backup-root-corruption/test.sh
> new file mode 100755
> index 000000000000..2fb117b3a928
> --- /dev/null
> +++ b/tests/misc-tests/038-backup-root-corruption/test.sh
> @@ -0,0 +1,50 @@
> +#!/bin/bash
> +# Test that a corrupted filesystem will correctly handle writing of 
> +# backup root
> +
> +source "$TEST_TOP/common"
> +
> +check_prereq mkfs.btrfs
> +check_prereq btrfs
> +check_prereq btrfs-corrupt-block
> +
> +setup_loopdevs 1
> +prepare_loopdevs
> +dev=${loopdevs[1]}

And the loop devices are not necessary at all

prepare_device

and be done.
> +
> +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$dev"
> +
> +# Create a file and unmount to commit some backup roots
> +run_check $SUDO_HELPER mount "$dev" "$TEST_MNT"
> +run_check touch "$TEST_MNT/file" && sync

sync is not necessary when it's followed by umount, besides that it
syncs all fileystems so it's an unnecessary slowdown

> +run_check $SUDO_HELPER umount "$TEST_MNT"
> +
> +# Ensure currently active backup slot is the expected one (slot 3)
> +backup2_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \

this should use run_check_stdout so we have the full output logged, as
the inspect-part is called several times I added a helper for that.

> +	| grep -A1 "backup 2" | grep backup_tree_root | awk '{print $2}')
> +
> +main_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
> +	| grep root | head -n1 | awk '{print $2}')
> +
> +[[ "$backup2_root_ptr" -eq "$main_root_ptr" ]] || _fail "Backup slot 2 is not in use"

[[ ]] is not necessary when it's a simple check that [ ] can do

All fixed and pushed.
diff mbox series

Patch

diff --git a/tests/misc-tests/038-backup-root-corruption/test.sh b/tests/misc-tests/038-backup-root-corruption/test.sh
new file mode 100755
index 000000000000..2fb117b3a928
--- /dev/null
+++ b/tests/misc-tests/038-backup-root-corruption/test.sh
@@ -0,0 +1,50 @@ 
+#!/bin/bash
+# Test that a corrupted filesystem will correctly handle writing of 
+# backup root
+
+source "$TEST_TOP/common"
+
+check_prereq mkfs.btrfs
+check_prereq btrfs
+check_prereq btrfs-corrupt-block
+
+setup_loopdevs 1
+prepare_loopdevs
+dev=${loopdevs[1]}
+
+run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$dev"
+
+# Create a file and unmount to commit some backup roots
+run_check $SUDO_HELPER mount "$dev" "$TEST_MNT"
+run_check touch "$TEST_MNT/file" && sync
+run_check $SUDO_HELPER umount "$TEST_MNT"
+
+# Ensure currently active backup slot is the expected one (slot 3)
+backup2_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
+	| grep -A1 "backup 2" | grep backup_tree_root | awk '{print $2}')
+
+main_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
+	| grep root | head -n1 | awk '{print $2}')
+
+[[ "$backup2_root_ptr" -eq "$main_root_ptr" ]] || _fail "Backup slot 2 is not in use"
+
+run_check "$TOP/btrfs-corrupt-block" -m $main_root_ptr -f generation "$dev"
+
+## should fail because the root is corrupted
+run_mustfail "Unexpected successful mount" $SUDO_HELPER mount "$dev" "$TEST_MNT"
+
+# Cycle mount with the backup to force rewrite of slot 3 
+run_check $SUDO_HELPER mount -ousebackuproot "$dev" "$TEST_MNT"
+run_check $SUDO_HELPER umount "$TEST_MNT"
+
+
+# Since we've used backup 1 as the usable root, then backup 2 should have been 
+# overwritten 
+main_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
+	| grep root | head -n1 | awk '{print $2}')
+backup2_new_root_ptr=$($SUDO_HELPER "$TOP/btrfs" inspect-internal dump-super -f "$dev" \
+	| grep -A1 "backup 2" | grep backup_tree_root | awk '{print $2}')
+
+[[ "$backup2_root_ptr" -ne "$backup2_new_root_ptr" ]] || _fail "Backup 2 not overwritten"
+
+cleanup_loopdevs