---

# -------------------------------------------------------------------------------------------------
# Job Name
# -------------------------------------------------------------------------------------------------
name: Smoke


# -------------------------------------------------------------------------------------------------
# When to run
# -------------------------------------------------------------------------------------------------
on:
  # Runs on Pull Requests
  pull_request:

  # Runs on master Branch and Tags
  push:
    branches:
      - master
    tags:
      - '[0-9]+.[0-9]+*'


# -------------------------------------------------------------------------------------------------
# What to run
# -------------------------------------------------------------------------------------------------
jobs:
  smoke_linux:

    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        php:
          - "5.3"
          - "5.4"
          - "5.5"
          - "5.6"
          - "7.0"
          - "7.1"
          - "7.2"
          - "7.3"
          - "7.4"
          - "8.0"
        httpd:
          - "apache-2.2"
          - "apache-2.4"
          - "nginx-stable"
          - "nginx-mainline"
        config:
          - "default"
          - "custom"

    name: "[PHP ${{ matrix.php }}] vs [${{ matrix.httpd }}] (${{ matrix.config }})"
    steps:

      # ------------------------------------------------------------
      # Checkout repository
      # ------------------------------------------------------------
      - name: Checkout repository
        uses: actions/checkout@v1

      - name: Show environment
        shell: bash
        run: |
          env

      - name: Show network
        shell: bash
        run: |
          netstat -an || true
          ss -tlun || true

      - name: Show Docker version
        shell: bash
        run: |
          docker version

      # ------------------------------------------------------------
      # Setup
      # ------------------------------------------------------------
      - name: Configure
        shell: bash
        run: |
          cd .tests/
          # Set the two candidates to test against
          make configure KEY=PHP_SERVER   VAL=${PHP}
          make configure KEY=HTTPD_SERVER VAL=${HTTPD}

          # Set projects directory
          make configure KEY=HOST_PATH_HTTPD_DATADIR VAL=.tests/www

          # Set uid/gid
          make configure KEY=NEW_UID VAL=$(id -u)
          make configure KEY=NEW_GID VAL=$(id -g)

          # Make it more verbose
          make configure KEY=DEBUG_COMPOSE_ENTRYPOINT VAL=2

          # 3306 is taken, so chose another one
          make configure KEY=HOST_PORT_MYSQL VAL=3307

          # Test full customization
          if [ "${CONFIG}" = "custom" ]; then
            make configure KEY=DEBUG_COMPOSE_ENTRYPOINT VAL="$(( RANDOM % 3 ))"
            make configure KEY=DOCKER_LOGS VAL="$(( RANDOM % 1 ))"
            make configure KEY=TLD_SUFFIX VAL=loc2
            make configure KEY=TIMEZONE VAL='Europe/Berlin'
            make configure KEY=PHP_MODULES_DISABLE VAL=
            make configure KEY=HTTPD_TEMPLATE_DIR VAL=.config
            make configure KEY=HOST_PORT_HTTPD VAL=8080
            make configure KEY=HOST_PORT_HTTPD_SSL VAL=8443
            make configure KEY=MYSQL_ROOT_PASSWORD VAL=mysqlpass
            make configure KEY=PGSQL_ROOT_USER VAL=pgroot
            make configure KEY=PGSQL_ROOT_PASSWORD VAL=pgsqlpass
          fi
        env:
          PHP: ${{ matrix.php }}
          HTTPD: ${{ matrix.httpd }}
          CONFIG: ${{ matrix.config }}

      - name: Pull images
        shell: bash
        run: |
          cd .tests/
          make pull

      - name: Startup
        shell: bash
        run: |
          cd .tests/
          make start

      - name: Docker logs
        shell: bash
        run: |
          cd .tests/
          make logs

      # ------------------------------------------------------------
      # Run tests
      # ------------------------------------------------------------
      - name: Test Modules
        shell: bash
        run: |
          cd .tests/
          make test-smoke-modules
        if: success() || failure()

      - name: Test Config
        shell: bash
        run: |
          cd .tests/
          make test-smoke-config
        if: success() || failure()

      - name: Test Intranet
        shell: bash
        run: |
          cd .tests/
          make test-smoke-intranet
        if: success() || failure()

      - name: Test Vendors
        shell: bash
        run: |
          cd .tests/
          make test-smoke-vendors
        if: success() || failure()

      - name: Test Vhosts
        shell: bash
        run: |
          cd .tests/
          make test-smoke-vhosts
        if: success() || failure()

      - name: Test Reverse Proxy
        shell: bash
        run: |
          cd .tests/
          make test-smoke-rproxies
        if: success() || failure()

      - name: Test SSL
        shell: bash
        run: |
          cd .tests/
          make test-smoke-ssl
        if: success() || failure()

      - name: Test Bind
        shell: bash
        run: |
          cd .tests/
          make test-smoke-bind
        if: success() || failure()

      - name: Test Autostart
        shell: bash
        run: |
          cd .tests/
          make test-smoke-autostart
        if: success() || failure()

      - name: "Test Framework: CakePHP"
        shell: bash
        run: |
          cd .tests/
          make test-smoke-framework-cakephp
        if: success() || failure()

      - name: "Test Framework: Drupal"
        shell: bash
        run: |
          cd .tests/
          make test-smoke-framework-drupal
        if: success() || failure()

      # ------------------------------------------------------------
      # Finish
      # ------------------------------------------------------------

      - name: "Finish: Docker logs"
        shell: bash
        run: |
          cd .tests/
          make logs
        if: success() || failure()