]> rtime.felk.cvut.cz Git - novaboot.git/blob - tests/server.wv
eddc28d1012f3ba16db1ac5bdfe4513c1f034d5e
[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 'Second connection to console prints queue'
63 coproc console1 { exec_server console; }
64 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
65 coproc console2 { exec_server console; }
66 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
67 kill $console1_PID $console2_PID; wait
68
69 WVSTART 'Printed queue contains correct username'
70 coproc console1 { username=my_cool_username exec_server console; }
71 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
72 coproc console2 { username=another_username exec_server console; }
73 WVPASS sed -e '/my_cool_username/q0' -e '3q1' <&${console2[0]}
74 kill $console1_PID $console2_PID; wait
75
76 WVSTART Admin sees help for admin subcommands
77 admin=1 run_server help > log
78 WVPASS grep 'add-key' log
79
80 WVSTART Only admin can run shell
81 WVFAIL run_server shell
82 admin=1 WVPASS run_server shell < /dev/null
83
84 WVSTART 'Cannot reset when somebody else is connected to console'
85 coproc console1 { exec_server console; }
86 WVPASS sed -e '/novaboot-shell: Connected/q0' -e '3q1' <&${console1[0]}
87 coproc console2 { run_server reset; }
88 WVPASS sed -e '/Target is occupied by:/q0' <&${console2[0]}
89 WVPASS test ! -e reset_done
90 WVPASS kill $console1_PID $console2_PID; wait
91
92 WVSTART "Can reset when I'm connected to console"
93 coproc exec_server console
94 WVPASS sed -e '/novaboot-shell: Connected/q0' <&${COPROC[0]}
95 WVPASS run_server reset
96 WVPASS test -e reset_done
97 WVPASS kill $COPROC_PID
98
99 WVSTART "Quoting of config variables"
100 (
101     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
102
103     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch file1 file2"'
104     WVPASS run_server reset
105     WVPASS test -e file1 -a -e file2
106
107     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1 file2\""'
108     WVPASS run_server reset
109     WVPASS test -e "file1 file2"
110
111     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"file1   file2\""'
112     WVPASS run_server reset
113     WVPASS test -e "file1   file2"
114
115     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'reset_cmd="touch \"\\\"file1 file2\\\"\""'
116     WVPASS run_server reset
117     WVPASS test -e '"file1 file2"'
118 )
119
120 WVSTART "Config variables can depend on user name"
121 (
122     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
123
124     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd="echo Hello $NB_USER"'
125     username=asdf WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello asdf"
126     username=qwer WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello qwer"
127 )
128
129 WVSTART "Missing console_cmd results in failure"
130 NOVABOOT_SHELL_CONFIG="/dev/null" WVFAIL run_server console
131
132 WVSTART "console_cmd may contain a shell function name"
133 (
134     export NOVABOOT_SHELL_CONFIG="./.novaboot-shell"
135     WVPASS tee "$NOVABOOT_SHELL_CONFIG" <<<'console_cmd () { echo Hello user; }
136 console_cmd=console_cmd'
137     WVPASS run_server console | tee /dev/stderr | WVPASS grep -F "Hello user"
138 )
139
140 if [ -n "$DEB_HOST_ARCH" ]; then
141     echo >&2 "Skipping server tests in Debian package build, because these cannot pass."
142     exit 0
143 fi
144
145 SHELLRC="$NBT_HOME/.novaboot-shell"
146 TFTPROOT="$NBT_HOME/tftproot"
147 AUTH="$NBT_HOME/.ssh/authorized_keys"
148 sudo_() { sudo -u novaboot-test "$@"; }
149
150 WVSTART "Create and check ssh keys needed for following tests"
151 # Start our custom ssh-agent to not mess up with the user's agent
152 eval "$(ssh-agent)"
153 trap 'ssh-agent -k' EXIT
154 WVPASS test -n "$SSH_AUTH_SOCK" || exit 1
155 if [ ! -f id_rsa ]; then
156     ssh-keygen -t rsa -C "generated by $0" -N '' -f ./id_rsa
157 fi
158 WVPASS ssh-add ./id_rsa
159
160 WVSTART "Setup SSH server's admin key"
161 WVPASS sudo_ mkdir -p $(dirname "$AUTH")
162 WVPASS sudo_ chmod 700 $(dirname "$AUTH")
163 WVPASS sudo_ tee "$AUTH" <<<"command=\"user test admin\" $(cat ./id_rsa.pub)"
164 exit
165
166 WVSTART "Novaboot --ssh option (connect, rsync, reset)"
167 WVPASS sudo_ rm -rf "$TFTPROOT"
168 TS=$(date --rfc-3339=ns)
169 WVPASS sudo_ tee "$SHELLRC" <<EOF
170 console_cmd=cat
171 reset_cmd="echo $TS > reset.stamp"
172 target_config=""
173 EOF
174 date > file
175 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
176 copy file
177 EOF
178 # Check that file was copied to tftproot
179 WVPASS diff -u file <(sudo_ cat $TFTPROOT/file)
180 # Check that reset command was executed
181 WVPASS test "$TS" = "$(sudo_ cat "$NBT_HOME/reset.stamp")"
182
183 WVSTART "Novaboot --ssh remote config"
184 WVPASS sudo_ rm -rf "$TFTPROOT"
185 WVPASS sudo_ tee "$SHELLRC" <<EOF
186 console_cmd=cat
187 reset_cmd=true
188 target_config="\
189 --prefix=/prefix
190 --grub2
191 "
192 EOF
193 date > file
194 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
195 load file
196 EOF
197 WVPASS diff -u file <(sudo_ cat "$TFTPROOT/file")
198 WVPASS sudo_ grep /prefix/file "$TFTPROOT/grub.cfg"
199
200 WVSTART "Novaboot --ssh remote config fails on non-safe options"
201 WVPASS sudo_ tee "$SHELLRC" <<EOF
202 console_cmd=cat
203 reset_cmd=true
204 target_config="\
205 --prefix=/prefix
206 --make
207 "
208 EOF
209 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
210 stderr=$(novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null)
211 echo "$stderr" | WVPASS grep -q -F 'Unknown option: make'
212 echo "$stderr" | WVPASS grep -q -F 'Error processing configuration from the server'
213
214 WVSTART "Novaboot --ssh remote config fails on unknown arguments"
215 WVPASS sudo_ tee "$SHELLRC" <<EOF
216 console_cmd=cat
217 reset_cmd=true
218 target_config="\
219 --prefix=/prefix
220 blablabla
221 "
222 EOF
223 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
224 novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
225     WVPASS grep -F "Unsuported configuration received from the server: blablabla"
226
227 WVSTART "add-key subcommand"
228 WVPASS ssh-keygen -t ed25519 -N '' -f key
229 WVFAIL sudo_ grep new_user "$AUTH"
230 WVPASS ssh novaboot-test@localhost add-key new_user < key.pub
231 WVPASS sudo_ grep -F "user new_user" "$AUTH"
232
233 WVSTART "add-key user must not contain spaces"
234 WVPASS sudo_ cat "$AUTH" > auth
235 WVFAIL ssh novaboot-test@localhost add-key "new user" < /dev/null
236 WVPASS diff -u <(sudo_ cat "$AUTH") auth
237
238 WVSTART "add-key requires username"
239 WVPASS sudo_ cat "$AUTH" > auth
240 WVFAIL ssh novaboot-test@localhost add-key < /dev/null
241 WVPASS diff -u <(sudo_ cat "$AUTH") auth
242
243 WVSTART "Suggest using ssh -t for shell"
244 WVPASS ssh novaboot-test@localhost shell < /dev/null | WVPASS grep -e 'ssh -t'
245 echo exit|WVPASS ssh -tt novaboot-test@localhost shell | WVFAIL grep -e 'ssh -t'
246
247
248
249 # Hi-lock: (("^.*\\(?:WVSTART\\).*$" (0 (quote hi-black-b) t)))