name: CI-Tests

on:
  push:
    branches: [ master, release-0.* ]
  pull_request:
    branches: [ master, release-0.* ]

defaults:
  run:
    shell: bash

jobs:
  verify-and-unit-tests:
    name: Run verify and unit tests
    runs-on: ubuntu-20.04

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0 # Fetch all history for all tags and branches

    # https://github.com/mvdan/github-actions-golang#how-do-i-set-up-caching-between-builds
    - uses: actions/cache@v2
      with:
        # In order:
        # * Module download cache
        # * Build cache (Linux)
        path: |
          ~/go/pkg/mod
          ~/.cache/go-build
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - uses: actions/setup-go@v2
      with:
        go-version: '1.16'

    - name: Verify modules
      run: go mod verify

    - name: Verify format
      run: |
        make fmt
        git diff --exit-code

    - name: Verify manifests
      run: |
        make manifests
        git diff --exit-code

    - name: Verify imported manifests
      run: |
        make import-manifests
        git diff --exit-code

    - name: Verify bindata
      run: |
        make verify-bindata

    - name: Unit and Integration Tests
      run: make test

  e2e-tests:
    name: Run e2e tests
    runs-on: ubuntu-20.04

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0 # Fetch all history for all tags and branches

    # https://github.com/mvdan/github-actions-golang#how-do-i-set-up-caching-between-builds
    - uses: actions/cache@v2
      with:
        # In order:
        # * Module download cache
        # * Build cache (Linux)
        path: |
          ~/go/pkg/mod
          ~/.cache/go-build
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - uses: actions/setup-go@v2
      with:
        go-version: '1.16'

    - name: Download binaries
      run: |
        mkdir -p ${GITHUB_WORKSPACE}/bin
        echo "${GITHUB_WORKSPACE}/bin" >> ${GITHUB_PATH}
        make download-binaries

    - name: Create K8s KinD Cluster
      run: |
        kind version
        kind create cluster --image kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6 --wait=5m

    - name: Build and Push Test Container Image to KIND node
      run: |
        make docker-build IMG=localhost:5000/gatekeeper-operator:$GITHUB_SHA
        kind load docker-image localhost:5000/gatekeeper-operator:$GITHUB_SHA

    - name: E2E Tests
      run: |
        make deploy-ci NAMESPACE=mygatekeeper IMG=localhost:5000/gatekeeper-operator:$GITHUB_SHA
        make test-e2e NAMESPACE=mygatekeeper
        kubectl delete --wait namespace mygatekeeper

  gatekeeper-e2e-tests:
    name: Run gatekeeper e2e tests
    runs-on: ubuntu-20.04

    defaults:
      run:
        working-directory: gatekeeper-operator

    strategy:
      matrix:
        NAMESPACE: ["gatekeeper-system"]

    steps:
    - uses: actions/checkout@v2
      with:
        path: gatekeeper-operator
        fetch-depth: 0 # Fetch all history for all tags and branches

    # https://github.com/mvdan/github-actions-golang#how-do-i-set-up-caching-between-builds
    - uses: actions/cache@v2
      with:
        # In order:
        # * Module download cache
        # * Build cache (Linux)
        path: |
          ~/go/pkg/mod
          ~/.cache/go-build
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-

    - uses: actions/setup-go@v2
      with:
        go-version: '1.16'

    - name: Download binaries
      run: |
        mkdir -p ${GITHUB_WORKSPACE}/bin
        echo "${GITHUB_WORKSPACE}/bin" >> ${GITHUB_PATH}
        make download-binaries

    - name: Create K8s KinD Cluster
      run: |
        kind version
        kind create cluster --image kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6 --wait=5m

    - name: Build and Push Test Container Image to KIND node
      run: |
        make docker-build IMG=localhost:5000/gatekeeper-operator:$GITHUB_SHA
        kind load docker-image localhost:5000/gatekeeper-operator:$GITHUB_SHA

    - name: Set Up Environment Variables
      run: |
        GATEKEEPER_VERSION=$(awk '/^GATEKEEPER_VERSION/ {print $3}' Makefile)
        echo "GATEKEEPER_VERSION=${GATEKEEPER_VERSION}" >> ${GITHUB_ENV}

    # Checkout a local copy of Gatekeeper to use its bats e2e tests.
    - name: Checkout Gatekeeper to verify imported manifests
      uses: actions/checkout@v2
      with:
        repository: open-policy-agent/gatekeeper
        ref: ${{ env.GATEKEEPER_VERSION }}
        path: gatekeeper
        fetch-depth: 0 # Fetch all history for all tags and branches

    - name: Gatekeeper E2E Tests
      run: |
        make deploy-ci NAMESPACE=${{ matrix.NAMESPACE }} IMG=localhost:5000/gatekeeper-operator:$GITHUB_SHA
        make test-gatekeeper-e2e
        cd ../gatekeeper
        make test-e2e GATEKEEPER_NAMESPACE=${{ matrix.NAMESPACE }} ENABLE_MUTATION_TESTS=1
