]> rtime.felk.cvut.cz Git - novaboot.git/blob - tests/server.wv
4a4cd2552e5372c954dcb98f39abbbcd25f66546
[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 SHELLRC="$NBT_HOME/.novaboot-shell"
141 TFTPROOT="$NBT_HOME/tftproot"
142 AUTH="$NBT_HOME/.ssh/authorized_keys"
143 sudo_() { sudo -u novaboot-test "$@"; }
144
145 WVSTART "Create and check ssh keys needed for following tests"
146 # Start our custom ssh-agent to not mess up with the user's agent
147 eval "$(ssh-agent)"
148 trap 'ssh-agent -k' EXIT
149 WVPASS test -n "$SSH_AUTH_SOCK" || exit 1
150 if [ ! -f id_rsa ]; then
151     ssh-keygen -t rsa -C "generated by $0" -N '' -f ./id_rsa
152 fi
153 WVPASS ssh-add ./id_rsa
154
155 WVSTART "Setup SSH server's admin key"
156 WVPASS sudo_ mkdir -p $(dirname "$AUTH")
157 WVPASS sudo_ chmod 700 $(dirname "$AUTH")
158 WVPASS sudo_ tee "$AUTH" <<<"command=\"user test admin\" $(cat ./id_rsa.pub)"
159 exit
160
161 WVSTART "Novaboot --ssh option (connect, rsync, reset)"
162 WVPASS sudo_ rm -rf "$TFTPROOT"
163 TS=$(date --rfc-3339=ns)
164 WVPASS sudo_ tee "$SHELLRC" <<EOF
165 console_cmd=cat
166 reset_cmd="echo $TS > reset.stamp"
167 target_config=""
168 EOF
169 date > file
170 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
171 copy file
172 EOF
173 # Check that file was copied to tftproot
174 WVPASS diff -u file <(sudo_ cat $TFTPROOT/file)
175 # Check that reset command was executed
176 WVPASS test "$TS" = "$(sudo_ cat "$NBT_HOME/reset.stamp")"
177
178 WVSTART "Novaboot --ssh remote config"
179 WVPASS sudo_ rm -rf "$TFTPROOT"
180 WVPASS sudo_ tee "$SHELLRC" <<EOF
181 console_cmd=cat
182 reset_cmd=true
183 target_config="\
184 --prefix=/prefix
185 --grub2
186 "
187 EOF
188 date > file
189 WVPASS novaboot --ssh novaboot-test@localhost <<EOF
190 load file
191 EOF
192 WVPASS diff -u file <(sudo_ cat "$TFTPROOT/file")
193 WVPASS sudo_ grep /prefix/file "$TFTPROOT/grub.cfg"
194
195 WVSTART "Novaboot --ssh remote config fails on non-safe options"
196 WVPASS sudo_ tee "$SHELLRC" <<EOF
197 console_cmd=cat
198 reset_cmd=true
199 target_config="\
200 --prefix=/prefix
201 --make
202 "
203 EOF
204 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
205 stderr=$(novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null)
206 echo "$stderr" | WVPASS grep -q -F 'Unknown option: make'
207 echo "$stderr" | WVPASS grep -q -F 'Error processing configuration from the server'
208
209 WVSTART "Novaboot --ssh remote config fails on unknown arguments"
210 WVPASS sudo_ tee "$SHELLRC" <<EOF
211 console_cmd=cat
212 reset_cmd=true
213 target_config="\
214 --prefix=/prefix
215 blablabla
216 "
217 EOF
218 WVFAIL novaboot --ssh novaboot-test@localhost <<<"load file < date"
219 novaboot --ssh novaboot-test@localhost <<<"load file < date" 2>&1 >/dev/null |\
220     WVPASS grep -F "Unsuported configuration received from the server: blablabla"
221
222 WVSTART "add-key subcommand"
223 WVPASS ssh-keygen -t ed25519 -N '' -f key
224 WVFAIL sudo_ grep new_user "$AUTH"
225 WVPASS ssh novaboot-test@localhost add-key new_user < key.pub
226 WVPASS sudo_ grep -F "user new_user" "$AUTH"
227
228 WVSTART "add-key user must not contain spaces"
229 WVPASS sudo_ cat "$AUTH" > auth
230 WVFAIL ssh novaboot-test@localhost add-key "new user" < /dev/null
231 WVPASS diff -u <(sudo_ cat "$AUTH") auth
232
233 WVSTART "add-key requires username"
234 WVPASS sudo_ cat "$AUTH" > auth
235 WVFAIL ssh novaboot-test@localhost add-key < /dev/null
236 WVPASS diff -u <(sudo_ cat "$AUTH") auth
237
238 WVSTART "Suggest using ssh -t for shell"
239 WVPASS ssh novaboot-test@localhost shell < /dev/null | WVPASS grep -e 'ssh -t'
240 echo exit|WVPASS ssh -tt novaboot-test@localhost shell | WVFAIL grep -e 'ssh -t'
241
242
243
244 # Hi-lock: (("^.*\\(?:WVSTART\\).*$" (0 (quote hi-black-b) t)))