name: e2e-tests

on:
  push:
    branches:
      - main
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+*'
  pull_request:

jobs:
  build-auto-instrumentation:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Build auto-instrumentation
        run: |
          IMG=otel-go-instrumentation:latest make docker-build
          docker save otel-go-instrumentation:latest -o otel-go-instrumentation.tar
      - name: Upload Docker image artifact
        uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
        with:
          name: otel-go-instrumentation
          path: otel-go-instrumentation.tar
  kubernetes-test:
    needs:
      - build-auto-instrumentation
    strategy:
      matrix:
        k8s-version: ["v1.26.0"]
        library: ["autosdk", "nethttp", "nethttp_custom", "gin", "databasesql", "grpc", "otelglobal", "kafka-go"]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
      - name: Setup BATS
        uses: mig4/setup-bats@af9a00deb21b5d795cabfeaa8d9060410377686d # v1
      - name: Build sample app from script
        if: ${{ hashFiles(format('internal/test/e2e/{0}/build.sh', matrix.library)) != '' }}
        run: ./internal/test/e2e/${{ matrix.library }}/build.sh -t sample-app:latest
      - name: Build sample app
        if: ${{ hashFiles(format('internal/test/e2e/{0}/build.sh', matrix.library)) == '' }}
        working-directory: ./internal/test/e2e/${{ matrix.library }}
        run: docker build -t sample-app:latest .
      - name: Set up Helm
        uses: azure/setup-helm@fe7b79cd5ee1e45176fcad797de68ecaf3ca4814 # v4
        with:
          version: v3.9.0
      - name: Create kind cluster
        uses: helm/kind-action@0025e74a8c7512023d06dc019c617aa3cf561fde # v1.10.0
        with:
          node_image: kindest/node:${{ matrix.k8s-version }}
          kubectl_version: ${{ matrix.k8s-version }}
      - name: Check kind
        run: |
          kubectl cluster-info --context kind-chart-testing
          kubectl get node
          docker ps -a --filter label=io.x-k8s.kind.cluster=chart-testing
      - name: Download Docker image artifact
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
        with:
          name: otel-go-instrumentation
      - name: Load Docker image
        run: |
          docker load -i otel-go-instrumentation.tar
      - name: Kind load images
        run: |
          kind load docker-image otel-go-instrumentation --name chart-testing
          kind load docker-image sample-app --name chart-testing
      - name: Add Dependencies
        shell: bash
        run: |
            helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
        with:
          repository: 'open-telemetry/opentelemetry-helm-charts'
          path: opentelemetry-helm-charts
      - name: Helm install collector
        run: |
          if [ -f ./internal/test/e2e/${{ matrix.library }}/collector-helm-values.yml ]; then
            helm install test -f ./internal/test/e2e/${{ matrix.library }}/collector-helm-values.yml opentelemetry-helm-charts/charts/opentelemetry-collector
          else
            helm install test -f .github/workflows/e2e/k8s/collector-helm-values.yml opentelemetry-helm-charts/charts/opentelemetry-collector
          fi
      - name: check collector status
        # The loop is needed if the pod is not created yet
        # once https://github.com/kubernetes/kubectl/issues/1516 is fixed we can remove the loop
        run: |
          while : ; do
            kubectl get pod/test-opentelemetry-collector-0 && break
            sleep 5
          done
          kubectl wait --for=condition=Ready --timeout=60s pod/test-opentelemetry-collector-0
      - name: start sample job
        run: |
          kubectl -n default create -f .github/workflows/e2e/k8s/sample-job.yml
      - name: check job status
        id: job-status
        run: |
          kubectl wait --for=condition=Complete --timeout=60s job/sample-job
      - name: copy telemetry trace output
        run: |
          kubectl cp -c filecp default/test-opentelemetry-collector-0:tmp/trace.json ./internal/test/e2e/${{ matrix.library }}/traces-orig.json
          rm -f ./internal/test/e2e/${{ matrix.library }}/traces.json
        if: always() && steps.job-status.outcome == 'success'
      - name: print auto-instrumentation logs
        run: |
          kubectl logs -l app=sample -c auto-instrumentation --tail=300
          exit 1
        if: always() && steps.job-status.outcome == 'failure'
      - name: verify output and redact to traces.json
        run: |
          bats ./internal/test/e2e/${{ matrix.library }}/verify.bats
        if: always() && steps.job-status.outcome == 'success'
