| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 
 | # This template sets variable PROXY_PID to be used for shutdown later.
parameters:
  rootFolder: '$(Build.SourcesDirectory)'
  runProxy: true
  targetVersion: ''
  templateRoot: '$(Build.SourcesDirectory)'
  condition: true
steps:
  - pwsh: |
        ${{ parameters.templateRoot }}/eng/common/scripts/trust-proxy-certificate.ps1
    displayName: 'Language Specific Certificate Trust'
    condition: and(succeeded(), ${{ parameters.condition }})
  - task: PowerShell@2
    displayName: 'Override proxy version if necessary'
    condition: and(succeeded(), ${{ parameters.condition }}, ne('${{ parameters.targetVersion }}', ''))
    inputs:
      targetType: filePath
      filePath: '${{ parameters.templateRoot }}/eng/common/testproxy/scripts/override-proxy-version.ps1'
      arguments: '-TargetVersion "${{ parameters.targetVersion }}"'
      pwsh: true
  - pwsh: |
      $standardVersion = "${{ parameters.templateRoot }}/eng/common/testproxy/target_version.txt"
      $overrideVersion = "${{ parameters.templateRoot }}/eng/target_proxy_version.txt"
      $version = $(Get-Content $standardVersion -Raw).Trim()
      if (Test-Path $overrideVersion) {
        $version = $(Get-Content $overrideVersion -Raw).Trim()
      }
      Write-Host "Installing test-proxy version $version"
      $invocation = @"
      dotnet tool install azure.sdk.tools.testproxy `
        --tool-path $(Build.BinariesDirectory)/test-proxy `
        --add-source https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json `
        --version $version
      "@
      Write-Host $invocation
      dotnet tool install azure.sdk.tools.testproxy `
        --tool-path $(Build.BinariesDirectory)/test-proxy `
        --add-source https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json `
        --version $version
    displayName: "Install test-proxy"
    condition: and(succeeded(), ${{ parameters.condition }})
  - pwsh: |
      Write-Host "Prepending path with the test proxy tool install location: '$(Build.BinariesDirectory)/test-proxy'"
      Write-Host "##vso[task.prependpath]$(Build.BinariesDirectory)/test-proxy"
    displayName: "Prepend path with test-proxy tool install location"
  - ${{ if eq(parameters.runProxy, 'true') }}:
    - pwsh: |
        Write-Host "Setting ASPNETCORE_Kestrel__Certificates__Default__Path to '${{ parameters.templateRoot }}/eng/common/testproxy/dotnet-devcert.pfx'"
        Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Path]${{ parameters.templateRoot }}/eng/common/testproxy/dotnet-devcert.pfx"
        Write-Host "Setting ASPNETCORE_Kestrel__Certificates__Default__Password to 'password'"
        Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Password]password"
        Write-Host "Setting PROXY_MANUAL_START to 'true'"
        Write-Host "##vso[task.setvariable variable=PROXY_MANUAL_START]true"
      displayName: 'Configure Kestrel and PROXY_MANUAL_START Variables'
      condition: and(succeeded(), ${{ parameters.condition }})
    - pwsh: |
        $invocation = @"
        Start-Process $(Build.BinariesDirectory)/test-proxy/test-proxy.exe
          -ArgumentList `"start -u --storage-location ${{ parameters.rootFolder }}`"
          -NoNewWindow -PassThru -RedirectStandardOutput ${{ parameters.rootFolder }}/test-proxy.log
          -RedirectStandardError ${{ parameters.rootFolder }}/test-proxy-error.log
        "@
        Write-Host $invocation
        $Process = Start-Process $(Build.BinariesDirectory)/test-proxy/test-proxy.exe `
          -ArgumentList "start -u --storage-location ${{ parameters.rootFolder }}" `
          -NoNewWindow -PassThru -RedirectStandardOutput ${{ parameters.rootFolder }}/test-proxy.log `
          -RedirectStandardError ${{ parameters.rootFolder }}/test-proxy-error.log
        Write-Host "Setting PROXY_PID to $($Process.Id)"
        Write-Host "##vso[task.setvariable variable=PROXY_PID]$($Process.Id)"
      displayName: 'Run the testproxy - windows'
      condition: and(succeeded(), eq(variables['Agent.OS'],'Windows_NT'), ${{ parameters.condition }})
      env:
        DOTNET_ROLL_FORWARD: 'Major'
    # nohup does NOT continue beyond the current session if you use it within powershell
    - bash: |
        nohup $(Build.BinariesDirectory)/test-proxy/test-proxy 1>${{ parameters.rootFolder }}/test-proxy.log 2>${{ parameters.rootFolder }}/test-proxy-error.log &
        echo $! > $(Build.SourcesDirectory)/test-proxy.pid
        echo "Setting PROXY_PID to $(cat $(Build.SourcesDirectory)/test-proxy.pid)"
        echo "##vso[task.setvariable variable=PROXY_PID]$(cat $(Build.SourcesDirectory)/test-proxy.pid)"
      displayName: "Run the testproxy - linux/mac"
      condition: and(succeeded(), ne(variables['Agent.OS'],'Windows_NT'), ${{ parameters.condition }})
      workingDirectory: "${{ parameters.rootFolder }}"
      env:
        DOTNET_ROLL_FORWARD: 'Major'
    - pwsh: |
        for ($i = 0; $i -lt 10; $i++) {
            try {
                Write-Host "Invoke-WebRequest -Uri `"http://localhost:5000/Admin/IsAlive`" | Out-Null"
                Invoke-WebRequest -Uri "http://localhost:5000/Admin/IsAlive" | Out-Null
                Write-Host "Successfully connected to the test proxy on port 5000."
                exit 0
            } catch {
                Write-Warning "Failed to successfully connect to test proxy. Retrying..."
                Start-Sleep 6
            }
        }
        Write-Error "Could not connect to test proxy."
        exit 1
      displayName: Test Proxy IsAlive
      condition: and(succeeded(), ${{ parameters.condition }})
 |