name: Test

on:
  pull_request:
    branches:
      - 'develop'
      - 'main'
  push: # needed to upload test coverage report to codecov
    branches:
      - 'develop'
      - 'main'

concurrency:
  group: mizu-tests-validation-${{ github.ref }}
  cancel-in-progress: true

jobs:
  run-unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2
        with:
          fetch-depth: 2

      - name: Set up Go 1.17
        uses: actions/setup-go@v2
        with:
          go-version: '^1.17'

      - name: Install libpcap
        shell: bash
        run: |
          sudo apt-get install libpcap-dev

      - id: 'auth'
        uses: 'google-github-actions/auth@v0'
        with:
          credentials_json: '${{ secrets.GCR_JSON_KEY }}'

      - name: 'Set up Cloud SDK'
        uses: 'google-github-actions/setup-gcloud@v0'

      - name: Check CLI modified files
        id: cli_modified_files
        run: devops/check_modified_files.sh cli/

      - name: CLI Test
        if: github.event_name == 'push' || steps.cli_modified_files.outputs.matched == 'true'
        run: make test-cli

      - name: Check Agent modified files
        id: agent_modified_files
        run: devops/check_modified_files.sh agent/

      - name: Agent Test
        if: github.event_name == 'push' || steps.agent_modified_files.outputs.matched == 'true'
        run: make test-agent

      - name: Shared Test
        run: make test-shared
      
      - name: Check extensions modified files
        id: ext_modified_files
        run: devops/check_modified_files.sh tap/extensions/

      - name: Extensions Test
        if: github.event_name == 'push' || steps.ext_modified_files.outputs.matched == 'true'
        run: make test-extensions

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v2

