# Build, tag, and push the Docker images to the GitLab Docker registry
build integ_test:
  variables:
    IMAGE_NAME: 'tester_integration' # name of the microservice
    IMAGE_NAME_TEST: 'tester_integration-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/common/**
      - src/context/**
      - src/device/**
      - src/service/**
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# Pull, execute, and run unitary tests for the Docker image from the GitLab registry
unit_test integ_test:
  variables:
    IMAGE_NAME: 'tester_functional' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: test
  needs:
    - build integ_test
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/common/**
      - src/context/**
      - src/device/**
      - src/service/**
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# Pull, execute, and run integration tests for the Docker image from the GitLab registry
integ_test execute:
  variables:
    IMAGE_NAME: 'tester_integration' # name of the microservice
    IMAGE_NAME_TEST: 'tester_integration-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: integ_test
  needs:
    - build integ_test
    - unit_test context
    - unit_test device
    - unit_test service
    - unit_test integ_test
    - dependencies all
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi  
  script:
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker run -d -p 6379:6379 --name redis --network=teraflowbridge "redis:6.2"
    - docker ps -a
    - sleep 5
    - docker ps -a
    - docker run -d --name $IMAGE_NAME --network=teraflowbridge --env "DB_ENGINE=redis" --env "REDIS_SERVICE_HOST=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis)" --env "REDIS_SERVICE_PORT=6379" --env "REDIS_DATABASE_ID=0" "$IMAGE_NAME:$IMAGE_TAG" bash -c "pytest --log-level=INFO --verbose common/database/tests/test_engine_redis.py tester_integration/test_context_device_service.py"
  after_script:
    - docker stop $IMAGE_NAME redis
    - docker rm $IMAGE_NAME redis
  rules:
    - changes:
      - src/common/**
      - src/context/**
      - src/device/**
      - src/service/**
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml
