From ad24f2d206dc711152cb640b1213c502e7eacf36 Mon Sep 17 00:00:00 2001 From: Gennady Trafimenkov Date: Tue, 26 Jul 2016 01:07:15 +0400 Subject: [PATCH] Enable color output from integration tests in Docker containers (#16660) Enable color output from integration tests in Docker containers: * In run_tests.sh when output is attached to a terminal. * In shippable/integration.sh using force mode (can be disabled). Also fix blocks tests to work with or without color output --- test/integration/Makefile | 8 +++++--- test/utils/run_tests.sh | 9 ++++++++- test/utils/shippable/integration.sh | 5 +++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/test/integration/Makefile b/test/integration/Makefile index 8a73698ec3..52cf4b29a6 100644 --- a/test/integration/Makefile +++ b/test/integration/Makefile @@ -186,12 +186,14 @@ blocks: setup rm -f block_test.out # run the test and check to make sure the right number of completions was logged ansible-playbook -vv -e outputdir=$(TEST_DIR) test_blocks/main.yml | tee block_test.out - [ "$$(grep 'TEST COMPLETE' block_test.out | wc -l)" = "$$(egrep '^[0-9]+ plays in' block_test.out | cut -f1 -d' ')" ] + sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' block_test.out >block_test_wo_colors.out + [ "$$(grep 'TEST COMPLETE' block_test.out | wc -l)" = "$$(egrep '^[0-9]+ plays in' block_test_wo_colors.out | cut -f1 -d' ')" ] # cleanup the output log again, to make sure the test is clean - rm -f block_test.out + rm -f block_test.out block_test_wo_colors.out # run test with free strategy and again count the completions ansible-playbook -vv -e outputdir=$(TEST_DIR) test_blocks/main.yml -e test_strategy=free | tee block_test.out - [ "$$(grep 'TEST COMPLETE' block_test.out | wc -l)" = "$$(egrep '^[0-9]+ plays in' block_test.out | cut -f1 -d' ')" ] + sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' block_test.out >block_test_wo_colors.out + [ "$$(grep 'TEST COMPLETE' block_test.out | wc -l)" = "$$(egrep '^[0-9]+ plays in' block_test_wo_colors.out | cut -f1 -d' ')" ] cloud: amazon rackspace azure diff --git a/test/utils/run_tests.sh b/test/utils/run_tests.sh index 63105c2931..88c3012295 100755 --- a/test/utils/run_tests.sh +++ b/test/utils/run_tests.sh @@ -30,7 +30,14 @@ else fi export C_NAME="testAbull_$$_$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)" docker pull ansible/ansible:${TARGET} - docker run -d --volume="${PWD}:/root/ansible:Z" $LINKS --name "${C_NAME}" --env HTTPTESTER=1 ${TARGET_OPTIONS:=''} ansible/ansible:${TARGET} > /tmp/cid_${TARGET} + + # enable colors if output is going to a terminal + COLOR_SETTINGS="" + if [ -t 1 ]; then + COLOR_SETTINGS="--env TERM=$TERM" + fi + + docker run -d --volume="${PWD}:/root/ansible:Z" $LINKS --name "${C_NAME}" $COLOR_SETTINGS --env HTTPTESTER=1 ${TARGET_OPTIONS:=''} ansible/ansible:${TARGET} > /tmp/cid_${TARGET} docker exec -ti $(cat /tmp/cid_${TARGET}) /bin/sh -c "export TEST_FLAGS='${TEST_FLAGS:-''}'; cd /root/ansible; . hacking/env-setup; (cd test/integration; LC_ALL=en_US.utf-8 make ${MAKE_TARGET:-})" docker kill $(cat /tmp/cid_${TARGET}) diff --git a/test/utils/shippable/integration.sh b/test/utils/shippable/integration.sh index aa9cdf114c..eea87b465c 100755 --- a/test/utils/shippable/integration.sh +++ b/test/utils/shippable/integration.sh @@ -13,6 +13,10 @@ http_image="${HTTP_IMAGE:-ansible/ansible:httptester}" keep_containers="${KEEP_CONTAINERS:-}" copy_source="${COPY_SOURCE:-}" +# Force ansible color output by default. +# To disable color force mode use FORCE_COLOR=0 +force_color="${FORCE_COLOR:-1}" + if [ "${SHIPPABLE_BUILD_DIR:-}" ]; then host_shared_dir="/home/shippable/cache/build-${BUILD_NUMBER}" controller_shared_dir="/home/shippable/cache/build-${BUILD_NUMBER}" @@ -69,6 +73,7 @@ fi httptester_id=$(docker run -d "${http_image}") container_id=$(docker run -d \ + --env "ANSIBLE_FORCE_COLOR=${force_color}" \ -v "/sys/fs/cgroup:/sys/fs/cgroup:ro" \ -v "${host_shared_dir}:${test_shared_dir}" \ --link="${httptester_id}:ansible.http.tests" \