From 55387b0ee3d14a2f95cda605c12e39cf53959f90 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 7 May 2023 23:01:24 +0100 Subject: [PATCH] extract common shell fns in tests --- tests/jffs2/test.nix | 23 ----------------------- tests/min-copy-closure/test.nix | 19 +------------------ tests/pppoe/test.nix | 21 +-------------------- tests/test-helpers.sh | 21 +++++++++++++++++++++ tests/wlan/test.nix | 18 +----------------- 5 files changed, 24 insertions(+), 78 deletions(-) create mode 100644 tests/test-helpers.sh diff --git a/tests/jffs2/test.nix b/tests/jffs2/test.nix index b50a1e9..7a04b03 100644 --- a/tests/jffs2/test.nix +++ b/tests/jffs2/test.nix @@ -15,29 +15,6 @@ in pkgs.runCommand "check" { socat ] ; } '' -serverstatedir=$(mktemp -d -t routeros-XXXXXX) - -killpid(){ - if test -e $1 && test -d /proc/`cat $1` ; then - pid=$(cat $1) - kill $pid - fi -} - -cleanup(){ - killpid $serverstatedir/pid - test -n "$MPLCONFIGDIR" && test -d "$MPLCONFIGDIR" && rm -rf "$MPLCONFIGDIR" - killpid foo.pid -} -trap cleanup EXIT - -fatal(){ - err=$? - echo "FAIL: command $(eval echo $BASH_COMMAND) exited with code $err" - exit $err -} -trap fatal ERR - mkdir vm mips-vm --background ./vm ${img}/vmlinux ${img}/rootfs expect ${./script.expect} >$out diff --git a/tests/min-copy-closure/test.nix b/tests/min-copy-closure/test.nix index 48bb726..fc58b68 100644 --- a/tests/min-copy-closure/test.nix +++ b/tests/min-copy-closure/test.nix @@ -19,24 +19,7 @@ in pkgs.runCommand "check" { rogue ] ; } '' -killpid(){ - if test -e $1 && test -d /proc/`cat $1` ; then - pid=$(cat $1) - kill $pid - fi -} - -cleanup(){ - killpid ./vm/pid -} - -trap cleanup EXIT -fatal(){ - err=$? - echo "FAIL: command $(eval echo $BASH_COMMAND) exited with code $err" - exit $err -} -trap fatal ERR +. ${../test-helpers.sh} mkdir vm LAN=user,hostfwd=tcp::2022-:22 mips-vm --background ./vm ${img}/vmlinux ${img}/rootfs diff --git a/tests/pppoe/test.nix b/tests/pppoe/test.nix index ef51013..c5d261f 100644 --- a/tests/pppoe/test.nix +++ b/tests/pppoe/test.nix @@ -23,26 +23,7 @@ serverstatedir=$(mktemp -d -t routeros-XXXXXX) # a sandbox with no $HOME, hence this environment variable export MPLCONFIGDIR=$(mktemp -d -t routeros-XXXXXX) -killpid(){ - if test -e $1 && test -d /proc/`cat $1` ; then - pid=$(cat $1) - kill $pid - fi -} - -cleanup(){ - killpid $serverstatedir/pid - test -n "$MPLCONFIGDIR" && test -d "$MPLCONFIGDIR" && rm -rf "$MPLCONFIGDIR" - killpid foo.pid -} -trap cleanup EXIT - -fatal(){ - err=$? - echo "FAIL: command $(eval echo $BASH_COMMAND) exited with code $err" - exit $err -} -trap fatal ERR +. ${../test-helpers.sh} routeros $serverstatedir mkdir vm diff --git a/tests/test-helpers.sh b/tests/test-helpers.sh new file mode 100644 index 0000000..b9f81e2 --- /dev/null +++ b/tests/test-helpers.sh @@ -0,0 +1,21 @@ +killpid(){ + if test -e $1 && test -d /proc/`cat $1` ; then + pid=$(cat $1) + kill $pid + fi +} + +cleanup(){ + killpid $serverstatedir/pid + test -n "$MPLCONFIGDIR" && test -d "$MPLCONFIGDIR" && rm -rf "$MPLCONFIGDIR" + killpid foo.pid + killpid ./vm/pid +} +trap cleanup EXIT + +fatal(){ + err=$? + echo "FAIL: command $(eval echo $BASH_COMMAND) exited with code $err" + exit $err +} +trap fatal ERR diff --git a/tests/wlan/test.nix b/tests/wlan/test.nix index 24e2a3f..4afc38d 100644 --- a/tests/wlan/test.nix +++ b/tests/wlan/test.nix @@ -15,23 +15,7 @@ in pkgs.runCommand "check" { socat ] ; } '' -killpid(){ - if test -e $1 && test -d /proc/`cat $1` ; then - pid=$(cat $1) - kill $pid - fi -} - -cleanup(){ - killpid ./vm/pid -} -trap cleanup EXIT -fatal(){ - err=$? - echo "FAIL: command $(eval echo $BASH_COMMAND) exited with code $err" - exit $err -} -trap fatal ERR +. ${../test-helpers.sh} mkdir vm mips-vm --background ./vm ${img}/vmlinux ${img}/rootfs