xref: /openbmc/qemu/tests/qemu-iotests/212 (revision 47451466)
1#!/usr/bin/env python
2#
3# Test parallels and file image creation
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23import iotests
24from iotests import imgfmt
25
26iotests.verify_image_format(supported_fmts=['parallels'])
27iotests.verify_protocol(supported=['file'])
28
29def blockdev_create(vm, options):
30    result = vm.qmp_log('blockdev-create', job_id='job0', options=options)
31
32    if 'return' in result:
33        assert result['return'] == {}
34        vm.run_job('job0')
35    iotests.log("")
36
37with iotests.FilePath('t.parallels') as disk_path, \
38     iotests.VM() as vm:
39
40    #
41    # Successful image creation (defaults)
42    #
43    iotests.log("=== Successful image creation (defaults) ===")
44    iotests.log("")
45
46    size = 128 * 1024 * 1024
47
48    vm.launch()
49    blockdev_create(vm, { 'driver': 'file',
50                          'filename': disk_path,
51                          'size': 0 })
52
53    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
54               node_name='imgfile')
55
56    blockdev_create(vm, { 'driver': imgfmt,
57                          'file': 'imgfile',
58                          'size': size })
59    vm.shutdown()
60
61    iotests.img_info_log(disk_path)
62
63    #
64    # Successful image creation (explicit defaults)
65    #
66    iotests.log("=== Successful image creation (explicit defaults) ===")
67    iotests.log("")
68
69    # Choose a different size to show that we got a new image
70    size = 64 * 1024 * 1024
71
72    vm.launch()
73    blockdev_create(vm, { 'driver': 'file',
74                          'filename': disk_path,
75                          'size': 0 })
76    blockdev_create(vm, { 'driver': imgfmt,
77                          'file': {
78                              'driver': 'file',
79                              'filename': disk_path,
80                          },
81                          'size': size,
82                          'cluster-size': 1048576 })
83    vm.shutdown()
84
85    iotests.img_info_log(disk_path)
86
87    #
88    # Successful image creation (with non-default options)
89    #
90    iotests.log("=== Successful image creation (with non-default options) ===")
91    iotests.log("")
92
93    # Choose a different size to show that we got a new image
94    size = 32 * 1024 * 1024
95
96    vm.launch()
97    blockdev_create(vm, { 'driver': 'file',
98                          'filename': disk_path,
99                          'size': 0 })
100    blockdev_create(vm, { 'driver': imgfmt,
101                          'file': {
102                              'driver': 'file',
103                              'filename': disk_path,
104                          },
105                          'size': size,
106                          'cluster-size': 65536 })
107    vm.shutdown()
108
109    iotests.img_info_log(disk_path)
110
111    #
112    # Invalid BlockdevRef
113    #
114    iotests.log("=== Invalid BlockdevRef ===")
115    iotests.log("")
116
117    vm.launch()
118    blockdev_create(vm, { 'driver': imgfmt,
119                          'file': "this doesn't exist",
120                          'size': size })
121    vm.shutdown()
122
123    #
124    # Zero size
125    #
126    iotests.log("=== Zero size ===")
127    iotests.log("")
128
129    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
130    vm.launch()
131    blockdev_create(vm, { 'driver': imgfmt,
132                          'file': 'node0',
133                          'size': 0 })
134    vm.shutdown()
135
136    iotests.img_info_log(disk_path)
137
138    #
139    # Maximum size
140    #
141    iotests.log("=== Maximum size ===")
142    iotests.log("")
143
144    vm.launch()
145    blockdev_create(vm, { 'driver': imgfmt,
146                          'file': 'node0',
147                          'size': 4503599627369984})
148    vm.shutdown()
149
150    iotests.img_info_log(disk_path)
151
152    #
153    # Invalid sizes
154    #
155
156    # TODO Negative image sizes aren't handled correctly, but this is a problem
157    # with QAPI's implementation of the 'size' type and affects other commands
158    # as well. Once this is fixed, we may want to add a test case here.
159
160    # 1. Misaligned image size
161    # 2. 2^64 - 512
162    # 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
163    # 4. 2^63 - 512 (generally valid, but with the image header the file will
164    #                exceed 63 bits)
165    # 5. 2^52 (512 bytes more than maximum image size)
166
167    iotests.log("=== Invalid sizes ===")
168    iotests.log("")
169
170    vm.launch()
171    for size in [ 1234, 18446744073709551104, 9223372036854775808,
172                  9223372036854775296, 4503599627370497 ]:
173        blockdev_create(vm, { 'driver': imgfmt,
174                              'file': 'node0',
175                              'size': size })
176    vm.shutdown()
177
178    #
179    # Invalid cluster size
180    #
181    iotests.log("=== Invalid cluster size ===")
182    iotests.log("")
183
184    vm.launch()
185    for csize in [ 1234, 128, 4294967296, 9223372036854775808,
186                   18446744073709551104, 0 ]:
187        blockdev_create(vm, { 'driver': imgfmt,
188                              'file': 'node0',
189                              'size': 67108864,
190                              'cluster-size': csize })
191    blockdev_create(vm, { 'driver': imgfmt,
192                          'file': 'node0',
193                          'size': 281474976710656,
194                          'cluster-size': 512 })
195    vm.shutdown()
196