parameters:
- name: dutMatrix
  type: object
- name: runDevTest
  type: boolean
  default: true
- name: runPyTest
  type: boolean
  default: true

jobs:
- job: build_uhd_run_hardware_test
  displayName: Build uhd and run hardware test
  strategy:
    matrix: ${{ parameters.dutMatrix }}

  pool:
    name: 'de-dre-lab'
    demands: uhd_ats -equals $(pipelineAgent)

  steps:
  - checkout: ettus-rts
    clean: true

  - checkout: self
    clean: true

  - template: steps-build-uhd-make.yml
    parameters:
      uhdSrcDir: $(Build.SourcesDirectory)/uhddev
      uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build

  - script: |
      mkdir -p $(Common.TestResultsDirectory)/devtest
      cd $(Common.TestResultsDirectory)/devtest
      python3 $(Build.SourcesDirectory)/uhddev/host/tests/devtest/run_testsuite.py \
        --src-dir $(Build.SourcesDirectory)/uhddev/host/tests/devtest \
        --devtest-pattern $(devtestPattern) --args addr=$(devAddr),type=$(devType) \
        --build-type Release --build-dir $(Build.BinariesDirectory)/uhddev/build \
        --python-interp python3 --xml
    continueOnError: true
    condition: and(succeeded(), eq('${{ parameters.runDevTest }}', true), eq(variables['deviceEnabled'], 'true'), eq(variables['devBus'], 'ip'))
    displayName: Run devtest for IP

  - script: |
      mkdir -p $(Common.TestResultsDirectory)/devtest
      cd $(Common.TestResultsDirectory)/devtest
      python3 $(Build.SourcesDirectory)/uhddev/host/tests/devtest/run_testsuite.py \
        --src-dir $(Build.SourcesDirectory)/uhddev/host/tests/devtest \
        --devtest-pattern $(devtestPattern) --args serial=$(devSerial),type=$(devType) \
        --build-type Release --build-dir $(Build.BinariesDirectory)/uhddev/build \
        --python-interp python3 --xml
    continueOnError: true
    condition: and(succeeded(), eq('${{ parameters.runDevTest }}', true), eq(variables['deviceEnabled'], 'true'), ne(variables['devBus'], 'ip'))
    displayName: Run devtest for PCIe / USB

  - script: |
      mkdir -p $(Common.TestResultsDirectory)/pytest
      cd $(Build.SourcesDirectory)/ettus-rts/config/remote/python_tests

      export PATH=$(Build.BinariesDirectory)/uhddev/build/utils:$(Build.BinariesDirectory)/uhddev/build/examples:$PATH
      export LD_LIBRARY_PATH=$(Build.BinariesDirectory)/uhddev/build/lib:$LD_LIBRARY_PATH
      export PYTHONPATH=$(Build.BinariesDirectory)/uhddev/build/python/build/lib:$PYTHONPATH

      python3 automated_main.py --ats_config $(pytestAtsConfig) --dut $(pytestDUT) \
        --results_path $(Common.TestResultsDirectory)/pytest
    continueOnError: true
    condition: and(succeeded(), eq('${{ parameters.runPyTest }}', true), eq(variables['deviceEnabled'], 'true'))
    displayName: Run pytests

  - script: |
      cd $(Common.TestResultsDirectory)/devtest
      python3 $(Build.SourcesDirectory)/uhddev/.ci/utils/format_devtest_junitxml.py \
        $(Common.TestResultsDirectory)/devtest \
        $(Common.TestResultsDirectory)/devtest/devtestresults.xml
    continueOnError: true
    condition: and(succeeded(), eq('${{ parameters.runDevTest }}', true), eq(variables['deviceEnabled'], 'true'))
    displayName: Format devtest xml

  - task: PublishTestResults@2
    inputs:
      testResultsFormat: 'JUnit'
      testResultsFiles: '$(Common.TestResultsDirectory)/devtest/devtestresults.xml'
      testRunTitle: $(devName) devtest
      buildConfiguration: 'Release'
      mergeTestResults: true
      failTaskOnFailedTests: true
    condition: and(eq('${{ parameters.runDevTest }}', true), eq(variables['deviceEnabled'], 'true'))
    displayName: Upload devtest results

  - task: PublishTestResults@2
    inputs:
      testResultsFormat: 'JUnit'
      testResultsFiles: '$(Common.TestResultsDirectory)/pytest/**/*.xml'
      testRunTitle: $(devName) pytest
      buildConfiguration: 'Release'
      mergeTestResults: true
    condition: and(succeeded(), eq('${{ parameters.runPyTest }}', true), eq(variables['deviceEnabled'], 'true'))
    displayName: Upload pytest results
