commit: Fix completion with extra reference
[qemu/kevin.git] / tests / qemu-iotests / 040
blob9d381d9b72946d9dd1365869c95e93b98ef7714f
1 #!/usr/bin/env python
3 # Tests for image block commit.
5 # Copyright (C) 2012 IBM, Corp.
6 # Copyright (C) 2012 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/>.
21 # Test for live block commit
22 # Derived from Image Streaming Test 030
24 import time
25 import os
26 import iotests
27 from iotests import qemu_img, qemu_io
28 import struct
29 import errno
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')
35 class ImageCommitTestCase(iotests.QMPTestCase):
36 '''Abstract base class for image commit test cases'''
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')
57 self.assert_no_active_block_jobs()
58 self.vm.shutdown()
60 def run_commit_test(self, top, base, need_ready=False):
61 self.assert_no_active_block_jobs()
62 result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
63 self.assert_qmp(result, 'return', {})
64 self.wait_for_complete(need_ready)
66 def run_default_commit_test(self):
67 self.assert_no_active_block_jobs()
68 result = self.vm.qmp('block-commit', device='drive0')
69 self.assert_qmp(result, 'return', {})
70 self.wait_for_complete()
72 class TestSingleDrive(ImageCommitTestCase):
73 # Need some space after the copied data so that throttling is effective in
74 # tests that use it rather than just completing the job immediately
75 image_len = 2 * 1024 * 1024
76 test_len = 1 * 1024 * 256
78 def setUp(self):
79 iotests.create_image(backing_img, self.image_len)
80 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
81 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
82 qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img)
83 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
84 self.vm = iotests.VM().add_drive(test_img, interface="none")
85 self.vm.add_device("virtio-scsi-pci")
86 self.vm.add_device("scsi-hd,id=scsi0,drive=drive0")
87 self.vm.launch()
89 def tearDown(self):
90 self.vm.shutdown()
91 os.remove(test_img)
92 os.remove(mid_img)
93 os.remove(backing_img)
95 def test_commit(self):
96 self.run_commit_test(mid_img, backing_img)
97 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
98 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
100 def test_device_not_found(self):
101 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
102 self.assert_qmp(result, 'error/class', 'DeviceNotFound')
104 def test_top_same_base(self):
105 self.assert_no_active_block_jobs()
106 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
107 self.assert_qmp(result, 'error/class', 'GenericError')
108 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
110 def test_top_invalid(self):
111 self.assert_no_active_block_jobs()
112 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
113 self.assert_qmp(result, 'error/class', 'GenericError')
114 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
116 def test_base_invalid(self):
117 self.assert_no_active_block_jobs()
118 result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
119 self.assert_qmp(result, 'error/class', 'GenericError')
120 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
122 def test_top_is_active(self):
123 self.run_commit_test(test_img, backing_img, need_ready=True)
124 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
125 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
127 def test_top_is_default_active(self):
128 self.run_default_commit_test()
129 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
130 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
132 def test_top_and_base_reversed(self):
133 self.assert_no_active_block_jobs()
134 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
135 self.assert_qmp(result, 'error/class', 'GenericError')
136 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
138 # When the job is running on a BB that is automatically deleted on hot
139 # unplug, the job is cancelled when the device disappears
140 def test_hot_unplug(self):
141 if self.image_len == 0:
142 return
144 self.assert_no_active_block_jobs()
145 result = self.vm.qmp('block-commit', device='drive0', top=mid_img,
146 base=backing_img, speed=(self.image_len / 4))
147 self.assert_qmp(result, 'return', {})
148 result = self.vm.qmp('device_del', id='scsi0')
149 self.assert_qmp(result, 'return', {})
151 cancelled = False
152 deleted = False
153 while not cancelled or not deleted:
154 for event in self.vm.get_qmp_events(wait=True):
155 if event['event'] == 'DEVICE_DELETED':
156 self.assert_qmp(event, 'data/device', 'scsi0')
157 deleted = True
158 elif event['event'] == 'BLOCK_JOB_CANCELLED':
159 self.assert_qmp(event, 'data/device', 'drive0')
160 cancelled = True
161 else:
162 self.fail("Unexpected event %s" % (event['event']))
164 self.assert_no_active_block_jobs()
166 class TestRelativePaths(ImageCommitTestCase):
167 image_len = 1 * 1024 * 1024
168 test_len = 1 * 1024 * 256
170 dir1 = "dir1"
171 dir2 = "dir2/"
172 dir3 = "dir2/dir3/"
174 test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
175 mid_img = "../mid.img"
176 backing_img = "../dir1/backing.img"
178 backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
179 mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
181 def setUp(self):
182 try:
183 os.mkdir(os.path.join(iotests.test_dir, self.dir1))
184 os.mkdir(os.path.join(iotests.test_dir, self.dir2))
185 os.mkdir(os.path.join(iotests.test_dir, self.dir3))
186 except OSError as exception:
187 if exception.errno != errno.EEXIST:
188 raise
189 iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
190 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
191 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
192 qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
193 qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
194 qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self.backing_img_abs)
195 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
196 self.vm = iotests.VM().add_drive(self.test_img)
197 self.vm.launch()
199 def tearDown(self):
200 self.vm.shutdown()
201 os.remove(self.test_img)
202 os.remove(self.mid_img_abs)
203 os.remove(self.backing_img_abs)
204 try:
205 os.rmdir(os.path.join(iotests.test_dir, self.dir1))
206 os.rmdir(os.path.join(iotests.test_dir, self.dir3))
207 os.rmdir(os.path.join(iotests.test_dir, self.dir2))
208 except OSError as exception:
209 if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
210 raise
212 def test_commit(self):
213 self.run_commit_test(self.mid_img, self.backing_img)
214 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
215 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
217 def test_device_not_found(self):
218 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
219 self.assert_qmp(result, 'error/class', 'DeviceNotFound')
221 def test_top_same_base(self):
222 self.assert_no_active_block_jobs()
223 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
224 self.assert_qmp(result, 'error/class', 'GenericError')
225 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
227 def test_top_invalid(self):
228 self.assert_no_active_block_jobs()
229 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
230 self.assert_qmp(result, 'error/class', 'GenericError')
231 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
233 def test_base_invalid(self):
234 self.assert_no_active_block_jobs()
235 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
236 self.assert_qmp(result, 'error/class', 'GenericError')
237 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
239 def test_top_is_active(self):
240 self.run_commit_test(self.test_img, self.backing_img)
241 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
242 self.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
244 def test_top_and_base_reversed(self):
245 self.assert_no_active_block_jobs()
246 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
247 self.assert_qmp(result, 'error/class', 'GenericError')
248 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
251 class TestSetSpeed(ImageCommitTestCase):
252 image_len = 80 * 1024 * 1024 # MB
254 def setUp(self):
255 qemu_img('create', backing_img, str(TestSetSpeed.image_len))
256 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
257 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
258 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 0 512', test_img)
259 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xef 524288 524288', mid_img)
260 self.vm = iotests.VM().add_drive(test_img)
261 self.vm.launch()
263 def tearDown(self):
264 self.vm.shutdown()
265 os.remove(test_img)
266 os.remove(mid_img)
267 os.remove(backing_img)
269 def test_set_speed(self):
270 self.assert_no_active_block_jobs()
272 self.vm.pause_drive('drive0')
273 result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
274 self.assert_qmp(result, 'return', {})
276 # Ensure the speed we set was accepted
277 result = self.vm.qmp('query-block-jobs')
278 self.assert_qmp(result, 'return[0]/device', 'drive0')
279 self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
281 self.cancel_and_wait(resume=True)
283 class TestActiveZeroLengthImage(TestSingleDrive):
284 image_len = 0
286 class TestReopenOverlay(ImageCommitTestCase):
287 image_len = 1024 * 1024
288 img0 = os.path.join(iotests.test_dir, '0.img')
289 img1 = os.path.join(iotests.test_dir, '1.img')
290 img2 = os.path.join(iotests.test_dir, '2.img')
291 img3 = os.path.join(iotests.test_dir, '3.img')
293 def setUp(self):
294 iotests.create_image(self.img0, self.image_len)
295 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img0, self.img1)
296 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img1, self.img2)
297 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.img2, self.img3)
298 qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0xab 0 128K', self.img1)
299 self.vm = iotests.VM().add_drive(self.img3)
300 self.vm.launch()
302 def tearDown(self):
303 self.vm.shutdown()
304 os.remove(self.img0)
305 os.remove(self.img1)
306 os.remove(self.img2)
307 os.remove(self.img3)
309 # This tests what happens when the overlay image of the 'top' node
310 # needs to be reopened in read-write mode in order to update the
311 # backing image string.
312 def test_reopen_overlay(self):
313 self.run_commit_test(self.img1, self.img0)
315 if __name__ == '__main__':
316 iotests.main(supported_fmts=['qcow2', 'qed'])