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', event
['data']['len'])
48 self
.assertTrue(ready
, "Expecting BLOCK_JOB_COMPLETED event")
50 elif event
['event'] == 'BLOCK_JOB_READY':
52 self
.assert_qmp(event
, 'data/type', 'commit')
53 self
.assert_qmp(event
, 'data/device', 'drive0')
54 self
.vm
.qmp('block-job-complete', device
='drive0')
56 self
.assert_no_active_block_jobs()
59 def run_commit_test(self
, top
, base
, need_ready
=False):
60 self
.assert_no_active_block_jobs()
61 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=top
, base
=base
)
62 self
.assert_qmp(result
, 'return', {})
63 self
.wait_for_complete(need_ready
)
65 def run_default_commit_test(self
):
66 self
.assert_no_active_block_jobs()
67 result
= self
.vm
.qmp('block-commit', device
='drive0')
68 self
.assert_qmp(result
, 'return', {})
69 self
.wait_for_complete()
71 class TestSingleDrive(ImageCommitTestCase
):
72 image_len
= 1 * 1024 * 1024
73 test_len
= 1 * 1024 * 256
76 iotests
.create_image(backing_img
, self
.image_len
)
77 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
78 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
79 qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', backing_img
)
80 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0xef 524288 524288', mid_img
)
81 self
.vm
= iotests
.VM().add_drive(test_img
)
88 os
.remove(backing_img
)
90 def test_commit(self
):
91 self
.run_commit_test(mid_img
, backing_img
)
92 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
93 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
95 def test_device_not_found(self
):
96 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % mid_img
)
97 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
99 def test_top_same_base(self
):
100 self
.assert_no_active_block_jobs()
101 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % backing_img
)
102 self
.assert_qmp(result
, 'error/class', 'GenericError')
103 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % backing_img
)
105 def test_top_invalid(self
):
106 self
.assert_no_active_block_jobs()
107 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % backing_img
)
108 self
.assert_qmp(result
, 'error/class', 'GenericError')
109 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
111 def test_base_invalid(self
):
112 self
.assert_no_active_block_jobs()
113 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % mid_img
, base
='badfile')
114 self
.assert_qmp(result
, 'error/class', 'GenericError')
115 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
117 def test_top_is_active(self
):
118 self
.run_commit_test(test_img
, backing_img
, need_ready
=True)
119 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
120 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
122 def test_top_is_default_active(self
):
123 self
.run_default_commit_test()
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_and_base_reversed(self
):
128 self
.assert_no_active_block_jobs()
129 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % mid_img
)
130 self
.assert_qmp(result
, 'error/class', 'GenericError')
131 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % mid_img
)
134 class TestRelativePaths(ImageCommitTestCase
):
135 image_len
= 1 * 1024 * 1024
136 test_len
= 1 * 1024 * 256
142 test_img
= os
.path
.join(iotests
.test_dir
, dir3
, 'test.img')
143 mid_img
= "../mid.img"
144 backing_img
= "../dir1/backing.img"
146 backing_img_abs
= os
.path
.join(iotests
.test_dir
, dir1
, 'backing.img')
147 mid_img_abs
= os
.path
.join(iotests
.test_dir
, dir2
, 'mid.img')
151 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
152 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
153 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
154 except OSError as exception
:
155 if exception
.errno
!= errno
.EEXIST
:
157 iotests
.create_image(self
.backing_img_abs
, TestRelativePaths
.image_len
)
158 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.backing_img_abs
, self
.mid_img_abs
)
159 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.mid_img_abs
, self
.test_img
)
160 qemu_img('rebase', '-u', '-b', self
.backing_img
, self
.mid_img_abs
)
161 qemu_img('rebase', '-u', '-b', self
.mid_img
, self
.test_img
)
162 qemu_io('-f', 'raw', '-c', 'write -P 0xab 0 524288', self
.backing_img_abs
)
163 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0xef 524288 524288', self
.mid_img_abs
)
164 self
.vm
= iotests
.VM().add_drive(self
.test_img
)
169 os
.remove(self
.test_img
)
170 os
.remove(self
.mid_img_abs
)
171 os
.remove(self
.backing_img_abs
)
173 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
174 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
175 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
176 except OSError as exception
:
177 if exception
.errno
!= errno
.EEXIST
and exception
.errno
!= errno
.ENOTEMPTY
:
180 def test_commit(self
):
181 self
.run_commit_test(self
.mid_img
, self
.backing_img
)
182 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self
.backing_img_abs
).find("verification failed"))
183 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self
.backing_img_abs
).find("verification failed"))
185 def test_device_not_found(self
):
186 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % self
.mid_img
)
187 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
189 def test_top_same_base(self
):
190 self
.assert_no_active_block_jobs()
191 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='%s' % self
.mid_img
)
192 self
.assert_qmp(result
, 'error/class', 'GenericError')
193 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
195 def test_top_invalid(self
):
196 self
.assert_no_active_block_jobs()
197 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % self
.backing_img
)
198 self
.assert_qmp(result
, 'error/class', 'GenericError')
199 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
201 def test_base_invalid(self
):
202 self
.assert_no_active_block_jobs()
203 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='badfile')
204 self
.assert_qmp(result
, 'error/class', 'GenericError')
205 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
207 def test_top_is_active(self
):
208 self
.run_commit_test(self
.test_img
, self
.backing_img
)
209 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xab 0 524288', self
.backing_img_abs
).find("verification failed"))
210 self
.assertEqual(-1, qemu_io('-f', 'raw', '-c', 'read -P 0xef 524288 524288', self
.backing_img_abs
).find("verification failed"))
212 def test_top_and_base_reversed(self
):
213 self
.assert_no_active_block_jobs()
214 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.backing_img
, base
='%s' % self
.mid_img
)
215 self
.assert_qmp(result
, 'error/class', 'GenericError')
216 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
219 class TestSetSpeed(ImageCommitTestCase
):
220 image_len
= 80 * 1024 * 1024 # MB
223 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
224 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
225 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
226 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0x1 0 512', test_img
)
227 qemu_io('-f', iotests
.imgfmt
, '-c', 'write -P 0xef 524288 524288', mid_img
)
228 self
.vm
= iotests
.VM().add_drive(test_img
)
235 os
.remove(backing_img
)
237 def test_set_speed(self
):
238 self
.assert_no_active_block_jobs()
240 self
.vm
.pause_drive('drive0')
241 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=mid_img
, speed
=1024 * 1024)
242 self
.assert_qmp(result
, 'return', {})
244 # Ensure the speed we set was accepted
245 result
= self
.vm
.qmp('query-block-jobs')
246 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
247 self
.assert_qmp(result
, 'return[0]/speed', 1024 * 1024)
249 self
.cancel_and_wait(resume
=True)
251 class TestActiveZeroLengthImage(TestSingleDrive
):
254 if __name__
== '__main__':
255 iotests
.main(supported_fmts
=['qcow2', 'qed'])