Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
DOCKER_COMPOSE_FILES='docker-compose1.yml,docker-compose2.yml'
DOCKER_COMPOSE_UP_MODE=1 #1|0
DOCKER_COMPOSE_UP_LOG_FILE='/tmp/log.txt'
DOCKER_COMPOSE_BUILD_OPTIONS="" #any options for `docker-compose build`
DOCKER_COMPOSE_UP_OPTIONS="" #any options for `docker-compose up`
DOCKER_COMPOSE_RUN_OPTIONS="" #any options for `docker-compose run`
TEST_CONTAINERS="application1=/test.sh,application2=/test2.sh"
##### get port remover #####
wget https://raw.githubusercontent.com/paunin/docker-compose-v2-ports-remover/master/docker-compose-v2-ports-remover.py -O ./docker-compose-v2-ports-remover.py
chmod +x ./docker-compose-v2-ports-remover.py
##### remove ports to avoid problems on parallel runs #####
DOCKER_FILES_OPTION=""
IFS=',' read -ra DC_FILES <<< "$DOCKER_COMPOSE_FILES"
for DC_FILE in ${DC_FILES[@]}
do
./docker-compose-v2-ports-remover.py -i $DC_FILE -o $DC_FILE
DOCKER_FILES_OPTION="$DOCKER_FILES_OPTION --file $DC_FILE"
done
##### build docker compose #####
docker-compose $DOCKER_FILES_OPTION build $DOCKER_COMPOSE_BUILD_OPTIONS
##### up docker compose or not #####
if [[ "$DOCKER_COMPOSE_UP_MODE" == "1" ]]; then
echo "Up docker-compose environment"
docker-compose $DOCKER_FILES_OPTION up -d $DOCKER_COMPOSE_UP_OPTIONS
else
echo "Will not do docker-compose up"
fi
#### run tests ####
if [[ "$DOCKER_COMPOSE_UP_MODE" == "1" ]]; then
EXEC_COMMAND='exec'
else
EXEC_COMMAND='run'
fi
IFS=',' read -r -a arrayOfContainers <<< "$TEST_CONTAINERS"
for containerCommand in "${arrayOfContainers[@]}"
do
IFS='=' read -r -a containerAndCommand <<< "$containerCommand"
container=${containerAndCommand[0]}
command=${containerAndCommand[1]}
bash -c "docker-compose $DOCKER_FILES_OPTION $EXEC_COMMAND $DOCKER_COMPOSE_RUN_OPTIONS $container $command"
done
#### collect logs ####
if [[ "$DOCKER_COMPOSE_UP_MODE" == "1" ]]; then
echo "Collect Logs in file $DOCKER_COMPOSE_UP_LOG_FILE"
docker-compose $DOCKER_FILES_OPTION logs > $DOCKER_COMPOSE_UP_LOG_FILE
else
echo "Will not collect logs for non-up mode"
fi
#### stop containers ####
docker-compose $DOCKER_FILES_OPTION down
docker-compose $DOCKER_FILES_OPTION kill
docker-compose $DOCKER_FILES_OPTION rm -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment