From 8477cf49de988a15b3fc58d91c54efef295543fd Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 9 Jul 2016 20:14:58 +0100 Subject: [PATCH 05/11] tests: Multiple small additions and cleanups to the test scripts. Use a common 'functions.sh' file for test functions. --- TODO | 2 ++ tests/Makefile.am | 10 ++++-- tests/functions.sh | 39 +++++++++++++++++++++ tests/test-captive.sh | 7 ++-- tests/test-dump-plugin.sh | 8 ++--- tests/test-foreground.sh | 4 +-- tests/test-help-plugin.sh | 3 +- tests/test-ipv4.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++ tests/test-random-sock.sh | 5 ++- tests/test-single.sh | 3 +- tests/test-start.sh | 5 ++- tests/test-version-plugin.sh | 42 ++++++++++++++++++++++ tests/test-version.sh | 41 ++++++++++++++++++++++ 13 files changed, 232 insertions(+), 20 deletions(-) create mode 100644 tests/functions.sh create mode 100755 tests/test-ipv4.sh create mode 100755 tests/test-version-plugin.sh create mode 100755 tests/test-version.sh diff --git a/TODO b/TODO index 30bf72e..6c5bb5b 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,5 @@ +* Listen on specific interfaces or protocols (eg. only IPv6). + * Can we do language bindings using #!'s? You would enter: nbdkit foo [args] diff --git a/tests/Makefile.am b/tests/Makefile.am index 08fd02a..6f6cfa2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -41,6 +41,7 @@ EXTRA_DIST = \ test-foreground.sh \ test-help.sh \ test-help-plugin.sh \ + test-ipv4.sh \ test.pl \ test.py \ test_ocaml_plugin.ml \ @@ -48,20 +49,25 @@ EXTRA_DIST = \ test.rb \ test-single.sh \ test-start.sh \ - test-random-sock.sh + test-random-sock.sh \ + test-version.sh \ + test-version-plugin.sh # Basic server command line and start-up tests. TESTS = \ test-help.sh \ test-help-plugin.sh \ + test-version.sh \ + test-version-plugin.sh \ test-dump-config.sh \ test-dump-plugin.sh \ test-start.sh \ test-foreground.sh \ test-single.sh \ test-captive.sh \ - test-random-sock.sh + test-random-sock.sh \ + test-ipv4.sh # In-depth tests need libguestfs, since that is a convenient way to # drive qemu. diff --git a/tests/functions.sh b/tests/functions.sh new file mode 100644 index 0000000..4ebddbb --- /dev/null +++ b/tests/functions.sh @@ -0,0 +1,39 @@ +# nbdkit +# Copyright (C) 2016 Red Hat Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of Red Hat nor the names of its contributors may be +# used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +# Some functions used by the tests. + +# Return the path to a named nbdkit plugin. +nbdkit_plugin () +{ + echo "../plugins/$1/.libs/nbdkit-$1-plugin.so" +} diff --git a/tests/test-captive.sh b/tests/test-captive.sh index 5f18b5f..098024b 100755 --- a/tests/test-captive.sh +++ b/tests/test-captive.sh @@ -33,14 +33,15 @@ set -e set -x +source ./functions.sh # Test nbdkit --run (captive nbdkit) option. rm -f captive.sock captive.out -../src/nbdkit -U captive.sock \ - ../plugins/example1/.libs/nbdkit-example1-plugin.so \ - --run 'sleep 5; echo nbd=$nbd; echo port=$port; echo socket=$unixsocket' > captive.out +../src/nbdkit -U captive.sock `nbdkit_plugin example1` --run ' + sleep 5; echo nbd=$nbd; echo port=$port; echo socket=$unixsocket + ' > captive.out # Check the output. if [ "$(cat captive.out)" != "nbd=nbd:unix:$(pwd)/captive.sock diff --git a/tests/test-dump-plugin.sh b/tests/test-dump-plugin.sh index 5cde25b..2517689 100755 --- a/tests/test-dump-plugin.sh +++ b/tests/test-dump-plugin.sh @@ -32,12 +32,10 @@ # SUCH DAMAGE. set -e +source ./functions.sh -output="$( - ../src/nbdkit \ - ../plugins/example1/.libs/nbdkit-example1-plugin.so --dump-plugin - )" -if [[ ! ( "$output" =~ ^path= ) ]]; then +output="$(../src/nbdkit `nbdkit_plugin example1` --dump-plugin)" +if [[ ! ( "$output" =~ name\=example1 ) ]]; then echo "$0: unexpected output from nbdkit --dump-plugin" echo "$output" exit 1 diff --git a/tests/test-foreground.sh b/tests/test-foreground.sh index daa733e..6d23bf4 100755 --- a/tests/test-foreground.sh +++ b/tests/test-foreground.sh @@ -33,14 +33,14 @@ set -e set -x +source ./functions.sh # Test nbdkit -f option. rm -f foreground.pid foreground.sock ../src/nbdkit \ - -f -P foreground.pid -U foreground.sock \ - ../plugins/example1/.libs/nbdkit-example1-plugin.so & + -f -P foreground.pid -U foreground.sock `nbdkit_plugin example1` & bg_pid=$! # We may have to wait a short time for the pid file to appear. diff --git a/tests/test-help-plugin.sh b/tests/test-help-plugin.sh index dba1028..92bb277 100755 --- a/tests/test-help-plugin.sh +++ b/tests/test-help-plugin.sh @@ -32,8 +32,9 @@ # SUCH DAMAGE. set -e +source ./functions.sh -output="$(../src/nbdkit ../plugins/file/.libs/nbdkit-file-plugin.so --help)" +output="$(../src/nbdkit `nbdkit_plugin file` --help)" if [[ ! ( "$output" =~ "nbdkit file plugin" ) ]]; then echo "$0: unexpected output from nbdkit file --help" echo "$output" diff --git a/tests/test-ipv4.sh b/tests/test-ipv4.sh new file mode 100755 index 0000000..8eda61c --- /dev/null +++ b/tests/test-ipv4.sh @@ -0,0 +1,83 @@ +#!/bin/bash - +# nbdkit +# Copyright (C) 2016 Red Hat Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the name of Red Hat nor the names of its contributors may be +# used to endorse or promote products derived from this software without +# specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +# Every other test uses a Unix domain socket. This tests nbdkit over +# IPv4 localhost connections. +# +# XXX We should be able to test "just IPv6". However there is +# currently no option to listen only on particular interfaces. + +set -e +source ./functions.sh + +# Don't fail if certain commands aren't available. +if ! ss --version; then + echo "$0: 'ss' command not available" + exit 77 +fi +if ! socat -h; then + echo "$0: 'socat' command not available" + exit 77 +fi + +# Find an unused port to listen on. +for port in `seq 49152 65535`; do + if ! ss -ltn | grep -sqE ":$port\b"; then break; fi +done +echo picked unused port $port + +../src/nbdkit -P ipv4.pid -p $port `nbdkit_plugin example1` + +# We may have to wait a short time for the pid file to appear. +for i in `seq 1 10`; do + if test -f ipv4.pid; then + break + fi + sleep 1 +done +if ! test -f ipv4.pid; then + echo "$0: PID file was not created" + exit 1 +fi + +pid="$(cat ipv4.pid)" + +# Check the process exists. +kill -s 0 $pid + +# Check we can connect to the socket. +socat TCP:localhost:$port STDIO