diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 1e28cdf10..500bd89d0 100755 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -18,7 +18,7 @@ param ( [ValidatePattern('^[-\w\._\(\)]+$')] [string] $ResourceGroupName, - [Parameter(Mandatory = $true, Position = 0)] + [Parameter(Position = 0)] [string] $ServiceDirectory, [Parameter()] @@ -159,10 +159,13 @@ if ($initialContext) { # try..finally will also trap Ctrl+C. try { - # Enumerate test resources to deploy. Fail if none found. - $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path - $root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path + $root = $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path + + if($ServiceDirectory) { + $root = "$repositoryRoot/sdk/$ServiceDirectory" | Resolve-Path + } + if ($TestResourcesDirectory) { $root = $TestResourcesDirectory | Resolve-Path # Add an explicit check below in case ErrorActionPreference is overridden and Resolve-Path doesn't stop execution @@ -171,6 +174,7 @@ try { } Write-Verbose "Overriding test resources search directory to '$root'" } + $templateFiles = @() "$ResourceType-resources.json", "$ResourceType-resources.bicep" | ForEach-Object { @@ -192,7 +196,12 @@ try { exit } + # returns empty string if $ServiceDirectory is not set $serviceName = GetServiceLeafDirectoryName $ServiceDirectory + + # in ci, random names are used + # in non-ci, without BaseName, ResourceGroupName or ServiceDirectory, all invocations will + # generate the same resource group name and base name for a given user $BaseName, $ResourceGroupName = GetBaseAndResourceGroupNames ` -baseNameDefault $BaseName ` -resourceGroupNameDefault $ResourceGroupName ` @@ -296,7 +305,7 @@ try { } } - # This needs to happen after we set the TenantId but before we use the ResourceGroupName + # This needs to happen after we set the TenantId but before we use the ResourceGroupName if ($wellKnownTMETenants.Contains($TenantId)) { # Add a prefix to the resource group name to avoid flagging the usages of local auth # 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 { $ProvisionerApplicationOid = $sp.Id } - $tags = @{ - Owners = (GetUserName) - ServiceDirectory = $ServiceDirectory + $tags = @{ Owners = (GetUserName) } + + if ($ServiceDirectory) { + $tags['ServiceDirectory'] = $ServiceDirectory } # Tag the resource group to be deleted after a certain number of hours. diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 4479d9783..232c86196 100755 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -157,10 +157,6 @@ $context = Get-AzContext if (!$ResourceGroupName) { if ($CI) { - if (!$ServiceDirectory) { - Write-Warning "ServiceDirectory parameter is empty, nothing to remove" - exit 0 - } $envVarName = (BuildServiceDirectoryPrefix (GetServiceLeafDirectoryName $ServiceDirectory)) + "RESOURCE_GROUP" $ResourceGroupName = [Environment]::GetEnvironmentVariable($envVarName) if (!$ResourceGroupName) { @@ -221,7 +217,12 @@ if ($wellKnownSubscriptions.ContainsKey($subscriptionName)) { Log "Selected subscription '$subscriptionName'" if ($ServiceDirectory) { - $root = [System.IO.Path]::Combine("$PSScriptRoot/../../../sdk", $ServiceDirectory) | Resolve-Path + $root = "$PSScriptRoot/../../.." + if($ServiceDirectory) { + $root = "$root/sdk/$ServiceDirectory" + } + $root = $root | Resolve-Path + $preRemovalScript = Join-Path -Path $root -ChildPath "remove-$ResourceType-resources-pre.ps1" if (Test-Path $preRemovalScript) { Log "Invoking pre resource removal script '$preRemovalScript'" diff --git a/eng/common/TestResources/SubConfig-Helpers.ps1 b/eng/common/TestResources/SubConfig-Helpers.ps1 index ab2344283..061160d59 100644 --- a/eng/common/TestResources/SubConfig-Helpers.ps1 +++ b/eng/common/TestResources/SubConfig-Helpers.ps1 @@ -1,4 +1,7 @@ function BuildServiceDirectoryPrefix([string]$serviceName) { + if(!$serviceName) { + return "" + } $serviceName = $serviceName -replace '[\./\\]', '_' return $serviceName.ToUpperInvariant() + "_" } @@ -32,10 +35,15 @@ function GetBaseAndResourceGroupNames( if ($CI) { $base = 't' + (New-Guid).ToString('n').Substring(0, 16) # Format the resource group name based on resource group naming recommendations and limitations. - $generatedGroup = "rg-{0}-$base" -f ($serviceName -replace '[\.\\\/:]', '-'). - Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)). - Trim('-'). - ToLowerInvariant() + if ($serviceDirectoryName) { + $generatedGroup = "rg-{0}-$base" -f ($serviceName -replace '[\.\\\/:]', '-'). + Substring(0, [Math]::Min($serviceDirectoryName.Length, 90 - $base.Length - 4)). + Trim('-'). + ToLowerInvariant() + } else { + $generatedGroup = "rg-$base" + } + $group = $resourceGroupNameDefault ? $resourceGroupNameDefault : $generatedGroup Log "Generated resource base name '$base' and resource group name '$group' for CI build" diff --git a/eng/common/TestResources/deploy-test-resources.yml b/eng/common/TestResources/deploy-test-resources.yml index 9a4887622..e64404e7c 100644 --- a/eng/common/TestResources/deploy-test-resources.yml +++ b/eng/common/TestResources/deploy-test-resources.yml @@ -1,5 +1,5 @@ parameters: - ServiceDirectory: not-set + ServiceDirectory: '' ArmTemplateParameters: '@{}' DeleteAfterHours: 8 Location: ''