name: E2E Tests

on:
  merge_group:
  pull_request:
    branches:
      - main
      - stable

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  build-cli:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '~1.22'
          check-latest: true
          cache: true
          cache-dependency-path: |
            **/go.sum

      - name: Build CLI
        run: |
          cd cli
          go build -tags=embed_manifests -o odigos

      - name: Upload CLI
        uses: actions/upload-artifact@v4
        with:
          name: odigos-cli
          path: cli/odigos

  build-odigos-images:
    runs-on: warp-ubuntu-latest-x64-8x-spot
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build Odigos Images
        run: |
          TAG=e2e-test make build-images
          docker save -o odigos-images.tar $(docker images --format "{{.Repository}}:{{.Tag}}" | grep "odigos")

      - name: Upload Odigos Images
        uses: actions/upload-artifact@v4
        with:
          name: odigos-images
          path: odigos-images.tar

  kubernetes-test:
    needs:
      - build-odigos-images
      - build-cli
    runs-on: warp-ubuntu-latest-x64-8x-spot
    strategy:
      fail-fast: false
      matrix:
        kube-version:
          - '1.20.15'
          - '1.23'
          - '1.30'
        test-scenario:
          - 'ui'
          - 'multi-apps'
          - 'helm-chart'
          - 'cli-upgrade'
          - 'workload-lifecycle'
        include:
          - kube-version: '1.20.15'
            kind-image: 'kindest/node:v1.20.15@sha256:a32bf55309294120616886b5338f95dd98a2f7231519c7dedcec32ba29699394'
          - kube-version: '1.23'
            kind-image: 'kindest/node:v1.23.17@sha256:14d0a9a892b943866d7e6be119a06871291c517d279aedb816a4b4bc0ec0a5b3'
          - kube-version: '1.30'
            kind-image: 'kindest/node:v1.30.0@sha256:047357ac0cfea04663786a612ba1eaba9702bef25227a794b52890dd8bcd692e'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '~1.22'
          check-latest: true
          cache: true
          cache-dependency-path: |
            **/go.sum

      - name: Set up Helm
        uses: azure/setup-helm@v4
        with:
          version: v3.9.0

      - name: Install chainsaw
        uses: kyverno/action-install-chainsaw@v0.2.8

      - name: Create Kind Cluster
        uses: helm/kind-action@v1.10.0
        with:
          node_image: ${{ matrix.kind-image }}
          version: 'v0.23.0'
          cluster_name: kind

      - name: Download and Load Docker Images to Kind
        uses: actions/download-artifact@v4
        with:
          name: odigos-images
      - run: |
          docker load -i odigos-images.tar
          TAG=e2e-test make load-to-kind

      - name: Download CLI binary
        uses: actions/download-artifact@v4
        with:
          name: odigos-cli
      - run: |
          mv odigos cli/odigos
          chmod +x cli/odigos

      - name: Run E2E Tests
        run: |
          chainsaw test tests/e2e/${{ matrix.test-scenario }}