[PATCH] iotests/nbd-tls-iothread: use UNIX sockets for nbd server

Andrey Drobyshev posted 1 patch 3 weeks, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260507110352.449937-1-andrey.drobyshev@virtuozzo.com
Maintainers: Eric Blake <eblake@redhat.com>, Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>
tests/qemu-iotests/tests/nbd-tls-iothread     | 35 ++++---------------
tests/qemu-iotests/tests/nbd-tls-iothread.out |  7 ++--
2 files changed, 9 insertions(+), 33 deletions(-)
[PATCH] iotests/nbd-tls-iothread: use UNIX sockets for nbd server
Posted by Andrey Drobyshev 3 weeks, 2 days ago
The test is picking an unused port to start NBD server, by running 'ss -ltn'.
This is racy, which is specified in the the comment, as we may hit a situation
where port gets used by some other process in between 'ss -ltn' and qemu-nbd
runs.

This is exactly the case which I've recently seen, which leads to the test
failing with:

  -{"return": {}}
  +{"error": {"class": "GenericError", "desc": "Failed to find an available
               port: Address already in use"}}

The original assumption was that QEMU doesn't permit running NBD+TLS over
a UNIX domain socket.  However starting from the commit e8ae8b1a75e
("block/nbd: don't restrict TLS usage to IP sockets") it's possible.  So
let's do just that, simplifying the test and eliminating the race.

Signed-off-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
---
 tests/qemu-iotests/tests/nbd-tls-iothread     | 35 ++++---------------
 tests/qemu-iotests/tests/nbd-tls-iothread.out |  7 ++--
 2 files changed, 9 insertions(+), 33 deletions(-)

diff --git a/tests/qemu-iotests/tests/nbd-tls-iothread b/tests/qemu-iotests/tests/nbd-tls-iothread
index a2fb07206e5..c386c77e377 100755
--- a/tests/qemu-iotests/tests/nbd-tls-iothread
+++ b/tests/qemu-iotests/tests/nbd-tls-iothread
@@ -47,38 +47,15 @@ cd ..
 _supported_fmt qcow2  # Hardcoded to qcow2 command line and QMP below
 _supported_proto file
 
-# pick_unused_port
-#
-# Picks and returns an "unused" port, setting the global variable
-# $port.
-#
-# This is inherently racy, but we need it because qemu does not currently
-# permit NBD+TLS over a Unix domain socket
-pick_unused_port ()
-{
-    if ! (ss --version) >/dev/null 2>&1; then
-        _notrun "ss utility required, skipped this test"
-    fi
-
-    # Start at a random port to make it less likely that two parallel
-    # tests will conflict.
-    port=$(( 50000 + (RANDOM%15000) ))
-    while ss -ltn | grep -sqE ":$port\b"; do
-        ((port++))
-        if [ $port -eq 65000 ]; then port=50000; fi
-    done
-    echo picked unused port
-}
-
 tls_x509_init
 
 size=1G
 DST_IMG="$TEST_DIR/dst.qcow2"
+nbd_sock="$SOCK_DIR/nbd-tls-iothread.sock"
 
 echo
-echo "== preparing TLS creds and spare port =="
+echo "== preparing TLS creds =="
 
-pick_unused_port
 tls_x509_create_root_ca "ca1"
 tls_x509_create_server "ca1" "server1"
 tls_x509_create_client "ca1" "client1"
@@ -136,8 +113,8 @@ echo === Dst VM: Enable NBD server for incoming storage migration ===
 echo
 
 _send_qemu_cmd $h2 '{"execute": "nbd-server-start", "arguments":
-    {"addr": {"type": "inet", "data": {"host": "127.0.0.1", "port": "'$port'"}},
-              "tls-creds": "tls0"}}' '{"return": {}}' | sed "s/\"$port\"/PORT/g"
+    {"addr": {"type": "unix", "data": {"path": "'$nbd_sock'"}},
+              "tls-creds": "tls0"}}' '{"return": {}}'
 _send_qemu_cmd $h2 '{"execute": "block-export-add", "arguments":
     {"node-name": "drive_image1", "type": "nbd", "writable": true,
       "id": "drive_image1"}}' '{"return": {}}'
@@ -148,9 +125,9 @@ echo
 
 _send_qemu_cmd $h1 '{"execute": "blockdev-add", "arguments":
     {"node-name": "mirror", "driver": "nbd",
-     "server": {"type": "inet", "host": "127.0.0.1", "port": "'$port'"},
+     "server": {"type": "unix", "path": "'$nbd_sock'"},
      "export": "drive_image1", "tls-creds": "tls0",
-     "tls-hostname": "127.0.0.1"}}' '{"return": {}}' | sed "s/\"$port\"/PORT/g"
+     "tls-hostname": "127.0.0.1"}}' '{"return": {}}'
 _send_qemu_cmd $h1 '{"execute": "blockdev-mirror", "arguments":
     {"sync": "full", "device": "drive_image1", "target": "mirror",
      "job-id": "drive_image1_53"}}' '{"return": {}}'
diff --git a/tests/qemu-iotests/tests/nbd-tls-iothread.out b/tests/qemu-iotests/tests/nbd-tls-iothread.out
index 1d83d4f903b..997789a77eb 100644
--- a/tests/qemu-iotests/tests/nbd-tls-iothread.out
+++ b/tests/qemu-iotests/tests/nbd-tls-iothread.out
@@ -1,7 +1,6 @@
 QA output created by nbd-tls-iothread
 
-== preparing TLS creds and spare port ==
-picked unused port
+== preparing TLS creds ==
 Generating a self signed certificate...
 Generating a signed certificate...
 Generating a signed certificate...
@@ -23,7 +22,7 @@ Formatting 'TEST_DIR/dst.IMGFMT', fmt=IMGFMT size=1073741824
 === Dst VM: Enable NBD server for incoming storage migration ===
 
 {"execute": "nbd-server-start", "arguments":
-    {"addr": {"type": "inet", "data": {"host": "127.0.0.1", "port": PORT}},
+    {"addr": {"type": "unix", "data": {"path": "SOCK_DIR/nbd-tls-iothread.sock"}},
               "tls-creds": "tls0"}}
 {"return": {}}
 {"execute": "block-export-add", "arguments":
@@ -35,7 +34,7 @@ Formatting 'TEST_DIR/dst.IMGFMT', fmt=IMGFMT size=1073741824
 
 {"execute": "blockdev-add", "arguments":
     {"node-name": "mirror", "driver": "nbd",
-     "server": {"type": "inet", "host": "127.0.0.1", "port": PORT},
+     "server": {"type": "unix", "path": "SOCK_DIR/nbd-tls-iothread.sock"},
      "export": "drive_image1", "tls-creds": "tls0",
      "tls-hostname": "127.0.0.1"}}
 {"return": {}}
-- 
2.47.1