parameters:
- name: Location
  type: string
  default: ''
- name: SubscriptionConfiguration
  type: string
  default: $(sub-config-azure-cloud-test-resources)
- name: ServiceDirectory
  type: string
  default: not-specified
- name: CtestRegex
  type: string
  default: .*
- name: Coverage
  type: string
  default: 'enabled'
- name: CoverageReportPath
  type: string
  default: sdk/*/*/*cov_xml.xml
- name: TimeoutInMinutes
  type: number
  default: 60

jobs:
- job: Validate
  condition: and(succeededOrFailed(), ne(variables['Skip.LiveTest'], 'true'))
  timeoutInMinutes: ${{ parameters.TimeoutInMinutes }}
  strategy:
    matrix:
      Linux_x64_with_unit_test:
        Pool: azsdk-pool-mms-ubuntu-1804-general
        OSVmImage: MMSUbuntu18.04
        VcpkgInstall: 'curl[ssl] libxml2 openssl'
        VCPKG_DEFAULT_TRIPLET: 'x64-linux'
        CmakeArgs: ' -DBUILD_TESTING=ON -DBUILD_PERFORMANCE_TESTS=ON -DRUN_LONG_UNIT_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DBUILD_CODE_COVERAGE=ON'
        AptDependencies: 'gcovr lcov'
        CODE_COVERAGE: '${{ parameters.Coverage }}'
        # Avoid re-running tests again for code coverage since the tests were previously ran
        CODE_COVERAGE_COLLECT_ONLY: 1
        AZURE_CORE_ENABLE_JSON_TESTS: 1
        BuildArgs: '-j 4'
      # Not asking for any transport adapter will default to OS -> windows:winHttp or !windows:libcurl
      Win_x86_with_unit_test_winHttp:
        Pool: azsdk-pool-mms-win-2019-general
        OSVmImage: MMS2019
        VcpkgInstall: 'libxml2'
        VCPKG_DEFAULT_TRIPLET: 'x86-windows-static'
        CMAKE_GENERATOR: 'Visual Studio 16 2019'
        CMAKE_GENERATOR_PLATFORM: Win32
        CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_PERFORMANCE_TESTS=ON '
        BuildArgs: '-v --parallel 8'
        AZURE_CORE_ENABLE_JSON_TESTS: 1
      Win_x64_with_unit_test_winHttp:
        Pool: azsdk-pool-mms-win-2019-general
        OSVmImage: MMS2019
        VcpkgInstall: 'libxml2'
        VCPKG_DEFAULT_TRIPLET: 'x64-windows-static'
        CMAKE_GENERATOR: 'Visual Studio 16 2019'
        CMAKE_GENERATOR_PLATFORM: x64
        CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_PERFORMANCE_TESTS=ON '
        BuildArgs: '-v --parallel 8'
        AZURE_CORE_ENABLE_JSON_TESTS: 1
      # specify libcurl to be used on Windows
      Win_x86_with_unit_test_libcurl:
        Pool: azsdk-pool-mms-win-2019-general
        OSVmImage: MMS2019
        VcpkgInstall: 'curl[winssl] libxml2'
        VCPKG_DEFAULT_TRIPLET: 'x86-windows-static'
        CMAKE_GENERATOR: 'Visual Studio 16 2019'
        CMAKE_GENERATOR_PLATFORM: Win32
        CmakeArgs: ' -DBUILD_TRANSPORT_CURL=ON -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_PERFORMANCE_TESTS=ON '
        BuildArgs: '-v --parallel 8'
        #AZURE_CORE_ENABLE_JSON_TESTS: 1  # Testing Json lib on Win+WinHttp only, No need to repeat here as it is independent to the http transport adapter.
      Win_x64_with_unit_test_libcurl:
        Pool: azsdk-pool-mms-win-2019-general
        OSVmImage: MMS2019
        VcpkgInstall: 'curl[winssl] libxml2'
        VCPKG_DEFAULT_TRIPLET: 'x64-windows-static'
        CMAKE_GENERATOR: 'Visual Studio 16 2019'
        CMAKE_GENERATOR_PLATFORM: x64
        CmakeArgs: ' -DBUILD_TRANSPORT_CURL=ON -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_PERFORMANCE_TESTS=ON '
        BuildArgs: '-v --parallel 8'
        #AZURE_CORE_ENABLE_JSON_TESTS: 1  # Testing Json lib on Win+WinHttp only, No need to repeat here as it is independent to the http transport adapter.
      MacOS_x64_with_unit_test:
        Pool: Azure Pipelines
        OSVmImage: 'macOS-10.14'
        VcpkgInstall: 'curl[ssl] libxml2 openssl'
        VCPKG_DEFAULT_TRIPLET: 'x64-osx'
        CmakeArgs: ' -DBUILD_TESTING=ON -DRUN_LONG_UNIT_TESTS=ON -DBUILD_PERFORMANCE_TESTS=ON '
        AZURE_CORE_ENABLE_JSON_TESTS: 1
        BuildArgs: '-j 4'
  pool:
    name: $(Pool)
    vmImage: $(OSVmImage)
  variables:
    CMOCKA_XML_FILE: "%g-test-results.xml"
    CMOCKA_MESSAGE_OUTPUT: "xml"
    AZURE_ENABLE_STATIC_ANALYSIS: 1
    AZURE_KEYVAULT_AVOID_THROTTLED: 1

  steps:
  - checkout: self
    submodules: recursive

  - template: /eng/common/pipelines/templates/steps/verify-agent-os.yml
    parameters:
      AgentImage: $(OSVmImage)

  - template: /eng/common/pipelines/templates/steps/bypass-local-dns.yml

  # Install apt dependencies (if appropriate)
  - bash: sudo apt install -y $(AptDependencies)
    condition: and(succeededOrFailed(), ne(variables['AptDependencies'], ''))
    displayName: Install dependencies from apt

  - template: /eng/pipelines/templates/steps/vcpkg.yml
    parameters:
      DependenciesVariableName: VcpkgInstall

  - script: |
      dotnet tool install -g dotnet-reportgenerator-globaltool
      dotnet tool install dotnet-reportgenerator-globaltool --tool-path tools
    displayName: Install coverage tools
    # CODE_COVERAGE variable is '' (do-not-generate) in all matrix but linux-gcc
    # It is 'enabled' by default on linux-gcc but it can be opt-out by each pipeline (disabled) 
    condition: and(succeededOrFailed(), ne(variables['CODE_COVERAGE'], 'disabled'), ne(variables['CODE_COVERAGE'], ''))

  - template: /eng/pipelines/templates/steps/cmake-build.yml
    parameters:
      GenerateArgs: $(CmakeArgs)
      BuildArgs: "$(BuildArgs)"

  - template: /eng/common/TestResources/deploy-test-resources.yml
    parameters:
      ServiceDirectory: ${{ parameters.ServiceDirectory }}
      Location: ${{ parameters.Location }}
      SubscriptionConfiguration: ${{ parameters.SubscriptionConfiguration }}

  - script: ctest -C Debug -V --tests-regex ${{ parameters.CtestRegex }}
    workingDirectory: build
    displayName: ctest

  # Make coverage targets (specified in coverage_targets.txt) and assemble
  # coverage report
  - bash: |
      make `cat ${{ parameters.ServiceDirectory }}-targets-coverage.txt`
      ../tools/reportgenerator "-reports:${{ parameters.CoverageReportPath }}" "-targetdir:." "-reporttypes:Cobertura"
    workingDirectory: build
    displayName: Generate Code Coverage Data
    condition: and(succeededOrFailed(), ne(variables['CODE_COVERAGE'], 'disabled'), ne(variables['CODE_COVERAGE'], ''))
  
  - task: PublishCodeCoverageResults@1
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: '$(Build.SourcesDirectory)/**/Cobertura.xml'
    displayName: Publish Code Coverage to DevOps
    condition: and(succeededOrFailed(), ne(variables['CODE_COVERAGE'], 'disabled'), ne(variables['CODE_COVERAGE'], ''))    

  - template: /eng/common/TestResources/remove-test-resources.yml
    parameters:
      ServiceDirectory: ${{ parameters.ServiceDirectory }}
      SubscriptionConfiguration: ${{ parameters.SubscriptionConfiguration }}
