0

I have a rest api. I want to have a docker-compose setup that:

  • starts the api server
  • "waits" until it's up and running
  • runs some api tests against the endpoints
  • stops everything once the test job finished.

Now,

  • The first part I can do.
  • As for waiting for the backend to be up and runnning, as I understand it, depends_on does not quite cut it. the rest api does have a /ping endpoint tho in case we need it.
  • struggling to find a minimal example online that:
    • uses volumes and does not explicitly copy tests files over.
    • runs the tests through a command in the docker file (as opposed to in the DockerFile)
    • again not sure if there is an idiomatic way of stopping everything after tests are done, but I did come across a somewhat related solution that suggests using docker-compose up --abort-on-container-exit. is that the best way of achieving this?

currently my docker-compose file looks like this:

docker-compose.yml

version: '3.8'

networks:
  development:
    driver: bridge

services:
  app:
    build:
      context: ../
      dockerfile: ../Dockerfile
    command: sbt run
    image: sbt
    ports:
      - "8080:8080"
    volumes:
      - "../:/root/build"
    networks:
      - development
  tests:
    build:
      dockerfile: ./Dockerfile
    command: npm run test
    volumes:
      - .:/usr/tests/
      - /usr/tests/node_modules
    networks:
      - development
    depends_on:
      - app

and the node Dockerfile looking this:

FROM node:16

ADD package*.json /usr/tests/
ADD test.js /usr/tests/

WORKDIR /usr/tests/

RUN npm install

Full repo is here: https://github.com/ShahOdin/dockerise-everything/pull/1

Maths noob
  • 1,684
  • 20
  • 42
  • Do the tests themselves need to run inside Docker, or do you just need something that can make HTTP requests to the published `ports:`? That set of tasks seems a little beyond what Compose is capable of on its own, but if you had a test driver that ran outside of Docker it should be straightforward to script all of that (including starting and stopping the Compose stack). – David Maze Dec 03 '21 at 18:54
  • ideally I'd like everything containerized. btw, I just realised I could have a request with retries and a timeout of say 10 sec calling the ping endpoint. so the waiting part is probably not too big of an issue. still struggling with my node setup tho. – Maths noob Dec 03 '21 at 20:57

1 Answers1

1

You can wait for another service to become available with docker-compose-wait project.

Add the 'docker-compose-wait' binary to the 'test container' and run the 'docker-compose-wait' binary before testing the API server inside the container's entrypoint.

You can give some time interval before and after checking if the service is ready.

Subbu VidyaSekar
  • 2,503
  • 3
  • 21
  • 39
J. Song
  • 209
  • 1
  • 6