4 # Tests for qmp command nbd-server-remove.
6 # Copyright (c) 2017 Virtuozzo International GmbH
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/>.
26 from iotests import qemu_img_create, qemu_io, filter_qemu_io, QemuIoInteractive
28 nbd_sock = os.path.join(iotests.sock_dir, 'nbd_sock')
29 nbd_uri = 'nbd+unix:///exp?socket=' + nbd_sock
30 disk = os.path.join(iotests.test_dir, 'disk')
33 class TestNbdServerRemove(iotests.QMPTestCase):
35 qemu_img_create('-f', iotests.imgfmt, disk, '1M')
37 self.vm = iotests.VM().add_drive(disk)
47 result = self.vm.qmp('nbd-server-start', addr=address)
48 self.assert_qmp(result, 'return', {})
49 result = self.vm.qmp('nbd-server-add', device='drive0', name='exp')
50 self.assert_qmp(result, 'return', {})
57 def remove_export(self, name, mode=None):
59 return self.vm.qmp('nbd-server-remove', name=name)
61 return self.vm.qmp('nbd-server-remove', name=name, mode=mode)
63 def assertExportNotFound(self, name):
64 result = self.vm.qmp('nbd-server-remove', name=name)
65 self.assert_qmp(result, 'error/desc', "Export 'exp' is not found")
67 def assertExistingClients(self, result):
68 self.assert_qmp(result, 'error/desc', "export 'exp' still in use")
70 def assertReadOk(self, qemu_io_output):
72 filter_qemu_io(qemu_io_output).strip(),
73 'read 512/512 bytes at offset 0\n' +
74 '512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)')
76 def assertReadFailed(self, qemu_io_output):
77 self.assertEqual(filter_qemu_io(qemu_io_output).strip(),
78 'read failed: Input/output error')
80 def assertConnectFailed(self, qemu_io_output):
81 self.assertEqual(filter_qemu_io(qemu_io_output).strip(),
82 "qemu-io: can't open device " + nbd_uri +
83 ": Requested export not available\n"
84 "server reported: export 'exp' not present")
86 def do_test_connect_after_remove(self, mode=None):
87 args = ('-r', '-f', 'raw', '-c', 'read 0 512', nbd_uri)
88 self.assertReadOk(qemu_io(*args).stdout)
90 result = self.remove_export('exp', mode)
91 self.assert_qmp(result, 'return', {})
93 self.assertExportNotFound('exp')
94 self.assertConnectFailed(qemu_io(*args, check=False).stdout)
96 def test_connect_after_remove_default(self):
97 self.do_test_connect_after_remove()
99 def test_connect_after_remove_safe(self):
100 self.do_test_connect_after_remove('safe')
102 def test_connect_after_remove_force(self):
103 self.do_test_connect_after_remove('hard')
105 def do_test_remove_during_connect_safe(self, mode=None):
106 qio = QemuIoInteractive('-r', '-f', 'raw', nbd_uri)
107 self.assertReadOk(qio.cmd('read 0 512'))
109 result = self.remove_export('exp', mode)
110 self.assertExistingClients(result)
112 self.assertReadOk(qio.cmd('read 0 512'))
116 result = self.remove_export('exp', mode)
117 self.assert_qmp(result, 'return', {})
119 self.assertExportNotFound('exp')
121 def test_remove_during_connect_default(self):
122 self.do_test_remove_during_connect_safe()
124 def test_remove_during_connect_safe(self):
125 self.do_test_remove_during_connect_safe('safe')
127 def test_remove_during_connect_hard(self):
128 qio = QemuIoInteractive('-r', '-f', 'raw', nbd_uri)
129 self.assertReadOk(qio.cmd('read 0 512'))
131 result = self.remove_export('exp', 'hard')
132 self.assert_qmp(result, 'return', {})
134 self.assertReadFailed(qio.cmd('read 0 512'))
135 self.assertExportNotFound('exp')
139 def test_remove_during_connect_safe_hard(self):
140 qio = QemuIoInteractive('-r', '-f', 'raw', nbd_uri)
141 self.assertReadOk(qio.cmd('read 0 512'))
143 result = self.remove_export('exp', 'safe')
144 self.assertExistingClients(result)
146 self.assertReadOk(qio.cmd('read 0 512'))
148 result = self.remove_export('exp', 'hard')
149 self.assert_qmp(result, 'return', {})
151 self.assertExportNotFound('exp')
152 self.assertReadFailed(qio.cmd('read 0 512'))
156 if __name__ == '__main__':
157 iotests.main(supported_fmts=['raw'],
158 supported_protocols=['nbd'])