name: windows-agent-robot-test

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

on:
  workflow_dispatch:
  schedule:
    - cron: "0 0 * * 1-5"
  pull_request:
    paths:
      - ".github/scripts/agent_robot_test.ps1"
      - ".github/scripts/windows-agent-compile.ps1"
      - ".github/workflows/windows-agent-robot-test.yml"
      - "agent/**"
      - "engine/modules/opentelemetry**"
      - "tests/resources/engine-scripts/*.ps1"

jobs:
  dependency-scan:
    uses: centreon/security-tools/.github/workflows/dependency-analysis.yml@main

  get-environment:
    needs: [dependency-scan]
    uses: ./.github/workflows/get-environment.yml
    with:
      version_file: CMakeLists.txt

  build-collect:
    needs: [get-environment]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    uses: ./.github/workflows/package-collect.yml
    with:
      major_version: ${{ needs.get-environment.outputs.major_version }}
      minor_version: ${{ needs.get-environment.outputs.minor_version }}
      img_version: ${{ needs.get-environment.outputs.packaging_img_version }}
      cma_major_version: ${{ needs.get-environment.outputs.cma_major_version }}
      cma_minor_version: ${{ needs.get-environment.outputs.cma_minor_version }}
      release: ${{ needs.get-environment.outputs.release }}
      commit_hash: ${{ github.sha }}
      stability: ${{ needs.get-environment.outputs.stability }}
      legacy_engine: "OFF"
      build_debug_packages: false
      packages_in_artifact: false
      image: packaging-centreon-collect-vcpkg-noble
      distrib: noble
      arch: amd64
      only_robot_cma: true
    secrets:
      collect_s3_access_key: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      collect_s3_secret_key: ${{ secrets.COLLECT_S3_SECRET_KEY }}
      registry_username: ${{ secrets.HARBOR_CENTREON_PULL_USERNAME }}
      registry_password: ${{ secrets.HARBOR_CENTREON_PULL_TOKEN }}
      rpm_gpg_key: ${{ secrets.RPM_GPG_SIGNING_KEY }}
      rpm_gpg_signing_key_id: ${{ secrets.RPM_GPG_SIGNING_KEY_ID }}
      rpm_gpg_signing_passphrase: ${{ secrets.RPM_GPG_SIGNING_PASSPHRASE }}
      jira_base_url: ${{ secrets.JIRA_BASE_URL }}
      jira_user_email: ${{ secrets.XRAY_JIRA_USER_EMAIL }}
      jira_api_token: ${{ secrets.XRAY_JIRA_TOKEN }}

  build-agent:
    needs: [get-environment]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    runs-on: windows-latest
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.COLLECT_S3_ACCESS_KEY }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.COLLECT_S3_SECRET_KEY }}
    steps:
      - name: nocrlf conversion
        run: git config --system core.autocrlf false

      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      # Install Inno Setup (ISCC) and expose its path
      # To remove later when innosetup version will be stable on runners
      - name: Install Inno Setup
        shell: powershell
        run: |
          choco install innosetup --allow-downgrade -y --no-progress --version 6.6.1

      - name: compile Agent
        run: .github/scripts/windows-agent-compile.ps1 -compile_ut Off -compile_installer On
        shell: powershell

      - name: save agent installer and proto in cache
        uses: actions/cache/save@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            opentelemetry-proto
            agent\installer\centreon-monitoring-agent.exe
            build_windows\agent\Release\centagent.exe
          key: ${{ github.run_id }}-${{ github.sha }}-exe-centreon-monitoring-agent-installer-${{ github.head_ref || github.ref_name }}

  execute-test:
    needs: [get-environment, build-collect, build-agent]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      needs.get-environment.outputs.stability != 'stable' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    runs-on: windows-latest
    strategy:
      matrix:
        robot-files: [cma.robot, cma2.robot]

    name: ${{matrix.robot-files}}
    steps:
      - name: nocrlf conversion
        run: git config --system core.autocrlf false

      - name: Checkout sources
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: distrib availables
        run: wsl --list --online

      - name: install Ubuntu noble 24.04
        uses: Vampire/setup-wsl@6a8db447be7ed35f2f499c02c6e60ff77ef11278 # v6.0.0
        with:
          wsl-version: 2
          distribution: Ubuntu-24.04
          use-cache: "true"
          update: "true"
          wsl-conf: |
            [automount]
            enabled = true
            [wsl2]
            localhostForwarding=true
          #            networkingMode = mirrored
          additional-packages: mariadb-server
            libmariadb3
            librrd8
            liblua5.3
            python3
            python3-pip
            python3-venv
            rrdtool
            jq

      - name: IP info
        run: |
          Write-Host "ip config"
          ipconfig /all
          Write-Host "ip address show"
          wsl ip address show

      - name: Restore agent installer and proto
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            opentelemetry-proto
            agent\installer\centreon-monitoring-agent.exe
            build_windows\agent\Release\centagent.exe
          key: ${{ github.run_id }}-${{ github.sha }}-exe-centreon-monitoring-agent-installer-${{ github.head_ref || github.ref_name }}
          fail-on-cache-miss: true

      - name: install database
        run: |
          $current_dir = (pwd).Path
          $wsl_path =  "/mnt/" + $current_dir.SubString(0,1).ToLower() + "/" + $current_dir.SubString(3).replace('\','/')
          Write-Host "install mariadb"
          wsl cd $wsl_path `&`& .github/scripts/collect-setup-database.sh

      - name: Restore packages
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
        with:
          path: ./*.deb
          key: ${{ github.run_id }}-${{ github.sha }}-deb-centreon-collect-noble-amd64-${{ github.head_ref || github.ref_name }}
          fail-on-cache-miss: true
          enableCrossOsArchive: true

      - name: list packages
        run: |
          $current_dir = (pwd).Path
          $wsl_path =  "/mnt/" + $current_dir.SubString(0,1).ToLower() + "/" + $current_dir.SubString(3).replace('\','/')
          wsl ls -l $wsl_path

      - name: install collect packages
        run: |
          $current_dir = (pwd).Path
          $wsl_path =  "/mnt/" + $current_dir.SubString(0,1).ToLower() + "/" + $current_dir.SubString(3).replace('\','/')
          wsl cd $wsl_path `&`& dpkg -i --force-all ./*.deb

      - name: prepare robot tests
        run: |
          copy tests\resources\engine-scripts\echo.ps1 C:\Users\public\
          copy tests\resources\engine-scripts\check.ps1 C:\Users\public\

      - name: robot tests
        run: .github/scripts/agent_robot_test.ps1 -robot ${{matrix.robot-files}}
        shell: powershell

      - name: Upload Test Results
        if: ${{ failure() }}
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: reports-${{matrix.robot-files}}-windows
          path: reports
          retention-days: 1

  set-skip-label:
    needs: [get-environment, execute-test]
    if: |
      needs.get-environment.outputs.skip_workflow == 'false' &&
      ! cancelled() &&
      ! contains(needs.*.result, 'failure') &&
      ! contains(needs.*.result, 'cancelled')
    uses: ./.github/workflows/set-pull-request-skip-label.yml
