Skip to content

Commit fb547ae

Browse files
azure-sdkhallipr
andauthored
Sync eng/common directory with azure-sdk-tools for PR 10532 (#2606)
* Allow for empty serviceDirectory in deploy-test-resources * Revert some unintentional changes * Restore missing comment --------- Co-authored-by: Patrick Hallisey <[email protected]>
1 parent d56b2bf commit fb547ae

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ param (
1818
[ValidatePattern('^[-\w\._\(\)]+$')]
1919
[string] $ResourceGroupName,
2020

21-
[Parameter(Mandatory = $true, Position = 0)]
21+
[Parameter(Position = 0)]
2222
[string] $ServiceDirectory,
2323

2424
[Parameter()]
@@ -159,10 +159,13 @@ if ($initialContext) {
159159

160160
# try..finally will also trap Ctrl+C.
161161
try {
162-
163162
# Enumerate test resources to deploy. Fail if none found.
164-
$repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
165-
$root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path
163+
$root = $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
164+
165+
if($ServiceDirectory) {
166+
$root = "$repositoryRoot/sdk/$ServiceDirectory" | Resolve-Path
167+
}
168+
166169
if ($TestResourcesDirectory) {
167170
$root = $TestResourcesDirectory | Resolve-Path
168171
# Add an explicit check below in case ErrorActionPreference is overridden and Resolve-Path doesn't stop execution
@@ -171,6 +174,7 @@ try {
171174
}
172175
Write-Verbose "Overriding test resources search directory to '$root'"
173176
}
177+
174178
$templateFiles = @()
175179

176180
"$ResourceType-resources.json", "$ResourceType-resources.bicep" | ForEach-Object {
@@ -192,7 +196,12 @@ try {
192196
exit
193197
}
194198

199+
# returns empty string if $ServiceDirectory is not set
195200
$serviceName = GetServiceLeafDirectoryName $ServiceDirectory
201+
202+
# in ci, random names are used
203+
# in non-ci, without BaseName, ResourceGroupName or ServiceDirectory, all invocations will
204+
# generate the same resource group name and base name for a given user
196205
$BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames `
197206
-baseNameDefault $BaseName `
198207
-resourceGroupNameDefault $ResourceGroupName `
@@ -296,7 +305,7 @@ try {
296305
}
297306
}
298307

299-
# This needs to happen after we set the TenantId but before we use the ResourceGroupName
308+
# This needs to happen after we set the TenantId but before we use the ResourceGroupName
300309
if ($wellKnownTMETenants.Contains($TenantId)) {
301310
# Add a prefix to the resource group name to avoid flagging the usages of local auth
302311
# See details at https://eng.ms/docs/products/onecert-certificates-key-vault-and-dsms/key-vault-dsms/certandsecretmngmt/credfreefaqs#how-can-i-disable-s360-reporting-when-testing-customer-facing-3p-features-that-depend-on-use-of-unsafe-local-auth
@@ -364,9 +373,10 @@ try {
364373
$ProvisionerApplicationOid = $sp.Id
365374
}
366375

367-
$tags = @{
368-
Owners = (GetUserName)
369-
ServiceDirectory = $ServiceDirectory
376+
$tags = @{ Owners = (GetUserName) }
377+
378+
if ($ServiceDirectory) {
379+
$tags['ServiceDirectory'] = $ServiceDirectory
370380
}
371381

372382
# Tag the resource group to be deleted after a certain number of hours.

eng/common/TestResources/Remove-TestResources.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,6 @@ $context = Get-AzContext
157157

158158
if (!$ResourceGroupName) {
159159
if ($CI) {
160-
if (!$ServiceDirectory) {
161-
Write-Warning "ServiceDirectory parameter is empty, nothing to remove"
162-
exit 0
163-
}
164160
$envVarName = (BuildServiceDirectoryPrefix (GetServiceLeafDirectoryName $ServiceDirectory)) + "RESOURCE_GROUP"
165161
$ResourceGroupName = [Environment]::GetEnvironmentVariable($envVarName)
166162
if (!$ResourceGroupName) {
@@ -221,7 +217,12 @@ if ($wellKnownSubscriptions.ContainsKey($subscriptionName)) {
221217
Log "Selected subscription '$subscriptionName'"
222218

223219
if ($ServiceDirectory) {
224-
$root = [System.IO.Path]::Combine("$PSScriptRoot/../../../sdk", $ServiceDirectory) | Resolve-Path
220+
$root = "$PSScriptRoot/../../.."
221+
if($ServiceDirectory) {
222+
$root = "$root/sdk/$ServiceDirectory"
223+
}
224+
$root = $root | Resolve-Path
225+
225226
$preRemovalScript = Join-Path -Path $root -ChildPath "remove-$ResourceType-resources-pre.ps1"
226227
if (Test-Path $preRemovalScript) {
227228
Log "Invoking pre resource removal script '$preRemovalScript'"

eng/common/TestResources/SubConfig-Helpers.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function BuildServiceDirectoryPrefix([string]$serviceName) {
2+
if(!$serviceName) {
3+
return ""
4+
}
25
$serviceName = $serviceName -replace '[\./\\]', '_'
36
return $serviceName.ToUpperInvariant() + "_"
47
}
@@ -32,10 +35,15 @@ function GetBaseAndResourceGroupNames(
3235
if ($CI) {
3336
$base = 't' + (New-Guid).ToString('n').Substring(0, 16)
3437
# Format the resource group name based on resource group naming recommendations and limitations.
35-
$generatedGroup = "rg-{0}-$base" -f ($serviceName -replace '[\.\\\/:]', '-').
36-
Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)).
37-
Trim('-').
38-
ToLowerInvariant()
38+
if ($serviceDirectoryName) {
39+
$generatedGroup = "rg-{0}-$base" -f ($serviceName -replace '[\.\\\/:]', '-').
40+
Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)).
41+
Trim('-').
42+
ToLowerInvariant()
43+
} else {
44+
$generatedGroup = "rg-$base"
45+
}
46+
3947
$group = $resourceGroupNameDefault ? $resourceGroupNameDefault : $generatedGroup
4048

4149
Log "Generated resource base name '$base' and resource group name '$group' for CI build"

eng/common/TestResources/deploy-test-resources.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
ServiceDirectory: not-set
2+
ServiceDirectory: ''
33
ArmTemplateParameters: '@{}'
44
DeleteAfterHours: 8
55
Location: ''

0 commit comments

Comments
 (0)