evergreen/tests/utilities

107 lines
3.1 KiB
Bash

container_under_test_prefix=evergreen-testing
container_under_test=$container_under_test_prefix-$RANDOM
COMPOSE="./tools/compose"
RED='\033[0;31m'
NC='\033[0m' # No Color
warn() {
echo -e "$RED**** $@ ****$NC"
}
info() {
echo "$@"
}
cleanup () {
echo
theDate=$( date +%Y-%m-%d_%H:%M:%S )
logsDirectory=build/tests-logs-$theDate
mkdir -p $logsDirectory
echo -n "Retrieving container logs before shutdown... Logs will be in the $logsDirectory directory"
# Normally should be something like evergreen_backend_1, evergreen_db_1 and evergreen_instance_1
containers=$( $COMPOSE ps | head -5 | tail -3 | awk '{print $1}' )
for container in $containers
do
docker logs $container > $logsDirectory/${container}.log 2>&1
done
echo "Done getting logs for $containers"
echo -n "Cleaning up... "
$COMPOSE down
for container in $( docker ps -aq --filter "name=$container_under_test_prefix" )
do
docker kill $container 2>/dev/null >/dev/null || echo "Already dead."
done
echo "G'day!"
}
# Utilities
find_free_port() {
candidate_port=$(( ( $RANDOM % ( 65535 - 1024 ) ) + 1024 ))
used_ports=$( netstat -ntap 2> /dev/null | tr -s ' ' | cut -d ' ' -f4 | grep ':' | awk -F ":" '{print $NF}' )
echo $candidate_port
}
# Test functions
setup_container_under_test() {
# FIXME $(find_free_port)
TEST_PORT=8080
# TODO use docker-compose to use network and avoid all this?
info "Start containers under test (port=$TEST_PORT) and wait a bit for its startup:"
#docker run --rm --name $container_under_test -p $TEST_PORT:8080 -d jenkins/evergreen:latest
$COMPOSE up -d
sleep 3
determine_container_name
}
determine_container_name() {
docker ps
container_under_test=$( docker ps --filter "name=instance" --format='{{.Names}}' )
}
wait_for_jenkins() {
# FIXME: FLAKY matching
# evergreen_instance_1 could be not the only one, also what do we do if preexisting containers are running?
echo "Running containers (beware, ideally there should be none to avoid issues)"
determine_container_name
# FIXME: have to wait pretty long because plugin installations
# Possibly we'll want a special mode to accelerate testing, maybe by downloading plugins
# from a local cache by overriding JENKINS_UC?
max_attempts=20
cur_attempts=0
while true
do
cur_attempts=$(( cur_attempts + 1 ))
if ( docker logs $container_under_test | grep "Jenkins is fully up and running" ); then
info "Jenkins has started."
break;
elif (( $cur_attempts > $max_attempts )); then
warn "Jenkins did not start before timeout. Tests are expected to fail."
break;
else
info "Waiting for Jenkins startup a bit more... ($cur_attempts/$max_attempts attempts) "
fi
sleep $cur_attempts
done
}
# trick to silence shellcheck which does not handle very well variables coming from sourced file
export container_under_test=${container_under_test:?}
oneTimeSetUp() {
setup_container_under_test
# shellcheck disable=SC2016
JENKINS_HOME="$( docker exec "$container_under_test" bash -c 'echo $JENKINS_HOME' )"
}
oneTimeTearDown() {
cleanup
}
# vim: ft=sh