name: "12 - Test: Linux Distros (Post-release)"

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to test (with 'v' prefix, e.g., v1.0.0)"
        required: true
        type: string

env:
  # Version from input, used to download the correct release artifacts
  VERSION: ${{ github.event.inputs.version }}
  # Disable update checks during smoke tests
  STEAMPIPE_UPDATE_CHECK: false
  # Slack webhook URL for notifications
  SLACK_WEBHOOK_URL: ${{ secrets.PIPELING_RELEASE_BOT_WEBHOOK_URL }}

jobs:
  smoke_test_ubuntu_24:
    name: Smoke test (Ubuntu 24, x86_64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Download Linux Release Artifact
        run: |
          mkdir -p ./artifacts
          gh release download ${{ env.VERSION }} \
            --pattern "*linux_amd64.tar.gz" \
            --dir ./artifacts \
            --repo ${{ github.repository }}
          # Rename to expected format
          mv ./artifacts/*linux_amd64.tar.gz ./artifacts/linux.tar.gz
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

      - name: Pull Ubuntu latest Image
        run: docker pull ubuntu:latest

      - name: Create and Start Ubuntu latest Container
        run: |
          docker run -d --name ubuntu-24-test -v ${{ github.workspace }}/artifacts:/artifacts -v ${{ github.workspace }}/scripts:/scripts ubuntu:latest tail -f /dev/null

      - name: Get runner/container info
        run: |
          docker exec ubuntu-24-test /scripts/linux_container_info.sh

      - name: Install dependencies, create user, and assign necessary permissions
        run: |
          docker exec ubuntu-24-test /scripts/prepare_ubuntu_container.sh

      - name: Run smoke tests
        run: |
          docker exec -u steampipe ubuntu-24-test /scripts/smoke_test.sh

      - name: Stop and Remove Container
        run: |
          docker stop ubuntu-24-test
          docker rm ubuntu-24-test

  smoke_test_ubuntu_22:
    name: Smoke test (Ubuntu 22, x86_64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Download Linux Release Artifact
        run: |
          mkdir -p ./artifacts
          gh release download ${{ env.VERSION }} \
            --pattern "*linux_amd64.tar.gz" \
            --dir ./artifacts \
            --repo ${{ github.repository }}
          # Rename to expected format
          mv ./artifacts/*linux_amd64.tar.gz ./artifacts/linux.tar.gz
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

      - name: Pull Ubuntu latest Image
        run: docker pull ubuntu:latest

      - name: Create and Start Ubuntu latest Container
        run: |
          docker run -d --name ubuntu-22-test -v ${{ github.workspace }}/artifacts:/artifacts -v ${{ github.workspace }}/scripts:/scripts ubuntu:22.04 tail -f /dev/null

      - name: Get runner/container info
        run: |
          docker exec ubuntu-22-test /scripts/linux_container_info.sh

      - name: Install dependencies, create user, and assign necessary permissions
        run: |
          docker exec ubuntu-22-test /scripts/prepare_ubuntu_container.sh

      - name: Run smoke tests
        run: |
          docker exec -u steampipe ubuntu-22-test /scripts/smoke_test.sh

      - name: Stop and Remove Container
        run: |
          docker stop ubuntu-22-test
          docker rm ubuntu-22-test

  smoke_test_centos_9:
    name: Smoke test (CentOS Stream 9, x86_64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Download Linux Release Artifact
        run: |
          mkdir -p ./artifacts
          gh release download ${{ env.VERSION }} \
            --pattern "*linux_amd64.tar.gz" \
            --dir ./artifacts \
            --repo ${{ github.repository }}
          # Rename to expected format
          mv ./artifacts/*linux_amd64.tar.gz ./artifacts/linux.tar.gz
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

      - name: Pull CentOS Stream 9 image
        run: docker pull quay.io/centos/centos:stream9

      - name: Create and Start CentOS stream9 Container
        run: |
          docker run -d --name centos-stream9-test -v ${{ github.workspace }}/artifacts:/artifacts -v ${{ github.workspace }}/scripts:/scripts quay.io/centos/centos:stream9 tail -f /dev/null

      - name: Get runner/container info
        run: |
          docker exec centos-stream9-test /scripts/linux_container_info.sh

      - name: Install dependencies, create user, and assign necessary permissions
        run: |
          docker exec centos-stream9-test /scripts/prepare_centos_container.sh

      - name: Run smoke tests
        run: |
          docker exec -u steampipe centos-stream9-test /scripts/smoke_test.sh

      - name: Stop and Remove Container
        run: |
          docker stop centos-stream9-test
          docker rm centos-stream9-test

  smoke_test_amazonlinux:
    name: Smoke test (Amazon Linux 2023, x86_64)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Download Linux Release Artifact
        run: |
          mkdir -p ./artifacts
          gh release download ${{ env.VERSION }} \
            --pattern "*linux_amd64.tar.gz" \
            --dir ./artifacts \
            --repo ${{ github.repository }}
          # Rename to expected format
          mv ./artifacts/*linux_amd64.tar.gz ./artifacts/linux.tar.gz
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

      - name: Pull Amazon Linux 2023 Image
        run: docker pull amazonlinux:2023

      - name: Create and Start Amazon Linux 2023 Container
        run: |
          docker run -d --name amazonlinux-2023-test -v ${{ github.workspace }}/artifacts:/artifacts -v ${{ github.workspace }}/scripts:/scripts amazonlinux:2023 tail -f /dev/null

      - name: Get runner/container info
        run: |
          docker exec amazonlinux-2023-test /scripts/linux_container_info.sh

      - name: Install dependencies, create user, and assign necessary permissions
        run: |
          docker exec amazonlinux-2023-test /scripts/prepare_amazonlinux_container.sh

      - name: Run smoke tests
        run: |
          docker exec -u steampipe amazonlinux-2023-test /scripts/smoke_test.sh

      - name: Stop and Remove Container
        run: |
          docker stop amazonlinux-2023-test
          docker rm amazonlinux-2023-test

  smoke_test_linux_arm64:
    name: Smoke test (Ubuntu 24, ARM64)
    runs-on: ubuntu-24.04-arm
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Download Linux Release Artifact
        run: |
          mkdir -p ./artifacts
          gh release download ${{ env.VERSION }} \
            --pattern "*linux_arm64.tar.gz" \
            --dir ./artifacts \
            --repo ${{ github.repository }}
          # Rename to expected format
          mv ./artifacts/*linux_arm64.tar.gz ./artifacts/linux.tar.gz
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract Linux Artifacts and Install Binary
        run: |
          sudo tar -xzf ./artifacts/linux.tar.gz -C /usr/local/bin
          sudo chmod +x /usr/local/bin/steampipe

      - name: Install jq
        run: |
          sudo apt-get update
          sudo apt-get install -y jq

      - name: Create steampipe user and setup environment
        run: |
          sudo useradd -m steampipe
          sudo mkdir -p /home/steampipe/.steampipe/logs
          sudo chown -R steampipe:steampipe /home/steampipe

      - name: Get runner/container info
        run: |
          uname -a
          cat /etc/os-release

      - name: Run smoke tests
        run: |
          chmod +x $GITHUB_WORKSPACE/scripts/smoke_test.sh
          sudo cp $GITHUB_WORKSPACE/scripts/smoke_test.sh /home/steampipe/smoke_test.sh
          sudo chown steampipe:steampipe /home/steampipe/smoke_test.sh
          sudo -u steampipe /home/steampipe/smoke_test.sh

  notify_completion:
    name: Notify completion
    runs-on: ubuntu-latest
    needs:
      [
        smoke_test_ubuntu_24,
        smoke_test_centos_9,
        smoke_test_amazonlinux,
        smoke_test_linux_arm64,
      ]
    if: always()
    steps:
      - name: Check results and notify
        run: |
          # Check if all jobs succeeded
          UBUNTU_24_RESULT="${{ needs.smoke_test_ubuntu_24.result }}"
          CENTOS_9_RESULT="${{ needs.smoke_test_centos_9.result }}"
          AMAZONLINUX_RESULT="${{ needs.smoke_test_amazonlinux.result }}"
          ARM64_RESULT="${{ needs.smoke_test_linux_arm64.result }}"

          WORKFLOW_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"

          if [ "$UBUNTU_24_RESULT" = "success" ] && [ "$CENTOS_9_RESULT" = "success" ] && [ "$AMAZONLINUX_RESULT" = "success" ] && [ "$ARM64_RESULT" = "success" ]; then
            MESSAGE="✅ Steampipe ${{ env.VERSION }} smoke tests passed!\n\n🔗 View details: $WORKFLOW_URL"
          else
            MESSAGE="❌ Steampipe ${{ env.VERSION }} smoke tests failed!\n\n🔗 View details: $WORKFLOW_URL"
          fi

          curl -X POST -H 'Content-type: application/json' \
            --data "{\"text\":\"$MESSAGE\"}" \
            ${{ env.SLACK_WEBHOOK_URL }}
