From: Ian Jackson Date: Sun, 22 Aug 2021 21:24:52 +0000 (+0100) Subject: a passing test! X-Git-Tag: hippotat/1.0.0~86 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=73e3a319fdbcc942fb6f980e1d7168d2aa4a4836;p=hippotat.git a passing test! Signed-off-by: Ian Jackson --- diff --git a/test/common b/test/common new file mode 100644 index 0000000..428d954 --- /dev/null +++ b/test/common @@ -0,0 +1,80 @@ +# -*- shell-script -*- +# Copyright 2021 Ian Jackson and contributors to Hippotat +# SPDX-License-Identifier: GPL-3.0-or-later +# There is NO WARRANTY. + +set -x + +ssrc="${0%/*}" +src="${ssrc%/*}" +test="${ssrc%/*}/test" + +fail () { echo >&2 "$0: fail: $*"; exit 1; } + +test-prep () { + + case "${0##*/}" in + t-*) tname="${0##*/t-}" ;; + *) fail "bad test script name $0" ;; + esac + + tmp=tmp/$tname + rm -rf "$tmp" + mkdir -p $tmp + + $test/netns-setup "$tname" + + trap ' + rc=$? + shutdown + if [ $rc = 0 ]; then echo "OK $tname"; fi + ' 0 +} + +kill-pids () { + for p in $pids; do kill -9 $p; done +} + +shutdown () { + kill-pids +} + +in-ns () { + local client_server=$1; shift + $exec ip netns exec hippotat-t-$tname-$client_server "$@" +} + +run-client () { + in-ns client \ + target/debug/hippotat --config $test/test.cfg -DD "$@" +} +run-server () { + in-ns server \ + target/debug/hippotatd --config $test/test.cfg -DD "$@" +} +spawn () { + { exec=exec; "$@"; } & + pids+=" $!" +} + +in-ns-await-up () { + local sc="$1"; shift + local addr="$1"; shift + local t=1 + while sleep $(( $t / 10 )).$(( $t % 10 )); do + if in-ns $sc ip -o addr show | fgrep " inet $addr "; then + return + fi + t=$(( $t + 1 )) + if [ $t -gt 10 ]; then fail "$sc did not come up $addr"; fi + done +} + +start-server () { + spawn run-server + in-ns-await-up server 192.0.2.1 +} +start-client () { + spawn run-client + in-ns-await-up client 192.0.2.3 +} diff --git a/test/go-with-unshare b/test/go-with-unshare new file mode 100755 index 0000000..ef8825e --- /dev/null +++ b/test/go-with-unshare @@ -0,0 +1,15 @@ +#!/bin/bash +# Copyright 2021 Ian Jackson and contributors to Hippotat +# SPDX-License-Identifier: GPL-3.0-or-later +# There is NO WARRANTY. + +set -e +. "${0%/*}"/common + +case "$1" in +*/*) ;; +?*) tname="$1"; shift; set -- "$test/$tname" "$@" ;; +'') fail 'bad usage: need program or test name' ;; +esac + +$src/test/with-unshare "$@" diff --git a/test/run-client b/test/run-client deleted file mode 100755 index 9b20eb7..0000000 --- a/test/run-client +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -# Copyright 2021 Ian Jackson and contributors to Hippotat -# SPDX-License-Identifier: GPL-3.0-or-later -# There is NO WARRANTY. - -set -e -slug=$1; if [ $# -gt 0 ]; then shift; fi - -if [ "x$1" = x--- ]; then - shift -else - set target/debug/hippotat --config src/test/test.cfg --debug "$@" -fi - -ip netns exec hippotat-t-$slug-client "$@" - diff --git a/test/run-server b/test/run-server deleted file mode 100755 index 8b99b83..0000000 --- a/test/run-server +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -# Copyright 2021 Ian Jackson and contributors to Hippotat -# SPDX-License-Identifier: GPL-3.0-or-later -# There is NO WARRANTY. - -set -e -slug=$1; if [ $# -gt 0 ]; then shift; fi - -if [ "x$1" = x--- ]; then - shift -else - set target/debug/hippotatd --config src/test/test.cfg --debug "$@" -fi - -ip netns exec hippotat-t-$slug-server "$@" - diff --git a/test/t-basic b/test/t-basic index 3dbc782..b1c145e 100755 --- a/test/t-basic +++ b/test/t-basic @@ -1,24 +1,14 @@ #!/bin/bash -set -ex +# Copyright 2021 Ian Jackson and contributors to Hippotat +# SPDX-License-Identifier: GPL-3.0-or-later +# There is NO WARRANTY. -# expects to be run inside the uml +set -e +. "${0%/*}"/common +test-prep -fail () { echo >&2 "$0: fail: $*"; exit 1; } +start-server +start-client -tname="${0#**/t-}" -case "$tname" in -.* | */* | '') fail "bad tname $tname" ;;# -esac - -test="${0%/*}" - -tmp=tmp/$tname -rm -rf "$tmp" -mkdir -p $tmp - -$test/netns-setup "$tname" -$test/run-server "$tname" & spid=$! -$test/run-client "$tname" & cpid=$1 - -$test/netns-run-client "$tname" -- ping -c 10 192.0.2.1 >$tmp/ping +in-ns client ping -c 10 192.0.2.1 >$tmp/ping grep ' 0% packet loss' $tmp/ping diff --git a/test/with-unshare b/test/with-unshare new file mode 100755 index 0000000..f1fdadd --- /dev/null +++ b/test/with-unshare @@ -0,0 +1,18 @@ +#!/bin/bash +# Copyright 2021 Ian Jackson and contributors to Hippotat +# SPDX-License-Identifier: GPL-3.0-or-later +# There is NO WARRANTY. + +set -e +. "${0%/*}"/common + + +#case "$1" in +#T/*) prog="$src/test/${1#T/}"; shift; set -- "$prog" "$@" ;; +#esac + +unshare -Urnm bash -xec ' + mount -t tmpfs tmpfs /run + PATH="$PATH:/usr/local/sbin:/sbin:/usr/sbin" + exec "$@" +' x "$@"