From 07ed1e15455f64a78fec593a13b48d72ca075496 Mon Sep 17 00:00:00 2001 From: Lukas Matt Date: Sat, 29 Dec 2018 20:35:40 +0000 Subject: [PATCH] testproject fix count check --- test_helper.bash | 25 ++++++++++++++++--------- testproject.bats | 16 +++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/test_helper.bash b/test_helper.bash index 9b13e8c..5367920 100644 --- a/test_helper.bash +++ b/test_helper.bash @@ -76,9 +76,9 @@ function get() { function start_app() { params="" if [ ! -z ${PRSHA} ]; then - echo "!!!!!!! Custom build active !!!!!!!" - echo "Using repository $PRREPO" - echo "With SHA $PRSHA" + echo "# +++ Custom build active +++" >&3 + echo "# Project $PROJECT ($PRREPO)" >&3 + echo "# With SHA $PRSHA" >&3 params="-e PROJECT=$PROJECT -e PRSHA=$PRSHA -e PRREPO=$PRREPO" fi cid=$(container_id "$1") @@ -141,8 +141,9 @@ function wait_for() { TIMEOUT_PID=$! while true; do - $1 2>&1 |grep -m 1 "$2" >/dev/null - if [ "$?" -eq 0 ]; then break; fi + res=$(eval $1 2>&1); code=$? + echo "$res" |grep -m 1 "$2" >/dev/null + [ "$?" -eq 0 ] && [ "$code" -eq 0 ] && break sleep 1 done & >/dev/null 2>&1 CMD_PID=$! @@ -176,14 +177,20 @@ function query() { # init_database will be triggered on setup() function init_database() { - docker run -d --name $(container_id "postgres") postgres:latest - sleep 5 # timeout for container otherwise docker-logs fails + docker run -d --name $(container_id postgres) postgres:latest + cmd="docker logs $(container_id postgres)" + text="ready for start up" + code=$(wait_for "$cmd" "$text") + [ "$code" -eq "0" ] } # init_redis will be triggered on setup() function init_redis() { - docker run -d --name $(container_id "redis") redis:latest - sleep 5 # timeout for container otherwise docker-logs fails + docker run -d --name $(container_id redis) redis:latest + cmd="docker logs $(container_id redis)" + text="Ready to accept connections" + code=$(wait_for "$cmd" "$text") + [ "$code" -eq "0" ] } # create_database "g1" diff --git a/testproject.bats b/testproject.bats index 71598da..9cd2e4f 100644 --- a/testproject.bats +++ b/testproject.bats @@ -32,7 +32,7 @@ load test_helper @test "$btf start testproject#2 server" { start_app tp2 3000 testproject$(latest_tag testproject) [ "$?" -eq 0 ] - cmd="docker logs $(container_id tp1)" + cmd="docker logs $(container_id tp2)" text="CREATE TABLE" code=$(wait_for "$cmd" "$text") [ "$code" -eq "0" ] @@ -54,17 +54,15 @@ load test_helper } @test "$btf test count on tp1, it should be one" { - get "http://$(container_ip tp1):3000" - echo "expected 200, got $HTTP_STATUS_CODE" - [ "$HTTP_STATUS_CODE" == "200" ] - echo "expected 1, got $HTTP_BODY" - [ "$HTTP_BODY" == "1" ] + cmd='get "http://'$(container_ip tp1)':3000"' + code=$(wait_for "$cmd" "1") + [ "$code" -eq "0" ] } @test "$btf check the database on tp1 too" { - result=$(query tp1 "select count(*) from testtable;") - echo "expected 1, got $result" - [[ $result =~ "1" ]] + cmd='query tp1 "select count(*) from testtable;"' + code=$(wait_for "$cmd" "1") + [ "$code" -eq "0" ] } @test "$btf stop and delete the containers" { -- GitLab