#
# 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/>.

variables:
   CI_REGISTRY_IMAGE: $CI_REGISTRY/caosdb-advanced-testenv
   # With dind the docker deamon is available on the network
   DOCKER_HOST: tcp://docker:2375/
   # When using dind, it's wise to use the overlayfs driver for
   # improved performance.
   DOCKER_DRIVER: overlay2

services:
  - docker:18.09.7-dind

stages: 
  - setup
  - style
  - test

test:
  tags: [notls]
  stage: test
  image: 
    name: docker/compose:1.24.1
    entrypoint: ["/bin/sh", "-c"]
  script:
      - if [[ "$CAOSDB_TAG" == "" ]]; then
            CAOSDB_TAG=master-latest;
        fi
      - echo $CAOSDB_TAG
      - docker login -u testuser -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
      - docker pull $CI_REGISTRY_IMAGE:latest
      - EXEPATH=`pwd` CAOSDB_TAG=$CAOSDB_TAG docker-compose 
        -f .docker/docker-compose.yml up -d
      - docker-compose -f .docker/tester.yml run tester 
      - rc=$?
      - docker-compose -f .docker/docker-compose.yml down
      - exit $rc

build-testenv:
  tags: [notls]
  image: docker:18.09
  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


style:
  tags: [notls]
  stage: style
  image: $CI_REGISTRY_IMAGE:latest
  script:
      - autopep8 -r --diff --exit-code .
  allow_failure: true
