image: hatsoftwares/lutim-test-ci:latest
stages:
  - publish_changelog
  - pouet_it
  - podcheck
  - carton
  - carton_bdd
  - tests
  - cover
before_script:
  - rm -f *.db
variables:
  POSTGRES_DB: lutim_db
  POSTGRES_USER: lutim
  POSTGRES_PASSWORD: lutim_pwd

### Jobs templates
##
#
.retry: &retry
  retry: 2
  except:
    - tags
.carton_bdd_template: &carton_bdd_definition
  <<: *retry
  stage: carton_bdd
  artifacts:
    paths:
      - local/
  needs:
    - carton
.sqlite_template: &sqlite_definition
  <<: *retry
  stage: tests
  artifacts:
    paths:
      - cover_db/
  needs:
    - carton_sqlite
.pg_template: &pg_definition
  <<: *retry
  stage: tests
  artifacts:
    paths:
      - cover_db/
  needs:
    - carton_postgresql
  services:
    - name: postgres:9.6
      alias: postgres

### Publish tag changelog and create a toot
##
#
include:
  - 'https://framagit.org/fiat-tux/gitlabci-snippets/raw/4e4e03322e95e9b0124c714456ebf1bdc02ad43f/publish_changelog.gitlab-ci.yml'
  - 'https://framagit.org/fiat-tux/gitlabci-snippets/raw/4e4e03322e95e9b0124c714456ebf1bdc02ad43f/pouet-it-from-ci.gitlab-ci.yml'

### Podcheck
##
#
podcheck:
  <<: *retry
  stage: podcheck
  script:
    - make podcheck

### Install common dependencies
##
#
carton:
  <<: *retry
  stage: carton
  artifacts:
    paths:
      - local/
  dependencies: []
  script:
    - carton install --deployment --without=sqlite --without=postgresql --without=minion --without=cache --without=memcached
  when: always

### Install DB related dependencies
##
#
carton_sqlite:
  <<: *carton_bdd_definition
  script:
    - carton install --deployment --without=postgresql --without=minion --without=cache --without=memcached
carton_postgresql:
  <<: *carton_bdd_definition
  script:
    - carton install --deployment --without=sqlite --without=minion --without=cache --without=memcached

### SQLite tests
##
#
sqlite1:
  <<: *sqlite_definition
  script:
    - carton install --deployment --without=postgresql --without=minion --without=cache --without=memcached
    - MOJO_CONFIG=t/sqlite1.conf make test-sqlite
    - MOJO_CONFIG=t/sqlite1.conf make watch
    - MOJO_CONFIG=t/sqlite1.conf make cleanbdd
    - MOJO_CONFIG=t/sqlite1.conf make cleanfiles
    - MOJO_CONFIG=t/sqlite1.conf make stats
sqlite2:
  <<: *sqlite_definition
  script:
    - carton install --deployment --without=postgresql --without=cache --without=memcached
    - MOJO_CONFIG=t/sqlite2.conf make minion &
    - sleep 3
    - MOJO_CONFIG=t/sqlite2.conf make test-sqlite
    - MOJO_CONFIG=t/sqlite2.conf make watch
    - MOJO_CONFIG=t/sqlite2.conf make cleanbdd
    - MOJO_CONFIG=t/sqlite2.conf make cleanfiles
    - MOJO_CONFIG=t/sqlite2.conf make stats
sqlite3:
  <<: *sqlite_definition
  services:
    - name: postgres:9.6
      alias: postgres
  script:
    - carton install --deployment --without=cache --without=memcached
    - export PGPASSWORD=lutim_pwd; echo 'CREATE DATABASE lutim_minion WITH OWNER lutim;' | psql -h postgres -U lutim lutim_db
    - MOJO_CONFIG=t/sqlite3.conf make minion &
    - sleep 3
    - MOJO_CONFIG=t/sqlite3.conf make test-sqlite
    - MOJO_CONFIG=t/sqlite3.conf make watch
    - MOJO_CONFIG=t/sqlite3.conf make cleanbdd
    - MOJO_CONFIG=t/sqlite3.conf make cleanfiles
    - MOJO_CONFIG=t/sqlite3.conf make stats

### PostgreSQL tests
##
#
postgresql1:
  <<: *pg_definition
  script:
    - carton install --deployment --without=sqlite --without=minion --without=cache --without=memcached
    - MOJO_CONFIG=t/postgresql1.conf make test-pg
    - MOJO_CONFIG=t/postgresql1.conf make watch
    - MOJO_CONFIG=t/postgresql1.conf make cleanbdd
    - MOJO_CONFIG=t/postgresql1.conf make cleanfiles
    - MOJO_CONFIG=t/postgresql1.conf make stats
postgresql2:
  <<: *pg_definition
  script:
    - carton install --deployment --without=cache --without=memcached
    - MOJO_CONFIG=t/postgresql2.conf make minion &
    - sleep 3
    - MOJO_CONFIG=t/postgresql2.conf make test-pg
    - MOJO_CONFIG=t/postgresql2.conf make watch
    - MOJO_CONFIG=t/postgresql2.conf make cleanbdd
    - MOJO_CONFIG=t/postgresql2.conf make cleanfiles
    - MOJO_CONFIG=t/postgresql2.conf make stats
postgresql3:
  <<: *pg_definition
  script:
    - carton install --deployment --without=sqlite --without=cache --without=memcached
    - export PGPASSWORD=lutim_pwd; echo 'CREATE DATABASE lutim_minion WITH OWNER lutim;' | psql -h postgres -U lutim lutim_db
    - MOJO_CONFIG=t/postgresql3.conf make minion &
    - sleep 3
    - MOJO_CONFIG=t/postgresql3.conf make test-pg
    - MOJO_CONFIG=t/postgresql3.conf make watch
    - MOJO_CONFIG=t/postgresql3.conf make cleanbdd
    - MOJO_CONFIG=t/postgresql3.conf make cleanfiles
    - MOJO_CONFIG=t/postgresql3.conf make stats

### Code coverage
##
#
cover:
  stage: cover
  script:
    - make cover
  coverage: '/Total .*\d+\.\d+$/'
  except:
    - tags
