]> rtime.felk.cvut.cz Git - novaboot.git/blobdiff - tests/server.wv
server: Add support for default_cmd configuration variable
[novaboot.git] / tests / server.wv
index 233cc9686c6e4aa040745178259e6d21c2183a1e..8c8545101c34fefeff1b91a9bab38bbef25202a5 100755 (executable)
@@ -1,20 +1,23 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 cd $(dirname $0)
 . wvtest.sh
 
+NBT_HOME=$(getent passwd novaboot-test|cut -d: -f6)
 export WV_BASE_DIR
 export NOVABOOT_SHELL_CONFIG="$WV_BASE_DIR/.novaboot-shell"
 cat <<EOF > $NOVABOOT_SHELL_CONFIG
-console_cmd="/bin/sh -c 'while :; do sleep 0.1; date; done'"
+console_cmd="echo 'Hello console'; while :; do sleep 0.1; date; done"
 reset_cmd="touch reset_done"
 target_config='--prefix=asdf'
 EOF
 
+# Run server synchronously
 function run_server() {
     SSH_ORIGINAL_COMMAND="$*" SSH_CONNECTION="127.0.0.1 1234 127.0.0.1 22" $exec $WV_BASE_DIR/../server/novaboot-shell -c "${account:-user} ${username:-test} ${admin:+admin}"
 }
 
+# Exec server (for use with coproc - then the server can be easily killed)
 function exec_server() {
     local exec
     exec=exec
@@ -26,14 +29,6 @@ WVPASS run_server help | WVPASS tee log
 WVPASS grep 'Target commands:' log
 WVFAIL grep 'add-key' log
 
-WVSTART Help is printed without any arguments
-# Invocation with SSH_ORIGINAL_COMMAND
-WVPASS run_server help | WVPASS tee log
-WVPASS grep 'Target commands:' log
-# Invocation without SSH_ORIGINAL_COMMAND
-WVPASS $WV_BASE_DIR/../server/novaboot-shell | WVPASS tee log2
-WVPASS grep 'Target commands:' log2
-
 WVSTART Get-config command works
 run_server get-config > log
 WVPASS grep -e '^--prefix=asdf$' log
@@ -47,6 +42,23 @@ coproc exec_server console
 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${COPROC[0]}
 kill $COPROC_PID; wait
 
+WVSTART "Without any argument console_cmd is executed if no default_cmd is configured"
+coproc exec_server
+WVPASS sed -e '/Hello console/q0' -e '3q1' <&${COPROC[0]}
+kill $COPROC_PID; wait
+
+WVSTART "Without any argument default_cmd is executed if configured"
+(
+    export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
+    cat <<EOF > $NOVABOOT_SHELL_CONFIG
+default_cmd='echo "Hello default"'
+console_cmd='echo "Hello console"'
+EOF
+    WVPASS run_server | WVPASS grep -F 'Hello default'
+
+    WVPASS run_server console | WVPASS grep -F 'Hello console'
+)
+
 WVSTART 'Second connection to console prints queue'
 coproc console1 { exec_server console; }
 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
@@ -105,21 +117,45 @@ WVSTART "Quoting of config variables"
     WVPASS test -e '"file1 file2"'
 )
 
-SHELLRC="$WV_BASE_DIR/home-novaboot-test/.novaboot-shell"
-TFTPROOT="$WV_BASE_DIR/home-novaboot-test/tftproot"
-AUTH="$WV_BASE_DIR/home-novaboot-test/.ssh/authorized_keys"
+WVSTART "Config variables can depend on user name"
+(
+    export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
+
+    WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd="echo Hello $NB_USER"'
+    username=asdf WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello asdf"
+    username=qwer WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello qwer"
+)
+
+WVSTART "Missing console_cmd results in failure"
+NOVABOOT_SHELL_CONFIG="/dev/null" WVFAIL run_server console
+
+WVSTART "console_cmd may contain a shell function name"
+(
+    export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
+    WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd () { echo Hello user; }
+console_cmd=console_cmd'
+    WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello user"
+)
+
+SHELLRC="$NBT_HOME/.novaboot-shell"
+TFTPROOT="$NBT_HOME/tftproot"
+AUTH="$NBT_HOME/.ssh/authorized_keys"
+sudo_() { sudo -u novaboot-test "$@"; }
+
+WVSTART "Check that ssh-agent needed for following tests is running"
+WVPASS test -n "$SSH_AUTH_SOCK" || exit 1
 
 WVSTART "Setup SSH server's admin key"
 WVPASS ssh-add -L > keys
 WVPASS test $(wc -l < keys) -gt 0
