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
27 from iotests
import qemu_img
, qemu_io
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):
42 for event
in self
.vm
.get_qmp_events(wait
=True):
43 if event
['event'] == 'BLOCK_JOB_COMPLETED':
44 self
.assert_qmp(event
, 'data/type', 'commit')
45 self
.assert_qmp(event
, 'data/device', 'drive0')
46 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
47 self
.assert_qmp(event
, 'data/len', self
.image_len
)
49 self
.assertTrue(ready
, "Expecting BLOCK_JOB_COMPLETED event")
51 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')
58 self
.assert_no_active_block_jobs()
61 def run_commit_test(self
, top
, base
, need_ready
=False):
62 self
.assert_no_active_block_jobs()
63 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=top
, base
=base
)
64 self
.assert_qmp(result
, 'return', {})
65 self
.wait_for_complete(need_ready
)
67 def run_default_commit_test(self
):
68 self
.assert_no_active_block_jobs()
69 result
= self
.vm
.qmp('block-commit', device
='drive0')
70 self
.assert_qmp(result
, 'return', {})
71 self
.wait_for_complete()
73 class TestSingleDrive(ImageCommitTestCase
):
74 image_len
= 1 * 1024 * 1024
75 test_len
= 1 * 1024 * 256
78 iotests
.create_image(backing_img
, self
.image_len
)
79 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
80 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
81 qemu_io('-c', 'write -P 0xab 0 524288', backing_img
)
82 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img
)
83 self
.vm
= iotests
.VM().add_drive(test_img
)
90 os
.remove(backing_img
)
92 def test_commit(self
):
93 self
.run_commit_test(mid_img
, backing_img
)
94 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
95 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
97 def test_device_not_found(self
):
98 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % mid_img
)
99 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
101 def test_top_same_base(self
):
102 self
.assert_no_active_block_jobs()
103 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % backing_img
)
104 self
.assert_qmp(result
, 'error/class', 'GenericError')
105 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % backing_img
)
107 def test_top_invalid(self
):
108 self
.assert_no_active_block_jobs()
109 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % backing_img
)
110 self
.assert_qmp(result
, 'error/class', 'GenericError')
111 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
113 def test_base_invalid(self
):
114 self
.assert_no_active_block_jobs()
115 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % mid_img
, base
='badfile')
116 self
.assert_qmp(result
, 'error/class', 'GenericError')
117 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
119 def test_top_is_active(self
):
120 self
.run_commit_test(test_img
, backing_img
, need_ready
=True)
121 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
122 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
124 def test_top_is_default_active(self
):
125 self
.run_default_commit_test()
126 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
127 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
129 def test_top_and_base_reversed(self
):
130 self
.assert_no_active_block_jobs()
131 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % mid_img
)
132 self
.assert_qmp(result
, 'error/class', 'GenericError')
133 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % mid_img
)
136 class TestRelativePaths(ImageCommitTestCase
):
137 image_len
= 1 * 1024 * 1024
138 test_len
= 1 * 1024 * 256
144 test_img
= os
.path
.join(iotests
.test_dir
, dir3
, 'test.img')
145 mid_img
= "../mid.img"
146 backing_img
= "../dir1/backing.img"
148 backing_img_abs
= os
.path
.join(iotests
.test_dir
, dir1
, 'backing.img')
149 mid_img_abs
= os
.path
.join(iotests
.test_dir
, dir2
, 'mid.img')
153 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
154 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
155 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
156 except OSError as exception
:
157 if exception
.errno
!= errno
.EEXIST
:
159 iotests
.create_image(self
.backing_img_abs
, TestRelativePaths
.image_len
)
160 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.backing_img_abs
, self
.mid_img_abs
)
161 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.mid_img_abs
, self
.test_img
)
162 qemu_img('rebase', '-u', '-b', self
.backing_img
, self
.mid_img_abs
)
163 qemu_img('rebase', '-u', '-b', self
.mid_img
, self
.test_img
)
164 qemu_io('-c', 'write -P 0xab 0 524288', self
.backing_img_abs
)
165 qemu_io('-c', 'write -P 0xef 524288 524288', self
.mid_img_abs
)
166 self
.vm
= iotests
.VM().add_drive(self
.test_img
)
171 os
.remove(self
.test_img
)
172 os
.remove(self
.mid_img_abs
)
173 os
.remove(self
.backing_img_abs
)
175 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
176 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
177 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
178 except OSError as exception
:
179 if exception
.errno
!= errno
.EEXIST
and exception
.errno
!= errno
.ENOTEMPTY
:
182 def test_commit(self
):
183 self
.run_commit_test(self
.mid_img
, self
.backing_img
)
184 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self
.backing_img_abs
).find("verification failed"))
185 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self
.backing_img_abs
).find("verification failed"))
187 def test_device_not_found(self
):
188 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % self
.mid_img
)
189 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
191 def test_top_same_base(self
):
192 self
.assert_no_active_block_jobs()
193 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='%s' % self
.mid_img
)
194 self
.assert_qmp(result
, 'error/class', 'GenericError')
195 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
197 def test_top_invalid(self
):
198 self
.assert_no_active_block_jobs()
199 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % self
.backing_img
)
200 self
.assert_qmp(result
, 'error/class', 'GenericError')
201 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
203 def test_base_invalid(self
):
204 self
.assert_no_active_block_jobs()
205 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='badfile')
206 self
.assert_qmp(result
, 'error/class', 'GenericError')
207 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
209 def test_top_is_active(self
):
210 self
.run_commit_test(self
.test_img
, self
.backing_img
)
211 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self
.backing_img_abs
).find("verification failed"))
212 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self
.backing_img_abs
).find("verification failed"))
214 def test_top_and_base_reversed(self
):
215 self
.assert_no_active_block_jobs()
216 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.backing_img
, base
='%s' % self
.mid_img
)
217 self
.assert_qmp(result
, 'error/class', 'GenericError')
218 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
221 class TestSetSpeed(ImageCommitTestCase
):
222 image_len
= 80 * 1024 * 1024 # MB
225 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
226 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
227 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
228 qemu_io('-c', 'write -P 0x1 0 512', test_img
)
229 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img
)
230 self
.vm
= iotests
.VM().add_drive(test_img
)
237 os
.remove(backing_img
)
239 def test_set_speed(self
):
240 self
.assert_no_active_block_jobs()
242 self
.vm
.pause_drive('drive0')
243 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=mid_img
, speed
=1024 * 1024)
244 self
.assert_qmp(result
, 'return', {})
246 # Ensure the speed we set was accepted
247 result
= self
.vm
.qmp('query-block-jobs')
248 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
249 self
.assert_qmp(result
, 'return[0]/speed', 1024 * 1024)
251 self
.cancel_and_wait(resume
=True)
253 class TestActiveZeroLengthImage(TestSingleDrive
):
256 if __name__
== '__main__':
257 iotests
.main(supported_fmts
=['qcow2', 'qed'])