image: registry.znuny.com/docker/zammad-ruby:2.4.4

# Global variables added to the ENV of each job
variables:
  # for faster translation loading
  Z_LOCALES: "en-us:de-de"
  # Browser tests and variables
  RAILS_SERVE_STATIC_FILES: "true"
  REMOTE_URL: "http://selenium:24444/wd/hub"
  TZ: "Europe/London"
  # docker elgalu/selenium variables for parallel browser instance creation
  MAX_INSTANCES: "50"
  MAX_SESSIONS: "50"
  # docker-imap-devel variables required for browser tests
  MAILNAME: "mail.test.dc.zammad.com"
  MAILBOX_INIT: "zammad@mail.test.dc.zammad.com:zammad"
  MAIL_ADDRESS: "zammad@mail.test.dc.zammad.com"
  MAIL_PASS: "zammad"

# Artifacts are stored for failed jobs for 2 days
.artifacts_error_template: &artifacts_error
  artifacts:
    expire_in: 2 days
    when: on_failure
    paths:
    - tmp/screenshot*
    - tmp/screenshots/*
    - log/*.log

# Workaround to enable usage of mixed SSH and Docker GitLab CI runners
.docker_env_template: &docker_env
  tags:
    - docker

.base_env_template: &base_env
  <<: *docker_env
  <<: *artifacts_error

# General required Docker services for different DB envs
.services_mysql_template: &services_mysql
  services:
    - name: registry.znuny.com/docker/zammad-mysql:latest
      alias: mysql

.services_postgresql_template: &services_postgresql
  services:
    - name: registry.znuny.com/docker/zammad-postgresql:latest
      alias: postgresql

# Cache gems in between jobs and pipelines
cache:
  key: "ruby24"
  paths:
    - vendor/ruby

# Initialize application env
before_script:
  - bundle install -j $(nproc) --path vendor
  - bundle exec ruby script/build/database_config.rb

# Stages
stages:
  - pre
  - test
  - browser-core
  - browser-integration

# pre stage

# Workaround to enable usage of mixed SSH and Docker GitLab CI runners
.pre_stage_template: &pre_stage
  <<: *docker_env
  stage: pre
  before_script:
    - '' # disable before_script for pre "non-application" env

pre:rubocop:
  <<: *pre_stage
  script:
    - bundle install -j $(nproc) --path vendor
    - bundle exec rubocop

pre:coffeelint:
  <<: *pre_stage
  script:
    - coffeelint app/

pre:bundle-audit:
  <<: *pre_stage
  script:
    - gem install bundler-audit
    - bundle-audit update
    - bundle-audit

pre:github:
  <<: *pre_stage
  tags:
    - deploy
  script:
    - script/build/sync_repo.sh git@github.com:zammad/zammad.git

# test stage

## RSpec

.script_rspec_template: &script_rspec_definition
  <<: *base_env
  variables:
    RAILS_ENV: "test"
  script:
    - bundle exec rake zammad:db:init
    - bundle exec rspec -t ~type:system

test:rspec:mysql:
  stage: test
  <<: *services_mysql
  <<: *script_rspec_definition

test:rspec:postgresql:
  stage: test
  <<: *services_postgresql
  <<: *script_rspec_definition

## Unit and Controller tests

.script_unit_template: &script_unit_definition
  <<: *base_env
  variables:
    RAILS_ENV: "test"
  script:
    - bundle exec rake zammad:db:init
    - bundle exec rake test:units
    - bundle exec ruby -I test/ test/integration/object_manager_test.rb
    - bundle exec ruby -I test/ test/integration/package_test.rb

test:unit:mysql:
  stage: test
  <<: *services_mysql
  <<: *script_unit_definition

test:unit:postgresql:
  stage: test
  <<: *services_postgresql
  <<: *script_unit_definition

## Integration tests

.test_integration_template: &test_integration_definition
  <<: *base_env
  <<: *services_postgresql
  stage: test
  variables:
    RAILS_ENV: "test"

test:integration:email_helper_deliver:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/email_helper_test.rb
    - bundle exec ruby -I test/ test/integration/email_deliver_test.rb
    - bundle exec ruby -I test/ test/integration/email_keep_on_server_test.rb

test:integration:facebook:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:init
    - bundle exec ruby -I test/ test/integration/facebook_test.rb
  allow_failure: true

test:integration:geo:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/geo_calendar_test.rb
    - bundle exec ruby -I test/ test/integration/geo_location_test.rb
    - bundle exec ruby -I test/ test/integration/geo_ip_test.rb

test:integration:user_agent:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/user_agent_test.rb
    - export ZAMMAD_PROXY_TEST=true
    - bundle exec ruby -I test/ test/integration/user_agent_test.rb
  allow_failure: true

test:integration:slack:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:unseeded
    - echo "gem 'slack-api'" >> Gemfile.local
    - bundle install -j $(nproc)
    - bundle exec ruby -I test test/integration/slack_test.rb

test:integration:clearbit:
  <<: *test_integration_definition
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test test/integration/clearbit_test.rb
  allow_failure: true

### Elasticsearch

.script_elasticsearch_template: &script_elasticsearch_definition
  <<: *base_env
  stage: test
  variables:
    RAILS_ENV: "test"
    ES_INDEX_RAND: "true"
    ES_URL: "http://elasticsearch:9200"
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/elasticsearch_active_test.rb
    - bundle exec ruby -I test/ test/integration/elasticsearch_test.rb
    - bundle exec ruby -I test/ test/integration/report_test.rb
    - bundle exec rspec --tag searchindex

test:integration:es_mysql:
  <<: *script_elasticsearch_definition
  services:
    - name: registry.znuny.com/docker/zammad-mysql:latest
      alias: mysql
    - name: registry.znuny.com/docker/zammad-elasticsearch:latest
      alias: elasticsearch

test:integration:es_postgresql:
  <<: *script_elasticsearch_definition
  services:
    - name: registry.znuny.com/docker/zammad-postgresql:latest
      alias: postgresql
    - name: registry.znuny.com/docker/zammad-elasticsearch:latest
      alias: elasticsearch

### Zendesk

.script_integration_zendesk_template: &script_integration_zendesk_definition
  <<: *base_env
  <<: *services_postgresql
  stage: test
  variables:
    RAILS_ENV: "test"
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/zendesk_import_test.rb
  allow_failure: true

test:integration:zendesk_mysql:
  <<: *services_mysql
  <<: *script_integration_zendesk_definition

test:integration:zendesk_postgresql:
  <<: *services_postgresql
  <<: *script_integration_zendesk_definition

### OTRS

.script_integration_otrs_template: &script_integration_otrs_definition
  <<: *base_env
  stage: test
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec ruby -I test/ test/integration/otrs_import_test.rb

.variables_integration_otrs_6_template: &variables_integration_otrs_6_definition
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "https://vz1185.test.znuny.com/otrs/public.pl?Action=ZammadMigrator"

test:integration:otrs_6_mysql:
  <<: *services_mysql
  <<: *script_integration_otrs_definition
  <<: *variables_integration_otrs_6_definition

test:integration:otrs_6_postgresql:
  <<: *services_postgresql
  <<: *script_integration_otrs_definition
  <<: *variables_integration_otrs_6_definition

test:integration:otrs_5:
  <<: *services_postgresql
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "http://vz1109.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator"
  <<: *script_integration_otrs_definition

test:integration:otrs_4:
  <<: *services_postgresql
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "http://vz383.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator"
  <<: *script_integration_otrs_definition

test:integration:otrs_33:
  <<: *services_postgresql
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "http://vz305.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator"
  <<: *script_integration_otrs_definition

test:integration:otrs_32:
  <<: *services_postgresql
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "http://vz382.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator"
  <<: *script_integration_otrs_definition

test:integration:otrs_31:
  <<: *services_postgresql
  variables:
    RAILS_ENV: "test"
    IMPORT_OTRS_ENDPOINT: "http://vz381.demo.znuny.com/otrs/public.pl?Action=ZammadMigrator"
  <<: *script_integration_otrs_definition

# Browser tests

## preparation (asset precompile)

browser:build:
  <<: *base_env
  <<: *services_postgresql
  stage: test
  variables:
    RAILS_ENV: "production"
  script:
    - bundle exec rake zammad:db:unseeded
    - bundle exec rake assets:precompile
  artifacts:
    expire_in: 1 week
    paths:
    - public/assets/.sprockets-manifest*
    - public/assets/application-*
    - public/assets/print-*

.services_browser_postgresql_template: &services_browser_postgresql_definition
  services:
    - name: registry.znuny.com/docker/zammad-postgresql:latest
      alias: postgresql
    - name: registry.znuny.com/docker/zammad-elasticsearch:latest
      alias: elasticsearch
    - name: docker.io/elgalu/selenium:latest
      alias: selenium
    - name: registry.znuny.com/docker/docker-imap-devel:latest
      alias: mail

.services_browser_mysql_template: &services_browser_mysql_definition
  services:
    - name: registry.znuny.com/docker/zammad-mysql:latest
      alias: mysql
    - name: registry.znuny.com/docker/zammad-elasticsearch:latest
      alias: elasticsearch
    - name: docker.io/elgalu/selenium:latest
      alias: selenium
    - name: registry.znuny.com/docker/docker-imap-devel:latest
      alias: mail

## Capybara

.test_capybara_template: &test_capybara_definition
  <<: *base_env
  stage: browser-core
  script:
    - bundle exec rake zammad:ci:test:prepare[with_elasticsearch]
    - bundle exec rspec --fail-fast -t type:system

.variables_capybara_chrome_template: &variables_capybara_chrome_definition
  <<: *test_capybara_definition
  variables:
    RAILS_ENV: "test"
    NO_RESET_BEFORE_SUITE: "true"
    BROWSER: "chrome"

.variables_capybara_ff_template: &variables_capybara_ff_definition
  <<: *test_capybara_definition
  variables:
    RAILS_ENV: "test"
    NO_RESET_BEFORE_SUITE: "true"
    BROWSER: "firefox"

test:browser:core:capybara_chrome_postgresql:
  <<: *variables_capybara_chrome_definition
  <<: *services_browser_postgresql_definition

test:browser:core:capybara_chrome_mysql:
  <<: *variables_capybara_chrome_definition
  <<: *services_browser_mysql_definition

test:browser:core:capybara_ff_postgresql:
  <<: *variables_capybara_ff_definition
  <<: *services_browser_postgresql_definition

test:browser:core:capybara_ff_mysql:
  <<: *variables_capybara_ff_definition
  <<: *services_browser_mysql_definition

## Browser core tests

.variables_browser_template: &variables_browser_definition
  RAILS_ENV: "production"
  APP_RESTART_CMD: "bundle exec rake zammad:ci:app:restart"

.test_browser_core_template: &test_browser_core_definition
  <<: *base_env
  stage: browser-core
  dependencies:
    - browser:build

### API clients

test:browser:integration:api_client_ruby:
  <<: *test_browser_core_definition
  <<: *services_postgresql
  variables:
    <<: *variables_browser_definition
  script:
    - RAILS_ENV=test bundle exec rake db:create
    - cp contrib/auto_wizard_test.json auto_wizard.json
    - bundle exec rake zammad:ci:test:start
    - git clone https://github.com/zammad/zammad-api-client-ruby.git
    - cd zammad-api-client-ruby
    - bundle install -j $(nproc)
    - bundle exec rspec

test:browser:integration:api_client_php:
  <<: *test_browser_core_definition
  <<: *services_postgresql
  variables:
    <<: *variables_browser_definition
    ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_URL: "http://localhost:3000"
    ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_USERNAME: "master@example.com"
    ZAMMAD_PHP_API_CLIENT_UNIT_TESTS_PASSWORD: "test"
  script:
    - RAILS_ENV=test bundle exec rake db:create
    - bundle exec rake zammad:ci:test:start zammad:setup:auto_wizard
    - git clone https://github.com/zammad/zammad-api-client-php.git
    - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    - php composer-setup.php --install-dir=/usr/local/bin
    - ln -s /usr/local/bin/composer.phar /usr/local/bin/composer
    - cd zammad-api-client-php
    - composer install
    - vendor/bin/phpunit

### Browser test slices

#### Templates

.script_browser_slice_template: &script_browser_slice_definition
  script:
    # temporary workaround to check Yahoo! mailbox only in test:browser:core:ff_3_* tests
    - if [[ $CI_JOB_NAME != test:browser:core:ff_3_* ]]; then unset MAILBOX_AUTO1 ; fi
    - if [[ $CI_JOB_NAME != test:browser:core:ff_3_* ]]; then unset MAILBOX_AUTO2 ; fi
    - if [[ $CI_JOB_NAME != test:browser:core:ff_3_* ]]; then unset MAILBOX_MANUAL1 ; fi
    - if [[ $CI_JOB_NAME != test:browser:core:ff_3_* ]]; then unset MAILBOX_MANUAL2 ; fi
    - env
    - script/build/test_slice_tests.sh $TEST_SLICE
    - RAILS_ENV=test bundle exec rake db:create
    - bundle exec rake zammad:ci:test:start[with_elasticsearch]
    - bundle exec rake test:browser

.test_browser_core_postgresql_template: &test_browser_core_postgresql_definition
  <<: *test_browser_core_definition
  <<: *script_browser_slice_definition
  <<: *services_browser_postgresql_definition

.test_browser_core_mysql_template: &test_browser_core_mysql_definition
  <<: *test_browser_core_definition
  <<: *script_browser_slice_definition
  <<: *services_browser_mysql_definition

#### Firefox

test:browser:core:ff_1_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "1"

test:browser:core:ff_2_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "2"

test:browser:core:ff_3_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "3"

test:browser:core:ff_4_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "4"

test:browser:core:ff_5_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "5"

test:browser:core:ff_6_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "6"

test:browser:core:ff_1_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "1"

test:browser:core:ff_2_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "2"

test:browser:core:ff_3_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "3"

test:browser:core:ff_4_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "4"

test:browser:core:ff_5_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "5"

test:browser:core:ff_6_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    TEST_SLICE: "6"

### Chrome

test:browser:core:chrome_1_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "1"

test:browser:core:chrome_2_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "2"

test:browser:core:chrome_3_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "3"

test:browser:core:chrome_4_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "4"

test:browser:core:chrome_5_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "5"

test:browser:core:chrome_6_mysql:
  <<: *test_browser_core_mysql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "6"

test:browser:core:chrome_1_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "1"

test:browser:core:chrome_2_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "2"

test:browser:core:chrome_3_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "3"

test:browser:core:chrome_4_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "4"

test:browser:core:chrome_5_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "5"

test:browser:core:chrome_6_postgresql:
  <<: *test_browser_core_postgresql_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    TEST_SLICE: "6"

### Auto wizard

.auto_wizard_services_template: &auto_wizard_services
  services:
    - name: registry.znuny.com/docker/zammad-postgresql:latest
      alias: postgresql
    - name: docker.io/elgalu/selenium:latest
      alias: selenium

.test_browser_integration_template: &test_browser_integration_definition
  <<: *base_env
  <<: *auto_wizard_services
  stage: browser-integration
  dependencies:
    - browser:build

.script_integration_auto_wizard_template: &script_integration_auto_wizard_definition
  script:
    - RAILS_ENV=test bundle exec rake db:create
    - cp $AUTO_WIZARD_FILE auto_wizard.json
    - bundle exec rake zammad:ci:test:start
    - bundle exec ruby -I test/ $TEST_FILE

.browser_core_auto_wizard_template: &browser_core_auto_wizard_definition
  <<: *test_browser_core_definition
  <<: *auto_wizard_services
  <<: *script_integration_auto_wizard_definition

test:browser:autowizard_chrome:
  <<: *browser_core_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_example.json"
    TEST_FILE: "test/integration/auto_wizard_browser_test.rb"

test:browser:autowizard_ff:
  <<: *browser_core_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_example.json"
    TEST_FILE: "test/integration/auto_wizard_browser_test.rb"

### Browser integration tests

.browser_integration_auto_wizard_template: &browser_integration_auto_wizard_definition
  <<: *test_browser_integration_definition
  <<: *script_integration_auto_wizard_definition

test:browser:integration:twitter_chrome:
  <<: *browser_integration_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_test.json"
    TEST_FILE: "test/integration/twitter_browser_test.rb"

test:browser:integration:twitter_ff:
  <<: *browser_integration_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_test.json"
    TEST_FILE: "test/integration/twitter_browser_test.rb"

test:browser:integration:facebook_chrome:
  <<: *browser_integration_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_test.json"
    TEST_FILE: "test/integration/facebook_browser_test.rb"

test:browser:integration:facebook_ff:
  <<: *browser_integration_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "firefox"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_test.json"
    TEST_FILE: "test/integration/facebook_browser_test.rb"

test:browser:integration:idoit_chrome:
  <<: *browser_integration_auto_wizard_definition
  variables:
    <<: *variables_browser_definition
    BROWSER: "chrome"
    AUTO_WIZARD_FILE: "contrib/auto_wizard_test.json"
    TEST_FILE: "test/integration/idoit_browser_test.rb"

### Browser integration tests

.variables_browser_import_template: &variables_browser_import_definition
  BROWSER: "chrome"
  RAILS_SERVE_STATIC_FILES: "true"
  RAILS_ENV: "production"

.browser_integration_import_template: &browser_integration_import_definition
  <<: *test_browser_integration_definition
  script:
    - RAILS_ENV=test bundle exec rake db:create
    - bundle exec rake zammad:ci:test:start
    - bundle exec ruby -I test/ $TEST_FILE

test:browser:integration:otrs_chrome:
  <<: *browser_integration_import_definition
  variables:
    <<: *variables_browser_import_definition
    TEST_FILE: "test/integration/otrs_import_browser_test.rb"

test:browser:integration:zendesk_chrome:
  <<: *browser_integration_import_definition
  variables:
    <<: *variables_browser_import_definition
    TEST_FILE: "test/integration/zendesk_import_browser_test.rb"
