]> rtime.felk.cvut.cz Git - novaboot.git/blob - tests/server.wv
server: Ensure that configuration can depend on logged-in user name
[novaboot.git] / tests / server.wv
1 #!/bin/bash
2
3 cd $(dirname $0)
4 . wvtest.sh
5
6 NBT_HOME=$(getent passwd novaboot-test|cut -d: -f6)
7 export WV_BASE_DIR
8 export NOVABOOT_SHELL_CONFIG="$WV_BASE_DIR/.novaboot-shell"
9 cat <<EOF > $NOVABOOT_SHELL_CONFIG
10 console_cmd="/bin/sh -c 'while :; do sleep 0.1; date; done'"
11 reset_cmd="touch reset_done"
12 target_config='--prefix=asdf'
13 EOF
14
15 function run_server() {
16     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}"
17 }
18
19 function exec_server() {
20     local exec
21     exec=exec
22     run_server "$@"
23 }
24
25 WVSTART Help subcommand
26 WVPASS run_server help | WVPASS tee log
27 WVPASS grep 'Target commands:' log
28 WVFAIL grep 'add-key' log
29
30 WVSTART Get-config command works
31 run_server get-config > log
32 WVPASS grep -e '^--prefix=asdf$' log
33
34 WVSTART "Multi-word commands work when user not specified"
35 WVPASS $WV_BASE_DIR/../server/novaboot-shell -c "help xxx" | WVPASS tee log
36 WVPASS grep 'Target commands:' log
37
38 WVSTART "Console prints a 'connected' message"
39 coproc exec_server console
40 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${COPROC[0]}
41 kill $COPROC_PID; wait
42
43 WVSTART "Console command is executed without any argument"
44 coproc exec_server
45 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${COPROC[0]}
46 kill $COPROC_PID; wait
47
48 WVSTART 'Second connection to console prints queue'
49 coproc console1 { exec_server console; }
50 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
51 coproc console2 { exec_server console; }
52 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
53 kill $console1_PID $console2_PID; wait
54
55 WVSTART 'Printed queue contains correct username'
56 coproc console1 { username=my_cool_username exec_server console; }
57 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
58 coproc console2 { username=another_username exec_server console; }
59 WVPASS sed -e '/my_cool_username/q0' -e '3q1' <&${console2[0]}
60 kill $console1_PID $console2_PID; wait
61
62 WVSTART Admin sees help for admin subcommands
63 admin=1 run_server help > log
64 WVPASS grep 'add-key' log
65
66 WVSTART Only admin can run shell
67 WVFAIL run_server shell
68 admin=1 WVPASS run_server shell < /dev/null
69
70 WVSTART 'Cannot reset when somebody else is connected to console'
71 coproc console1 { exec_server console; }
72 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
73 coproc console2 { run_server reset; }
74 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
75 WVPASS test ! -e reset_done
76 WVPASS kill $console1_PID $console2_PID; wait
77
78 WVSTART "Can reset when I'm connected to console"
79 coproc exec_server console
80 WVPASS sed -e '/novaboot-shell: Connected/q0' <&${COPROC[0]}
81 WVPASS run_server reset
82 WVPASS test -e reset_done
83 WVPASS kill $COPROC_PID
84
85 WVSTART "Quoting of config variables"
86 (
87     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
88
89     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch file1 file2"'
90     WVPASS run_server reset
91     WVPASS test -e file1 -a -e file2
92
93     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1 file2\""'
94     WVPASS run_server reset
95     WVPASS test -e "file1 file2"
96
97     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1   file2\""'
98     WVPASS run_server reset
99     WVPASS test -e "file1   file2"
100
101     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"\\\"file1 file2\\\"\""'
102     WVPASS run_server reset
103     WVPASS test -e '"file1 file2"'
104 )
105
106 WVSTART "Config variables can depend on user name"
107 (
108     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
109
110     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd="echo Hello $NB_USER"'
111     username=asdf WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello asdf"
112     username=qwer WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello qwer"
113 )
114
115 SHELLRC="$NBT_HOME/.novaboot-shell"
116 TFTPROOT="$NBT_HOME/tftproot"
117 AUTH="$NBT_HOME/.ssh/authorized_keys"
118 sudo_() { sudo -u novaboot-test "$@"; }
119
120 WVSTART "Check that ssh-agent needed for following tests is running"
121 WVPASS test -n "$SSH_AUTH_SOCK" || exit 1
122
123 WVSTART "Setup SSH server's admin key"
124 WVPASS ssh-add -L > keys
125 WVPASS test $(wc -l < keys) -gt 0
126 WVPASS sudo_ mkdir -p $(dirname "$AUTH")
127 WVPASS sudo_ chmod 700 $(dirname "$AUTH")
128 WVPASS sudo_ tee "$AUTH" <<<"command=\"user test admin\" $(tail -n 1 keys)"
129
130 WVSTART "Novaboot --ssh option (connect, rsync, reset)"
131 WVPASS sudo_ rm -rf "$TFTPROOT"
132 TS=$(date --rfc-3339=ns)
133 WVPASS sudo_ tee "$SHELLRC" <<EOF
134 console_cmd=cat
135 reset_cmd="echo $TS > reset.stamp"
136 target_config=""
137 EOF
138 date > file
139 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
140 copy file
141 EOF
142 # Check that file was copied to tftproot
143 WVPASS diff -u file <(sudo_ cat $TFTPROOT/file)
144 # Check that reset command was executed
145 WVPASS test "$TS" = "$(sudo_ cat "$NBT_HOME/reset.stamp")"
146
147 WVSTART "Novaboot --ssh remote config"
148 WVPASS sudo_ rm -rf "$TFTPROOT"
149 WVPASS sudo_ tee "$SHELLRC" <<EOF
150 console_cmd=cat
151 reset_cmd=true
152 target_config="\
153 --prefix=/prefix
154 --grub2
155 "
156 EOF
157 date > file
158 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
159 load file
160 EOF
161 WVPASS diff -u file <(sudo_ cat "$TFTPROOT/file")
162 WVPASS sudo_ grep /prefix/file "$TFTPROOT/grub.cfg"
163
164 WVSTART "Novaboot --ssh remote config fails on non-safe options"
165 WVPASS sudo_ tee "$SHELLRC" <<EOF
166 console_cmd=cat
167 reset_cmd=true
168 target_config="\
169 --prefix=/prefix
170 --make
171 "
172 EOF
173 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
174 novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
175     tee >(WVPASS grep -q -F 'Unknown option: make') \
176         >(WVPASS grep -q -F 'Error processing configuration from the server')
177
178 WVSTART "Novaboot --ssh remote config fails on unknown arguments"
179 WVPASS sudo_ tee "$SHELLRC" <<EOF
180 console_cmd=cat
181 reset_cmd=true
182 target_config="\
183 --prefix=/prefix
184 blablabla
185 "
186 EOF
187 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
188 novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
189     WVPASS grep -F "Unsuported configuration received from the server: blablabla"
190
191 WVSTART "add-key subcommand"
192 WVPASS ssh-keygen -t ed25519 -N '' -f key
193 WVFAIL sudo_ grep new_user "$AUTH"
194 WVPASS ssh novaboot-test@localhost add-key new_user < key.pub
195 WVPASS sudo_ grep -F "user new_user" "$AUTH"
196
197 WVSTART "add-key user must not contain spaces"
198 WVPASS sudo_ cat "$AUTH" > auth
199 WVFAIL ssh novaboot-test@localhost add-key "new user" < /dev/null
200 WVPASS diff -u <(sudo_ cat "$AUTH") auth
201
202 WVSTART "add-key requires username"
203 WVPASS sudo_ cat "$AUTH" > auth
204 WVFAIL ssh novaboot-test@localhost add-key < /dev/null
205 WVPASS diff -u <(sudo_ cat "$AUTH") auth
206
207 WVSTART "Suggest using ssh -t for shell"
208 WVPASS ssh novaboot-test@localhost shell < /dev/null | WVPASS grep -e 'ssh -t'
209 echo exit|WVPASS ssh -tt novaboot-test@localhost shell | WVFAIL grep -e 'ssh -t'
210
211
212
213 # Hi-lock: (("^.*\\(?:WVSTART\\).*$" (0 (quote hi-black-b) t)))