name: KinD tests

on:
  push:
    branches:
      - main
      - release-2.[3-9]
  pull_request:
    branches:
      - main
      - release-2.[3-9]

defaults:
  run:
    shell: bash
    working-directory: governance-policy-propagator

jobs:
  kind-tests:
    runs-on: ubuntu-latest
    env:
      REGISTRY: localhost:5000
    strategy:
      matrix:
        go:
          - '1.16'
        # Run tests on oldest and newest supported OCP Kubernetes
        # - OCP 4.5 runs Kubernetes v1.18
        # KinD tags: https://hub.docker.com/r/kindest/node/tags
        kind:
          - 'v1.18.15'
          - 'latest'
    name: KinD tests
    steps:
    - name: Checkout Governance Policy Propagator
      uses: actions/checkout@v2
      with:
        path: governance-policy-propagator
        fetch-depth: 0 # Fetch all history for all tags and branches

    - name: Set up Go - ${{ matrix.go }}
      uses: actions/setup-go@v2
      id: go
      with:
        go-version: ${{ matrix.go }}

    - name: Verify modules
      run: |
        go mod verify

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

    - name: Create K8s KinD Cluster - ${{ matrix.kind }}
      env:
        KIND_VERSION: ${{ matrix.kind }}
      run: |
        make kind-bootstrap-cluster-dev

    - name: Build and Push Image to KinD node
      env:
        TAG: ${{ github.sha }}
      run: |
        make build-images
        make kind-deploy-controller-dev

    - name: Unit and Integration Tests
      run: |
        make test-dependencies
        make test

    - name: E2E Tests
      run: |
        export GOPATH=$(go env GOPATH)
        make e2e-dependencies
        make e2e-test

    - name: SonarCloud Analysis
      if: ${{ github.event_name == 'push' }}
      run: |
        echo "SonarCloud not implemented."

    - name: Debug
      if: ${{ failure() }}
      run: |
        make e2e-debug

    - name: Clean up cluster
      if: ${{ always() }}
      run: |
        make kind-delete-cluster
