skip to Main Content

Hi, all

I am trying to implement automated test running from Makefile target. As my test dependent on running docker container, I need to check that container is up and running during whole test execution, and restart it, if it’s down. I am trying do it with bash script, running in background mode.
At glance code looks like this:

run-tests:
        ./check_container.sh & 
        docker-compose run --rm tests; 
        #Need to finish check_container.sh right here, after tests execution
        RESULT=$$?; 
        docker-compose logs test-container; 
        docker-compose kill; 
        docker-compose rm -fv; 
        exit $$RESULT

Tests has vary execution time (from 20min to 2hrs), so I don’t know before, how much time it will take. So, I try to poll it within script longer, than the longest test suite. Script looks like:

#!/bin/bash

time=0

while [ $time -le 5000 ]; do
  num=$(docker ps | grep selenium--standalone-chrome -c)
  if [[ "$num" -eq 0 ]]; then
    echo 'selenium--standalone-chrome container is down!';
    echo 'try to recreate'
    docker-compose up -d selenium
  elif [[ "$num" -eq 1 ]]; then
    echo 'selenium--standalone-chrome container is up and running'
  else
    docker ps | grep selenium--standalone-chrome
    echo 'more than one selenium--standalone-chrome containers is up and running'
  fi
  time=$(($time + 1))
  sleep 30
done

So, I am looking for how to exit script exactly after test running is finished, its meant, after command docker-compose run --rm tests is finished?

P.S. It is also ok, if background process can be finished on Makefile target finish

2

Answers


  1. Docker (and Compose) can restart containers automatically when they exit. If your docker-compose.yml file has:

    version: '3.8'
    services:
      selenium:
        restart: unless-stopped
    

    Then Docker will do everything your shell script does. If it also has

    services:
      tests:
        depends_on:
          - selenium
    

    then the docker-compose run tests line will also cause the selenium container to be started, and you don’t need a script to start it at all.

    Login or Signup to reply.
  2. When you launch a command in the background, the special parameter $! contains its process ID. You can save this in a variable and later kill(1) it.

    In plain shell-script syntax, without Make-related escaping:

    ./check_container.sh &
    CHECK_CONTAINER_PID=$!
    
    docker-compose run --rm tests
    RESULT=$?
    
    kill "$CHECK_CONTAINER_PID"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search