btrfs-progs: tests: enhance API to request type of the converted filesystem
[platform/upstream/btrfs-progs.git] / tests / common.convert
1 #!/bin/bash
2 # helpers for btrfs-convert tests
3
4 populate_fs() {
5
6         for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
7                 generate_dataset "$dataset_type"
8         done
9 }
10
11 # verbose message before the test, same arguments as convert_test
12 convert_test_preamble() {
13         local features
14         local msg
15
16         features="$1"
17         msg="$2"
18         shift 3
19         echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
20         echo "creating ext image with: $@" >> "$RESULTS"
21 }
22
23 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
24 #  size is 512MB
25 #  $1: type of the filesystem
26 #  $2+: free form, command to create the filesystem, with appended -F
27 convert_test_prep_fs() {
28         local fstype
29
30         fstype="$1"
31         shift
32         # TEST_DEV not removed as the file might have special permissions, eg.
33         # when test image is on NFS and would not be writable for root
34         run_check truncate -s 0 "$TEST_DEV"
35         # 256MB is the smallest acceptable btrfs image.
36         run_check truncate -s 512M "$TEST_DEV"
37         run_check "$@" -F "$TEST_DEV"
38
39         # create a file to check btrfs-convert can convert regular file correct
40         run_check_mount_test_dev
41
42         # create a file inside the fs before convert, to make sure there is
43         # data covering btrfs backup superblock range (64M)
44         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
45                 of="$TEST_MNT/convert_space_holder"
46 }
47
48 # generate md5 checksums of files on $TEST_MNT
49 # $1: path where the checksums will be stored
50 convert_test_gen_checksums() {
51         _assert_path "$1"
52
53         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
54                 count=1 >/dev/null 2>&1
55         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
56 }
57 # list $TEST_MNT data set file permissions.
58 # $1: path where the permissions will be stored
59 convert_test_perm() {
60         local PERMTMP
61
62         _assert_path "$1"
63         PERMTMP="$1"
64         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
65
66         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
67                 count=1 >/dev/null 2>&1
68         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -fprint "$FILES_LIST"
69         # Fix directory entries order
70         sort "$FILES_LIST" -o "$FILES_LIST"
71         for file in `cat "$FILES_LIST"` ;do
72                 run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
73         done
74         rm -- "$FILES_LIST"
75 }
76 # list acls of files on $TEST_MNT
77 # $1: path where the acls will be stored
78 convert_test_acl() {
79         local ACLSTMP
80         ACLTMP="$1"
81         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
82
83         run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
84         # Fix directory entries order
85         sort "$FILES_LIST" -o "$FILES_LIST"
86         for file in `cat "$FILES_LIST"`;do
87                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
88         done
89         rm -- "$FILES_LIST"
90 }
91
92 # do conversion with given features and nodesize, fsck afterwards
93 # $1: features, argument of -O, can be empty
94 # $2: nodesize, argument of -N, can be empty
95 convert_test_do_convert() {
96         run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
97         run_check "$TOP/btrfs" check "$TEST_DEV"
98         run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
99 }
100
101 # post conversion check, verify file permissions.
102 # $1: file with ext permissions.
103 convert_test_post_check_permissions() {
104         local EXT_PERMTMP
105         local BTRFS_PERMTMP
106
107         _assert_path "$1"
108         EXT_PERMTMP="$1"
109         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
110         convert_test_perm "$BTRFS_PERMTMP"
111
112         btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
113         ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`
114
115         if [ "$btrfs_perm" != "$ext_perm" ];
116         then
117                 btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
118                 ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
119                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
120         fi
121
122         rm -- "$BTRFS_PERMTMP"
123 }
124 # post conversion check, compare BTRFS file acls against EXT.
125 # $1: file with ext acls.
126 convert_test_post_check_acl() {
127         local EXT_ACLTMP
128         local BTRFS_ACLTMP
129
130         _assert_path "$1"
131         EXT_ACLTMP="$1"
132         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
133         convert_test_acl "$BTRFS_ACLTMP"
134
135         btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
136         ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`
137
138         if [ "$btrfs_acl" != "$ext_acl" ]
139         then
140                 btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
141                 ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
142                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
143         fi
144
145         rm -- "$BTRFS_ACLTMP"
146 }
147
148 # post conversion checks, verify md5sums
149 convert_test_post_check_checksums() {
150         _assert_path "$1"
151         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
152                 grep -q 'FAILED' && _fail "file validation failed"
153 }
154
155 # post conversion checks, all three in one call, on an unmounted image
156 # $1: file with checksums
157 # $2: file with permissions.
158 # $3: file with acl entries.
159 convert_test_post_checks_all() {
160         _assert_path "$1"
161         _assert_path "$2"
162         _assert_path "$3"
163
164         run_check_mount_test_dev
165         convert_test_post_check_checksums "$1"
166         convert_test_post_check_permissions "$2"
167         convert_test_post_check_acl "$3"
168
169         # Create a large file to trigger data chunk allocation
170         generate_dataset "large"
171         run_check_umount_test_dev
172 }
173
174 # do rollback and fsck
175 convert_test_post_rollback() {
176         run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
177         run_check fsck -n -t ext2,ext3,ext4 "$TEST_DEV"
178 }
179
180 # simple wrapper for a convert test
181 # $1: type of the converted filesystem
182 # $2: btrfs features, argument to -O
183 # $3: description of the test "ext2 8k nodesize"
184 # $4: nodesize value
185 # $5 + rest: command to create the ext2 image
186 convert_test() {
187         local fstype
188         local features
189         local nodesize
190         local msg
191         local CHECKSUMTMP
192         local EXT_PERMTMP
193         local EXT_ACLTMP
194
195         fstype="$1"
196         features="$2"
197         msg="$3"
198         nodesize="$4"
199         shift 4
200         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
201         convert_test_prep_fs "$fstype" "$@"
202         populate_fs
203         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
204         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
205         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
206         convert_test_gen_checksums "$CHECKSUMTMP"
207         convert_test_perm "$EXT_PERMTMP"
208         convert_test_acl "$EXT_ACLTMP"
209
210         run_check_umount_test_dev
211
212         convert_test_do_convert "$features" "$nodesize"
213         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
214         rm -- "$CHECKSUMTMP"
215         rm -- "$EXT_PERMTMP"
216         rm -- "$EXT_ACLTMP"
217
218         convert_test_post_rollback
219 }