---
name: Verify Antithesis Docker Compose Pipeline

permissions:
  contents: read

on:
  push:
    branches:
      - main
    paths:
      - 'tests/antithesis/**'
      - '.github/workflows/antithesis-verify.yml'
  pull_request:
    paths:
      - 'tests/antithesis/**'
      - '.github/workflows/antithesis-verify.yml'

jobs:
  test-docker-compose:
    strategy:
      matrix:
        node-count: [1, 3]
    name: Test ${{ matrix.node-count }}-node cluster
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Build etcd-server and etcd-client images
        run: |
          make -C tests/antithesis antithesis-build-etcd-image
          make -C tests/antithesis antithesis-build-client-docker-image

      - name: Run docker-compose up
        working-directory: ./tests/antithesis
        run: |
          make antithesis-docker-compose-up CFG_NODE_COUNT=${{ matrix.node-count }} &

      - name: Check for healthy cluster
        working-directory: ./tests/antithesis
        run: |
          timeout=120
          interval=10
          end_time=$(( $(date +%s) + timeout ))

          while [ $(date +%s) -lt $end_time ]; do
            # The client container might not be running yet, so ignore errors from docker compose logs
            if docker compose -f config/docker-compose-${{ matrix.node-count }}-node.yml logs client 2>/dev/null | grep -q "Client \[entrypoint\]: cluster is healthy!"; then
              echo "Cluster is healthy!"
              exit 0
            fi
            echo "Waiting for cluster to become healthy..."
            sleep $interval
          done

          echo "Cluster did not become healthy in ${timeout} seconds."
          docker compose -f config/docker-compose-${{ matrix.node-count }}-node.yml logs
          exit 1

      - name: Run traffic
        working-directory: ./tests/antithesis
        run: make antithesis-run-container-traffic CFG_NODE_COUNT=${{ matrix.node-count }}

      - name: Run validation
        working-directory: ./tests/antithesis
        run: make antithesis-run-container-validation CFG_NODE_COUNT=${{ matrix.node-count }}

      - name: Clean up
        if: always()
        working-directory: ./tests/antithesis
        run: make antithesis-clean CFG_NODE_COUNT=${{ matrix.node-count }}
