qemu/tests/qemu-iotests/147
Markus Armbruster 9445673ea6 nbd: Tidy up blockdev-add interface
SocketAddress is a simple union, and simple unions are awkward: they
have their variant members wrapped in a "data" object on the wire, and
require additional indirections in C.  I intend to limit its use to
existing external interfaces, and convert all internal interfaces to
SocketAddressFlat.

BlockdevOptionsNbd is an external interface using SocketAddress.  We
already use SocketAddressFlat elsewhere in blockdev-add.  Replace it
by SocketAddressFlat while we can (it's new in 2.9) for simplicity and
consistency.  For example,

    { "execute": "blockdev-add",
      "arguments": { "node-name": "foo", "driver": "nbd",
                     "server": { "type": "inet",
		                 "data": { "host": "localhost",
				           "port": "12345" } } } }

becomes

    { "execute": "blockdev-add",
      "arguments": { "node-name": "foo", "driver": "nbd",
                     "server": { "type": "inet",
		                 "host": "localhost", "port": "12345" } } }

Since the internal interfaces still take SocketAddress, this requires
conversion function socket_address_crumple().  It'll go away when I
update the interfaces.

Unfortunately, SocketAddress is also visible in -drive since 2.8:

    -drive if=none,driver=nbd,server.type=inet,server.data.host=127.0.0.1,server.data.port=12345

Nobody should be using it, as it's fairly new and has never been
documented, so adding still more compatibility gunk to keep it working
isn't worth the trouble.  You now have to use

    -drive if=none,driver=nbd,server.type=inet,server.host=127.0.0.1,server.port=12345

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-id: 1490895797-29094-9-git-send-email-armbru@redhat.com

[mreitz: Change iotest 147 accordingly]

Because of this interface change, iotest 147 has to be adapted.
Unfortunately, we cannot just flatten all of the addresses because
nbd-server-start still takes a plain SocketAddress. Therefore, we need
both and this is most easily achieved by writing the SocketAddress into
the code and flattening it where necessary.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-id: 20170330221243.17333-1-mreitz@redhat.com

Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
2017-04-03 17:11:39 +02:00

205 lines
6.9 KiB
Python
Executable File

#!/usr/bin/env python
#
# Test case for NBD's blockdev-add interface
#
# Copyright (C) 2016 Red Hat, Inc.
#
# 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 socket
import stat
import time
import iotests
from iotests import cachemode, imgfmt, qemu_img, qemu_nbd
NBD_PORT = 10811
test_img = os.path.join(iotests.test_dir, 'test.img')
unix_socket = os.path.join(iotests.test_dir, 'nbd.socket')
def flatten_sock_addr(crumpled_address):
result = { 'type': crumpled_address['type'] }
result.update(crumpled_address['data'])
return result
class NBDBlockdevAddBase(iotests.QMPTestCase):
def blockdev_add_options(self, address, export=None):
options = { 'node-name': 'nbd-blockdev',
'driver': 'raw',
'file': {
'driver': 'nbd',
'server': address
} }
if export is not None:
options['file']['export'] = export
return options
def client_test(self, filename, address, export=None):
bao = self.blockdev_add_options(address, export)
result = self.vm.qmp('blockdev-add', **bao)
self.assert_qmp(result, 'return', {})
result = self.vm.qmp('query-named-block-nodes')
for node in result['return']:
if node['node-name'] == 'nbd-blockdev':
if isinstance(filename, str):
self.assert_qmp(node, 'image/filename', filename)
else:
self.assert_json_filename_equal(node['image']['filename'],
filename)
break
result = self.vm.qmp('blockdev-del', node_name='nbd-blockdev')
self.assert_qmp(result, 'return', {})
class QemuNBD(NBDBlockdevAddBase):
def setUp(self):
qemu_img('create', '-f', iotests.imgfmt, test_img, '64k')
self.vm = iotests.VM()
self.vm.launch()
def tearDown(self):
self.vm.shutdown()
os.remove(test_img)
try:
os.remove(unix_socket)
except OSError:
pass
def _server_up(self, *args):
self.assertEqual(qemu_nbd('-f', imgfmt, test_img, *args), 0)
def test_inet(self):
self._server_up('-p', str(NBD_PORT))
address = { 'type': 'inet',
'data': {
'host': 'localhost',
'port': str(NBD_PORT)
} }
self.client_test('nbd://localhost:%i' % NBD_PORT,
flatten_sock_addr(address))
def test_unix(self):
self._server_up('-k', unix_socket)
address = { 'type': 'unix',
'data': { 'path': unix_socket } }
self.client_test('nbd+unix://?socket=' + unix_socket,
flatten_sock_addr(address))
class BuiltinNBD(NBDBlockdevAddBase):
def setUp(self):
qemu_img('create', '-f', iotests.imgfmt, test_img, '64k')
self.vm = iotests.VM()
self.vm.launch()
self.server = iotests.VM('.server')
self.server.add_drive_raw('if=none,id=nbd-export,' +
'file=%s,' % test_img +
'format=%s,' % imgfmt +
'cache=%s' % cachemode)
self.server.launch()
def tearDown(self):
self.vm.shutdown()
self.server.shutdown()
os.remove(test_img)
try:
os.remove(unix_socket)
except OSError:
pass
def _server_up(self, address):
result = self.server.qmp('nbd-server-start', addr=address)
self.assert_qmp(result, 'return', {})
result = self.server.qmp('nbd-server-add', device='nbd-export')
self.assert_qmp(result, 'return', {})
def _server_down(self):
result = self.server.qmp('nbd-server-stop')
self.assert_qmp(result, 'return', {})
def test_inet(self):
address = { 'type': 'inet',
'data': {
'host': 'localhost',
'port': str(NBD_PORT)
} }
self._server_up(address)
self.client_test('nbd://localhost:%i/nbd-export' % NBD_PORT,
flatten_sock_addr(address), 'nbd-export')
self._server_down()
def test_inet6(self):
address = { 'type': 'inet',
'data': {
'host': '::1',
'port': str(NBD_PORT),
'ipv4': False,
'ipv6': True
} }
filename = { 'driver': 'raw',
'file': {
'driver': 'nbd',
'export': 'nbd-export',
'server': flatten_sock_addr(address)
} }
self._server_up(address)
self.client_test(filename, flatten_sock_addr(address), 'nbd-export')
self._server_down()
def test_unix(self):
address = { 'type': 'unix',
'data': { 'path': unix_socket } }
self._server_up(address)
self.client_test('nbd+unix:///nbd-export?socket=' + unix_socket,
flatten_sock_addr(address), 'nbd-export')
self._server_down()
def test_fd(self):
self._server_up({ 'type': 'unix',
'data': { 'path': unix_socket } })
sockfd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sockfd.connect(unix_socket)
result = self.vm.send_fd_scm(str(sockfd.fileno()))
self.assertEqual(result, 0, 'Failed to send socket FD')
result = self.vm.qmp('getfd', fdname='nbd-fifo')
self.assert_qmp(result, 'return', {})
address = { 'type': 'fd',
'data': { 'str': 'nbd-fifo' } }
filename = { 'driver': 'raw',
'file': {
'driver': 'nbd',
'export': 'nbd-export',
'server': flatten_sock_addr(address)
} }
self.client_test(filename, flatten_sock_addr(address), 'nbd-export')
self._server_down()
if __name__ == '__main__':
# Need to support image creation
iotests.main(supported_fmts=['vpc', 'parallels', 'qcow', 'vdi', 'qcow2',
'vmdk', 'raw', 'vhdx', 'qed'])