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 assert_no_active_commit(self
):
39 result
= self
.vm
.qmp('query-block-jobs')
40 self
.assert_qmp(result
, 'return', [])
42 class TestSingleDrive(ImageCommitTestCase
):
43 image_len
= 1 * 1024 * 1024
44 test_len
= 1 * 1024 * 256
47 iotests
.create_image(backing_img
, TestSingleDrive
.image_len
)
48 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
49 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
50 qemu_io('-c', 'write -P 0xab 0 524288', backing_img
)
51 qemu_io('-c', 'write -P 0xef 524288 524288', mid_img
)
52 self
.vm
= iotests
.VM().add_drive(test_img
)
59 os
.remove(backing_img
)
61 def test_commit(self
):
62 self
.assert_no_active_commit()
63 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % mid_img
)
64 self
.assert_qmp(result
, 'return', {})
68 for event
in self
.vm
.get_qmp_events(wait
=True):
69 if event
['event'] == 'BLOCK_JOB_COMPLETED':
70 self
.assert_qmp(event
, 'data/type', 'commit')
71 self
.assert_qmp(event
, 'data/device', 'drive0')
72 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
73 self
.assert_qmp(event
, 'data/len', self
.image_len
)
76 self
.assert_no_active_commit()
79 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img
).find("verification failed"))
80 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img
).find("verification failed"))
82 def test_device_not_found(self
):
83 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % mid_img
)
84 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
86 def test_top_same_base(self
):
87 self
.assert_no_active_commit()
88 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % backing_img
)
89 self
.assert_qmp(result
, 'error/class', 'GenericError')
90 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % backing_img
)
92 def test_top_invalid(self
):
93 self
.assert_no_active_commit()
94 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % backing_img
)
95 self
.assert_qmp(result
, 'error/class', 'GenericError')
96 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
98 def test_base_invalid(self
):
99 self
.assert_no_active_commit()
100 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % mid_img
, base
='badfile')
101 self
.assert_qmp(result
, 'error/class', 'GenericError')
102 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
104 def test_top_is_active(self
):
105 self
.assert_no_active_commit()
106 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % test_img
, base
='%s' % backing_img
)
107 self
.assert_qmp(result
, 'error/class', 'GenericError')
108 self
.assert_qmp(result
, 'error/desc', 'Top image as the active layer is currently unsupported')
110 def test_top_and_base_reversed(self
):
111 self
.assert_no_active_commit()
112 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % backing_img
, base
='%s' % mid_img
)
113 self
.assert_qmp(result
, 'error/class', 'GenericError')
114 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % mid_img
)
116 def test_top_omitted(self
):
117 self
.assert_no_active_commit()
118 result
= self
.vm
.qmp('block-commit', device
='drive0')
119 self
.assert_qmp(result
, 'error/class', 'GenericError')
120 self
.assert_qmp(result
, 'error/desc', "Parameter 'top' is missing")
122 class TestRelativePaths(ImageCommitTestCase
):
123 image_len
= 1 * 1024 * 1024
124 test_len
= 1 * 1024 * 256
130 test_img
= os
.path
.join(iotests
.test_dir
, dir3
, 'test.img')
131 mid_img
= "../mid.img"
132 backing_img
= "../dir1/backing.img"
134 backing_img_abs
= os
.path
.join(iotests
.test_dir
, dir1
, 'backing.img')
135 mid_img_abs
= os
.path
.join(iotests
.test_dir
, dir2
, 'mid.img')
139 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
140 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
141 os
.mkdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
142 except OSError as exception
:
143 if exception
.errno
!= errno
.EEXIST
:
145 iotests
.create_image(self
.backing_img_abs
, TestRelativePaths
.image_len
)
146 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.backing_img_abs
, self
.mid_img_abs
)
147 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % self
.mid_img_abs
, self
.test_img
)
148 qemu_img('rebase', '-u', '-b', self
.backing_img
, self
.mid_img_abs
)
149 qemu_img('rebase', '-u', '-b', self
.mid_img
, self
.test_img
)
150 qemu_io('-c', 'write -P 0xab 0 524288', self
.backing_img_abs
)
151 qemu_io('-c', 'write -P 0xef 524288 524288', self
.mid_img_abs
)
152 self
.vm
= iotests
.VM().add_drive(self
.test_img
)
157 os
.remove(self
.test_img
)
158 os
.remove(self
.mid_img_abs
)
159 os
.remove(self
.backing_img_abs
)
161 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir1
))
162 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir3
))
163 os
.rmdir(os
.path
.join(iotests
.test_dir
, self
.dir2
))
164 except OSError as exception
:
165 if exception
.errno
!= errno
.EEXIST
and exception
.errno
!= errno
.ENOTEMPTY
:
168 def test_commit(self
):
169 self
.assert_no_active_commit()
170 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
)
171 self
.assert_qmp(result
, 'return', {})
175 for event
in self
.vm
.get_qmp_events(wait
=True):
176 if event
['event'] == 'BLOCK_JOB_COMPLETED':
177 self
.assert_qmp(event
, 'data/type', 'commit')
178 self
.assert_qmp(event
, 'data/device', 'drive0')
179 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
180 self
.assert_qmp(event
, 'data/len', self
.image_len
)
183 self
.assert_no_active_commit()
186 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', self
.backing_img_abs
).find("verification failed"))
187 self
.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', self
.backing_img_abs
).find("verification failed"))
189 def test_device_not_found(self
):
190 result
= self
.vm
.qmp('block-commit', device
='nonexistent', top
='%s' % self
.mid_img
)
191 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
193 def test_top_same_base(self
):
194 self
.assert_no_active_commit()
195 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='%s' % self
.mid_img
)
196 self
.assert_qmp(result
, 'error/class', 'GenericError')
197 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
199 def test_top_invalid(self
):
200 self
.assert_no_active_commit()
201 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='badfile', base
='%s' % self
.backing_img
)
202 self
.assert_qmp(result
, 'error/class', 'GenericError')
203 self
.assert_qmp(result
, 'error/desc', 'Top image file badfile not found')
205 def test_base_invalid(self
):
206 self
.assert_no_active_commit()
207 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.mid_img
, base
='badfile')
208 self
.assert_qmp(result
, 'error/class', 'GenericError')
209 self
.assert_qmp(result
, 'error/desc', 'Base \'badfile\' not found')
211 def test_top_is_active(self
):
212 self
.assert_no_active_commit()
213 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.test_img
, base
='%s' % self
.backing_img
)
214 self
.assert_qmp(result
, 'error/class', 'GenericError')
215 self
.assert_qmp(result
, 'error/desc', 'Top image as the active layer is currently unsupported')
217 def test_top_and_base_reversed(self
):
218 self
.assert_no_active_commit()
219 result
= self
.vm
.qmp('block-commit', device
='drive0', top
='%s' % self
.backing_img
, base
='%s' % self
.mid_img
)
220 self
.assert_qmp(result
, 'error/class', 'GenericError')
221 self
.assert_qmp(result
, 'error/desc', 'Base \'%s\' not found' % self
.mid_img
)
224 class TestSetSpeed(ImageCommitTestCase
):
225 image_len
= 80 * 1024 * 1024 # MB
228 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
229 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
230 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
231 qemu_io('-c', 'write -P 0x1 0 512', test_img
)
232 self
.vm
= iotests
.VM().add_drive(test_img
)
239 os
.remove(backing_img
)
241 def test_set_speed(self
):
242 self
.assert_no_active_commit()
244 self
.vm
.pause_drive('drive0')
245 result
= self
.vm
.qmp('block-commit', device
='drive0', top
=mid_img
, speed
=1024 * 1024)
246 self
.assert_qmp(result
, 'return', {})
248 # Ensure the speed we set was accepted
249 result
= self
.vm
.qmp('query-block-jobs')
250 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
251 self
.assert_qmp(result
, 'return[0]/speed', 1024 * 1024)
253 self
.cancel_and_wait(resume
=True)
256 if __name__
== '__main__':
257 iotests
.main(supported_fmts
=['qcow2', 'qed'])