aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/139
blob: b5470f709cd7f356c61b5cbe59f9fba41575fe4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env python
#
# Test cases for the QMP 'x-blockdev-del' command
#
# Copyright (C) 2015 Igalia, S.L.
# Author: Alberto Garcia <berto@igalia.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
import iotests
import time

base_img = os.path.join(iotests.test_dir, 'base.img')
new_img = os.path.join(iotests.test_dir, 'new.img')

class TestBlockdevDel(iotests.QMPTestCase):

    def setUp(self):
        iotests.qemu_img('create', '-f', iotests.imgfmt, base_img, '1M')
        self.vm = iotests.VM()
        self.vm.launch()

    def tearDown(self):
        self.vm.shutdown()
        os.remove(base_img)
        if os.path.isfile(new_img):
            os.remove(new_img)

    # Check whether a BlockBackend exists
    def checkBlockBackend(self, backend, node, must_exist = True):
        result = self.vm.qmp('query-block')
        backends = filter(lambda x: x['device'] == backend, result['return'])
        self.assertLessEqual(len(backends), 1)
        self.assertEqual(must_exist, len(backends) == 1)
        if must_exist:
            if node:
                self.assertEqual(backends[0]['inserted']['node-name'], node)
            else:
                self.assertFalse(backends[0].has_key('inserted'))

    # Check whether a BlockDriverState exists
    def checkBlockDriverState(self, node, must_exist = True):
        result = self.vm.qmp('query-named-block-nodes')
        nodes = filter(lambda x: x['node-name'] == node, result['return'])
        self.assertLessEqual(len(nodes), 1)
        self.assertEqual(must_exist, len(nodes) == 1)

    # Add a new BlockBackend (with its attached BlockDriverState)
    def addBlockBackend(self, backend, node):
        file_node = '%s_file' % node
        self.checkBlockBackend(backend, node, False)
        self.checkBlockDriverState(node, False)
        self.checkBlockDriverState(file_node, False)
        opts = {'driver': iotests.imgfmt,
                'id': backend,
                'node-name': node,
                'file': {'driver': 'file',
                         'node-name': file_node,
                         'filename': base_img}}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockBackend(backend, node)
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(file_node)

    # Add a BlockDriverState without a BlockBackend
    def addBlockDriverState(self, node):
        file_node = '%s_file' % node
        self.checkBlockDriverState(node, False)
        self.checkBlockDriverState(file_node, False)
        opts = {'driver': iotests.imgfmt,
                'node-name': node,
                'file': {'driver': 'file',
                         'node-name': file_node,
                         'filename': base_img}}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(file_node)

    # Add a BlockDriverState that will be used as overlay for the base_img BDS
    def addBlockDriverStateOverlay(self, node):
        self.checkBlockDriverState(node, False)
        iotests.qemu_img('create', '-f', iotests.imgfmt,
                         '-b', base_img, new_img, '1M')
        opts = {'driver': iotests.imgfmt,
                'node-name': node,
                'backing': '',
                'file': {'driver': 'file',
                         'filename': new_img}}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node)

    # Delete a BlockBackend
    def delBlockBackend(self, backend, node, expect_error = False,
                        destroys_media = True):
        self.checkBlockBackend(backend, node)
        if node:
            self.checkBlockDriverState(node)
        result = self.vm.qmp('x-blockdev-del', id = backend)
        if expect_error:
            self.assert_qmp(result, 'error/class', 'GenericError')
            if node:
                self.checkBlockDriverState(node)
        else:
            self.assert_qmp(result, 'return', {})
            if node:
                self.checkBlockDriverState(node, not destroys_media)
        self.checkBlockBackend(backend, node, must_exist = expect_error)

    # Delete a BlockDriverState
    def delBlockDriverState(self, node, expect_error = False):
        self.checkBlockDriverState(node)
        result = self.vm.qmp('x-blockdev-del', node_name = node)
        if expect_error:
            self.assert_qmp(result, 'error/class', 'GenericError')
        else:
            self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node, expect_error)

    # Add a device model
    def addDeviceModel(self, device, backend):
        result = self.vm.qmp('device_add', id = device,
                             driver = 'virtio-blk-pci', drive = backend)
        self.assert_qmp(result, 'return', {})

    # Delete a device model
    def delDeviceModel(self, device):
        result = self.vm.qmp('device_del', id = device)
        self.assert_qmp(result, 'return', {})

        result = self.vm.qmp('system_reset')
        self.assert_qmp(result, 'return', {})

        device_path = '/machine/peripheral/%s/virtio-backend' % device
        event = self.vm.event_wait(name="DEVICE_DELETED",
                                   match={'data': {'path': device_path}})
        self.assertNotEqual(event, None)

        event = self.vm.event_wait(name="DEVICE_DELETED",
                                   match={'data': {'device': device}})
        self.assertNotEqual(event, None)

    # Remove a BlockDriverState
    def ejectDrive(self, backend, node, expect_error = False,
                   destroys_media = True):
        self.checkBlockBackend(backend, node)
        self.checkBlockDriverState(node)
        result = self.vm.qmp('eject', device = backend)
        if expect_error:
            self.assert_qmp(result, 'error/class', 'GenericError')
            self.checkBlockDriverState(node)
            self.checkBlockBackend(backend, node)
        else:
            self.assert_qmp(result, 'return', {})
            self.checkBlockDriverState(node, not destroys_media)
            self.checkBlockBackend(backend, None)

    # Insert a BlockDriverState
    def insertDrive(self, backend, node):
        self.checkBlockBackend(backend, None)
        self.checkBlockDriverState(node)
        result = self.vm.qmp('blockdev-insert-medium',
                             device = backend, node_name = node)
        self.assert_qmp(result, 'return', {})
        self.checkBlockBackend(backend, node)
        self.checkBlockDriverState(node)

    # Create a snapshot using 'blockdev-snapshot-sync'
    def createSnapshotSync(self, node, overlay):
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(overlay, False)
        opts = {'node-name': node,
                'snapshot-file': new_img,
                'snapshot-node-name': overlay,
                'format': iotests.imgfmt}
        result = self.vm.qmp('blockdev-snapshot-sync', conv_keys=False, **opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(overlay)

    # Create a snapshot using 'blockdev-snapshot'
    def createSnapshot(self, node, overlay):
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(overlay)
        result = self.vm.qmp('blockdev-snapshot',
                             node = node, overlay = overlay)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(overlay)

    # Create a mirror
    def createMirror(self, backend, node, new_node):
        self.checkBlockBackend(backend, node)
        self.checkBlockDriverState(new_node, False)
        opts = {'device': backend,
                'target': new_img,
                'node-name': new_node,
                'sync': 'top',
                'format': iotests.imgfmt}
        result = self.vm.qmp('drive-mirror', conv_keys=False, **opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockBackend(backend, node)
        self.checkBlockDriverState(new_node)

    # Complete an existing block job
    def completeBlockJob(self, backend, node_before, node_after):
        self.checkBlockBackend(backend, node_before)
        result = self.vm.qmp('block-job-complete', device=backend)
        self.assert_qmp(result, 'return', {})
        self.wait_until_completed(backend)
        self.checkBlockBackend(backend, node_after)

    # Add a BlkDebug node
    # Note that the purpose of this is to test the x-blockdev-del
    # sanity checks, not to create a usable blkdebug drive
    def addBlkDebug(self, debug, node):
        self.checkBlockDriverState(node, False)
        self.checkBlockDriverState(debug, False)
        image = {'driver': iotests.imgfmt,
                 'node-name': node,
                 'file': {'driver': 'file',
                          'filename': base_img}}
        opts = {'driver': 'blkdebug',
                'node-name': debug,
                'image': image}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(node)
        self.checkBlockDriverState(debug)

    # Add a BlkVerify node
    # Note that the purpose of this is to test the x-blockdev-del
    # sanity checks, not to create a usable blkverify drive
    def addBlkVerify(self, blkverify, test, raw):
        self.checkBlockDriverState(test, False)
        self.checkBlockDriverState(raw, False)
        self.checkBlockDriverState(blkverify, False)
        iotests.qemu_img('create', '-f', iotests.imgfmt, new_img, '1M')
        node_0 = {'driver': iotests.imgfmt,
                  'node-name': test,
                  'file': {'driver': 'file',
                           'filename': base_img}}
        node_1 = {'driver': iotests.imgfmt,
                  'node-name': raw,
                  'file': {'driver': 'file',
                           'filename': new_img}}
        opts = {'driver': 'blkverify',
                'node-name': blkverify,
                'test': node_0,
                'raw': node_1}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(test)
        self.checkBlockDriverState(raw)
        self.checkBlockDriverState(blkverify)

    # Add a Quorum node
    def addQuorum(self, quorum, child0, child1):
        self.checkBlockDriverState(child0, False)
        self.checkBlockDriverState(child1, False)
        self.checkBlockDriverState(quorum, False)
        iotests.qemu_img('create', '-f', iotests.imgfmt, new_img, '1M')
        child_0 = {'driver': iotests.imgfmt,
                   'node-name': child0,
                   'file': {'driver': 'file',
                            'filename': base_img}}
        child_1 = {'driver': iotests.imgfmt,
                   'node-name': child1,
                   'file': {'driver': 'file',
                            'filename': new_img}}
        opts = {'driver': 'quorum',
                'node-name': quorum,
                'vote-threshold': 1,
                'children': [ child_0, child_1 ]}
        result = self.vm.qmp('blockdev-add', conv_keys = False, options = opts)
        self.assert_qmp(result, 'return', {})
        self.checkBlockDriverState(child0)
        self.checkBlockDriverState(child1)
        self.checkBlockDriverState(quorum)

    ########################
    # The tests start here #
    ########################

    def testWrongParameters(self):
        self.addBlockBackend('drive0', 'node0')
        result = self.vm.qmp('x-blockdev-del')
        self.assert_qmp(result, 'error/class', 'GenericError')
        result = self.vm.qmp('x-blockdev-del', id='drive0', node_name='node0')
        self.assert_qmp(result, 'error/class', 'GenericError')
        self.delBlockBackend('drive0', 'node0')

    def testBlockBackend(self):
        self.addBlockBackend('drive0', 'node0')
        # You cannot delete a BDS that is attached to a backend
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockBackend('drive0', 'node0')

    def testBlockDriverState(self):
        self.addBlockDriverState('node0')
        # You cannot delete a file BDS directly
        self.delBlockDriverState('node0_file', expect_error = True)
        self.delBlockDriverState('node0')

    def testEject(self):
        self.addBlockBackend('drive0', 'node0')
        self.ejectDrive('drive0', 'node0')
        self.delBlockBackend('drive0', None)

    def testDeviceModel(self):
        self.addBlockBackend('drive0', 'node0')
        self.addDeviceModel('device0', 'drive0')
        self.ejectDrive('drive0', 'node0', expect_error = True)
        self.delBlockBackend('drive0', 'node0', expect_error = True)
        self.delDeviceModel('device0')
        self.delBlockBackend('drive0', 'node0')

    def testAttachMedia(self):
        # This creates a BlockBackend and removes its media
        self.addBlockBackend('drive0', 'node0')
        self.ejectDrive('drive0', 'node0')
        # This creates a new BlockDriverState and inserts it into the backend
        self.addBlockDriverState('node1')
        self.insertDrive('drive0', 'node1')
        # The backend can't be removed: the new BDS has an extra reference
        self.delBlockBackend('drive0', 'node1', expect_error = True)
        self.delBlockDriverState('node1', expect_error = True)
        # The BDS still exists after being ejected, but now it can be removed
        self.ejectDrive('drive0', 'node1', destroys_media = False)
        self.delBlockDriverState('node1')
        self.delBlockBackend('drive0', None)

    def testSnapshotSync(self):
        self.addBlockBackend('drive0', 'node0')
        self.createSnapshotSync('node0', 'overlay0')
        # This fails because node0 is now being used as a backing image
        self.delBlockDriverState('node0', expect_error = True)
        # This succeeds because overlay0 only has the backend reference
        self.delBlockBackend('drive0', 'overlay0')
        self.checkBlockDriverState('node0', False)

    def testSnapshot(self):
        self.addBlockBackend('drive0', 'node0')
        self.addBlockDriverStateOverlay('overlay0')
        self.createSnapshot('node0', 'overlay0')
        self.delBlockBackend('drive0', 'overlay0', expect_error = True)
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockDriverState('overlay0', expect_error = True)
        self.ejectDrive('drive0', 'overlay0', destroys_media = False)
        self.delBlockBackend('drive0', None)
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockDriverState('overlay0')
        self.checkBlockDriverState('node0', False)

    def testMirror(self):
        self.addBlockBackend('drive0', 'node0')
        self.createMirror('drive0', 'node0', 'mirror0')
        # The block job prevents removing the device
        self.delBlockBackend('drive0', 'node0', expect_error = True)
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockDriverState('mirror0', expect_error = True)
        self.wait_ready('drive0')
        self.completeBlockJob('drive0', 'node0', 'mirror0')
        self.assert_no_active_block_jobs()
        self.checkBlockDriverState('node0', False)
        # This succeeds because the backend now points to mirror0
        self.delBlockBackend('drive0', 'mirror0')

    def testBlkDebug(self):
        self.addBlkDebug('debug0', 'node0')
        # 'node0' is used by the blkdebug node
        self.delBlockDriverState('node0', expect_error = True)
        # But we can remove the blkdebug node directly
        self.delBlockDriverState('debug0')
        self.checkBlockDriverState('node0', False)

    def testBlkVerify(self):
        self.addBlkVerify('verify0', 'node0', 'node1')
        # We cannot remove the children of a blkverify device
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockDriverState('node1', expect_error = True)
        # But we can remove the blkverify node directly
        self.delBlockDriverState('verify0')
        self.checkBlockDriverState('node0', False)
        self.checkBlockDriverState('node1', False)

    def testQuorum(self):
        self.addQuorum('quorum0', 'node0', 'node1')
        # We cannot remove the children of a Quorum device
        self.delBlockDriverState('node0', expect_error = True)
        self.delBlockDriverState('node1', expect_error = True)
        # But we can remove the Quorum node directly
        self.delBlockDriverState('quorum0')
        self.checkBlockDriverState('node0', False)
        self.checkBlockDriverState('node1', False)


if __name__ == '__main__':
    iotests.main(supported_fmts=["qcow2"])