name: QE 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@v2

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

  # docs/development/Building_PKI.md
  build:
    name: Building PKI
    needs: init
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
    steps:
      - name: Clone repository
        uses: actions/checkout@v2

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

      - name: Build runner image
        uses: docker/build-push-action@v2
        with:
          context: .
          build-args: |
            OS_VERSION=${{ matrix.os }}
            COPR_REPO=${{ needs.init.outputs.repo }}
            BUILD_OPTS=--with-timestamp --with-commit-id
          tags: pki-runner
          target: pki-runner
          outputs: type=docker,dest=/tmp/qe-runner.tar

      - name: Upload runner image
        uses: actions/upload-artifact@v2
        with:
          name: qe-runner-${{ matrix.os }}
          path: /tmp/qe-runner.tar

  # Tier 0
  installation-sanity-test:
    # This job uses Ansible playbooks in the tests dir to setup a PKI deployment.
    # All 5 subsystems are deployed on "discrete" instances
    name: Testing installation sanity
    needs: [init, build]
    runs-on: ubuntu-latest
    env:
      PKIDIR: /tmp/workdir/pki
    strategy:
      matrix: ${{ fromJSON(needs.init.outputs.matrix) }}
    steps:
      - name: Clone the repository
        uses: actions/checkout@v2

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install python3-pip
          pip3 install -r tests/dogtag/pytest-ansible/requirements.txt
          pip3 install -e tests/dogtag/pytest-ansible

      - name: Download runner image
        uses: actions/download-artifact@v2
        with:
          name: qe-runner-${{ matrix.os }}
          path: /tmp

      - name: Load runner image
        run: docker load --input /tmp/qe-runner.tar

      - name: Run master container
        run: |
          IMAGE=pki-runner \
          NAME=pki1 \
          HOSTNAME=pki1.example.com \
          tests/bin/runner-init.sh

      - name: Install dependencies in master container
        run: docker exec pki1 dnf install -y 389-ds-base python3-pip

      - name: Set up inventory
        run: |
          sed -e "s/TOPOLOGY/topology-02/g" tests/inventory > inventory
          ansible -i inventory -m setup master

      - name: Set up topology-02
        run: |
          mkdir -p /tmp/test_dir
          ansible-playbook \
              -b \
              -i inventory \
              -l all \
              -M tests/dogtag/pytest-ansible/common-modules \
              -vvv \
              tests/dogtag/pytest-ansible/installation/main.yml

      - name: Run sanity test
        run: |
          pytest \
              --ansible-host-pattern master \
              --ansible-inventory inventory \
              --ansible-module-path tests/dogtag/pytest-ansible/common-modules \
              --junit-xml pki_cert_junit.xml \
              -qsvvv \
              -x \
              tests/dogtag/pytest-ansible/pytest/sanity/test_role_users.py

      - name: Gather artifacts
        if: always()
        run: |
          tests/bin/ds-artifacts-save.sh pki1 topology-02-testingmaster
          mkdir -p /tmp/artifacts/pki1/etc/pki
          mkdir -p /tmp/artifacts/pki1/var/log
          docker exec pki1 ls -la /etc/pki
          docker cp pki1:/etc/pki/pki.conf /tmp/artifacts/pki1/etc/pki
          docker cp pki1:/etc/pki/topology-02-CA /tmp/artifacts/pki1/etc/pki
          docker cp pki1:/etc/pki/topology-02-KRA /tmp/artifacts/pki1/etc/pki
          docker cp pki1:/etc/pki/topology-02-OCSP /tmp/artifacts/pki1/etc/pki
          docker cp pki1:/etc/pki/topology-02-TKS /tmp/artifacts/pki1/etc/pki
          docker cp pki1:/etc/pki/topology-02-TPS /tmp/artifacts/pki1/etc/pki
          docker exec pki1 ls -la /var/log/pki
          docker cp pki1:/var/log/pki /tmp/artifacts/pki1/var/log
          docker exec pki1 journalctl -u pki-tomcatd@topology-02-CA > /tmp/artifacts/pki1/var/log/pki/topology-02-CA/systemd.log
          docker exec pki1 journalctl -u pki-tomcatd@topology-02-KRA > /tmp/artifacts/pki1/var/log/pki/topology-02-KRA/systemd.log
          docker exec pki1 journalctl -u pki-tomcatd@topology-02-OCSP > /tmp/artifacts/pki1/var/log/pki/topology-02-OCSP/systemd.log
          docker exec pki1 journalctl -u pki-tomcatd@topology-02-TKS > /tmp/artifacts/pki1/var/log/pki/topology-02-TKS/systemd.log
          docker exec pki1 journalctl -u pki-tomcatd@topology-02-TPS > /tmp/artifacts/pki1/var/log/pki/topology-02-TPS/systemd.log
        continue-on-error: true

      - name: Upload artifacts
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: sanity-${{ matrix.os }}
          path: |
            /tmp/artifacts/pki1
