[Libguestfs] [libnbd PATCH] nbdsh: Add test of handling globals in -c

Eric Blake eblake at redhat.com
Thu Sep 12 16:14:59 UTC 2019


Test that we can script the read of a specific pattern from
nbdkit. Also, test that we can concatenate global functions through
consecutive -c.  This test fails if commit d6cbd130 is reverted.
---
 sh/Makefile.am  |  5 ++++-
 sh/test-dump.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100755 sh/test-dump.sh

diff --git a/sh/Makefile.am b/sh/Makefile.am
index 2eab987..50cd395 100644
--- a/sh/Makefile.am
+++ b/sh/Makefile.am
@@ -21,6 +21,7 @@ EXTRA_DIST = \
 	nbdsh.pod \
 	examples/LICENSE-FOR-EXAMPLES \
 	examples/hexdump.sh \
+	test-dump.sh \
 	test-help.sh \
 	test-version.sh \
 	$(NULL)
@@ -51,7 +52,9 @@ TESTS = \

 if HAVE_NBDKIT

-TESTS +=
+TESTS += \
+	test-dump.sh \
+	$(NULL)

 endif HAVE_NBDKIT

diff --git a/sh/test-dump.sh b/sh/test-dump.sh
new file mode 100755
index 0000000..2d4e261
--- /dev/null
+++ b/sh/test-dump.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# nbd client library in userspace
+# Copyright (C) 2019 Red Hat Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+# Test interaction with nbdkit, and for correct global handling over -c.
+nbdkit --exit-with-parent --version || exit 77
+
+sock=`mktemp -u /tmp/nbdsh.XXXXXX`
+pidfile=test-dump.pid
+trap 'status=$?; rm -f $sock $pidfile; exit $status' INT QUIT TERM EXIT ERR
+nbdkit -v -P $pidfile --exit-with-parent -U $sock pattern size=1m &
+
+# Wait for the pidfile to appear.
+for i in {1..60}; do
+    if test -f "$pidfile"; then
+	break
+    fi
+    sleep 1
+done
+if ! test -f "$pidfile"; then
+    echo "$0: nbdkit PID file $pidfile was not created"
+    exit 1
+fi
+
+nbdsh --connect "nbd+unix://?socket=$sock" \
+    -c '
+def size():
+  return h.get_size()
+' \
+    -c 'assert 1024*1024 == size()' \
+    -c 'assert h.pread(8, 8) == b"\x00\x00\x00\x00\x00\x00\x00\x08"'
-- 
2.21.0




More information about the Libguestfs mailing list