image: python:3.6

include:
  - template: Security/License-Scanning.gitlab-ci.yml

variables:
  POSTGRES_DB: smash
  POSTGRES_USER: runner
  POSTGRES_PASSWORD: password


.test_template: &test_definition
  stage: test
  before_script:
    - apt-get update && apt-get install -y --allow-unauthenticated libsasl2-dev libssl-dev locales locales-all
    - pip install -r requirements.txt --default-timeout=180 -i https://pypi.lcsb.uni.lu/simple/
    - pip install -r requirements-dev.txt --default-timeout=180 -i https://pypi.lcsb.uni.lu/simple/

test_postgres:
    <<: *test_definition
    services:
         - postgres:latest
    script:
         - cp "local_settings_ci.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - coverage run --source web manage.py test -v3
         - coverage report -m --omit="*/test*,*/migrations*,*debug_utils*"

test_sqlite:
    <<: *test_definition
    script:
         - cp "local_settings_ci_sqlite.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - coverage run --source web manage.py test -v3
         - coverage report -m --omit="*/test*,*/migrations*,*debug_utils*"

test_python_3.7:
    <<: *test_definition
    image: python:3.7
    script:
         - cp "local_settings_ci_sqlite.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - coverage run --source web manage.py test -v3
         - coverage report -m --omit="*/test*,*/migrations*,*debug_utils*"

test_python_3.8:
    <<: *test_definition
    image: python:3.8
    script:
         - cp "local_settings_ci_sqlite.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - coverage run --source web manage.py test -v3
         - coverage report -m --omit="*/test*,*/migrations*,*debug_utils*"

test_python_latest:
    <<: *test_definition
    image: python:latest
    script:
         - cp "local_settings_ci_sqlite.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - coverage run --source web manage.py test -v3
         - coverage report -m --omit="*/test*,*/migrations*,*debug_utils*"

test_create_dummy_script:
    <<: *test_definition
    script:
         - cp "local_settings_ci_sqlite.py" "smash/smash/local_settings.py"
         - cd smash
         - python manage.py makemigrations web && python manage.py migrate
         - python db_scripts/create_dummy_data.py

build_debian:
  stage: build
  only:
    - tags
  artifacts:
# you cannot enforce never here yet
    expire_in: 1000 yrs
    paths:
      - "*.deb"
  script:
    - apt-get update
    - DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated libsasl2-dev libssl-dev locales locales-all python3 dh-make build-essential lintian devscripts xsltproc fakeroot xsltproc docbook-xsl curl gnupg git gcc g++ make
    - curl -sL https://deb.nodesource.com/setup_14.x | bash -
    - DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs

    - pip install -r requirements.txt --default-timeout=180 -i https://pypi.lcsb.uni.lu/simple/
    - pip install -r requirements-dev.txt --default-timeout=180 -i https://pypi.lcsb.uni.lu/simple/

    - export DEBFULLNAME="Piotr Gawron"
    - export DEBEMAIL="piotr.gawron@uni.lu"
    - chmod 0755 build_deb.sh
    - ./build_deb.sh

test_install_debian_package_ubuntu_18:
  image: ubuntu:18.04
  stage: test
  only:
    - tags
  services:
     - name: piotrgawron/debian-repo
       alias: debian-repo
  dependencies:
     - build_debian
  script:
     - apt-get update
     - DEBIAN_FRONTEND=noninteractive apt-get install -y ssh software-properties-common dirmngr wget curl
     - debian_file=$(ls *.deb)
     - echo "PUT $debian_file /docker/incoming" | sftp -o StrictHostKeyChecking=no user@debian-repo
     - ssh root@debian-repo /usr/local/sbin/reprepro-import
     - apt-add-repository "deb http://debian-repo/ unstable main" -n
     - apt-get update --allow-insecure-repositories
     - DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated smasch
#systemd is not available on docker so we need to start manually gunicorn (gunicorn.service is not tested)
     - cd /usr/lib/smasch/
     - /usr/lib/smasch/env/bin/gunicorn -b 127.0.0.1:8888 --pid /run/gunicorn/pid smash.wsgi --error-logfile /var/log/smasch/gunicorn.log --log-level DEBUG --capture-output --limit-request-line 8192 &
#we need to wait a bit for gunicorn start
     - sleep 15
     - wget http://localhost:8888/account/login/?next=/
#test if we can login and list projects
     - test 302 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/)
     - test 200 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/account/login/?next=/)

test_install_debian_package_ubuntu_20:
  image: ubuntu:20.04
  stage: test
  only:
    - tags
  services:
     - name: piotrgawron/debian-repo
       alias: debian-repo
  dependencies:
     - build_debian
  script:
     - apt-get update
     - DEBIAN_FRONTEND=noninteractive apt-get install -y ssh software-properties-common dirmngr wget curl
     - debian_file=$(ls *.deb)
     - echo "PUT $debian_file /docker/incoming" | sftp -o StrictHostKeyChecking=no user@debian-repo
     - ssh root@debian-repo /usr/local/sbin/reprepro-import
     - apt-add-repository "deb http://debian-repo/ unstable main" -n
     - apt-get update --allow-insecure-repositories
     - DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated smasch
#systemd is not available on docker so we need to start manually gunicorn (gunicorn.service is not tested)
     - cd /usr/lib/smasch/
     - /usr/lib/smasch/env/bin/gunicorn -b 127.0.0.1:8888 --pid /run/gunicorn/pid smash.wsgi --error-logfile /var/log/smasch/gunicorn.log --log-level DEBUG --capture-output --limit-request-line 8192 &
#we need to wait a bit for gunicorn start
     - sleep 15
     - wget http://localhost:8888/account/login/?next=/
#test if we can login and list projects
     - test 302 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/)
     - test 200 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/account/login/?next=/)

test_install_debian_package_debian_buster:
  image: debian:buster
  stage: test
  only:
    - tags
  services:
     - name: piotrgawron/debian-repo
       alias: debian-repo
  dependencies:
     - build_debian
  script:
     - apt-get update
     - DEBIAN_FRONTEND=noninteractive apt-get install -y ssh software-properties-common dirmngr wget curl
     - debian_file=$(ls *.deb)
     - echo "PUT $debian_file /docker/incoming" | sftp -o StrictHostKeyChecking=no user@debian-repo
     - ssh root@debian-repo /usr/local/sbin/reprepro-import
     - apt-add-repository "deb http://debian-repo/ unstable main"
     - apt-get update --allow-insecure-repositories
     - DEBIAN_FRONTEND=noninteractive apt-get install -y --allow-unauthenticated smasch
#systemd is not available on docker so we need to start manually gunicorn (gunicorn.service is not tested)
     - cd /usr/lib/smasch/
     - /usr/lib/smasch/env/bin/gunicorn -b 127.0.0.1:8888 --pid /run/gunicorn/pid smash.wsgi --error-logfile /var/log/smasch/gunicorn.log --log-level DEBUG --capture-output --limit-request-line 8192 &
#we need to wait a bit for gunicorn start
     - sleep 15
     - wget http://localhost:8888/account/login/?next=/
#test if we can login and list projects
     - test 302 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/)
     - test 200 = $(curl -X GET --write-out %{http_code} --silent --output /dev/null http://localhost:8888/account/login/?next=/)

test_version_in_base_html:
   script:
     - CURRENT_VERSION=`cat CHANGELOG |grep smasch |head -1 | cut -f2 -d'(' | cut -f1 -d')' | cut -f1 -d'-' `
     - test 1 = $(cat smash/web/templates/_base.html |grep "<strong>$CURRENT_VERSION</strong>" |wc -l)

