#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen
# Copyright (C) 2019 Henrik tom Wörden 
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
#
#

variables:
   CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb-pyint-testenv
   # When using dind service we need to instruct docker, to talk with the
   # daemon started inside of the service. The daemon is available with
   # a network connection instead of the default /var/run/docker.sock socket.
   #
   # The 'docker' hostname is the alias of the service container as described at
   # https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#accessing-the-services
   #
   # Note that if you're using the Kubernetes executor, the variable should be set to
   # tcp://localhost:2375/ because of how the Kubernetes executor connects services
   # to the job container
   # DOCKER_HOST: tcp://localhost:2375/
   #
   # For non-Kubernetes executors, we use tcp://docker:2375/
   DOCKER_HOST: tcp://docker:2375/
   # When using dind, it's wise to use the overlayfs driver for
   # improved performance.
   DOCKER_DRIVER: overlay2

services:
  - docker:dind

stages: 
  - setup
  - test

test:
  tags: [docker]
  image: 
    name: docker/compose:1.24.1
    entrypoint: ["/bin/sh", "-c"]
  #image: $CI_REGISTRY_IMAGE:latest
  script:
      - if [[ "$CAOSDB_TAG" == "" ]]; then
            CAOSDB_TAG=latest;
        fi
      - echo $CAOSDB_TAG
      - docker login -u testuser -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
      - cd .docker
      - CAOSDB_TAG=$CAOSDB_TAG docker-compose  up -d
      - docker cp -a docker_caosdb-server_1:/opt/caosdb/cert/ cert
      - docker-compose -f tester.yml run tester 
      - rc=$?
      - docker-compose  down
      - exit $rc
      - cd ..

build-testenv:
  tags: [docker]
  image: docker:latest
  stage: setup
  script: 
      - docker login -u testuser -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        # use here general latest or specific branch latest...
      - docker pull $CI_REGISTRY_IMAGE:latest || true
      - docker build 
        --pull
        --file .docker/Dockerfile
        --cache-from $CI_REGISTRY_IMAGE:latest 
        -t $CI_REGISTRY_IMAGE:latest .
      - docker push $CI_REGISTRY_IMAGE:latest
