name: test
on:
  push:
    paths-ignore:
      - docs/**
    branches:
      - main
  pull_request:
    paths-ignore:
      - docs/**
    branches:
      - main

jobs:
  validate:
    runs-on: buildjet-4vcpu-ubuntu-2204
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 1
      - uses: actions/setup-go@v4
        with:
          cache: false
          go-version: "1.20"
      - name: Setup Golang caches
        uses: buildjet/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-golang-
      - run: make validate-code
      - run: make build
  unit:
    runs-on: buildjet-4vcpu-ubuntu-2204
    needs: validate
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 1
      - uses: actions/setup-go@v4
        with:
          cache: false
          go-version: "1.20"
      - name: Setup Golang caches
        uses: buildjet/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-golang-
      - name: Run unit tests
        id: unit-test
        run: TEST_FLAGS="--junitfile unit-test-summary.xml" make unit
      - name: Build test summary
        uses: test-summary/action@v2
        if: "!cancelled() && steps.unit-test.conclusion != 'skipped'"
        with:
          paths: unit-test-summary.xml
  integration:
    runs-on: buildjet-4vcpu-ubuntu-2204
    needs: validate
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 1
      - uses: actions/setup-go@v4
        with:
          cache: false
          go-version: "1.20"
      - name: Setup Golang caches
        uses: buildjet/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-golang-
      - uses: debianmaster/actions-k3s@v1.0.5
        with:
          version: 'v1.27.2-k3s1'
      - run: make build
      - run: docker buildx install
      - run: make setup-ci-image
      - run: ./bin/acorn install --image acorn:v-ci --skip-checks --acorn-dns=disabled --network-policies=true
      - name: Run integration tests
        id: integration-tests
        run: TEST_ACORN_CONTROLLER=external TEST_FLAGS="--junitfile integration-test-summary.xml" make integration
      - name: Build test summary
        uses: test-summary/action@v2
        if: "!cancelled() && steps.integration-tests.conclusion != 'skipped'"
        with:
          paths: integration-test-summary.xml
