]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qemu-iotests/040
mirror: Go through ready -> complete process for 0 len image
[mirror_qemu.git] / tests / qemu-iotests / 040
CommitLineData
747051cd
JC
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
24import time
25import os
26import iotests
27from iotests import qemu_img, qemu_io
28import struct
6bf0d1f4 29import errno
747051cd
JC
30
31backing_img = os.path.join(iotests.test_dir, 'backing.img')
32mid_img = os.path.join(iotests.test_dir, 'mid.img')
33test_img = os.path.join(iotests.test_dir, 'test.img')
34
35class ImageCommitTestCase(iotests.QMPTestCase):
36 '''Abstract base class for image commit test cases'''
37
4de43470 38 def run_commit_test(self, top, base):
fb0a078f 39 self.assert_no_active_block_jobs()
4de43470
FZ
40 result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
41 self.assert_qmp(result, 'return', {})
42
43 completed = False
44 while not completed:
45 for event in self.vm.get_qmp_events(wait=True):
46 if event['event'] == 'BLOCK_JOB_COMPLETED':
47 self.assert_qmp(event, 'data/type', 'commit')
48 self.assert_qmp(event, 'data/device', 'drive0')
49 self.assert_qmp(event, 'data/offset', self.image_len)
50 self.assert_qmp(event, 'data/len', self.image_len)
51 completed = True
52 elif event['event'] == 'BLOCK_JOB_READY':
53 self.assert_qmp(event, 'data/type', 'commit')
54 self.assert_qmp(event, 'data/device', 'drive0')
55 self.assert_qmp(event, 'data/len', self.image_len)
56 self.vm.qmp('block-job-complete', device='drive0')
57
fb0a078f 58 self.assert_no_active_block_jobs()
4de43470
FZ
59 self.vm.shutdown()
60
747051cd
JC
61class TestSingleDrive(ImageCommitTestCase):
62 image_len = 1 * 1024 * 1024
63 test_len = 1 * 1024 * 256
64
65 def setUp(self):
915365a9 66 iotests.create_image(backing_img, TestSingleDrive.image_len)
747051cd
JC
67 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
68 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
69 qemu_io('-c', 'write -P 0xab 0 524288', backing_img)
70 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img)
71 self.vm = iotests.VM().add_drive(test_img)
72 self.vm.launch()
73
74 def tearDown(self):
75 self.vm.shutdown()
76 os.remove(test_img)
77 os.remove(mid_img)
78 os.remove(backing_img)
79
80 def test_commit(self):
4de43470 81 self.run_commit_test(mid_img, backing_img)
747051cd
JC
82 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
83 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
84
85 def test_device_not_found(self):
86 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % mid_img)
87 self.assert_qmp(result, 'error/class', 'DeviceNotFound')
88
89 def test_top_same_base(self):
fb0a078f 90 self.assert_no_active_block_jobs()
747051cd
JC
91 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % backing_img)
92 self.assert_qmp(result, 'error/class', 'GenericError')
d5208c45 93 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % backing_img)
747051cd
JC
94
95 def test_top_invalid(self):
fb0a078f 96 self.assert_no_active_block_jobs()
747051cd
JC
97 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % backing_img)
98 self.assert_qmp(result, 'error/class', 'GenericError')
99 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
100
101 def test_base_invalid(self):
fb0a078f 102 self.assert_no_active_block_jobs()
747051cd
JC
103 result = self.vm.qmp('block-commit', device='drive0', top='%s' % mid_img, base='badfile')
104 self.assert_qmp(result, 'error/class', 'GenericError')
105 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
106
107 def test_top_is_active(self):
4de43470
FZ
108 self.run_commit_test(test_img, backing_img)
109 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
110 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
747051cd
JC
111
112 def test_top_and_base_reversed(self):
fb0a078f 113 self.assert_no_active_block_jobs()
747051cd
JC
114 result = self.vm.qmp('block-commit', device='drive0', top='%s' % backing_img, base='%s' % mid_img)
115 self.assert_qmp(result, 'error/class', 'GenericError')
d5208c45 116 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % mid_img)
747051cd
JC
117
118 def test_top_omitted(self):
fb0a078f 119 self.assert_no_active_block_jobs()
747051cd
JC
120 result = self.vm.qmp('block-commit', device='drive0')
121 self.assert_qmp(result, 'error/class', 'GenericError')
122 self.assert_qmp(result, 'error/desc', "Parameter 'top' is missing")
123
6bf0d1f4
JC
124class TestRelativePaths(ImageCommitTestCase):
125 image_len = 1 * 1024 * 1024
126 test_len = 1 * 1024 * 256
127
128 dir1 = "dir1"
129 dir2 = "dir2/"
130 dir3 = "dir2/dir3/"
131
132 test_img = os.path.join(iotests.test_dir, dir3, 'test.img')
133 mid_img = "../mid.img"
134 backing_img = "../dir1/backing.img"
135
136 backing_img_abs = os.path.join(iotests.test_dir, dir1, 'backing.img')
137 mid_img_abs = os.path.join(iotests.test_dir, dir2, 'mid.img')
138
139 def setUp(self):
140 try:
141 os.mkdir(os.path.join(iotests.test_dir, self.dir1))
142 os.mkdir(os.path.join(iotests.test_dir, self.dir2))
143 os.mkdir(os.path.join(iotests.test_dir, self.dir3))
144 except OSError as exception:
145 if exception.errno != errno.EEXIST:
146 raise
915365a9 147 iotests.create_image(self.backing_img_abs, TestRelativePaths.image_len)
6bf0d1f4
JC
148 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.backing_img_abs, self.mid_img_abs)
149 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % self.mid_img_abs, self.test_img)
150 qemu_img('rebase', '-u', '-b', self.backing_img, self.mid_img_abs)
151 qemu_img('rebase', '-u', '-b', self.mid_img, self.test_img)
152 qemu_io('-c', 'write -P 0xab 0 524288', self.backing_img_abs)
153 qemu_io('-c', 'write -P 0xef 524288 524288', self.mid_img_abs)
154 self.vm = iotests.VM().add_drive(self.test_img)
155 self.vm.launch()
156
157 def tearDown(self):
158 self.vm.shutdown()
159 os.remove(self.test_img)
160 os.remove(self.mid_img_abs)
161 os.remove(self.backing_img_abs)
162 try:
163 os.rmdir(os.path.join(iotests.test_dir, self.dir1))
164 os.rmdir(os.path.join(iotests.test_dir, self.dir3))
165 os.rmdir(os.path.join(iotests.test_dir, self.dir2))
166 except OSError as exception:
167 if exception.errno != errno.EEXIST and exception.errno != errno.ENOTEMPTY:
168 raise
169
170 def test_commit(self):
4de43470 171 self.run_commit_test(self.mid_img, self.backing_img)
6bf0d1f4
JC
172 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
173 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
174
175 def test_device_not_found(self):
176 result = self.vm.qmp('block-commit', device='nonexistent', top='%s' % self.mid_img)
177 self.assert_qmp(result, 'error/class', 'DeviceNotFound')
178
179 def test_top_same_base(self):
fb0a078f 180 self.assert_no_active_block_jobs()
6bf0d1f4
JC
181 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='%s' % self.mid_img)
182 self.assert_qmp(result, 'error/class', 'GenericError')
183 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
184
185 def test_top_invalid(self):
fb0a078f 186 self.assert_no_active_block_jobs()
6bf0d1f4
JC
187 result = self.vm.qmp('block-commit', device='drive0', top='badfile', base='%s' % self.backing_img)
188 self.assert_qmp(result, 'error/class', 'GenericError')
189 self.assert_qmp(result, 'error/desc', 'Top image file badfile not found')
190
191 def test_base_invalid(self):
fb0a078f 192 self.assert_no_active_block_jobs()
6bf0d1f4
JC
193 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.mid_img, base='badfile')
194 self.assert_qmp(result, 'error/class', 'GenericError')
195 self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
196
197 def test_top_is_active(self):
4de43470
FZ
198 self.run_commit_test(self.test_img, self.backing_img)
199 self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self.backing_img_abs).find("verification failed"))
200 self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self.backing_img_abs).find("verification failed"))
6bf0d1f4
JC
201
202 def test_top_and_base_reversed(self):
fb0a078f 203 self.assert_no_active_block_jobs()
6bf0d1f4
JC
204 result = self.vm.qmp('block-commit', device='drive0', top='%s' % self.backing_img, base='%s' % self.mid_img)
205 self.assert_qmp(result, 'error/class', 'GenericError')
206 self.assert_qmp(result, 'error/desc', 'Base \'%s\' not found' % self.mid_img)
207
747051cd
JC
208
209class TestSetSpeed(ImageCommitTestCase):
210 image_len = 80 * 1024 * 1024 # MB
211
212 def setUp(self):
213 qemu_img('create', backing_img, str(TestSetSpeed.image_len))
214 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
215 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
b59b3d57 216 qemu_io('-c', 'write -P 0x1 0 512', test_img)
4de43470 217 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img)
747051cd
JC
218 self.vm = iotests.VM().add_drive(test_img)
219 self.vm.launch()
220
221 def tearDown(self):
222 self.vm.shutdown()
223 os.remove(test_img)
224 os.remove(mid_img)
225 os.remove(backing_img)
226
227 def test_set_speed(self):
fb0a078f 228 self.assert_no_active_block_jobs()
747051cd 229
b59b3d57 230 self.vm.pause_drive('drive0')
747051cd
JC
231 result = self.vm.qmp('block-commit', device='drive0', top=mid_img, speed=1024 * 1024)
232 self.assert_qmp(result, 'return', {})
233
234 # Ensure the speed we set was accepted
235 result = self.vm.qmp('query-block-jobs')
236 self.assert_qmp(result, 'return[0]/device', 'drive0')
237 self.assert_qmp(result, 'return[0]/speed', 1024 * 1024)
238
b59b3d57 239 self.cancel_and_wait(resume=True)
747051cd
JC
240
241
242if __name__ == '__main__':
243 iotests.main(supported_fmts=['qcow2', 'qed'])