parameters:
  ServiceDirectory: ''
  TestResourceDirectories:
  Artifacts: []
  AdditionalModules: []
  EnvVars: {}
  MaxParallel: 0
  PreSteps: []
  PostSteps: []
  TimeoutInMinutes: 60
  TestMode: 'LIVE'
  TestResultsFiles: ''
  CloudConfig: {}
  ArmTemplateParameters: '@{}'
  Location: ''
  Matrix: ''
  DependsOn: ''
  UsePlatformContainer: false
  DisableAzureResourceCreation: false
  BuildParallelization: '2C'
  TestGoals: $(TestGoals)
  TestOptions: $(TestOptions)
  TestParallelization: '1'
  TestVersionSupport: $(TestVersionSupport)
  UseHttpFaultInjector: false

jobs:
  - job:
    dependsOn: ${{ parameters.DependsOn }}
    condition: and(succeededOrFailed(), ne(${{ parameters.Matrix }}, '{}'))
    strategy:
      maxParallel: ${{ parameters.MaxParallel }}
      matrix: $[ ${{ parameters.Matrix }} ]

    variables:
      - template: /eng/pipelines/templates/variables/globals.yml
      - name: ArmTemplateParameters
        value: '@{}'

    timeoutInMinutes: ${{ parameters.TimeoutInMinutes }}

    pool:
      name: $(Pool)
      vmImage: $(OSVmImage)

    ${{ if eq(parameters.UsePlatformContainer, 'true') }}:
      # Add a default so the job doesn't fail when the matrix is empty
      container: $[ variables['Container'] ]

    steps:
      - template: /eng/pipelines/templates/steps/initialize-test-environment.yml
        parameters:
          Artifacts: ${{ parameters.Artifacts }}
          AdditionalModules: ${{ parameters.AdditionalModules }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          SDKType: ${{ parameters.SDKType }}

      # macOS has known issues running HTTP fault injector, change this once
      # https://github.com/Azure/azure-sdk-tools/pull/6216 is resolved
      - ${{ if and(eq(parameters.UseHttpFaultInjector, 'true'), not(contains(variables['OSVmImage'], 'mac'))) }}:
        - template: /eng/pipelines/templates/steps/http-fault-injector.yml

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

      - ${{ if ne(parameters.DisableAzureResourceCreation, 'true') }}:
        - template: /eng/common/TestResources/build-test-resource-config.yml
          parameters:
            SubscriptionConfiguration: ${{ parameters.CloudConfig.SubscriptionConfiguration }}
            SubscriptionConfigurations: ${{ parameters.CloudConfig.SubscriptionConfigurations }}

        - ${{ if parameters.TestResourceDirectories }}:
          - ${{ each directory in parameters.TestResourceDirectories }}:
            - template: /eng/common/TestResources/deploy-test-resources.yml
              parameters:
                ${{ if or(parameters.Location, parameters.CloudConfig.Location) }}:
                  Location: ${{ coalesce(parameters.Location, parameters.CloudConfig.Location) }}
                ServiceDirectory: '${{ directory }}'
                SubscriptionConfiguration: $(SubscriptionConfiguration)
                ArmTemplateParameters: $(ArmTemplateParameters)
        - ${{ if not(parameters.TestResourceDirectories) }}:
          - template: /eng/common/TestResources/deploy-test-resources.yml
            parameters:
              ${{ if or(parameters.Location, parameters.CloudConfig.Location) }}:
                Location: ${{ coalesce(parameters.Location, parameters.CloudConfig.Location) }}
              ServiceDirectory: '${{ parameters.ServiceDirectory }}'
              SubscriptionConfiguration: $(SubscriptionConfiguration)
              ArmTemplateParameters: $(ArmTemplateParameters)

      - ${{ parameters.PreSteps }}

      - template: /eng/pipelines/templates/steps/build-and-test.yml
        parameters:
          SDKType: ${{ parameters.SDKType }}
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          ParallelTestPlayback: 'false'
          BuildParallelization: ${{ parameters.BuildParallelization }}
          IsLiveTest: true
          TestGoals: ${{ parameters.TestGoals }}
          TestOptions: ${{ parameters.TestOptions }}
          TestParallelization: ${{ parameters.TestParallelization }}
          TestVersionSupport: ${{ parameters.TestVersionSupport }}
          TestEnvVars:
            AZURE_TEST_MODE: ${{ parameters.TestMode }}
            AZURE_VERSION_OVERRIDE_TESTS: ${{ parameters.TestVersionSupport }}
            ${{ each var in parameters.EnvVars }}:
              ${{ var.key }}: ${{ var.value }}

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

      # macOS has known issues running HTTP fault injector, change this once
      # https://github.com/Azure/azure-sdk-tools/pull/6216 is resolved
      - ${{ if and(eq(parameters.UseHttpFaultInjector, 'true'), not(contains(variables['OSVmImage'], 'mac'))) }}:
        - template: /eng/pipelines/templates/steps/publish-http-fault-injector-logs.yml

      - ${{ parameters.PostSteps }}

      - ${{ if ne(parameters.DisableAzureResourceCreation, 'true') }}:
        - ${{ if parameters.TestResourceDirectories }}:
          - ${{ each directory in parameters.TestResourceDirectories }}:
            - template: /eng/common/TestResources/remove-test-resources.yml
              parameters:
                ServiceDirectory: '${{ directory }}'
                SubscriptionConfiguration: $(SubscriptionConfiguration)
        - ${{ if not(parameters.TestResourceDirectories) }}:
          - template: /eng/common/TestResources/remove-test-resources.yml
            parameters:
              ServiceDirectory: '${{ parameters.ServiceDirectory }}'
              SubscriptionConfiguration: $(SubscriptionConfiguration)
