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