btrfs-progs: introduce TEST_TOP and INTERNAL_BIN for tests
[platform/upstream/btrfs-progs.git] / tests / misc-tests / 025-zstd-compression / test.sh
1 #!/bin/bash
2 # Test zstd compression support on a prebuilt btrfs image
3
4 source "$TEST_TOP/common"
5
6 check_prereq btrfs
7 check_global_prereq md5sum
8
9 # Extract the test image
10 image=$(extract_image compress.raw.xz)
11
12 check_dump_tree() {
13         local image=$1
14         local string=$2
15
16         run_check_stdout "$TOP/btrfs" inspect-internal dump-tree "$image" \
17                 | grep -q "$string" \
18                 || _fail "btrfs inspect-internal dump-tree didn't print $string"
19 }
20
21 # Check that there are blocks of each compression type
22 check_dump_tree "$image" "extent compression 1 (zlib)"
23 check_dump_tree "$image" "extent compression 2 (lzo)"
24 check_dump_tree "$image" "extent compression 3 (zstd)"
25
26 # Check that the filesystem has incompat COMPRESS_ZSTD
27 run_check_stdout "$TOP/btrfs" inspect-internal dump-super -f "$image" \
28         | grep -q COMPRESS_ZSTD \
29         || _fail "btrfs inspect-internal dump-super no incompat COMPRESS_ZSTD"
30
31 # Create a temporary directory and restore the filesystem
32 restore_tmp=$(mktemp --tmpdir -d btrfs-progs-022-zstd-compression.XXXXXXXXXX)
33 run_check "$TOP/btrfs" restore "$image" "$restore_tmp"
34
35 # Expect 3 files
36 num_files=$(ls -1 "$restore_tmp" | wc -l)
37 [ "$num_files" == 3 ] || _fail "number of files does not match"
38
39 check_md5() {
40         local file="$1"
41         local expect_md5="$2"
42
43         md5=$(run_check_stdout md5sum "$file" | cut -d ' ' -f 1)
44         [ "$md5" == "$expect_md5" ] \
45                 || _fail "$file digest $md5 does not match $expect_md5"
46 }
47
48 # Each should be 200K of zeros
49 expect_md5=$(dd if=/dev/zero bs=1K count=200 status=none | md5sum | cut -d ' ' -f 1)
50 check_md5 "$restore_tmp/zlib" "$expect_md5"
51 check_md5 "$restore_tmp/lzo" "$expect_md5"
52 check_md5 "$restore_tmp/zstd" "$expect_md5"
53
54 # Clean up
55 rm -r -- "$restore_tmp"
56 rm -- "$image"