4 # qemu-img measure sub-command tests
6 # Copyright (C) 2017 Red Hat, Inc.
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.
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.
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/>.
23 owner=stefanha@redhat.com
26 echo "QA output created by $seq"
28 status=1 # failure is the default!
33 _rm_test_img "$TEST_IMG.converted"
35 trap "_cleanup; exit \$status" 0 1 2 3 15
37 # get standard environment, filters and checks
42 _supported_fmt raw qcow2
47 echo "== Input validation =="
52 $QEMU_IMG measure # missing arguments
53 $QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed
54 $QEMU_IMG measure "$TEST_IMG" a # only one filename allowed
55 $QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # size or filename needed
56 $QEMU_IMG measure --image-opts # missing filename
57 $QEMU_IMG measure -f qcow2 # missing filename
58 $QEMU_IMG measure -l snap1 # missing filename
59 $QEMU_IMG measure -o , # invalid option list
60 $QEMU_IMG measure -l snapshot.foo=bar # invalid snapshot option
61 $QEMU_IMG measure --output foo # invalid output format
62 $QEMU_IMG measure --size -1 # invalid image size
63 $QEMU_IMG measure -O foo "$TEST_IMG" # unknown image file format
65 make_test_img_with_fmt() {
66 # Shadow global variables within this function
68 _make_test_img --no-opts "$2"
72 # Shadow global variables within this function
73 local QEMU_IO_OPTIONS=$(echo "$QEMU_IO_OPTIONS" | sed "s/-f $IMGFMT/-f $1/")
78 # The proof is in the pudding: converted image size cannot be larger than the
81 # Note: if a change to the image format code causes the file size to change,
82 # then this test fails! This is good because it's a reminder to check that the
83 # required size is still at least as big as the actual converted file size.
84 convert_and_show_size() {
87 $QEMU_IMG convert -f "$fmt" -O "$IMGFMT" "$TEST_IMG" "$@" "$TEST_IMG.converted"
88 stat -c "converted image file size in bytes: %s" "$TEST_IMG.converted"
91 for ofmt in human json; do
93 echo "== Size calculation for a new file ($ofmt) =="
96 # Try a few interesting sizes
97 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 0
98 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2G
99 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 64G
100 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 256G
101 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 1T
102 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2P
103 $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 7E
105 # Always test the raw input files but also IMGFMT
106 for fmt in $(echo -e "raw\n$IMGFMT\n" | sort -u); do
108 echo "== Empty $fmt input image ($ofmt) =="
110 make_test_img_with_fmt "$fmt" 0
111 $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
113 convert_and_show_size "$fmt"
116 echo "== $fmt input image with data ($ofmt) =="
118 make_test_img_with_fmt "$fmt" 1G
119 $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
120 qemu_io_with_fmt "$fmt" -c "write 512 512" "$TEST_IMG" | _filter_qemu_io
121 qemu_io_with_fmt "$fmt" -c "write 64K 64K" "$TEST_IMG" | _filter_qemu_io
122 if [ "$fmt" = "qcow2" ]; then
123 $QEMU_IMG snapshot -c snapshot1 "$TEST_IMG"
125 qemu_io_with_fmt "$fmt" -c "write 128M 63K" "$TEST_IMG" | _filter_qemu_io
126 $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
128 convert_and_show_size "$fmt"
130 if [ "$fmt" = "qcow2" ]; then
132 echo "== $fmt input image with internal snapshot ($ofmt) =="
134 $QEMU_IMG measure --output=$ofmt -f "$fmt" -l snapshot1 \
135 -O "$IMGFMT" "$TEST_IMG"
137 convert_and_show_size "$fmt" -l snapshot1
140 if [ "$IMGFMT" = "qcow2" ]; then
142 echo "== $fmt input image and a backing file ($ofmt) =="
144 # The backing file doesn't need to exist :)
145 $QEMU_IMG measure --output=$ofmt -o backing_file=x \
146 -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
149 echo "== $fmt input image and LUKS encryption =="
151 $QEMU_IMG measure --output=$ofmt \
152 --object secret,id=sec0,data=base \
153 -o encrypt.format=luks,encrypt.key-secret=sec0,encrypt.iter-time=10 \
154 -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
158 echo "== $fmt input image and preallocation ($ofmt) =="
160 $QEMU_IMG measure --output=$ofmt -o preallocation=full \
161 -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
163 convert_and_show_size "$fmt" -o preallocation=full
166 echo "== Fully-allocated $fmt input image ($ofmt) =="
168 make_test_img_with_fmt "$fmt" 8M
169 qemu_io_with_fmt "$fmt" -c "write 0 8M" "$TEST_IMG" | _filter_qemu_io
170 $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
172 convert_and_show_size "$fmt"