name: Test connection plugins

on:
  push:
    paths:
      - ".github/workflows/connections_tests.yml"
      - ".github/actions/setup-ansible-collection/action.yml"
      - ci/playbooks/*.yml
      - "ci/playbooks/connections/**"
      - "ci/run_connection_test.sh"
      - "plugins/connection/**"
      - "tests/integration/targets/connection/**"
      - "tests/integration/targets/connection_*/**"
    branches:
      - main
  pull_request:
    paths:
      - ".github/workflows/connections_tests.yml"
      - ".github/actions/setup-ansible-collection/action.yml"
      - ci/playbooks/*.yml
      - "ci/playbooks/connections/**"
      - "ci/run_connection_test.sh"
      - "plugins/connection/**"
      - "tests/integration/targets/connection/**"
      - "tests/integration/targets/connection_*/**"
  schedule:
    - cron: 3 0 * * * # Run daily at 0:03 UTC

jobs:
  build-collection-artifact-connection-tests:
    name: Build artifact
    runs-on: ${{ matrix.runner-os }}
    strategy:
      matrix:
        runner-os:
          - ubuntu-24.04
        ansible-version:
          - git+https://github.com/ansible/ansible.git@stable-2.15
        runner-python-version:
          - 3.9
    steps:
      - name: Check out ${{ github.repository }} on disk
        uses: actions/checkout@v6
      - name: Set up Python ${{ matrix.runner-python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.runner-python-version }}
      - name: Set up pip cache
        uses: actions/cache@v5
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('tests/sanity/requirements.txt') }}-${{ hashFiles('tests/unit/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-
            ${{ runner.os }}-
      - name: Install Ansible ${{ matrix.ansible-version }}
        run: >-
          python -m pip install --user '${{ matrix.ansible-version }}'
      - name: Build a collection tarball
        run: >-
          ~/.local/bin/ansible-galaxy collection build --output-path
          "${GITHUB_WORKSPACE}/.cache/collection-tarballs"
      - name: Store migrated collection artifacts
        uses: actions/upload-artifact@v6
        with:
          name: collection
          path: .cache/collection-tarballs

  test-podman-connection:
    name: Podman connection VM ${{ matrix.os || 'ubuntu-24.04' }}-${{ matrix.ansible-version }}
    needs:
      - build-collection-artifact-connection-tests
    runs-on: ${{ matrix.os || 'ubuntu-24.04' }}
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
      matrix:
        ansible-version:
          - git+https://github.com/ansible/ansible.git@stable-2.18
          - git+https://github.com/ansible/ansible.git@devel
        os:
          - ubuntu-24.04
          #- ubuntu-24.04
          #- ubuntu-16.04
          #- macos-latest
        python-version:
          - "3.12"
          # - 3.9
          #- 3.6
          #- 3.5
          #- 2.7

    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Setup Python/Ansible and install built collection
        uses: ./.github/actions/setup-ansible-collection
        with:
          python-version: ${{ matrix.python-version }}
          ansible-version: ${{ matrix.ansible-version }}
          artifact-name: collection
          artifact-path: .cache/collection-tarballs

      - name: Run collection tests for connection
        run: |
          export PATH=~/.local/bin:$PATH

          export ANSIBLE_CONFIG=$(pwd)/ci/ansible-dev.cfg

          echo $ANSIBLE_CONFIG
          command -v ansible-playbook
          pip --version
          python --version
          ansible-playbook --version

          ansible-playbook -vv ci/playbooks/pre.yml \
          -e host=localhost \
          -i localhost, \
          -e ansible_connection=local \
          -e setup_python=false

          ROOT= ./ci/run_connection_test.sh podman
          ROOT=true ./ci/run_connection_test.sh podman

          # Run advanced connection tests if they exist
          if [ -d "tests/integration/targets/connection_podman_advanced" ]; then
            echo "Running advanced podman connection tests..."
            cd tests/integration/targets/connection_podman_advanced
            ANSIBLECMD=~/.local/bin/ansible-playbook ./runme.sh || echo "Advanced tests skipped or failed"
            cd -
          fi
        shell: bash

  test-buildah-connection:
    name: Buildah connection VM ${{ matrix.os || 'ubuntu-24.04' }}-${{ matrix.ansible-version }}
    needs:
      - build-collection-artifact-connection-tests
    runs-on: ${{ matrix.os || 'ubuntu-24.04' }}
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
      matrix:
        ansible-version:
          - git+https://github.com/ansible/ansible.git@stable-2.18
          - git+https://github.com/ansible/ansible.git@devel
        os:
          - ubuntu-24.04
          #- ubuntu-24.04
          #- ubuntu-16.04
          #- macos-latest
        python-version:
          #- 3.9
          - "3.12"
          #- 3.6
          #- 3.5
          #- 2.7

    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Setup Python/Ansible and install built collection
        uses: ./.github/actions/setup-ansible-collection
        with:
          python-version: ${{ matrix.python-version }}
          ansible-version: ${{ matrix.ansible-version }}
          artifact-name: collection
          artifact-path: .cache/collection-tarballs

      - name: Run collection tests for connection
        run: |
          export PATH=~/.local/bin:$PATH

          export ANSIBLE_CONFIG=$(pwd)/ci/ansible-dev.cfg

          echo $ANSIBLE_CONFIG
          command -v ansible-playbook
          pip --version
          python --version
          ansible-playbook --version

          ansible-playbook -vv ci/playbooks/pre.yml \
          -e host=localhost \
          -i localhost, \
          -e ansible_connection=local \
          -e setup_python=false

      - name: Run buildah connection tests
        run: |
          export PATH=~/.local/bin:$PATH

          export ANSIBLE_CONFIG=$(pwd)/ci/ansible-dev.cfg

          ROOT= ./ci/run_connection_test.sh buildah
          ROOT=true ./ci/run_connection_test.sh buildah

          # Run advanced connection tests if they exist
          if [ -d "tests/integration/targets/connection_buildah_advanced" ]; then
            echo "Running advanced buildah connection tests..."
            cd tests/integration/targets/connection_buildah_advanced
            ANSIBLECMD=~/.local/bin/ansible-playbook ./runme.sh || echo "Advanced tests skipped or failed"
            cd -
          fi
        shell: bash

  test-example-playbooks:
    name: Test example playbooks with Podman and Buildah connection plugins
    needs:
      - build-collection-artifact-connection-tests
    runs-on: ${{ matrix.os || 'ubuntu-24.04' }}
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
      matrix:
        ansible-version:
          - git+https://github.com/ansible/ansible.git@stable-2.18
          - git+https://github.com/ansible/ansible.git@devel
        os:
          - ubuntu-24.04
          #- ubuntu-24.04
          #- ubuntu-16.04
          #- macos-latest
        python-version:
          #- 3.9
          - "3.12"
          #- 3.6
          #- 3.5
          #- 2.7

    steps:
      - name: Check out repository
        uses: actions/checkout@v6

      - name: Setup Python/Ansible and install built collection
        uses: ./.github/actions/setup-ansible-collection
        with:
          python-version: ${{ matrix.python-version }}
          ansible-version: ${{ matrix.ansible-version }}
          artifact-name: collection
          artifact-path: .cache/collection-tarballs

      - name: Run example Node build
        working-directory: playbooks/examples
        run: |
          echo "Python: $(command -v python)"
          echo "Python version: $(python --version)"
          echo "Ansible: $(command -v ansible-playbook)"
          echo "Ansible version: $(ansible --version)"

          ansible-playbook -vvv -i localhost, -c local build_node_ai_api.yml -e image_name=ci-ai-node:latest
          buildah images --format '{{.Name}}:{{.Tag}}' | grep -q 'ci-ai-node:latest$'

      - name: Run example Go multistage build
        working-directory: playbooks/examples
        run: |
          ansible-playbook -vv -i localhost, -c local build_go_ai_multistage.yml -e image_name=ci-ai-go:latest
          buildah images --format '{{.Name}}:{{.Tag}}' | grep -q 'ci-ai-go:latest$'

      - name: Run AI env build
        working-directory: playbooks/examples
        run: |
          ansible-playbook -vv -i localhost, -c local build_ai_env_with_ansible.yml -e image_name=ci-ai-env:latest
          buildah images --format '{{.Name}}:{{.Tag}}' | grep -q 'ci-ai-env:latest$'

      - name: Run Podman example - exec basic
        run: |
          ansible-playbook -vv -i playbooks/examples/inventory/podman_all.yml playbooks/examples/podman_exec_basic.yml

      - name: Run Podman example - copy and fetch
        run: |
          ansible-playbook -vv -i playbooks/examples/inventory/podman_all.yml playbooks/examples/podman_copy_fetch.yml

      - name: Run Podman example - multiuser tasks
        run: |
          ansible-playbook -vv -i playbooks/examples/inventory/podman_all.yml playbooks/examples/podman_multiuser_tasks.yml

      - name: Run Podman example - package manage
        run: |
          ansible-playbook -vv -i playbooks/examples/inventory/podman_all.yml playbooks/examples/podman_pkg_manage.yml

      - name: Show resulting images
        run: |
          buildah images
