]> Git Repo - qemu.git/blob - tests/qemu-iotests/040
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2018-09-25' into...
[qemu.git] / tests / qemu-iotests / 040
1 #!/usr/bin/env python
2 #
3 # Tests for image block commit.
4 #
5 # Copyright (C) 2012 IBM, Corp.
6 # Copyright (C) 2012 Red Hat, Inc.
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 # Test for live block commit
22 # Derived from Image Streaming Test 030
23
24 import time
25 import os
26 import iotests
27 from iotests import qemu_img, qemu_io
28 import struct
29 import errno
30
31 backing_img = os.path.join(iotests.test_dir, 'backing.img')
32 mid_img = os.path.join(iotests.test_dir, 'mid.img')
33 test_img = os.path.join(iotests.test_dir, 'test.img')
34
35 class ImageCommitTestCase(iotests.QMPTestCase):
36     '''Abstract base class for image commit test cases'''
37
38     def wait_for_complete(self, need_ready=False):
39         completed = False
40         ready = False
41         while not completed:
42             for event in self.vm.get_qmp_events(wait=True):
43                 if event['event'] == 'BLOCK_JOB_COMPLETED':
44                     self.assert_qmp_absent(event, 'data/error')
45                     self.assert_qmp(event, 'data/type', 'commit')
46                     self.assert_qmp(event, 'data/device', 'drive0')
47                     self.assert_qmp(event, 'data/offset', event['data']['len'])
48                     if need_ready:
49                         self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event")
50                     completed = True
51                 elif event['event'] == 'BLOCK_JOB_READY':
52                     ready = True
53                     self.assert_qmp(event, 'data/type', 'commit')
54                     self.assert_qmp(event, 'data/device', 'drive0')
55                     self.vm.qmp('block-job-complete', device='drive0')
56
57         self.assert_no_active_block_jobs()
58         self.vm.shutdown()
59
60     def run_commit_test(self, top, base, need_ready=False, node_names=False):
61         self.assert_no_active_block_jobs()
62         if node_names:
63             result = self.vm.qmp('block-commit', device='drive0', top_node=top, base_node=base)
64         else:
65             result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
66         self.assert_qmp(result, 'return', {})
67         self.wait_for_complete(need_ready)
68
69     def run_default_commit_test(self):
70         self.assert_no_active_block_jobs()
71         result = self.vm.qmp('block-commit', device='drive0')
72         self.assert_qmp(result, 'return', {})
73         self.wait_for_complete()
74
75 class TestSingleDrive(ImageCommitTestCase):
76     # Need some space after the copied data so that throttling is effective in
77     # tests that use it rather than just completing the job immediately
78     image_len = 2 * 1024 * 1024
79     test_len = 1 * 1024 * 256
80
81     def setUp(self):
82         iotests.create_image(backing_img, self.image_len)
83         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
84         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
85         qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
86         qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
87         self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=mid,backing.backing.node-name=base", interface="none")
88         if iotests.qemu_default_machine == 's390-ccw-virtio':
89             self.vm.add_device("virtio-scsi-ccw")
90         else:
91             self.vm.add_device("virtio-scsi-pci")
92
93         self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
94         self.vm.launch()
95
96     def tearDown(self):
97         self.vm.shutdown()
98         os.remove(test_img)
99         os.remove(mid_img)
100         os.remove(backing_img)
101
102     def test_commit(self):
103         self.run_commit_test(mid_img, backing_img)
104         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
105         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
106
107     def test_commit_node(self):
108         self.run_commit_test("mid", "base", node_names=True)
109         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
110         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
111
112     def test_device_not_found(self):
113         result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
114         self.assert_qmp(result, 'error/class', 'DeviceNotFound')
115
116     def test_top_same_base(self):
117         self.assert_no_active_block_jobs()
118         result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
119         self.assert_qmp(result, 'error/class', 'GenericError')
120         self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
121
122     def test_top_invalid(self):
123         self.assert_no_active_block_jobs()
124         result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
125         self.assert_qmp(result, 'error/class', 'GenericError')
126         self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
127
128     def test_base_invalid(self):
129         self.assert_no_active_block_jobs()
130         result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
131         self.assert_qmp(result, 'error/class', 'GenericError')
132         self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
133
134     def test_top_node_invalid(self):
135         self.assert_no_active_block_jobs()
136         result = self.vm.qmp('block-commit', device='drive0', top_node='badfile', base_node='base')
137         self.assert_qmp(result, 'error/class', 'GenericError')
138         self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
139
140     def test_base_node_invalid(self):
141         self.assert_no_active_block_jobs()
142         result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='badfile')
143         self.assert_qmp(result, 'error/class', 'GenericError')
144         self.assert_qmp(result, 'error/desc', "Cannot find device= nor node_name=badfile")
145
146     def test_top_path_and_node(self):
147         self.assert_no_active_block_jobs()
148         result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', top='%s' % mid_img)
149         self.assert_qmp(result, 'error/class', 'GenericError')
150         self.assert_qmp(result, 'error/desc', "'top-node' and 'top' are mutually exclusive")
151
152     def test_base_path_and_node(self):
153         self.assert_no_active_block_jobs()
154         result = self.vm.qmp('block-commit', device='drive0', top_node='mid', base_node='base', base='%s' % backing_img)
155         self.assert_qmp(result, 'error/class', 'GenericError')
156         self.assert_qmp(result, 'error/desc', "'base-node' and 'base' are mutually exclusive")
157
158     def test_top_is_active(self):
159         self.run_commit_test(test_img, backing_img, need_ready=True)
160         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
161         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
162
163     def test_top_is_default_active(self):
164         self.run_default_commit_test()
165         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
166         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
167
168     def test_top_and_base_reversed(self):
169         self.assert_no_active_block_jobs()
170         result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
171         self.assert_qmp(result, 'error/class', 'GenericError')
172         self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
173
174     def test_top_and_base_node_reversed(self):
175         self.assert_no_active_block_jobs()
176         result = self.vm.qmp('block-commit', device='drive0', top_node='base', base_node='top')
177         self.assert_qmp(result, 'error/class', 'GenericError')
178         self.assert_qmp(result, 'error/desc', "'top' is not in this backing file chain")
179
180     def test_top_node_in_wrong_chain(self):
181         self.assert_no_active_block_jobs()
182
183         result = self.vm.qmp('blockdev-add', driver='null-co', node_name='null')
184         self.assert_qmp(result, 'return', {})
185
186         result = self.vm.qmp('block-commit', device='drive0', top_node='null', base_node='base')
187         self.assert_qmp(result, 'error/class', 'GenericError')
188         self.assert_qmp(result, 'error/desc', "'null' is not in this backing file chain")
189
190     # When the job is running on a BB that is automatically deleted on hot
191     # unplug, the job is cancelled when the device disappears
192     def test_hot_unplug(self):
193         if self.image_len == 0:
194             return
195
196         self.assert_no_active_block_jobs()
197         result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
198                              base=backing_img, speed=(self.image_len / 4))
199         self.assert_qmp(result, 'return', {})
200         result = self.vm.qmp('device_del', id='scsi0')
201         self.assert_qmp(result, 'return', {})
202
203         cancelled = False
204         deleted = False
205         while not cancelled or not deleted:
206             for event in self.vm.get_qmp_events(wait=True):
207                 if event['event'] == 'DEVICE_DELETED':
208                     self.assert_qmp(event, 'data/device', 'scsi0')
209                     deleted = True
210                 elif event['event'] == 'BLOCK_JOB_CANCELLED':
211                     self.assert_qmp(event, 'data/device', 'drive0')
212                     cancelled = True
213                 elif event['event'] == 'JOB_STATUS_CHANGE':
214                     self.assert_qmp(event, 'data/id', 'drive0')
215                 else:
216                     self.fail("Unexpected event %s" % (event['event']))
217
218         self.assert_no_active_block_jobs()
219
220     # Tests that the insertion of the commit_top filter node doesn't make a
221     # difference to query-blockstat
222     def test_implicit_node(self):
223         if self.image_len == 0:
224             return
225
226         self.assert_no_active_block_jobs()
227         result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
228                              base=backing_img, speed=(self.image_len / 4))
229         self.assert_qmp(result, 'return', {})
230
231         result = self.vm.qmp('query-block')
232         self.assert_qmp(result, 'return[0]/inserted/file', test_img)
233         self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
234         self.assert_qmp(result, 'return[0]/inserted/backing_file', mid_img)
235         self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 2)
236         self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
237         self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', mid_img)
238         self.assert_qmp(result, 'return[0]/inserted/image/backing-image/backing-image/filename', backing_img)
239
240         result = self.vm.qmp('query-blockstats')
241         self.assert_qmp(result, 'return[0]/node-name', 'top')
242         self.assert_qmp(result, 'return[0]/backing/node-name', 'mid')
243         self.assert_qmp(result, 'return[0]/backing/backing/node-name', 'base')
244
245         self.cancel_and_wait()
246         self.assert_no_active_block_jobs()
247
248 class TestRelativePaths(ImageCommitTestCase):
249     image_len = 1 * 1024 * 1024
250     test_len = 1 * 1024 * 256
251
252     dir1 = "dir1"
253     dir2 = "dir2/"
254     dir3 = "dir2/dir3/"
255
256     test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
257     mid_img = "../mid.img"
258     backing_img = "../dir1/backing.img"
259
260     backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
261     mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
262
263     def setUp(self):
264         try:
265             os.mkdir(os.path.join(iotests.test_dir, self.dir1))
266             os.mkdir(os.path.join(iotests.test_dir, self.dir2))
267             os.mkdir(os.path.join(iotests.test_dir, self.dir3))
268         except OSError as exception:
269             if exception.errno != errno.EEXIST:
270                 raise
271         iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
272         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
273         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
274         qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
275         qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
276         qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
277         qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
278         self.vm = iotests.VM().add_drive(self.test_img)
279         self.vm.launch()
280
281     def tearDown(self):
282         self.vm.shutdown()
283         os.remove(self.test_img)
284         os.remove(self.mid_img_abs)
285         os.remove(self.backing_img_abs)
286         try:
287             os.rmdir(os.path.join(iotests.test_dir, self.dir1))
288             os.rmdir(os.path.join(iotests.test_dir, self.dir3))
289             os.rmdir(os.path.join(iotests.test_dir, self.dir2))
290         except OSError as exception:
291             if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
292                 raise
293
294     def test_commit(self):
295         self.run_commit_test(self.mid_img, self.backing_img)
296         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
297         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
298
299     def test_device_not_found(self):
300         result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
301         self.assert_qmp(result, 'error/class', 'DeviceNotFound')
302
303     def test_top_same_base(self):
304         self.assert_no_active_block_jobs()
305         result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
306         self.assert_qmp(result, 'error/class', 'GenericError')
307         self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
308
309     def test_top_invalid(self):
310         self.assert_no_active_block_jobs()
311         result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
312         self.assert_qmp(result, 'error/class', 'GenericError')
313         self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
314
315     def test_base_invalid(self):
316         self.assert_no_active_block_jobs()
317         result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
318         self.assert_qmp(result, 'error/class', 'GenericError')
319         self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
320
321     def test_top_is_active(self):
322         self.run_commit_test(self.test_img, self.backing_img)
323         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
324         self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
325
326     def test_top_and_base_reversed(self):
327         self.assert_no_active_block_jobs()
328         result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
329         self.assert_qmp(result, 'error/class', 'GenericError')
330         self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
331
332
333 class TestSetSpeed(ImageCommitTestCase):
334     image_len = 80 * 1024 * 1024 # MB
335
336     def setUp(self):
337         qemu_img('create', backing_img, str(TestSetSpeed.image_len))
338         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
339         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
340         qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
341         qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
342         self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
343         self.vm.launch()
344
345     def tearDown(self):
346         self.vm.shutdown()
347         os.remove(test_img)
348         os.remove(mid_img)
349         os.remove(backing_img)
350
351     def test_set_speed(self):
352         self.assert_no_active_block_jobs()
353
354         self.vm.pause_drive('drive0')
355         result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
356         self.assert_qmp(result, 'return', {})
357
358         # Ensure the speed we set was accepted
359         result = self.vm.qmp('query-block-jobs')
360         self.assert_qmp(result, 'return[0]/device', 'drive0')
361         self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
362
363         self.cancel_and_wait(resume=True)
364
365 class TestActiveZeroLengthImage(TestSingleDrive):
366     image_len = 0
367
368 class TestReopenOverlay(ImageCommitTestCase):
369     image_len = 1024 * 1024
370     img0 = os.path.join(iotests.test_dir, '0.img')
371     img1 = os.path.join(iotests.test_dir, '1.img')
372     img2 = os.path.join(iotests.test_dir, '2.img')
373     img3 = os.path.join(iotests.test_dir, '3.img')
374
375     def setUp(self):
376         iotests.create_image(self.img0, self.image_len)
377         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
378         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
379         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
380         qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
381         self.vm = iotests.VM().add_drive(self.img3)
382         self.vm.launch()
383
384     def tearDown(self):
385         self.vm.shutdown()
386         os.remove(self.img0)
387         os.remove(self.img1)
388         os.remove(self.img2)
389         os.remove(self.img3)
390
391     # This tests what happens when the overlay image of the 'top' node
392     # needs to be reopened in read-write mode in order to update the
393     # backing image string.
394     def test_reopen_overlay(self):
395         self.run_commit_test(self.img1, self.img0)
396
397 if __name__ == '__main__':
398     iotests.main(supported_fmts=['qcow2', 'qed'])
This page took 0.053825 seconds and 4 git commands to generate.