Skip to content

Commit 3210b4a

Browse files
azure-sdkheaths
andauthored
Sync eng/common directory with azure-sdk-tools for PR 1408 (Azure#18745)
* Improve TestResources docs and logging Resolves #1388 Resolves #1407 Also ignores cached service principal if it no longer exists. I ran into this while testing since I cleaned up old SPs. * Add ADP test sub to look-up Co-authored-by: Heath Stewart <heaths@microsoft.com>
1 parent df95879 commit 3210b4a

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,32 @@ try {
200200
# Make sure the user is logged in to create a service principal.
201201
$context = Get-AzContext;
202202
if (!$context) {
203-
Log "You are not logged in; connecting to 'Azure SDK Developer Playground'"
203+
$subscriptionName = $SubscriptionId
204+
205+
# Use cache of well-known team subs without having to be authenticated.
206+
$wellKnownSubscriptions = @{
207+
'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground'
208+
'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System'
209+
'2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources'
210+
}
211+
212+
if ($wellKnownSubscriptions.ContainsKey($SubscriptionId)) {
213+
$subscriptionName = '{0} ({1})' -f $wellKnownSubscriptions[$SubscriptionId], $SubscriptionId
214+
}
215+
216+
Log "You are not logged in; connecting to $subscriptionName"
204217
$context = (Connect-AzAccount -Subscription $SubscriptionId).Context
205218
}
206219

207220
# If no test application ID is specified during an interactive session, create a new service principal.
208221
if (!$TestApplicationId) {
209222

210223
# Cache the created service principal in this session for frequent reuse.
211-
$servicePrincipal = if ($AzureTestPrincipal) {
212-
Log "TestApplicationId was not specified; loading the cached service principal"
224+
$servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.ApplicationId)) {
225+
Log "TestApplicationId was not specified; loading cached service principal '$($AzureTestPrincipal.ApplicationId)'"
213226
$AzureTestPrincipal
214227
} else {
215-
Log "TestApplicationId was not specified; creating a new service principal"
228+
Log 'TestApplicationId was not specified; creating a new service principal'
216229
$global:AzureTestPrincipal = New-AzADServicePrincipal -Role Owner
217230

218231
Log "Created service principal '$AzureTestPrincipal'"

eng/common/TestResources/README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ scenarios as well as on hosted agents for continuous integration testing.
1919
## On the Desktop
2020

2121
To set up your Azure account to run live tests, you'll need to log into Azure,
22-
create a service principal, and set up your resources defined in
23-
test-resources.json as shown in the following example using Azure Search.
22+
and set up your resources defined in test-resources.json as shown in the following
23+
example using Azure Search. The script will create a service principal automatically,
24+
or you may create a service principal you can save and reuse subsequently.
2425

2526
Note that `-Subscription` is an optional parameter but recommended if your account
2627
is a member of multiple subscriptions.
@@ -80,13 +81,6 @@ you can remove the test resources you created above by running:
8081
Remove-TestResources.ps1 -BaseName 'myusername' -Force
8182
```
8283

83-
If you created a new service principal as shown above, you might also remove it:
84-
85-
```powershell
86-
Remove-AzADServicePrincipal -ApplicationId $sp.ApplicationId -Force
87-
88-
```
89-
9084
If you persisted environment variables, you should also remove those as well.
9185

9286
Some test-resources.json templates utilize the `AdditionalParameters` parameter to control additional resource configuration options. For example:

0 commit comments

Comments
 (0)