Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Commit

Permalink
[JENKINS-49861] Disable executors on master by default: test
Browse files Browse the repository at this point in the history
The test should fail to show it's actually testing something.
  • Loading branch information
batmat committed Mar 8, 2018
1 parent 387b4a8 commit 9382522
Showing 1 changed file with 63 additions and 38 deletions.
101 changes: 63 additions & 38 deletions scripts/test-container.sh
Expand Up @@ -2,60 +2,85 @@

set -euo pipefail

### FUNCTIONS / SETUP
### SETUP

# will use the local build, but could also be used to test a remote image
# TODO: pass an image:tag param?

C_NAME=evergreen-testing-$RANDOM
container_under_test=evergreen-testing-$RANDOM
exit_code=0
output=output.html

cleanup () {
exit_code=$?
docker kill $C_NAME 2>/dev/null >/dev/null || echo "Already dead."
rm -f $output
echo
echo -n "Cleaning up... "
docker kill $container_under_test 2>/dev/null >/dev/null || echo "Already dead."
echo "G'day!"
exit $exit_code
}

trap cleanup EXIT ERR INT TERM

# 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
}

### HERE STARTS THE REAL MEAT
TEST_PORT=$(find_free_port)
echo "Using the port $TEST_PORT"

# TODO use docker-compose to use network and avoid all this
echo "Start container under test and wait a bit for its startup:"
docker run --rm --name $C_NAME -p $TEST_PORT:8080 -d jenkins/evergreen:latest
sleep 2

set +e
max_attempts=10
while true
do
if ( docker logs $C_NAME | grep "Jenkins is fully up and running" ); then
echo "Started, running tests."
break;
elif (( $max_attempts < 1 )); then
echo "Jenkins did not start before timeout. Tests are expected to fail."
break;
else
echo "Waiting for Jenkins startup a bit more..."
fi
sleep 3
max_attempts=$(( max_attempts -1 ))
done
set -e
#sleep 10 # is there a better way? Like checking logs for

echo "Connect to Jenkins"
curl --silent http://localhost:$TEST_PORT > $output

echo "Check content"
grep "Authentication required" $output > /dev/null
# Test functions
setup_container_under_test() {
TEST_PORT=$(find_free_port)

# TODO use docker-compose to use network and avoid all this?
echo "Start container 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
sleep 5

set +e
max_attempts=10
while true
do
if ( docker logs $container_under_test | grep "Jenkins is fully up and running" ); then
echo "Started, running tests."
break;
elif (( $max_attempts < 1 )); then
echo "Jenkins did not start before timeout. Tests are expected to fail."
break;
else
echo "Waiting for Jenkins startup a bit more..."
fi
sleep 3
max_attempts=$(( max_attempts -1 ))
done
set -e
}

# TODO: use/study more standard test systems like BATS
function smoke() {
echo -n "Connect to Jenkins and check content... "
curl --silent http://localhost:$TEST_PORT | \
grep "Authentication required" > /dev/null
echo "OK!"
}

function no_executor() {
echo -n "Check master has no executor... "
docker exec $container_under_test cat /var/jenkins_home/config.xml | \
grep '<numExecutors>0</numExecutors>' > /dev/null
echo $?
echo "OK!"
}

### ACTUAL TEST CALLS

setup_container_under_test

echo "#################"
echo "# Running tests #"
echo "#################"
# Basic check
smoke

# JENKINS-49861
no_executor

0 comments on commit 9382522

Please sign in to comment.