name: Tomcat Tests

on: [push, pull_request]

jobs:
  init:
    name: Initializing Workflow
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.init.outputs.matrix }}
      repo: ${{ steps.init.outputs.repo }}
    steps:
      - name: Clone repository
        uses: actions/checkout@v3

      - name: Initialize workflow
        id: init
        env:
          BASE64_MATRIX: ${{ secrets.BASE64_MATRIX }}
          BASE64_REPO: ${{ secrets.BASE64_REPO }}
        run: |
          tests/bin/init-workflow.sh

  build:
    name: Building JSS
    needs: init
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
    steps:
      - name: Clone repository
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build runner image
        uses: docker/build-push-action@v3
        with:
          context: .
          build-args: |
            BASE_IMAGE=registry.fedoraproject.org/fedora:${{ matrix.os }}
            COPR_REPO=${{ needs.init.outputs.repo }}
            BUILD_OPTS=--with-timestamp --with-commit-id
          tags: jss-runner
          target: jss-runner
          outputs: type=docker,dest=tomcat-runner.tar

      - name: Store runner image
        uses: actions/cache@v3
        with:
          key: tomcat-runner-${{ matrix.os }}-${{ github.run_id }}
          path: tomcat-runner.tar

  # docs/admin/server/Configuring-HTTPS-Connector-with-NSS-Database.adoc
  https-nss-test:
    name: Testing HTTPS connector with NSS database
    needs: [init, build]
    runs-on: ubuntu-latest
    env:
      SHARED: /tmp/workdir/pki
    strategy:
      matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
    steps:
      - name: Clone repository
        uses: actions/checkout@v3

      - name: Retrieve runner image
        uses: actions/cache@v3
        with:
          key: tomcat-runner-${{ matrix.os }}-${{ github.run_id }}
          path: tomcat-runner.tar

      - name: Load runner image
        run: docker load --input tomcat-runner.tar

      - name: Create network
        run: docker network create example

      - name: Set up server container
        run: |
          IMAGE=jss-runner \
          NAME=server \
          HOSTNAME=server.example.com \
          tests/bin/runner-init.sh

      - name: Connect server container to network
        run: docker network connect example server --alias server.example.com

      - name: Install PKI packages
        run: docker exec server dnf install -y pki-server

      - name: Create Tomcat
        run: |
          docker exec server pki-server create -v

      - name: Create NSS database in Tomcat
        run: |
          docker exec server pki-server nss-create --no-password

      - name: Create SSL server cert
        run: |
          docker exec server pki -d /var/lib/pki/pki-tomcat/alias \
              nss-cert-request \
              --subject "CN=$HOSTNAME" \
              --ext /usr/share/pki/server/certs/sslserver.conf \
              --csr sslserver.csr
          docker exec server pki -d /var/lib/pki/pki-tomcat/alias \
              nss-cert-issue \
              --csr sslserver.csr \
              --ext /usr/share/pki/server/certs/sslserver.conf \
              --cert sslserver.crt
          docker exec server pki -d /var/lib/pki/pki-tomcat/alias \
              nss-cert-import \
              --cert sslserver.crt \
              sslserver

      - name: Create HTTPS connector with NSS database
        run: |
          docker exec server pki-server jss-enable
          docker exec server pki-server http-connector-add \
              --port 8443 \
              --scheme https \
              --secure true \
              --sslEnabled true \
              --sslProtocol SSL \
              --sslImpl org.dogtagpki.tomcat.JSSImplementation \
              Secure
          docker exec server pki-server http-connector-cert-add \
              --keyAlias sslserver \
              --keystoreType pkcs11 \
              --keystoreProvider Mozilla-JSS

      - name: Start Tomcat
        run: |
          docker exec server pki-server start

      - name: Set up client container
        run: |
          IMAGE=jss-runner \
          NAME=client \
          HOSTNAME=client.example.com \
          tests/bin/runner-init.sh

      - name: Connect client container to network
        run: docker network connect example client --alias client.example.com

      - name: Wait for Tomcat to start
        run: |
          tests/bin/tomcat-start-wait.sh client https://server.example.com:8443

      - name: Stop Tomcat
        run: |
          docker exec server pki-server stop --wait -v

      - name: Remove Tomcat
        run: |
          docker exec server pki-server remove -v

      - name: Gather artifacts from server container
        if: always()
        run: |
          tests/bin/pki-artifacts-save.sh server
        continue-on-error: true

      - name: Upload artifacts from server container
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: https-nss-test-${{ matrix.os }}
          path: |
            /tmp/artifacts/server
