Skip to contentSkip to conversation

btrfs-progs: Add test for btrfs fi usage output

PATCH In review Nikolay Borisov 2 participants 2 messages Nov 18, 2021 – Nov 25, 2021 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: Add test for btrfs fi usage output

There was a regression in btrfs progs release 5.15 due to changes in how the ratio for the various raid profiles got calculated and this in turn had a cascading effect on unallocated/allocated space reported.

Add a test to ensure this regression doesn't occur again.

---
 tests/cli-tests/016-btrfs-fi-usage/test.sh | 111 +++++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100755 tests/cli-tests/016-btrfs-fi-usage/test.sh
tests/cli-tests/016-btrfs-fi-usage/test.sh new file +111 -0
new file mode 100755
index 000000000000..6c1563f8e0ce
@@ -0,0 +1,111 @@
+#!/bin/bash
+# Tests 'btrfs fi usage' reports correct space/ratio with various RAID profiles
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+setup_root_helper
+setup_loopdevs 4
+prepare_loopdevs
+TEST_DEV=${loopdevs[1]}
+
+report_numbers()
+{
+	vars=($(run_check_stdout $SUDO_HELPER "$TOP/btrfs" filesystem usage -b "$TEST_MNT" | awk '
+	/Data ratio/ { ratio=$3 }
+	a {dev_alloc=$2; exit}
+	/Data,(DUP|RAID[0156][C34]{0,2}|single)/ { size=substr($2,6,length($2)-6); a=1 }
+END {print ratio " " size " " dev_alloc}'))
+
+	echo "${vars[@]}"
+}
+
+test_dup()
+{
+	run_check_mkfs_test_dev -ddup
+	run_check_mount_test_dev
+	vars=($(report_numbers))
+	data_chunk_size=${vars[1]}
+	used_on_dev=${vars[2]}
+	data_ratio=${vars[0]}
+
+	[[ $used_on_dev -eq $((2*$data_chunk_size)) ]] || _fail "DUP inconsistent chunk/device usage. Chunk: $data_chunk_size Device: $used_on_dev"
+
+	[[ "$data_ratio" = "2.00" ]] || _fail "DUP: Unexpected data ratio: $data_ratio (must be 2)"
+	run_check_umount_test_dev
+}
+
+test_raid1()
+{
+	# single is not raid1 but it has the same characteristics so put it in here
+	# as well
+	for i in single,1.00 raid1,2.00 raid1c3,3.00 raid1c4,4.00; do
+
+		# this allows to set the tuples to $1 and $2 respectively
+		OLDIFS=$IFS
+		IFS=","
+		set -- $i
+		IFS=$OLDIFS
+
+		run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -d$1 ${loopdevs[@]}
+		run_check_mount_test_dev
+		vars=($(report_numbers))
+		data_chunk_size=${vars[1]}
+		used_on_dev=${vars[2]}
+		data_ratio=${vars[0]}
+
+		[[ $used_on_dev -eq $data_chunk_size ]] || _fail "$1 inconsistent chunk/device usage. Chunk: $data_chunk_size Device: $used_on_dev"
+
+		[[ "$data_ratio" = "$2" ]] || _fail "$1: Unexpected data ratio: $data_ratio (must be $2)"
+
+		run_check_umount_test_dev
+	done
+}
+
+test_raid0()
+{
+	run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -draid0 ${loopdevs[@]}
+	run_check_mount_test_dev
+	vars=($(report_numbers))
+	data_chunk_size=${vars[1]}
+	used_on_dev=${vars[2]}
+	data_ratio=${vars[0]}
+
+	# Divide by 4 since 4 loopp devices are setup
+	[[ $used_on_dev -eq $(($data_chunk_size / 4)) ]] || _fail "raid0 inconsistent chunk/device usage. Chunk: $data_chunk_size Device: $used_on_dev"
+
+	[[ $data_ratio = "1.00" ]] || _fail "raid0: Unexpected data ratio: $data_ratio (must be 1.5)"
+	run_check_umount_test_dev
+}
+
+test_raid56()
+{
+	for i in raid5,1.33,3 raid6,2.00,2; do
+
+		# this allows to set the tuples to $1 and $2 respectively
+		OLDIFS=$IFS
+		IFS=","
+		set -- $i
+		IFS=$OLDIFS
+
+		run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f -d$1 ${loopdevs[@]}
+		run_check_mount_test_dev
+		vars=($(report_numbers))
+		data_chunk_size=${vars[1]}
+		used_on_dev=${vars[2]}
+		data_ratio=${vars[0]}
+
+		[[ $used_on_dev -eq $(($data_chunk_size / $3)) ]] || _fail "$i inconsistent chunk/device usage. Chunk: $data_chunk_size Device: $used_on_dev"
+
+		[[ $data_ratio = "$2" ]] || _fail "$1: Unexpected data ratio: $data_ratio (must be $2)"
+
+		run_check_umount_test_dev
+	done
+}
+
+test_dup
+test_raid1
+test_raid0
+test_raid56
+
+cleanup_loopdevs
Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Nikolay Borisov

There was a regression in btrfs progs release 5.15 due to changes in how the ratio for the various raid profiles got calculated and this in turn had a cascading effect on unallocated/allocated space reported.

Add a test to ensure this regression doesn't occur again.

Added to devel, thanks.

tests/cli-tests/016-btrfs-fi-usage/test.sh line 14

Quoted 3 lines
tests/cli-tests/016-btrfs-fi-usage/test.sh new file +3 -0
+report_numbers()
+{
+	vars=($(run_check_stdout $SUDO_HELPER "$TOP/btrfs" filesystem usage -b "$TEST_MNT" | awk '

Please declare local variables

Quoted 6 lines
tests/cli-tests/016-btrfs-fi-usage/test.sh new file +6 -0
+	/Data ratio/ { ratio=$3 }
+	a {dev_alloc=$2; exit}
+	/Data,(DUP|RAID[0156][C34]{0,2}|single)/ { size=substr($2,6,length($2)-6); a=1 }
+END {print ratio " " size " " dev_alloc}'))
+
+	echo "${vars[@]}"

tests/cli-tests/016-btrfs-fi-usage/test.sh line 30

Quoted 10 lines
tests/cli-tests/016-btrfs-fi-usage/test.sh new file +10 -0
+}
+
+test_dup()
+{
+	run_check_mkfs_test_dev -ddup
+	run_check_mount_test_dev
+	vars=($(report_numbers))
+	data_chunk_size=${vars[1]}
+	used_on_dev=${vars[2]}
+	data_ratio=${vars[0]}

And the whole bunch

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	[[ $used_on_dev -eq $((2*$data_chunk_size)) ]] || _fail "DUP inconsistent chunk/device usage. Chunk: $data_chunk_size Device: $used_on_dev"

Though there's no limit for the line width, if it can be at the pipe/|| point it's preferred, in this case the _fail is not hidden in the middle of the long command.

All fixed.