stages:
  - build
  - setup-cluster
  - install-apps
  - health-test
  - integration-test
  - cleanup

image: "${CI_REGISTRY_IMAGE}/openappstack-ci:${CI_COMMIT_REF_NAME}"

ci_test_image:
  stage: build
  variables:
    DOCKER_DRIVER: overlay2
  image: docker:stable
  services:
    - docker:18-dind  # FIXME This is an older version of DIND. Update when gitlab-runner fixes https://gitlab.com/gitlab-org/gitlab-runner/issues/4501
  before_script:
    - docker info
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build -t ${CI_REGISTRY_IMAGE}/openappstack-ci:${CI_COMMIT_REF_NAME} test/
    - docker push ${CI_REGISTRY_IMAGE}/openappstack-ci:${CI_COMMIT_REF_NAME}
  only:
    changes:
      - test/Dockerfile
      - test/requirements.txt

bootstrap:
  stage: setup-cluster
  before_script:
    - ansible --version
  script:
    # Ensure test/ is not world-writable otherwise ansible-playbook refuses to run, see
    # https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
    - chmod 755 test/
    - cd test/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - ANSIBLE_HOST_KEY_CHECKING=False python3 -u ./ci-bootstrap.py --create-droplet --create-domain-records --run-ansible --ansible-param skip-tags=helmfile
  artifacts:
    paths:
    - test/cluster_data/rke.log
    - test/inventory.yml
    - test/group_vars/all/settings.yml
    expire_in: 1 month
    when: always

install:
  stage: install-apps
  variables:
     ANSIBLE_HOST_KEY_CHECKING: 'False'
  script:
    # Ensure test/ is not world-writable otherwise ansible-playbook refuses to run, see
    # https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
    - chmod 755 test/
    - cd test/
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - python3 -u ./ci-bootstrap.py --use-existing-inventory --run-ansible --ansible-param tags=helmfile --write-behave-config
    # Show versions of installed apps/binaries
    - ansible master -m shell -a 'oas-version-info.sh 2>&1'
  artifacts:
    paths:
    - test/behave/behave.ini
    expire_in: 1 month

testinfra:
  stage: health-test
  script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - cd test/
    - py.test -v -m 'testinfra' --connection=ansible --ansible-inventory=./inventory.yml --hosts='ansible://*'

certs:
  stage: health-test
  variables:
    OAS_DOMAIN: 'ci-${CI_PIPELINE_ID}.ci.openappstack.net'
  allow_failure: true
  script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - cd test/
    - py.test -s -m 'certs' --connection=ansible --ansible-inventory=./inventory.yml --hosts='ansible://*'

behave-nextcloud:
  stage: integration-test
  script:
    # Debug failing CI caches which happened in the past
    - find . -name behave.ini
    - ls -al test/behave
    - grep -v 'password' test/behave/behave.ini
    - cd test/behave/
    - behave -D headless=True -t nextcloud || behave -D headless=True @rerun_failing.features -t nextcloud
  artifacts:
    paths:
    - test/behave/screenshots/
    expire_in: 1 month
    when: on_failure
  retry: 2

behave-grafana:
  stage: integration-test
  script:
    # Debug failing CI caches which happened in the past
    - find . -name behave.ini
    - ls -al test/behave
    - grep -v 'password' test/behave/behave.ini
    - cd test/behave/
    - behave -D headless=True -t grafana || behave -D headless=True @rerun_failing.features -t grafana
  artifacts:
    paths:
    - test/behave/screenshots/
    expire_in: 1 month
    when: on_failure

terminate:
  stage: cleanup
  script:
    # Remove droplet after successful tests
    - cd test/
    - echo "$CI_COMMIT_MESSAGE" | grep '!ci_dont_terminate' && echo 'Termination of droplet disabled in commit message.' || python3 -u ./ci-bootstrap.py --use-existing-inventory --terminate
    # Remove droplet older than 2 days
    - python3 -c "import cosmos; cosmos.terminate_droplets_by_name(\"^ci-\", 2)"