user1283776 user1283776 - 3 months ago 13
Bash Question

TravisCI build succeeds even when tests fail

This is where I am running my tests in

travis.yml
:

# Run tests
script:
# Test application in Docker container
- ./tools/docker-test.sh


The shell script
docker-test.sh
looks like this:

#!/usr/bin/env bash

githash="$(git rev-parse --short HEAD)"

echo "-------------------------------------------------"
echo "| Running unit tests |"
echo "-------------------------------------------------"

docker create -it --name test eu.gcr.io/test/test:$githash
docker start test
docker exec test /bin/sh -c "go test ./..."
docker stop test
docker rm -fv test


The TravisCI build is a success even if the tests fail.

How can I get TravisCI to know if test have failed or not? I don't know if this is a problem with errors not being propagated from Docker, errors not being propagated from the shell script or TravisCI not knowing when go tests succeed or fail.

Answer

Your script is exiting with the status code of the last command docker rm -fv test.

You need to capture the status code of the test's, then clean up docker, then exit.

This code example is from a slightly different question over here but it's the same solution.

#!/usr/bin/env bash
set -e

# Set a default return code
RC=2

# Cleanup
function cleanup {
  echo "Removing container"
  (docker stop test)
  (docker rm -f test)
  exit $RC
}
trap cleanup EXIT

# Test steps
docker create -it --name test path
docker start test
docker exec test /bin/sh -c "go test ./..."
RC=$?
Comments