This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
testrunner
executable file
·709 lines (571 loc) · 18.4 KB
/
testrunner
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
#!/bin/bash
set -Exeuo pipefail
# This script is run multiple times in parallel: once for
# each testsuite defined in the YAML file.
THIS_DIR=$(dirname $0)
source $THIS_DIR/utils/common.sh
main() {
# NB: see the various NBs in the main() of main.
# We take a single argument; the state dir index to use.
# But we still expect the global state dir to be in the
# $PWD.
state_idx=$1; shift
state=state/suite-${state_idx}
[ -d state ] && [ -d $state ]
# Make sure we update GitHub if we exit due to errexit.
# We also do a GitHub update on clean exit.
ensure_err_github_update
provision_env
prepare_env
build_and_test
fetch_artifacts
s3_upload
final_github_update
}
provision_env() {
if containerized; then
ensure_teardown_container
provision_container
else
ssh_setup_key
if clustered; then
ensure_teardown_cluster
provision_cluster
else
ensure_teardown_node
provision_node
fi
fi
}
provision_container() {
local image=$(cat $state/parsed/image)
if containerized; then
update_github pending "Provisioning container..."
fi
# Let's pre-pull the image so that it doesn't count
# as part of the test timeout.
if ! sudo docker pull "$image"; then
update_github error "Could not pull image '$image'."
exit 0
fi
sudo docker run -d \
--cidfile $state/cid \
"$image" sleep infinity
}
provision_node() {
update_github pending "Provisioning host..."
# substitute node to use in "<name>:<ip>" format
if [ -n "${RHCI_DEBUG_USE_NODE:-}" ]; then
echo "${RHCI_DEBUG_USE_NODE%:*}" > $state/host/node_name
echo "${RHCI_DEBUG_USE_NODE#*:}" > $state/host/node_addr
else
$THIS_DIR/provisioner $state $state/parsed/host $state/host
if [ -f $state/exit ]; then
# the provisioner encountered a user error and already updated GH
exit 0
fi
fi
}
provision_cluster() {
local nhosts=$(cat $state/parsed/nhosts)
update_github pending "Provisioning cluster..."
seq 0 $((nhosts - 1)) | xargs -P 0 -n 1 -I {} \
$THIS_DIR/provisioner $state $state/parsed/host-{} $state/host-{}
if [ -f $state/exit ]; then
# a provisioner encountered a user error and already updated GH
exit 0
fi
if container_controlled; then
provision_container
else
# make the first host the controller
ln -s host-0 $state/host
fi
local i=0
while [ $i -lt $nhosts ]; do
local name=$(cat $state/parsed/host-$i/name)
local addr=$(cat $state/host-$i/node_addr)
name=$(sed 's/[.-]/_/g' <<< "$name")
echo "export RHCI_${name}_IP=${addr}" >> $state/parsed/envs
i=$((i + 1))
done
}
prepare_env() {
local upload_dir=$state/$github_commit.$state_idx.$(date +%s%N)
echo $upload_dir > $state/upload_dir
mkdir $upload_dir
# inject extra repos before installing packages
if [ -f $state/parsed/rhci-extras.repo ]; then
envcmd mkdir -p /etc/yum.repos.d
envcp $state/parsed/rhci-extras.repo /etc/yum.repos.d
fi
if [ -f $state/parsed/packages ]; then
if on_atomic_host; then
overlay_packages
else
install_packages
fi
fi
if clustered; then
ssh_setup_cluster
fi
envcmd mkdir /var/tmp/checkout
envcp checkouts/$github_repo/. /var/tmp/checkout
}
ssh_setup_key() {
set +x
cat > $state/node_key <<< "$os_privkey"
chmod 0600 $state/node_key
set -x
}
ssh_setup_cluster() {
local nhosts=$(cat $state/parsed/nhosts)
# since the common case is to interact with the various
# nodes by ssh, let's make sure it's all set up nicely
# ahead of time
# let's go through the hosts once to collect keys
local i=0
while [ $i -lt $nhosts ]; do
local name=$(cat $state/parsed/host-$i/name)
local addr=$(cat $state/host-$i/node_addr)
ssh-keyscan $addr 2>/dev/null | \
sed "s/^/$name,/" >> $state/known_hosts
echo $addr $name >> $state/hosts
i=$((i + 1))
done
# We use a different key than the one used to provision
# the nodes here, since we don't want to expose the
# private key of the OpenStack keypair used. NB: not in
# a state dir; we don't want to regen a key on every
# run.
if [ ! -f cluster_keypair/id_rsa ]; then
# let's just stick with RSA since it's supported on
# all platforms
mkdir -p cluster_keypair
ssh-keygen -t rsa -b 4096 -N "" -f cluster_keypair/id_rsa
fi
if container_controlled; then
# most base images don't have ssh
if ! envcmd [ -x /bin/ssh ]; then
env_make_cache
envcmd yum install -y openssh-clients
fi
envcmd mkdir -m 0600 /root/.ssh
envcp cluster_keypair/id_rsa /root/.ssh
envcp $state/known_hosts /root/.ssh
envcp $state/hosts /etc/hosts.append
envcmd sh -c "cat /etc/hosts.append >> /etc/hosts"
fi
vmipssh() {
ip=$1; shift
ssh -q -i $state/node_key \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o UserKnownHostsFile=/dev/null \
root@$ip "$@"
}
i=0
while [ $i -lt $nhosts ]; do
local name=$(cat $state/parsed/host-$i/name)
local addr=$(cat $state/host-$i/node_addr)
# some of these could be redone more cleanly through
# cloud-init, though the dynamic aspect would
# probably end up making it look similar
vmipssh $addr hostnamectl set-hostname $name
vmipssh $addr "cat >> /etc/hosts" < $state/hosts
vmipssh $addr "cat >> /root/.ssh/known_hosts" < $state/known_hosts
vmipssh $addr "cat > /root/.ssh/id_rsa" < cluster_keypair/id_rsa
vmipssh $addr chmod 0400 /root/.ssh/id_rsa
vmipssh $addr "cat >> /root/.ssh/authorized_keys" \
< cluster_keypair/id_rsa.pub
i=$((i + 1))
done
unset -f vmipssh
}
overlay_packages() {
local upload_dir=$(cat $state/upload_dir)
local rc=0
logged_envcmd $upload_dir/setup.log / - - \
rpm-ostree install "$(cat $state/parsed/packages)" || rc=$?
if [ $rc != 0 ]; then
s3_upload
update_github error "Could not layer packages." "$(cat $state/url)"
exit 0
fi
vmreboot
}
install_packages() {
local upload_dir=$(cat $state/upload_dir)
mgr=yum
if envcmd rpm -q dnf; then
mgr=dnf
fi
env_make_cache
local rc=0
logged_envcmd $upload_dir/setup.log / - - \
$mgr install -y "$(cat $state/parsed/packages)" || rc=$?
if [ $rc != 0 ]; then
s3_upload
update_github error "Could not install packages." "$(cat $state/url)"
exit 0
fi
}
env_make_cache() {
# This is hacky and sad. Preemptively try to refresh
# yum/dnf cache to work around flaky distro infras.
local retries=5
while [ $retries -gt 0 ]; do
if envcmd yum makecache; then
break
fi
retries=$((retries - 1))
done
}
run_loop() {
local timeout=$1; shift
local logfile=$1; shift
local workdir=$1; shift
local testfile=$1; shift
local envfile=$1; shift
local max_date=$(($(date +%s) + $timeout))
while IFS='' read -r line || [[ -n $line ]]; do
timeout=$(($max_date - $(date +%s)))
if [ $timeout -le 0 ]; then
echo "### TIMED OUT" >> $logfile
rc=137
break
fi
rc=0
logged_envcmd $logfile $workdir $envfile $timeout "$line" || rc=$?
if [ $rc != 0 ]; then
break
fi
done < "$testfile"
return $rc
}
build_and_test() {
local upload_dir=$(cat $state/upload_dir)
local timeout=$(cat $state/parsed/timeout)
local checkout=checkouts/$github_repo
local rc=0
if [ -f $state/parsed/build ]; then
local config_opts=$(cat $state/parsed/build.config_opts)
local build_opts=$(cat $state/parsed/build.build_opts)
local install_opts=$(cat $state/parsed/build.install_opts)
update_github pending "Building..."
touch $state/build.sh
if [ ! -f $checkout/configure ]; then
if [ -f $checkout/autogen.sh ]; then
echo "NOCONFIGURE=1 ./autogen.sh" >> $state/build.sh
elif [ -f $checkout/autogen ]; then
echo "NOCONFIGURE=1 ./autogen" >> $state/build.sh
fi
fi
local njobs=$(envcmd getconf _NPROCESSORS_ONLN)
echo "./configure $config_opts" >> $state/build.sh
echo "make all --jobs $njobs $build_opts" >> $state/build.sh
echo "make install $install_opts" >> $state/build.sh
local max_date=$(($(date +%s) + $timeout))
run_loop \
$timeout \
$upload_dir/build.log \
/var/tmp/checkout \
$state/build.sh \
$state/parsed/envs || rc=$?
timeout=$(($max_date - $(date +%s)))
fi
if [ $rc = 0 ] && [ -f $state/parsed/tests ]; then
update_github pending "Running tests..."
run_loop \
$timeout \
$upload_dir/output.log \
/var/tmp/checkout \
$state/parsed/tests \
$state/parsed/envs || rc=$?
fi
echo "$rc" > $state/rc
}
fetch_artifacts() {
local upload_dir=$(cat $state/upload_dir)
# let's pull back the artifacts
if [ -f $state/parsed/artifacts ]; then
mkdir $upload_dir/artifacts
local fetched_at_least_one=0
if host_controlled; then
local node_addr=$(cat $state/host/node_addr)
while IFS='' read -r artifact || [[ -n $artifact ]]; do
path="/var/tmp/checkout/$artifact"
if vmssh [ -e "$path" ]; then
vmscp -r "root@$node_addr:$path" $upload_dir/artifacts
fetched_at_least_one=1
fi
done < $state/parsed/artifacts
else
local cid=$(cat $state/cid)
while IFS='' read -r artifact || [[ -n $artifact ]]; do
path="/var/tmp/checkout/$artifact"
if sudo docker exec $cid [ -e "$path" ]; then
sudo docker cp "$cid:$path" $upload_dir/artifacts
fetched_at_least_one=1
fi
done < $state/parsed/artifacts
fi
if [ $fetched_at_least_one == 0 ]; then
rm -rf $upload_dir/artifacts
fi
fi
}
s3_upload() {
local indexer=$(realpath $THIS_DIR/utils/indexer.py)
local upload_dir=$(cat $state/upload_dir)
local s3_object=index.html
# if we just have output.log or build.log, then just link directly to that
if [ $(find $upload_dir -mindepth 1 | wc -l) = 1 ]; then
if [ -f $upload_dir/output.log ]; then
s3_object=output.log
elif [ -f $upload_dir/build.log ]; then
s3_object=build.log
elif [ -f $upload_dir/setup.log ]; then
s3_object=setup.log
fi
fi
if [ $s3_object = index.html ]; then
# don't change directory in current session
( cd $upload_dir && $indexer )
fi
# only actually upload if we're given $s3_prefix
if [ -n "${s3_prefix:-}" ]; then
local full_prefix=$s3_prefix/$github_repo/$(basename $upload_dir)
# upload logs separately so that we can set the MIME type properly
aws s3 sync --exclude '*.log' \
$upload_dir s3://$full_prefix
aws s3 sync --exclude '*' --include '*.log' --content-type text/plain \
$upload_dir s3://$full_prefix
# full address we'll use for the final commit status update
printf "https://s3.amazonaws.com/%s/%s" \
$full_prefix $s3_object > $state/url
fi
}
final_github_update() {
local rc
local ghstate
local desc
rc=$(cat $state/rc)
if [ $rc == 124 ] || [ $rc == 137 ]; then
ghstate=failure
desc="Test timed out and was aborted."
elif [ $rc != 0 ]; then
ghstate=failure
desc="Test failed with rc $rc."
else
ghstate=success
desc="All tests passed"
if [ -n "${github_pull_id:-}" ] && [ ! -f state/is_merge_sha ]; then
desc="$desc, but merge commit could not be tested"
fi
desc="${desc}."
fi
local url=
if [ -f $state/url ]; then
url=$(cat $state/url)
fi
update_github $ghstate "$desc" "$url"
}
# $1 -- log file
# $2 -- workdir
# $3 -- envfile or -
# $4 -- timeout or -
logged_envcmd() {
local logfile=$1; shift
local workdir=$1; shift
local envfile=$1; shift
local timeout=$1; shift
# seed with standard info
if [ ! -f $logfile ]; then
echo "### $(date --utc)" > $logfile
if [ -n "${github_branch:-}" ]; then
echo "### Branch $github_branch" >> $logfile
else
echo -n "### PR #$github_pull_id" >> $logfile
# NB: is_merge_sha is in the top-level global state dir
if [ ! -f state/is_merge_sha ]; then
echo " (WARNING: not merge sha, check for conflicts)" \
>> $logfile
else
echo >> $logfile
fi
fi
if [ -n "${BUILD_ID:-}" ]; then
echo "### BUILD_ID $BUILD_ID" >> $logfile
fi
fi
echo '>>>' "$@" >> $logfile
# we just create a script and run that to make
# invocation and redirection easier
echo "set -euo pipefail" > $state/worker.sh
if [ $envfile != - ] && [ -f $envfile ]; then
cat $envfile >> $state/worker.sh
fi
echo "exec 2>&1" >> $state/worker.sh
echo "cd $workdir" >> $state/worker.sh
echo "$@" >> $state/worker.sh
envcp $state/worker.sh /var/tmp
local start=$(date +%s)
rc=0
timed_envcmd $timeout sh /var/tmp/worker.sh | tee -a $logfile || rc=$?
local duration=$(($(date +%s) - $start))
if [ $rc == 0 ]; then
echo "### COMPLETED IN ${duration}s" >> $logfile
elif [ $rc == 137 ]; then
echo "### TIMED OUT AFTER ${duration}s" >> $logfile
else
echo "### EXITED WITH CODE $rc AFTER ${duration}s" >> $logfile
fi
return $rc
}
# $1 -- timeout or -
timed_envcmd() {
timeout=$1; shift
if [[ $timeout == - ]]; then
timeout=infinity
fi
# There's a tricky bit to note here, docker exec and ssh
# don't handle arguments exactly the same way. SSH will
# effectively do a 'sh -c' on the passed command, which
# means that quoting might be an issue.
if container_controlled; then
local cid=$(cat $state/cid)
sudo timeout --signal=KILL $timeout \
docker exec $cid "$@"
else
local node_addr=$(cat $state/host/node_addr)
timeout --signal=KILL $timeout \
ssh -q -n -i $state/node_key \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o UserKnownHostsFile=/dev/null \
root@$node_addr "$@"
fi
}
envcmd() {
timed_envcmd infinity "$@"
}
envcp() {
target=$1; shift
remote=$1; shift
# N.B.: docker cp and rsync have almost the same
# semantics, which is nice. One exception is that docker
# wants 'dir/.' to signify copying dir contents, whereas
# rsync is happy with just 'dir/', so always use '/.'.
# Also, rsync creates nonexistent dirs, whereas docker
# does not, so explicitly mkdir beforehand.
if container_controlled; then
local cid=$(cat $state/cid)
sudo docker cp $target $cid:$remote
else
local node_addr=$(cat $state/host/node_addr)
rsync --quiet -az --no-owner --no-group \
-e "ssh -q -i $state/node_key \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o UserKnownHostsFile=/dev/null" \
$target root@$node_addr:$remote
fi
}
vmssh() {
# NB: we use -n because stdin may be in use (e.g. in a
# bash while read loop)
ssh -q -n -i $state/node_key \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o UserKnownHostsFile=/dev/null \
root@$(cat $state/host/node_addr) "$@"
}
vmscp() {
scp -q -i $state/node_key \
-o StrictHostKeyChecking=no \
-o PasswordAuthentication=no \
-o UserKnownHostsFile=/dev/null "$@"
}
vmreboot() {
vmssh systemctl reboot || :
sleep 3 # give time for port to go down
ssh_wait $(cat $state/host/node_addr) $state/node_key
}
update_github() {
local context=$(cat $state/parsed/context)
common_update_github "$context" "$@"
}
ensure_err_github_update() {
trap "update_github error 'An internal error occurred.'" ERR
}
teardown_node() {
teardown_node_impl \
$(cat $state/host/node_name) \
$(cat $state/host/node_addr)
}
teardown_node_impl() {
local node_name=$1; shift
local node_addr=$1; shift
if [ -z "$node_name" ]; then
return
fi
if [ -n "$node_addr" ] && \
[ -n "${os_floating_ip_pool:-}" ]; then
nova floating-ip-disassociate $node_name $node_addr
nova floating-ip-delete $node_addr
fi
nova delete $node_name
}
ensure_teardown_node() {
if [ -z "${RHCI_DEBUG_NO_TEARDOWN:-}" ]; then
trap teardown_node EXIT
fi
}
teardown_container() {
if [ -f $state/cid ]; then
sudo docker rm -f $(cat $state/cid)
fi
}
ensure_teardown_container() {
if [ -z "${RHCI_DEBUG_NO_TEARDOWN:-}" ]; then
trap teardown_container EXIT
fi
}
teardown_cluster() {
local nhosts=$(cat $state/parsed/nhosts)
local i=0
while [ $i -lt $nhosts ]; do
teardown_node_impl \
$(cat $state/host-$i/node_name) \
$(cat $state/host-$i/node_addr)
i=$((i + 1))
done
if container_controlled; then
teardown_container
fi
}
ensure_teardown_cluster() {
if [ -z "${RHCI_DEBUG_NO_TEARDOWN:-}" ]; then
trap teardown_cluster EXIT
fi
}
containerized() {
[ "$(cat $state/parsed/envtype)" = container ]
}
virtualized() {
[ "$(cat $state/parsed/envtype)" = host ]
}
clustered() {
[ "$(cat $state/parsed/envtype)" = cluster ]
}
container_controlled() {
[ "$(cat $state/parsed/controller)" = container ]
}
host_controlled() {
[ "$(cat $state/parsed/controller)" = host ]
}
on_atomic_host() {
! containerized && vmssh test -f /run/ostree-booted
}
main "$@"