3 # Test case for NBD's blockdev-add interface
5 # Copyright (C) 2016 Red Hat, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from iotests
import cachemode
, imgfmt
, qemu_img
, qemu_nbd
30 test_img
= os
.path
.join(iotests
.test_dir
, 'test.img')
31 unix_socket
= os
.path
.join(iotests
.test_dir
, 'nbd.socket')
33 class NBDBlockdevAddBase(iotests
.QMPTestCase
):
34 def blockdev_add_options(self
, address
, export
=None):
35 options
= { 'node-name': 'nbd-blockdev',
41 if export
is not None:
42 options
['file']['export'] = export
45 def client_test(self
, filename
, address
, export
=None):
46 bao
= self
.blockdev_add_options(address
, export
)
47 result
= self
.vm
.qmp('blockdev-add', **bao
)
48 self
.assert_qmp(result
, 'return', {})
50 result
= self
.vm
.qmp('query-named-block-nodes')
51 for node
in result
['return']:
52 if node
['node-name'] == 'nbd-blockdev':
53 if isinstance(filename
, str):
54 self
.assert_qmp(node
, 'image/filename', filename
)
56 self
.assert_json_filename_equal(node
['image']['filename'],
60 result
= self
.vm
.qmp('x-blockdev-del', node_name
='nbd-blockdev')
61 self
.assert_qmp(result
, 'return', {})
64 class QemuNBD(NBDBlockdevAddBase
):
66 qemu_img('create', '-f', iotests
.imgfmt
, test_img
, '64k')
67 self
.vm
= iotests
.VM()
74 os
.remove(unix_socket
)
78 def _server_up(self
, *args
):
79 self
.assertEqual(qemu_nbd('-f', imgfmt
, test_img
, *args
), 0)
82 self
._server
_up
('-p', str(NBD_PORT
))
83 address
= { 'type': 'inet',
88 self
.client_test('nbd://localhost:%i' % NBD_PORT
, address
)
91 self
._server
_up
('-k', unix_socket
)
92 address
= { 'type': 'unix',
93 'data': { 'path': unix_socket
} }
94 self
.client_test('nbd+unix://?socket=' + unix_socket
, address
)
97 class BuiltinNBD(NBDBlockdevAddBase
):
99 qemu_img('create', '-f', iotests
.imgfmt
, test_img
, '64k')
100 self
.vm
= iotests
.VM()
102 self
.server
= iotests
.VM('.server')
103 self
.server
.add_drive_raw('if=none,id=nbd-export,' +
104 'file=%s,' % test_img
+
105 'format=%s,' % imgfmt
+
106 'cache=%s' % cachemode
)
111 self
.server
.shutdown()
114 os
.remove(unix_socket
)
118 def _server_up(self
, address
):
119 result
= self
.server
.qmp('nbd-server-start', addr
=address
)
120 self
.assert_qmp(result
, 'return', {})
122 result
= self
.server
.qmp('nbd-server-add', device
='nbd-export')
123 self
.assert_qmp(result
, 'return', {})
125 def _server_down(self
):
126 result
= self
.server
.qmp('nbd-server-stop')
127 self
.assert_qmp(result
, 'return', {})
130 address
= { 'type': 'inet',
133 'port': str(NBD_PORT
)
135 self
._server
_up
(address
)
136 self
.client_test('nbd://localhost:%i/nbd-export' % NBD_PORT
,
137 address
, 'nbd-export')
140 def test_inet6(self
):
141 address
= { 'type': 'inet',
144 'port': str(NBD_PORT
),
148 filename
= { 'driver': 'raw',
151 'export': 'nbd-export',
154 self
._server
_up
(address
)
155 self
.client_test(filename
, address
, 'nbd-export')
159 address
= { 'type': 'unix',
160 'data': { 'path': unix_socket
} }
161 self
._server
_up
(address
)
162 self
.client_test('nbd+unix:///nbd-export?socket=' + unix_socket
,
163 address
, 'nbd-export')
167 self
._server
_up
({ 'type': 'unix',
168 'data': { 'path': unix_socket
} })
170 sockfd
= socket
.socket(socket
.AF_UNIX
, socket
.SOCK_STREAM
)
171 sockfd
.connect(unix_socket
)
173 result
= self
.vm
.send_fd_scm(str(sockfd
.fileno()))
174 self
.assertEqual(result
, 0, 'Failed to send socket FD')
176 result
= self
.vm
.qmp('getfd', fdname
='nbd-fifo')
177 self
.assert_qmp(result
, 'return', {})
179 address
= { 'type': 'fd',
180 'data': { 'str': 'nbd-fifo' } }
181 filename
= { 'driver': 'raw',
184 'export': 'nbd-export',
187 self
.client_test(filename
, address
, 'nbd-export')
192 if __name__
== '__main__':
193 # Need to support image creation
194 iotests
.main(supported_fmts
=['vpc', 'parallels', 'qcow', 'vdi', 'qcow2',
195 'vmdk', 'raw', 'vhdx', 'qed'])