# This is a reusable workflow used by master and release CI
on:
  workflow_call:
    inputs:
      arch:
        description: x86_64 or aarch64
        required: true
        type: string
      static:
        description: Falco packages use a static build
        required: false
        type: boolean
        default: false
      version:
        description: The Falco version to use when testing packages
        required: true
        type: string
      sanitizers:
        description: Use sanitizer enabled build
        required: false
        default: false
        type: boolean

permissions:  
  contents: read
  
jobs:
  test-packages:
    # See https://github.com/actions/runner/issues/409#issuecomment-1158849936
    runs-on: ${{ (inputs.arch == 'aarch64' && 'ubuntu-22.04-arm') || 'ubuntu-latest' }}
    steps:
      - name: Download binary
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
        with:
          name: falco-${{ inputs.version }}${{ inputs.static && '-static' || '' }}-${{ inputs.arch }}${{ inputs.sanitizers == true && '-sanitizers' || '' }}.tar.gz
      
      - name: Install Falco package
        run: |
          ls falco-*.tar.gz
          tar -xvf $(ls falco-*.tar.gz)
          cd falco-${{ inputs.version }}-${{ inputs.arch }}
          sudo cp -r * /
    
      - name: Install kernel headers for falco-driver-loader tests
        run: |
          sudo apt update -y
          sudo apt install -y --no-install-recommends linux-headers-$(uname -r)

      # Some builds use sanitizers, we always install support for them so they can run
      - name: Install sanitizer support
        if: inputs.sanitizers
        run: |
          sudo apt update -y
          sudo apt install -y libasan5 libubsan1
          
      - name: Run tests
        env:
          LSAN_OPTIONS: "intercept_tls_get_addr=0"
        uses: falcosecurity/testing@main
        with:
          test-falco: 'true'
          test-falcoctl: 'true'
          test-k8saudit: 'true'
          test-dummy: 'true'
          static: ${{ inputs.static && 'true' || 'false' }}
          test-drivers: 'true'
          show-all: 'true'
          report-name-suffix: ${{ inputs.static && '-static' || '' }}${{ inputs.sanitizers && '-sanitizers' || '' }}
