xref: /qemu/tests/qemu-iotests/287 (revision abff1abf)
1#!/usr/bin/env bash
2#
3# Test case for an image using zstd compression
4#
5# Copyright (c) 2020 Virtuozzo International GmbH
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=dplotnikov@virtuozzo.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27status=1	# failure is the default!
28
29# standard environment
30. ./common.rc
31. ./common.filter
32
33# This tests qocw2-specific low-level functionality
34_supported_fmt qcow2
35_supported_proto file
36_supported_os Linux
37_unsupported_imgopts 'compat=0.10' data_file
38
39COMPR_IMG="$TEST_IMG.compressed"
40RAND_FILE="$TEST_DIR/rand_data"
41
42_cleanup()
43{
44    _cleanup_test_img
45    _rm_test_img "$COMPR_IMG"
46    rm -f "$RAND_FILE"
47}
48trap "_cleanup; exit \$status" 0 1 2 3 15
49
50# for all the cases
51CLUSTER_SIZE=65536
52
53# Check if we can run this test.
54if IMGOPTS='compression_type=zstd' _make_test_img 64M |
55    grep "Invalid parameter 'zstd'"; then
56    _notrun "ZSTD is disabled"
57fi
58
59echo
60echo "=== Testing compression type incompatible bit setting for zlib ==="
61echo
62_make_test_img -o compression_type=zlib 64M
63$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
64
65echo
66echo "=== Testing compression type incompatible bit setting for zstd ==="
67echo
68_make_test_img -o compression_type=zstd 64M
69$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
70
71echo
72echo "=== Testing zlib with incompatible bit set ==="
73echo
74_make_test_img -o compression_type=zlib 64M
75$PYTHON qcow2.py "$TEST_IMG" set-feature-bit incompatible 3
76# to make sure the bit was actually set
77$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
78
79if $QEMU_IMG info "$TEST_IMG" >/dev/null 2>&1 ; then
80    echo "Error: The image opened successfully. The image must not be opened."
81fi
82
83echo
84echo "=== Testing zstd with incompatible bit unset ==="
85echo
86_make_test_img -o compression_type=zstd 64M
87$PYTHON qcow2.py "$TEST_IMG" set-header incompatible_features 0
88# to make sure the bit was actually unset
89$PYTHON qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
90
91if $QEMU_IMG info "$TEST_IMG" >/dev/null 2>&1 ; then
92    echo "Error: The image opened successfully. The image must not be opened."
93fi
94
95echo
96echo "=== Testing compression type values ==="
97echo
98# zlib=0
99_make_test_img -o compression_type=zlib 64M
100peek_file_be "$TEST_IMG" 104 1
101echo
102
103# zstd=1
104_make_test_img -o compression_type=zstd 64M
105peek_file_be "$TEST_IMG" 104 1
106echo
107
108echo
109echo "=== Testing simple reading and writing with zstd ==="
110echo
111_make_test_img -o compression_type=zstd 64M
112$QEMU_IO -c "write -c -P 0xAC 64K 64K " "$TEST_IMG" | _filter_qemu_io
113$QEMU_IO -c "read -P 0xAC 64K 64K " "$TEST_IMG" | _filter_qemu_io
114# read on the cluster boundaries
115$QEMU_IO -c "read -v 131070 8 " "$TEST_IMG" | _filter_qemu_io
116$QEMU_IO -c "read -v 65534 8" "$TEST_IMG" | _filter_qemu_io
117
118echo
119echo "=== Testing adjacent clusters reading and writing with zstd ==="
120echo
121_make_test_img -o compression_type=zstd 64M
122$QEMU_IO -c "write -c -P 0xAB 0 64K " "$TEST_IMG" | _filter_qemu_io
123$QEMU_IO -c "write -c -P 0xAC 64K 64K " "$TEST_IMG" | _filter_qemu_io
124$QEMU_IO -c "write -c -P 0xAD 128K 64K " "$TEST_IMG" | _filter_qemu_io
125
126$QEMU_IO -c "read -P 0xAB 0 64k " "$TEST_IMG" | _filter_qemu_io
127$QEMU_IO -c "read -P 0xAC 64K 64k " "$TEST_IMG" | _filter_qemu_io
128$QEMU_IO -c "read -P 0xAD 128K 64k " "$TEST_IMG" | _filter_qemu_io
129
130echo
131echo "=== Testing incompressible cluster processing with zstd ==="
132echo
133# create a 2M image and fill it with 1M likely incompressible data
134# and 1M compressible data
135dd if=/dev/urandom of="$RAND_FILE" bs=1M count=1 seek=1
136QEMU_IO_OPTIONS="$QEMU_IO_OPTIONS_NO_FMT" \
137$QEMU_IO -f raw -c "write -P 0xFA 0 1M" "$RAND_FILE" | _filter_qemu_io
138
139$QEMU_IMG convert -f raw -O $IMGFMT -c \
140-o "$(_optstr_add "$IMGOPTS" "compression_type=zlib")" "$RAND_FILE" \
141"$TEST_IMG" | _filter_qemu_io
142
143$QEMU_IMG convert -O $IMGFMT -c \
144-o "$(_optstr_add "$IMGOPTS" "compression_type=zstd")" "$TEST_IMG" \
145"$COMPR_IMG" | _filter_qemu_io
146
147$QEMU_IMG compare "$TEST_IMG" "$COMPR_IMG"
148
149# success, all done
150echo "*** done"
151rm -f $seq.full
152status=0
153