summaryrefslogtreecommitdiff
path: root/whispers-tree-tests
blob: ed20362ccc9ca45bfa9eacb01d36842337298ee2 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/sh

BOOT_WAIT_DEFAULT=25
# set GO_PLAY_KIDS to no to have a longer sequence of tests
GO_PLAY_KIDS=no

YESCOUNT=0
NOCOUNT=0

function testcount()
{
    test $1 = $2 && echo SUCCESS || echo FAILED
    test $1 = $2 && YESCOUNT=$(($YESCOUNT + 1)) || NOCOUNT=$(($NOCOUNT + 1))
}

function dirtyssh()
{
    ssh -q \
        -o UserKnownHostsFile=/dev/null \
        -o StrictHostKeyChecking=no \
        -p $1 \
        root@localhost \
        "$2"
}

function herd_service_status()
{
    echo "herd status of $2 on ${VM[$1]}:"
    dirtyssh ${PORT[$1]} "herd status $2"
    echo
}

function herd_test_service_status()
{
    echo "Test if the $2 service is $3 on ${VM[$1]}."
    RESULT=$(herd_service_status $1 $2 | awk -- '/It is '$3'/ {print $3}')
    testcount ${RESULT} $3
    echo
}

function test_forward()
{
    echo "From ${VM[$1]}, take reverse port forward opened by
${VM[$2]} to connect to ${VM[$3]} through
sshd of ${VM[$4]}."
    EXITHOST=reset
    EXITHOST=$(dirtyssh \
                   ${PORT[$1]} \
                   "ssh -q \\
                        -o UserKnownHostsFile=/dev/null \\
                        -o StrictHostKeyChecking=no \\
                        -p $5 \\
                        root@$6 \\
                        hostname")
    echo "Hostname at forward exit: ${EXITHOST}, expected: ${VM[$3]}"
    testcount ${EXITHOST} ${VM[$3]}
}

echo "*** DEFINING VM INSTANCIATION ARRAYS"
echo

I=0
SSHD_WILD=$I

VM[$I]=sshd-wild
echo "* VM: ${VM[$I]}"
EXTRA_SERVICES[$I]=""
PORT[$I]=$((10022+$I))
EXTRAPORT_HOST[$I]=$((${PORT[$I]}+1000))
EXTRAPORT_VM[$I]=$((${PORT[$I]}+2000))
ULTIMAPORT_HOST[$I]=$((${PORT[$I]}+3000))
ULTIMAPORT_VM[$I]=$((${PORT[$I]}+4000))
echo

I=$(($I+1))
SSHD_REMOTE=$I

VM[$I]=sshd-remote
echo "* VM: ${VM[$I]}"
ALLOW_GATEWAY[$I]="yes"
EXTRA_SERVICES[$I]=""
PORT[$I]=$((10022+$I))
EXTRAPORT_HOST[$I]=$((${PORT[$I]}+1000))
EXTRAPORT_VM[$I]=$((${PORT[$I]}+2000))
ULTIMAPORT_HOST[$I]=$((${PORT[$I]}+3000))
ULTIMAPORT_VM[$I]=$((${PORT[$I]}+4000))
EXTRA_SERVICES[$I]=""
echo

I=$(($I+1))
SSHD_SOCKS=$I

VM[$I]=sshd-socks
echo "* VM: ${VM[$I]}"
ALLOW_GATEWAY[$I]="yes"
BOOT_WAIT[$I]=25
EXTRA_SERVICES[$I]=""
PORT[$I]=$((10022+$I))
EXTRAPORT_HOST[$I]=$((${PORT[$I]}+1000))
EXTRAPORT_VM[$I]=$((${PORT[$I]}+2000))
ULTIMAPORT_HOST[$I]=$((${PORT[$I]}+3000))
ULTIMAPORT_VM[$I]=$((${PORT[$I]}+4000))
echo

I=$(($I+1))
WHISPERS=$I

VM[$I]=whispers
echo "* VM: ${VM[$I]}"
EXTRA_SERVICES[$I]=""
PORT[$I]=$((10022+$I))
EXTRAPORT_HOST[$I]=$((${PORT[$I]}+1000))
EXTRAPORT_VM[$I]=$((${PORT[$I]}+2000))
ULTIMAPORT_HOST[$I]=$((${PORT[$I]}+3000))
ULTIMAPORT_VM[$I]=$((${PORT[$I]}+4000))
EXTRA_SERVICES[$I]="
           (service
            whispers-service-type
            (whispers-configuration
             (lieutenants
              (list
               (service
                whispers-service-type
                (whispers-configuration
                 (name 'bare)
                 (lieutenants
                  (list
                   (service
                    persistent-ssh-service-type
                    (ssh-connection-configuration
                     (require-networking? #f)
                     (socks-proxy-config
                      (socks-proxy-configuration
                       (use-proxy? #t)
                       (dynamic-forward
                        (ssh-connection-configuration
                         (require-networking? #f)
                         (sshd-host \"10.0.2.2\")
                         (sshd-port ${PORT[$SSHD_SOCKS]})
                          (forwards
                           (list (dynamic-forward-configuration)))))))
                     (sshd-host \"10.0.2.2\")
                     (sshd-port ${PORT[$SSHD_REMOTE]})
                     (forwards
                      (list (reverse-port-forward-configuration
                             (entry-port ${EXTRAPORT_VM[$SSHD_REMOTE]})
                             (forward-host \"10.0.2.2\")
                             (exit-port ${PORT[$SSHD_WILD]}))))))
                   (service
                    whispers-service-type
                    (whispers-configuration
                     (name 'burr)
                     (user \"whispers\")
                     (group \"whispers\")))
                   (service
                    whispers-service-type
                    (whispers-configuration
                     (name 'bear)
                     (user \"whispers\")
                     (group \"whispers\")
                     (lieutenants
                      (list
                       (service
                        whispers-service-type
                        (whispers-configuration
                         (name 'bearable)
                         (user \"whispers\")
                         (group \"whispers\")))
                       (service
                        whispers-service-type
                        (whispers-configuration
                         (name 'bore)
                         (user \"whispers\")
                         (group \"whispers\")
                         (extend-user? #t)
                         (extend-group? #t)))))))))))))))"
echo

for I in ${!PORT[@]}
do
    EXTRACONTENT[$I]=""
    if [ -v ALLOW_TUNDEV[$I] ]
    then
        EXTRACONTENT[$I]="
                     (extra-content \"
PermitTunnel=point-to-point\")"
    fi
    GATEWAY[$I]=""
    if [ -v ALLOW_GATEWAY[$I] ]
    then
        GATEWAY[$I]="
                     (gateway-ports? #t)"
    fi
    WAIT[$I]=$BOOT_WAIT_DEFAULT
    if [ -v BOOT_WAIT[$I] ]
    then
        WAIT[$I]=${BOOT_WAIT[$I]}
    fi
done
echo

echo "*** CONCATENATING SYSTEM CONFIGURATIONS"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    echo "(use-modules (guix records)
                   (gnu)
                   (whispers services whispers)
                   (whispers services ssh-tunneler))
(use-service-modules networking ssh desktop)
(use-package-modules ssh lsof)

(operating-system
  (host-name \"${VM[$I]}\")
  (timezone \"Asia/Shanghai\")
  (locale \"en_US.utf8\")

  (bootloader (bootloader-configuration
                (bootloader grub-bootloader)
                (targets '(\"/dev/sda\"))))

  (file-systems (cons (file-system
                        (device (file-system-label \"root\"))
                        (mount-point \"/\")
                        (type \"ext4\"))
                      %base-file-systems))

  (users %base-user-accounts)

  (packages (append (list lsof)
                    %base-packages))

  (services
    (append
     (list (service dhcp-client-service-type)
           (service openssh-service-type
                    (openssh-configuration
                     (permit-root-login #t)
                     (allow-empty-passwords? #t)
                     (openssh openssh-sans-x)
                     (port-number 22)${GATEWAY[$I]}${EXTRACONTENT[$I]}))${EXTRA_SERVICES[$I]})
     %base-services)))" > /tmp/${VM[$I]}.scm
    echo
done
echo

echo "*** INSTANCIATING VMs"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    VMRUN[$I]=$(guix system vm /tmp/${VM[$I]}.scm)
    echo
done
echo

echo "*** SILENTLY BOOTING VMs..."
echo
for I in ${!PORT[@]}
do
    NICSTANCE=-"nic user,model=virtio-net-pci,hostfwd=tcp::${PORT[$I]}-:22,hostfwd=tcp::${EXTRAPORT_HOST[$I]}-:${EXTRAPORT_VM[$I]},hostfwd=tcp::${ULTIMAPORT_HOST[$I]}-:${ULTIMAPORT_VM[$I]}"
        echo "* VM: ${VM[$I]}"
        ${VMRUN[$I]} \
            $NICSTANCE \
            -display none &
        sleep ${WAIT[$I]}
    echo
done
echo

echo "*** SETTING PASSWORDLESS LOGIN FOR WHISPERS USERS..."
echo
echo "* VM: ${VM[$WHISPERS]}"
dirtyssh ${PORT[$CLIENT01]} 'passwd -d whispers'
echo
echo

echo "*** REPORTING SHEPHERD STATUS"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    dirtyssh ${PORT[$I]} 'herd status'
    echo
done
echo

echo "*** REPORTING NETWORK INTERFACES"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    dirtyssh ${PORT[$I]} 'ip addr show'
    echo
done
echo

echo "*** REPORTING ROUTES"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    dirtyssh ${PORT[$I]} 'ip route'
    echo
done
echo

function full_tests()
{
    herd_service_status $WHISPERS whispers
    herd_test_service_status $WHISPERS whispers running
}

if [[ foo$GO_PLAY_KIDS == foono ]]
then
    full_tests
    echo
fi

echo "*** TESTS SUMMARRY"
echo
echo Successes: $YESCOUNT
echo Failures: $NOCOUNT
echo
echo

echo "You can ssh into the VMs from another terminal of this host"
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}, port: ${PORT[$I]}"
done
echo
read -n 1 -r -s -p "When done playing, press any key to halt the VMs..."
echo
echo

echo "*** HALTING VMs"
echo
for I in ${!PORT[@]}
do
    echo "* VM: ${VM[$I]}"
    dirtyssh ${PORT[$I]} halt
    echo
done