parameters:
  - name: SDKType
    type: string
    default: client
  - name: ServiceDirectory
    type: string
    default: 'not-specified' # Set a default that breaks in obvious ways.
  - name: TestPipeline
    type: boolean
    default: false
  - name: Artifacts
    type: object
    default: []
  - name: AdditionalModules
    type: object
    default: []
  - name: PreTestSteps
    type: object
    default: []
  - name: CloudConfig
    type: object
    default: {}
  - name: PreBuildSteps
    type: object
    default: []
  - name: TestGoals
    type: string
    default: $(TestGoals)
  - name: TestOptions
    type: string
    default: $(TestOptions)
  - name: TestParallelization
    type: string
    default: '1C'
  - name: BuildParallelization
    type: string
    default: '2C'
  - name: VersionOverrides
    type: object
    default: []

jobs:
  - job:
    condition: ne(variables['Skip.Test'], 'true')

    pool:
      name: 'azsdk-pool-mms-ubuntu-2004-general'
      vmImage: 'MMSUbuntu20.04'

    variables:
      - template: /eng/pipelines/templates/variables/globals.yml

    steps:
      - pwsh: |
          Write-Host "##vso[task.setvariable variable=AZURE_VERSION_OVERRIDE_TESTS]true"
          Write-Host "##vso[task.setvariable variable=TestVersionSupport]true"
        displayName: "Set version override variables"

      - template: /eng/pipelines/templates/steps/initialize-test-environment.yml
        parameters:
          Artifacts: ${{ parameters.Artifacts }}
          AdditionalModules: ${{ parameters.AdditionalModules }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          CheckoutRecordings: true
          SDKType: ${{ parameters.SDKType }}
          OSVmImage: 'MMSUbuntu20.04'

      - template: /eng/common/testproxy/test-proxy-tool.yml
        parameters:
          runProxy: true

      - template: /eng/pipelines/templates/steps/restore-test-proxy-recordings.yml
        parameters:
          Paths: $(SparseCheckoutDirectories)

      - pwsh: |
          $files = Get-ChildItem -Path $(Build.SourcesDirectory) -Filter test-proxy.log
          foreach($file in $files){
              Write-Host "##[group]$file"
              cat $file
              Write-Host "##[endgroup]"
          }
        displayName: Dump Test-Proxy Logs
        condition: succeededOrFailed()

      - template: /eng/pipelines/templates/steps/install-reporting-tools.yml
        parameters:
          JdkVersion: $(JavaTestVersion)

      - ${{ parameters.PreBuildSteps }}

      - ${{ parameters.PreTestSteps }}

      - ${{ each versionOverride in parameters.VersionOverrides }}:
        - template: /eng/pipelines/templates/steps/build-and-test.yml
          parameters:
            SDKType: ${{ parameters.SDKType }}
            ServiceDirectory: ${{ parameters.ServiceDirectory }}
            ParallelTestPlayback: 'true'
            TestGoals: ${{ parameters.TestGoals }}
            TestOptions: ${{ parameters.TestOptions }}
            TestParallelization: ${{ parameters.TestParallelization }}
            BuildParallelization: ${{ parameters.BuildParallelization }}
            VersionOverride: ${{ versionOverride }}

        - pwsh: |
            git checkout -- .
            Write-Host "##vso[task.setvariable variable=FirstTestOverrideRan]true"
          displayName: "Reset version override changes"

      - template: /eng/pipelines/templates/steps/upload-repository-on-failure.yml

      - template: /eng/common/testproxy/publish-proxy-logs.yml
