nginx-vm-tests/test.sh

263 lines
5.1 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
dirn=$(dirname "$0")
source $dirn/common.sh
source $dirn/virt.sh
source $dirn/nginx.sh
if [[ ! $(vms_avail) ]]; then
log "no VMs available!"
exit 1
fi
nginx_dir=""
otel_dir=""
tests_dir=""
vm_list=""
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
log "test.sh: build and test code on many libvirt VMs at once"
log " -h, --help: show this help text"
log " -n <dir>, --nginx <dir>: specify an nginx directory"
log " -o <dir>, --otel <dir>: specify an nginx-otel directory"
log " -t <dir>, --tests <dir>: specify an nginx-tests directory"
log " --vm <name>: (optional) specify a VM to operate on"
log " may be specified multiple times, or none"
log " defaults to all available VMs"
exit 0
;;
-n | --nginx)
[ -d $2 ] || ( \
log "nginx flag requires valid dir" && \
exit 1 )
nginx_dir=$2
;;
-o | --otel)
[ -d $2 ] || ( \
log "otel flag requires valid dir" && \
exit 1 )
otel_dir=$2
;;
-t | --tests)
[ -d $2 ] || ( \
log "tests flag requires valid dir" && \
exit 1 )
[ $nginx_dir ] || [ $otel_dir ] || ( \
log "must set nginx or otel flag before tests flag" && \
exit 1 )
tests_dir=$2
;;
--vm)
[ $2 ] || ( \
log "VM must be specified" && \
exit 1 )
vm_list="${vm_list}$2"$'\n'
;;
*)
log "unknown argument: $1"
exit 1
esac
shift
shift
done
[ $nginx_dir ] && vm_nginx_dir=$(basename $nginx_dir)
[ $otel_dir ] && vm_otel_dir=$(basename $otel_dir)
[ $tests_dir ] && vm_tests_dir=$(basename $tests_dir)
if [[ ! $vm_list ]]; then
vm_list=$(vms_avail)
fi
section "script init..."
if [[ ! -d $test_log_dir ]]; then
log "prepping new test log dir"
ran=$((1+$RANDOM % 1000))
test_log_dir=/tmp/nginx_autotest_fmk_$ran
rm -rf $test_log_dir
mkdir $test_log_dir
fi
log "tests logs dir: $test_log_dir"
log "nginx code dir: $nginx_dir"
log "nginx test dir: $tests_dir"
log "otel code dir: $otel_dir"
function sync_all_to_remote() {
# test inverse because function can only return false
# when a command fails
([[ ! $nginx_dir ]] || sync_dir_to_vm $1 $nginx_dir) && \
([[ ! $tests_dir ]] || sync_dir_to_vm $1 $tests_dir) && \
([[ ! $otel_dir ]] || sync_dir_to_vm $1 $otel_dir )
}
function build_nginx_remote() {
vm_cmd $1 \
"set -ex;
$(typeset WHT)
$(typeset END)
$(typeset -f log)
$(typeset -f num_jobs)
$(typeset -f build_nginx)
cd $vm_nginx_dir;
build_nginx"
}
function build_otel_remote() {
vm_cmd $1 \
"set -ex;
$(typeset WHT)
$(typeset END)
$(typeset -f log)
$(typeset -f num_jobs)
$(typeset -f build_otel)
cd $vm_otel_dir;
build_otel"
}
function test_nginx_remote() {
vm_cmd $1 \
"set -ex;
$(typeset WHT)
$(typeset END)
$(typeset -f log)
$(typeset -f num_jobs)
$(typeset -f test_nginx)
cd $vm_tests_dir;
test_nginx"
}
function test_otel_remote() {
vm_cmd $1 \
"set -ex;
$(typeset WHT)
$(typeset END)
$(typeset -f log)
$(typeset -f num_jobs)
$(typeset -f test_otel)
cd $vm_otel_dir;
test_otel"
}
function clean_nginx_remote() {
vm_cmd $1 \
"set -ex;
$(typeset -f clean_nginx)
cd $vm_nginx_dir;
clean_nginx"
}
function clean_otel_remote() {
vm_cmd $1 \
"set -ex;
$(typeset -f clean_otel)
cd $vm_otel_dir;
clean_otel"
}
function cleanup() {
section "cleanup!"
log "cleaning build directories"
if ! parallel_invoke_and_wait \
clean_nginx_remote \
"$vm_list" \
"$test_log_dir/clean_nginx_"; then
error "Failed to clean NGINX build directory"
fi
if ! parallel_invoke_and_wait \
clean_otel_remote \
"$vm_list" \
"$test_log_dir/clean_otel_"; then
error "Failed to clean otel build directory"
fi
log "turning off VMs"
parallel_invoke_and_wait \
turn_off_vm \
"$vm_list" \
"$test_log_dir/off_"
}
section "launching VMs"
if ! parallel_invoke_and_wait \
turn_on_vm_and_wait \
"$vm_list" \
"$test_log_dir/on_"; then
error "Failed to turn on all VMs"
cleanup
exit 1
fi
section "syncing code to VMs"
if ! parallel_invoke_and_wait \
sync_all_to_remote \
"$vm_list" \
"$test_log_dir/sync_"; then
error "Failed to sync files to VMs"
cleanup
exit 1
fi
if [ "$nginx_dir" ]; then
section "building NGINX"
if ! parallel_invoke_and_wait \
build_nginx_remote \
"$vm_list" \
"$test_log_dir/build_nginx_"; then
error "NGINX build failures detected"
cleanup
exit 1
fi
fi
if [ "$otel_dir" ]; then
section "building NGINX Otel module"
if ! parallel_invoke_and_wait \
build_otel_remote \
"$vm_list" \
"$test_log_dir/build_otel_"; then
error "Otel build failures detected"
cleanup
exit 1
fi
fi
if [ "$tests_dir" ]; then
if [ "$nginx_dir" ]; then
section "testing NGINX"
if ! parallel_invoke_and_wait \
test_nginx_remote \
"$vm_list" \
"$test_log_dir/test_nginx_"; then
error "NGINX test failures detected"
cleanup
exit 1
fi
fi
if [ "$otel_dir" ]; then
section "testing NGINX Otel module"
if ! parallel_invoke_and_wait \
test_otel_remote \
"$vm_list" \
"$test_log_dir/test_otel_"; then
error "Otel test failures detected"
cleanup
exit 1
fi
fi
fi
# ------
cleanup
log "Finished :)"