-WVPASS mkdir -p $(dirname $AUTH)
-WVPASS chmod 700 $(dirname $AUTH)
-WVPASS echo 'command="user test admin"' $(tail -n 1 keys) > $AUTH
+WVPASS sudo_ mkdir -p $(dirname "$AUTH")
+WVPASS sudo_ chmod 700 $(dirname "$AUTH")
+WVPASS sudo_ tee "$AUTH" <<<"command=\"user test admin\" $(tail -n 1 keys)"
 
 WVSTART "Novaboot --ssh option (connect, rsync, reset)"
-WVPASS rm -rf $TFTPROOT
+WVPASS sudo_ rm -rf "$TFTPROOT"
 TS=$(date --rfc-3339=ns)
-WVPASS tee $SHELLRC <<EOF
+WVPASS sudo_ tee "$SHELLRC" <<EOF
 console_cmd=cat
 reset_cmd="echo $TS > reset.stamp"
 target_config=""
@@ -128,13 +164,14 @@ date > file
 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
 copy file
 EOF
-WVPASS diff -u file $TFTPROOT/file
-WVPASS diff -u <(echo $TS) "$WV_BASE_DIR/home-novaboot-test/reset.stamp"
-
+# Check that file was copied to tftproot
+WVPASS diff -u file <(sudo_ cat $TFTPROOT/file)
+# Check that reset command was executed
+WVPASS test "$TS" = "$(sudo_ cat "$NBT_HOME/reset.stamp")"
 
 WVSTART "Novaboot --ssh remote config"
-WVPASS rm -rf $TFTPROOT
-WVPASS tee $SHELLRC <<EOF
+WVPASS sudo_ rm -rf "$TFTPROOT"
+WVPASS sudo_ tee "$SHELLRC" <<EOF
 console_cmd=cat
 reset_cmd=true
 target_config="\
@@ -146,11 +183,11 @@ date > file
 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
 load file
 EOF
-WVPASS diff -u file $TFTPROOT/file
-WVPASS grep /prefix/file $TFTPROOT/grub.cfg
+WVPASS diff -u file <(sudo_ cat "$TFTPROOT/file")
+WVPASS sudo_ grep /prefix/file "$TFTPROOT/grub.cfg"
 
 WVSTART "Novaboot --ssh remote config fails on non-safe options"
-WVPASS tee $SHELLRC <<EOF
+WVPASS sudo_ tee "$SHELLRC" <<EOF
 console_cmd=cat
 reset_cmd=true
 target_config="\
@@ -158,12 +195,13 @@ target_config="\
 --make
 "
 EOF
-WVFAIL novaboot --ssh novaboot-test@localhost <<EOF
-load file < date
-EOF
+WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
+stderr=$(novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null)
+echo "$stderr" | WVPASS grep -q -F 'Unknown option: make'
+echo "$stderr" | WVPASS grep -q -F 'Error processing configuration from the server'
 
 WVSTART "Novaboot --ssh remote config fails on unknown arguments"
-WVPASS tee $SHELLRC <<EOF
+WVPASS sudo_ tee "$SHELLRC" <<EOF
 console_cmd=cat
 reset_cmd=true
 target_config="\
@@ -171,27 +209,27 @@ target_config="\
 blablabla
 "
 EOF
-WVFAIL novaboot --ssh novaboot-test@localhost <<EOF
-load file < date
-EOF
+WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
+novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
+    WVPASS grep -F "Unsuported configuration received from the server: blablabla"
 
 WVSTART "add-key subcommand"
 WVPASS ssh-keygen -t ed25519 -N '' -f key
-WVFAIL grep new_user $AUTH
+WVFAIL sudo_ grep new_user "$AUTH"
 WVPASS ssh novaboot-test@localhost add-key new_user < key.pub
-WVPASS grep new_user $AUTH
+WVPASS sudo_ grep -F "user new_user" "$AUTH"
 
 WVSTART "add-key user must not contain spaces"
-WVPASS cp $AUTH auth
+WVPASS sudo_ cat "$AUTH" > auth
 WVFAIL ssh novaboot-test@localhost add-key "new user" < /dev/null
-WVPASS diff -u $AUTH auth
+WVPASS diff -u <(sudo_ cat "$AUTH") auth
 
 WVSTART "add-key requires username"
-WVPASS cp $AUTH auth
+WVPASS sudo_ cat "$AUTH" > auth
 WVFAIL ssh novaboot-test@localhost add-key < /dev/null
-WVPASS diff -u $AUTH auth
+WVPASS diff -u <(sudo_ cat "$AUTH") auth
 
-WVSTART Suggest using ssh -t for shell
+WVSTART "Suggest using ssh -t for shell"
 WVPASS ssh novaboot-test@localhost shell < /dev/null | WVPASS grep -e 'ssh -t'
 echo exit|WVPASS ssh -tt novaboot-test@localhost shell | WVFAIL grep -e 'ssh -t'