]> rtime.felk.cvut.cz Git - novaboot.git/blob - tests/server.wv
tests: Fix ssh key setup for server tests
[novaboot.git] / tests / server.wv
1 #!/usr/bin/env 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="echo 'Hello console'; while :; do sleep 0.1; date; done"
11 reset_cmd="touch reset_done"
12 target_config='--prefix=asdf'
13 EOF
14
15 # Run server synchronously
16 function run_server() {
17     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}"
18 }
19
20 # Exec server (for use with coproc - then the server can be easily killed)
21 function exec_server() {
22     local exec
23     exec=exec
24     run_server "$@"
25 }
26
27 WVSTART Help subcommand
28 WVPASS run_server help | WVPASS tee log
29 WVPASS grep 'Target commands:' log
30 WVFAIL grep 'add-key' log
31
32 WVSTART Get-config command works
33 run_server get-config > log
34 WVPASS grep -e '^--prefix=asdf$' log
35
36 WVSTART "Multi-word commands work when user not specified"
37 WVPASS $WV_BASE_DIR/../server/novaboot-shell -c "help xxx" | WVPASS tee log
38 WVPASS grep 'Target commands:' log
39
40 WVSTART "Console prints a 'connected' message"
41 coproc exec_server console
42 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${COPROC[0]}
43 kill $COPROC_PID; wait
44
45 WVSTART "Without any argument console_cmd is executed if no default_cmd is configured"
46 coproc exec_server
47 WVPASS sed -e '/Hello console/q0' -e '3q1' <&${COPROC[0]}
48 kill $COPROC_PID; wait
49
50 WVSTART "Without any argument default_cmd is executed if configured"
51 (
52     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
53     cat <<EOF > $NOVABOOT_SHELL_CONFIG
54 default_cmd='echo "Hello default"'
55 console_cmd='echo "Hello console"'
56 EOF
57     WVPASS run_server | WVPASS grep -F 'Hello default'
58
59     WVPASS run_server console | WVPASS grep -F 'Hello console'
60 )
61
62 WVSTART "default_cmd can be set to run_novaboot"
63 (
64     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
65     cat <<EOF > default_nb_script
66 run echo "Hello default_nb_script"
67 EOF
68     cat <<'EOF' > $NOVABOOT_SHELL_CONFIG
69 default_cmd='run_novaboot default_nb_script --scriptmod=s/default_nb_script/default_nb_script_mod/'
70 console_cmd='while sleep 0.01; do echo "Hello console"; done'
71 reset_cmd='echo "Hello reset"'
72 target_config="\
73 --name=some name with spaces and hash #
74 --exiton=Hello console
75 "
76 EOF
77     WVPASS run_server | (
78         stdout=$(cat)
79         WVPASS grep -F 'Hello default_nb_script' <<<"$stdout"
80         WVPASS grep -F 'Hello default_nb_script_mod' <<<"$stdout"
81         WVPASS grep -F 'Hello console' <<<"$stdout"
82     )
83 )
84
85 WVSTART 'Second connection to console prints queue'
86 coproc console1 { exec_server console; }
87 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
88 coproc console2 { exec_server console; }
89 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
90 kill $console1_PID $console2_PID; wait
91
92 WVSTART 'Printed queue contains correct username'
93 coproc console1 { username=my_cool_username exec_server console; }
94 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
95 coproc console2 { username=another_username exec_server console; }
96 WVPASS sed -e '/my_cool_username/q0' -e '3q1' <&${console2[0]}
97 kill $console1_PID $console2_PID; wait
98
99 WVSTART Admin sees help for admin subcommands
100 admin=1 run_server help > log
101 WVPASS grep 'add-key' log
102
103 WVSTART Only admin can run shell
104 WVFAIL run_server shell
105 admin=1 WVPASS run_server shell < /dev/null
106
107 WVSTART 'Cannot reset when somebody else is connected to console'
108 coproc console1 { exec_server console; }
109 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
110 coproc console2 { run_server reset; }
111 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
112 WVPASS test ! -e reset_done
113 WVPASS kill $console1_PID $console2_PID; wait
114
115 WVSTART "Can reset when I'm connected to console"
116 coproc exec_server console
117 WVPASS sed -e '/novaboot-shell: Connected/q0' <&${COPROC[0]}
118 WVPASS run_server reset
119 WVPASS test -e reset_done
120 WVPASS kill $COPROC_PID
121
122 WVSTART "Quoting of config variables"
123 (
124     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
125
126     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch file1 file2"'
127     WVPASS run_server reset
128     WVPASS test -e file1 -a -e file2
129
130     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1 file2\""'
131     WVPASS run_server reset
132     WVPASS test -e "file1 file2"
133
134     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1   file2\""'
135     WVPASS run_server reset
136     WVPASS test -e "file1   file2"
137
138     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"\\\"file1 file2\\\"\""'
139     WVPASS run_server reset
140     WVPASS test -e '"file1 file2"'
141 )
142
143 WVSTART "Config variables can depend on user name"
144 (
145     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
146
147     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd="echo Hello $NB_USER"'
148     username=asdf WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello asdf"
149     username=qwer WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello qwer"
150 )
151
152 WVSTART "Missing console_cmd results in failure"
153 NOVABOOT_SHELL_CONFIG="/dev/null" WVFAIL run_server console
154
155 WVSTART "console_cmd may contain a shell function name"
156 (
157     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
158     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd () { echo Hello user; }
159 console_cmd=console_cmd'
160     WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello user"
161 )
162
163 WVSTART "reset_cmd may contain a shell function name"
164 (
165     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
166     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd () { echo Hello user; }
167 reset_cmd=reset_cmd'
168     WVPASS run_server reset | tee /dev/stderr | WVPASS grep -F "Hello user"
169 )
170
171 if [ -n "$DEB_HOST_ARCH" ]; then
172     echo >&2 "Skipping server tests in Debian package build, because these cannot pass."
173     exit 0
174 fi
175
176 SHELLRC="$NBT_HOME/.novaboot-shell"
177 TFTPROOT="$NBT_HOME/tftproot"
178 AUTH="$NBT_HOME/.ssh/authorized_keys"
179 sudo_() { sudo -u novaboot-test "$@"; }
180
181 WVSTART "Create and check ssh keys needed for following tests"
182 # Start our custom ssh-agent to not mess up with the user's agent
183 eval "$(ssh-agent)"
184 trap 'ssh-agent -k' EXIT
185 WVPASS test -n "$SSH_AUTH_SOCK" || exit 1
186 if [ ! -f id_rsa ]; then
187     ssh-keygen -t rsa -C "generated by $0" -N '' -f ./id_rsa
188 fi
189 ID_RSA=$(realpath ./id_rsa)
190 WVPASS ssh-add ./id_rsa
191
192 WVSTART "Setup SSH server's admin key"
193 WVPASS sudo_ mkdir -p $(dirname "$AUTH")
194 WVPASS sudo_ chmod 700 $(dirname "$AUTH")
195 WVPASS sudo_ tee "$AUTH" <<<"command=\"user test admin\" $(cat "$ID_RSA".pub)"
196 exit
197
198 WVSTART "Novaboot --ssh option (connect, rsync, reset)"
199 WVPASS sudo_ rm -rf "$TFTPROOT"
200 TS=$(date --rfc-3339=ns)
201 WVPASS sudo_ tee "$SHELLRC" <<EOF
202 console_cmd=cat
203 reset_cmd="echo $TS > reset.stamp"
204 target_config=""
205 EOF
206 date > file
207 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
208 copy file
209 EOF
210 # Check that file was copied to tftproot
211 WVPASS diff -u file <(sudo_ cat $TFTPROOT/file)
212 # Check that reset command was executed
213 WVPASS test "$TS" = "$(sudo_ cat "$NBT_HOME/reset.stamp")"
214
215 WVSTART "Novaboot --ssh remote config"
216 WVPASS sudo_ rm -rf "$TFTPROOT"
217 WVPASS sudo_ tee "$SHELLRC" <<EOF
218 console_cmd=cat
219 reset_cmd=true
220 target_config="\
221 --prefix=/prefix
222 --grub2
223 "
224 EOF
225 date > file
226 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
227 load file
228 EOF
229 WVPASS diff -u file <(sudo_ cat "$TFTPROOT/file")
230 WVPASS sudo_ grep /prefix/file "$TFTPROOT/grub.cfg"
231
232 WVSTART "Novaboot --ssh remote config fails on non-safe options"
233 WVPASS sudo_ tee "$SHELLRC" <<EOF
234 console_cmd=cat
235 reset_cmd=true
236 target_config="\
237 --prefix=/prefix
238 --make
239 "
240 EOF
241 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
242 stderr=$(novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null)
243 echo "$stderr" | WVPASS grep -q -F 'Unknown option: make'
244 echo "$stderr" | WVPASS grep -q -F 'Error processing configuration from the server'
245
246 WVSTART "Novaboot --ssh remote config fails on unknown arguments"
247 WVPASS sudo_ tee "$SHELLRC" <<EOF
248 console_cmd=cat
249 reset_cmd=true
250 target_config="\
251 --prefix=/prefix
252 blablabla
253 "
254 EOF
255 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
256 novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
257     WVPASS grep -F "Unsuported configuration received from the server: blablabla"
258
259 WVSTART "add-key subcommand"
260 WVPASS ssh-keygen -t ed25519 -N '' -f key
261 WVFAIL sudo_ grep new_user "$AUTH"
262 WVPASS ssh novaboot-test@localhost add-key new_user < key.pub
263 WVPASS sudo_ grep -F "user new_user" "$AUTH"
264
265 WVSTART "add-key user must not contain spaces"
266 WVPASS sudo_ cat "$AUTH" > auth
267 WVFAIL ssh novaboot-test@localhost add-key "new user" < /dev/null
268 WVPASS diff -u <(sudo_ cat "$AUTH") auth
269
270 WVSTART "add-key requires username"
271 WVPASS sudo_ cat "$AUTH" > auth
272 WVFAIL ssh novaboot-test@localhost add-key < /dev/null
273 WVPASS diff -u <(sudo_ cat "$AUTH") auth
274
275 WVSTART "Suggest using ssh -t for shell"
276 WVPASS ssh novaboot-test@localhost shell < /dev/null | WVPASS grep -e 'ssh -t'
277 echo exit|WVPASS ssh -tt novaboot-test@localhost shell | WVFAIL grep -e 'ssh -t'
278
279
280
281 # Hi-lock: (("^.*\\(?:WVSTART\\).*$" (0 (quote hi-black-b) t)))