aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/178
blob: f09b27caacf3eeef0b9d66ce4792f31c9118d675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
#
# qemu-img measure sub-command tests
#
# Copyright (C) 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# creator
owner=stefanha@redhat.com

seq=`basename $0`
echo "QA output created by $seq"

status=1    # failure is the default!

_cleanup()
{
    _cleanup_test_img
    _rm_test_img "$TEST_IMG.converted"
}
trap "_cleanup; exit \$status" 0 1 2 3 15

# get standard environment, filters and checks
. ./common.rc
. ./common.filter
. ./common.pattern

_supported_fmt raw qcow2
_supported_proto file
_supported_os Linux
_require_working_luks

echo "== Input validation =="
echo

_make_test_img 1G

$QEMU_IMG measure # missing arguments
$QEMU_IMG measure --size 2G "$TEST_IMG" # only one allowed
$QEMU_IMG measure "$TEST_IMG" a # only one filename allowed
$QEMU_IMG measure --object secret,id=sec0,data=MTIzNDU2,format=base64 # size or filename needed
$QEMU_IMG measure --image-opts # missing filename
$QEMU_IMG measure -f qcow2 # missing filename
$QEMU_IMG measure -l snap1 # missing filename
$QEMU_IMG measure -o , # invalid option list
$QEMU_IMG measure -l snapshot.foo # invalid snapshot option
$QEMU_IMG measure --output foo # invalid output format
$QEMU_IMG measure --size -1 # invalid image size
$QEMU_IMG measure -O foo "$TEST_IMG" # unknown image file format

make_test_img_with_fmt() {
    # Shadow global variables within this function
    local IMGFMT="$1"
    _make_test_img --no-opts "$2"
}

qemu_io_with_fmt() {
    # Shadow global variables within this function
    local QEMU_IO_OPTIONS=$(echo "$QEMU_IO_OPTIONS" | sed "s/-f $IMGFMT/-f $1/")
    shift
    $QEMU_IO "$@"
}

# The proof is in the pudding: converted image size cannot be larger than the
# required size.
#
# Note: if a change to the image format code causes the file size to change,
# then this test fails!  This is good because it's a reminder to check that the
# required size is still at least as big as the actual converted file size.
convert_and_show_size() {
    local fmt="$1"
    shift
    $QEMU_IMG convert -f "$fmt" -O "$IMGFMT" "$TEST_IMG" "$@" "$TEST_IMG.converted"
    stat -c "converted image file size in bytes: %s" "$TEST_IMG.converted"
}

for ofmt in human json; do
    echo
    echo "== Size calculation for a new file ($ofmt) =="
    echo

    # Try a few interesting sizes
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 0
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 64G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 256G
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 1T
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 2P
    $QEMU_IMG measure --output=$ofmt -O "$IMGFMT" --size 7E

    # Always test the raw input files but also IMGFMT
    for fmt in $(echo -e "raw\n$IMGFMT\n" | sort -u); do
        echo
        echo "== Empty $fmt input image ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 0
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"

        echo
        echo "== $fmt input image with data ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 1G
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        qemu_io_with_fmt "$fmt" -c "write 512 512" "$TEST_IMG" | _filter_qemu_io
        qemu_io_with_fmt "$fmt" -c "write 64K 64K" "$TEST_IMG" | _filter_qemu_io
        if [ "$fmt" = "qcow2" ]; then
            $QEMU_IMG snapshot -c snapshot1 "$TEST_IMG"
        fi
        qemu_io_with_fmt "$fmt" -c "write 128M 63K" "$TEST_IMG" | _filter_qemu_io
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"

        if [ "$fmt" = "qcow2" ]; then
            echo
            echo "== $fmt input image with internal snapshot ($ofmt) =="
            echo
            $QEMU_IMG measure --output=$ofmt -f "$fmt" -l snapshot1 \
                              -O "$IMGFMT" "$TEST_IMG"
            echo
            convert_and_show_size "$fmt" -l snapshot1
        fi

        if [ "$IMGFMT" = "qcow2" ]; then
            echo
            echo "== $fmt input image and a backing file ($ofmt) =="
            echo
            # The backing file doesn't need to exist :)
            $QEMU_IMG measure --output=$ofmt -o backing_file=x \
                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"

            echo
            echo "== $fmt input image and LUKS encryption =="
            echo
            $QEMU_IMG measure --output=$ofmt \
                              --object secret,id=sec0,data=base \
                              -o encrypt.format=luks,encrypt.key-secret=sec0,encrypt.iter-time=10 \
                              -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        fi

        echo
        echo "== $fmt input image and preallocation ($ofmt) =="
        echo
        $QEMU_IMG measure --output=$ofmt -o preallocation=full \
                          -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt" -o preallocation=full

        echo
        echo "== Fully-allocated $fmt input image ($ofmt) =="
        echo
        make_test_img_with_fmt "$fmt" 8M
        qemu_io_with_fmt "$fmt" -c "write 0 8M" "$TEST_IMG" | _filter_qemu_io
        $QEMU_IMG measure --output=$ofmt -f "$fmt" -O "$IMGFMT" "$TEST_IMG"
        echo
        convert_and_show_size "$fmt"
    done
done

# success, all done
echo "*** done"
rm -f $seq.full
status=0