Skip to content

Commit 61abca2

Browse files
azure-sdkscbedd
andauthored
Sync eng/common directory with azure-sdk-tools for PR 8549 (#36321)
* commit the file changes so that we can see them running * use standalone tool --------- Co-authored-by: Scott Beddall (from Dev Box) <[email protected]>
1 parent 174849a commit 61abca2

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Installs a standalone version of the test-proxy for use in tests. This function is intended to be used in CI/CD pipelines, and leaves behind
4+
the pipeline variable PROXY_EXE which contains the path to the test-proxy executable.
5+
6+
.PARAMETER Version
7+
The version of the proxy to install. Requires a full version to be provided. EG "1.0.0-dev.20240617.1"
8+
9+
.PARAMETER Directory
10+
The directory within which the test-proxy exe will exist after this function invokes. Defaults to CWD.
11+
#>
12+
param(
13+
[Parameter(Mandatory = $true)]
14+
$Version,
15+
[Parameter(Mandatory = $true)]
16+
$InstallDirectory
17+
)
18+
19+
. (Join-Path $PSScriptRoot test-proxy.ps1)
20+
21+
Write-Host "Attempting to download and install version `"$Version`" into `"$InstallDirectory`""
22+
23+
Install-Standalone-TestProxy -Version $Version -Directory $InstallDirectory
24+
25+
$PROXY_EXE = ""
26+
27+
if ($IsWindows) {
28+
$PROXY_EXE = Join-Path $InstallDirectory "Azure.Sdk.Tools.TestProxy.exe"
29+
} else {
30+
$PROXY_EXE = Join-Path $InstallDirectory "Azure.Sdk.Tools.TestProxy"
31+
}
32+
Write-Host "Downloaded test-proxy available at $PROXY_EXE."
33+
Write-Host "##vso[task.setvariable variable=PROXY_EXE]$PROXY_EXE"
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This template sets variable PROXY_PID to be used for shutdown later.
2+
parameters:
3+
rootFolder: '$(Build.SourcesDirectory)'
4+
runProxy: true
5+
targetVersion: ''
6+
templateRoot: '$(Build.SourcesDirectory)'
7+
condition: true
8+
9+
steps:
10+
- pwsh: |
11+
${{ parameters.templateRoot }}/eng/common/scripts/trust-proxy-certificate.ps1
12+
displayName: 'Language Specific Certificate Trust'
13+
condition: and(succeeded(), ${{ parameters.condition }})
14+
15+
- task: PowerShell@2
16+
displayName: 'Override proxy version if necessary'
17+
condition: and(succeeded(), ${{ parameters.condition }}, ne('${{ parameters.targetVersion }}', ''))
18+
inputs:
19+
targetType: filePath
20+
filePath: '${{ parameters.templateRoot }}/eng/common/testproxy/scripts/override-proxy-version.ps1'
21+
arguments: '-TargetVersion "${{ parameters.targetVersion }}"'
22+
pwsh: true
23+
24+
- pwsh: |
25+
$standardVersion = "${{ parameters.templateRoot }}/eng/common/testproxy/target_version.txt"
26+
$overrideVersion = "${{ parameters.templateRoot }}/eng/target_proxy_version.txt"
27+
28+
$version = $(Get-Content $standardVersion -Raw).Trim()
29+
30+
if (Test-Path $overrideVersion) {
31+
$version = $(Get-Content $overrideVersion -Raw).Trim()
32+
}
33+
34+
Write-Host "Installing test-proxy version $version"
35+
${{ parameters.templateRoot }}/eng/common/testproxy/install-test-proxy.ps1 -Version $version -InstallDirectory $(Build.BinariesDirectory)/test-proxy
36+
displayName: "Install test-proxy"
37+
condition: and(succeeded(), ${{ parameters.condition }})
38+
39+
- pwsh: |
40+
Write-Host "##vso[task.prependpath]$(Build.BinariesDirectory)/test-proxy"
41+
displayName: "Prepend path with test-proxy tool install location"
42+
43+
- ${{ if eq(parameters.runProxy, 'true') }}:
44+
- pwsh: |
45+
Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Path]${{ parameters.templateRoot }}/eng/common/testproxy/dotnet-devcert.pfx"
46+
Write-Host "##vso[task.setvariable variable=ASPNETCORE_Kestrel__Certificates__Default__Password]password"
47+
Write-Host "##vso[task.setvariable variable=PROXY_MANUAL_START]true"
48+
displayName: 'Configure Kestrel and PROXY_MANUAL_START Variables'
49+
condition: and(succeeded(), ${{ parameters.condition }})
50+
51+
- pwsh: |
52+
$Process = Start-Process $(PROXY_EXE) `
53+
-ArgumentList "start -u --storage-location ${{ parameters.rootFolder }}" `
54+
-NoNewWindow -PassThru -RedirectStandardOutput ${{ parameters.rootFolder }}/test-proxy.log
55+
56+
Write-Host "##vso[task.setvariable variable=PROXY_PID]$($Process.Id)"
57+
displayName: 'Run the testproxy - windows'
58+
condition: and(succeeded(), eq(variables['Agent.OS'],'Windows_NT'), ${{ parameters.condition }})
59+
60+
# nohup does NOT continue beyond the current session if you use it within powershell
61+
- bash: |
62+
nohup $(PROXY_EXE) &>${{ parameters.rootFolder }}/test-proxy.log &
63+
64+
echo $! > $(Build.SourcesDirectory)/test-proxy.pid
65+
echo "##vso[task.setvariable variable=PROXY_PID]$(cat $(Build.SourcesDirectory)/test-proxy.pid)"
66+
displayName: "Run the testproxy - linux/mac"
67+
condition: and(succeeded(), ne(variables['Agent.OS'],'Windows_NT'), ${{ parameters.condition }})
68+
workingDirectory: "${{ parameters.rootFolder }}"
69+
70+
- pwsh: |
71+
for ($i = 0; $i -lt 10; $i++) {
72+
try {
73+
Invoke-WebRequest -Uri "http://localhost:5000/Admin/IsAlive" | Out-Null
74+
exit 0
75+
} catch {
76+
Write-Warning "Failed to successfully connect to test proxy. Retrying..."
77+
Start-Sleep 6
78+
}
79+
}
80+
Write-Error "Could not connect to test proxy."
81+
exit 1
82+
displayName: Test Proxy IsAlive
83+
condition: and(succeeded(), ${{ parameters.condition }})
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
Set-StrictMode -Version 4
2+
$AVAILABLE_TEST_PROXY_BINARIES = @{
3+
"Windows" = @{
4+
"AMD64" = @{
5+
"system" = "Windows"
6+
"machine" = "AMD64"
7+
"file_name" = "test-proxy-standalone-win-x64.zip"
8+
"executable" = "Azure.Sdk.Tools.TestProxy.exe"
9+
}
10+
}
11+
"Linux" = @{
12+
"X86_64" = @{
13+
"system" = "Linux"
14+
"machine" = "X86_64"
15+
"file_name" = "test-proxy-standalone-linux-x64.tar.gz"
16+
"executable" = "Azure.Sdk.Tools.TestProxy"
17+
}
18+
"ARM64" = @{
19+
"system" = "Linux"
20+
"machine" = "ARM64"
21+
"file_name" = "test-proxy-standalone-linux-arm64.tar.gz"
22+
"executable" = "Azure.Sdk.Tools.TestProxy"
23+
}
24+
}
25+
"Darwin" = @{
26+
"X86_64" = @{
27+
"system" = "Darwin"
28+
"machine" = "X86_64"
29+
"file_name" = "test-proxy-standalone-osx-x64.zip"
30+
"executable" = "Azure.Sdk.Tools.TestProxy"
31+
}
32+
"ARM64" = @{
33+
"system" = "Darwin"
34+
"machine" = "ARM64"
35+
"file_name" = "test-proxy-standalone-osx-arm64.zip"
36+
"executable" = "Azure.Sdk.Tools.TestProxy"
37+
}
38+
}
39+
}
40+
41+
function Get-SystemArchitecture {
42+
$unameOutput = uname -m
43+
switch ($unameOutput) {
44+
"x86_64" { return "X86_64" }
45+
"aarch64" { return "ARM64" }
46+
"arm64" { return "ARM64" }
47+
default { throw "Unable to determine system architecture. uname -m returned $unameOutput." }
48+
}
49+
}
50+
51+
function Get-Proxy-Meta () {
52+
$ErrorActionPreferenceDefault = $ErrorActionPreference
53+
$ErrorActionPreference = "Stop"
54+
55+
$os = "unknown"
56+
$machine = Get-SystemArchitecture
57+
58+
if ($IsWindows) {
59+
$os = "Windows"
60+
# we only support x64 on windows, if that doesn't work the platform is unsupported
61+
$machine = "AMD64"
62+
} elseif ($IsLinux) {
63+
$os = "Linux"
64+
} elseif ($IsMacOS) {
65+
$os = "Darwin"
66+
}
67+
68+
$ErrorActionPreference = $ErrorActionPreferenceDefault
69+
70+
return $AVAILABLE_TEST_PROXY_BINARIES[$os][$machine]
71+
}
72+
73+
function Get-Proxy-Url (
74+
[Parameter(mandatory=$true)]$Version
75+
) {
76+
$systemDetails = Get-Proxy-Meta
77+
78+
$file = $systemDetails.file_name
79+
$url = "https://github.com/Azure/azure-sdk-tools/releases/download/Azure.Sdk.Tools.TestProxy_$Version/$file"
80+
81+
return $url
82+
}
83+
84+
function Cleanup-Directory ($path) {
85+
if (Test-Path -Path $path) {
86+
Remove-Item -Path $path -Recurse -Force
87+
}
88+
New-Item -ItemType Directory -Path $path -Force
89+
}
90+
91+
function Is-Work-Necessary (
92+
[Parameter(mandatory=$true)]
93+
$Version,
94+
[Parameter(mandatory=$true)]
95+
$Directory
96+
) {
97+
$savedVersionTxt = Join-Path $Directory "downloaded_version.txt"
98+
if (Test-Path $savedVersionTxt) {
99+
$result = (Get-Content -Raw $savedVersionTxt).Trim()
100+
101+
if ($result -eq $Version) {
102+
return $false
103+
}
104+
}
105+
106+
return $true
107+
}
108+
109+
<#
110+
.SYNOPSIS
111+
Installs a standalone version of the test-proxy.
112+
.PARAMETER Version
113+
The version of the proxy to install. Requires a full version to be provided. EG "1.0.0-dev.20240617.1"
114+
.PARAMETER Directory
115+
The directory within which the test-proxy exe will exist after this function invokes. Defaults to "."
116+
#>
117+
function Install-Standalone-TestProxy (
118+
[Parameter(mandatory=$true)]
119+
$Version,
120+
$Directory="."
121+
) {
122+
$ErrorActionPreference = "Stop"
123+
$systemDetails = Get-Proxy-Meta
124+
125+
if (!(Test-Path $Directory) -and $Directory -ne ".") {
126+
New-Item -ItemType Directory -Path $Directory -Force
127+
}
128+
129+
$downloadFolder = Resolve-Path $Directory
130+
$downloadUrl = Get-Proxy-Url $Version
131+
$downloadFile = $downloadUrl.Split('/')[-1]
132+
$downloadLocation = Join-Path $downloadFolder $downloadFile
133+
$savedVersionTxt = Join-Path $downloadFolder "downloaded_version.txt"
134+
135+
if (Is-Work-Necessary $version $downloadFolder) {
136+
Write-Host "Commencing installation of `"$Version`" to `"$downloadFolder`" from $downloadUrl."
137+
Invoke-WebRequest -Uri $downloadUrl -OutFile $downloadLocation
138+
139+
if ($downloadFile -like "*.zip") {
140+
Expand-Archive -Path $downloadLocation -DestinationPath $downloadFolder -Force
141+
} elseif ($downloadFile -like "*.tar.gz") {
142+
tar -xzf $downloadLocation -C $downloadFolder
143+
} else {
144+
throw "Unsupported file format"
145+
}
146+
147+
# Remove the downloaded file after extraction
148+
Remove-Item -Path $downloadLocation -Force
149+
150+
# Record downloaded version
151+
Set-Content -Path $savedVersionTxt -Value $Version
152+
153+
# Set executable permissions if on macOS (Darwin)
154+
$executable_path = Join-Path $downloadFolder $systemDetails.executable
155+
if ($IsMacOS) {
156+
chmod 755 $executable_path
157+
}
158+
}
159+
else {
160+
Write-Host "Target version `"$Version`" already present in target directory `"$downloadFolder.`""
161+
}
162+
}

0 commit comments

Comments
 (0)