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                        filters=[iotests.filter_qmp_testfiles])
32
33    if 'return' in result:
34        assert result['return'] == {}
35        vm.run_job('job0')
36    iotests.log("")
37
38with iotests.FilePath('t.parallels') as disk_path, \
39     iotests.VM() as vm:
40
41    #
42    # Successful image creation (defaults)
43    #
44    iotests.log("=== Successful image creation (defaults) ===")
45    iotests.log("")
46
47    size = 128 * 1024 * 1024
48
49    vm.launch()
50    blockdev_create(vm, { 'driver': 'file',
51                          'filename': disk_path,
52                          'size': 0 })
53
54    vm.qmp_log('blockdev-add', driver='file', filename=disk_path,
55               node_name='imgfile', filters=[iotests.filter_qmp_testfiles])
56
57    blockdev_create(vm, { 'driver': imgfmt,
58                          'file': 'imgfile',
59                          'size': size })
60    vm.shutdown()
61
62    iotests.img_info_log(disk_path)
63
64    #
65    # Successful image creation (explicit defaults)
66    #
67    iotests.log("=== Successful image creation (explicit defaults) ===")
68    iotests.log("")
69
70    # Choose a different size to show that we got a new image
71    size = 64 * 1024 * 1024
72
73    vm.launch()
74    blockdev_create(vm, { 'driver': 'file',
75                          'filename': disk_path,
76                          'size': 0 })
77    blockdev_create(vm, { 'driver': imgfmt,
78                          'file': {
79                              'driver': 'file',
80                              'filename': disk_path,
81                          },
82                          'size': size,
83                          'cluster-size': 1048576 })
84    vm.shutdown()
85
86    iotests.img_info_log(disk_path)
87
88    #
89    # Successful image creation (with non-default options)
90    #
91    iotests.log("=== Successful image creation (with non-default options) ===")
92    iotests.log("")
93
94    # Choose a different size to show that we got a new image
95    size = 32 * 1024 * 1024
96
97    vm.launch()
98    blockdev_create(vm, { 'driver': 'file',
99                          'filename': disk_path,
100                          'size': 0 })
101    blockdev_create(vm, { 'driver': imgfmt,
102                          'file': {
103                              'driver': 'file',
104                              'filename': disk_path,
105                          },
106                          'size': size,
107                          'cluster-size': 65536 })
108    vm.shutdown()
109
110    iotests.img_info_log(disk_path)
111
112    #
113    # Invalid BlockdevRef
114    #
115    iotests.log("=== Invalid BlockdevRef ===")
116    iotests.log("")
117
118    vm.launch()
119    blockdev_create(vm, { 'driver': imgfmt,
120                          'file': "this doesn't exist",
121                          'size': size })
122    vm.shutdown()
123
124    #
125    # Zero size
126    #
127    iotests.log("=== Zero size ===")
128    iotests.log("")
129
130    vm.add_blockdev('driver=file,filename=%s,node-name=node0' % (disk_path))
131    vm.launch()
132    blockdev_create(vm, { 'driver': imgfmt,
133                          'file': 'node0',
134                          'size': 0 })
135    vm.shutdown()
136
137    iotests.img_info_log(disk_path)
138
139    #
140    # Maximum size
141    #
142    iotests.log("=== Maximum size ===")
143    iotests.log("")
144
145    vm.launch()
146    blockdev_create(vm, { 'driver': imgfmt,
147                          'file': 'node0',
148                          'size': 4503599627369984})
149    vm.shutdown()
150
151    iotests.img_info_log(disk_path)
152
153    #
154    # Invalid sizes
155    #
156
157    # TODO Negative image sizes aren't handled correctly, but this is a problem
158    # with QAPI's implementation of the 'size' type and affects other commands
159    # as well. Once this is fixed, we may want to add a test case here.
160
161    # 1. Misaligned image size
162    # 2. 2^64 - 512
163    # 3. 2^63 = 8 EB (qemu-img enforces image sizes less than this)
164    # 4. 2^63 - 512 (generally valid, but with the image header the file will
165    #                exceed 63 bits)
166    # 5. 2^52 (512 bytes more than maximum image size)
167
168    iotests.log("=== Invalid sizes ===")
169    iotests.log("")
170
171    vm.launch()
172    for size in [ 1234, 18446744073709551104, 9223372036854775808,
173                  9223372036854775296, 4503599627370497 ]:
174        blockdev_create(vm, { 'driver': imgfmt,
175                              'file': 'node0',
176                              'size': size })
177    vm.shutdown()
178
179    #
180    # Invalid cluster size
181    #
182    iotests.log("=== Invalid cluster size ===")
183    iotests.log("")
184
185    vm.launch()
186    for csize in [ 1234, 128, 4294967296, 9223372036854775808,
187                   18446744073709551104, 0 ]:
188        blockdev_create(vm, { 'driver': imgfmt,
189                              'file': 'node0',
190                              'size': 67108864,
191                              'cluster-size': csize })
192    blockdev_create(vm, { 'driver': imgfmt,
193                          'file': 'node0',
194                          'size': 281474976710656,
195                          'cluster-size': 512 })
196    vm.shutdown()
197