version: '3'

services:

  mq:
    image: rabbitmq:latest

  postgres:
    image: postgres:latest
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pswd
      - POSTGRES_DB=replica
      - POSTGRES_HOST_AUTH_METHOD=md5
      - POSTGRES_INITDB_ARGS=--auth-host=md5

  replica:
    build:
      context: ..
      dockerfile: integration_tests/Dockerfile.Replica
    restart: always
    command: >
      bash -c "
      dockerize -wait tcp://mq:5672 -wait tcp://postgres:5432 -timeout 60s && 
      python manage.py makemigrations --settings=integration_tests.replica_settings &&
      python manage.py makemigrations dj_replica --settings=integration_tests.replica_settings &&
      python manage.py migrate --settings=integration_tests.replica_settings &&
      python manage.py cqrs_consume -w 2 --settings=integration_tests.replica_settings
      "
    depends_on:
      - mq
      - postgres
    volumes:
      - ../dj_cqrs:/replica/dj_cqrs
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=pswd
      - POSTGRES_DB=replica
      - CQRS_REPLICA_TRANSPORT=tests.dj.transport.RabbitMQTransportWithEvents
      - CQRS_BROKER_URL=amqp://mq:5672/

  master:
    build:
      context: ..
      dockerfile: integration_tests/Dockerfile.Master
    command: >
      bash -c "
      dockerize -wait tcp://mq:5672 -wait tcp://postgres:5432 -timeout 60s && 
      ./run_integration_tests.sh
      "
    depends_on:
      - mq
      - replica
    volumes:
      - ./tests/:/master/integration_tests/tests
      - ../dj_cqrs:/master/dj_cqrs
    environment:
      - CQRS_MASTER_TRANSPORT=dj_cqrs.transport.RabbitMQTransport
      - CQRS_BROKER_URL=amqp://mq:5672/