From 5210a490f5071a51fa965cfd3873385d9fff1c48 Mon Sep 17 00:00:00 2001 From: Heath Stewart Date: Fri, 12 Feb 2021 15:44:54 -0800 Subject: [PATCH] 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. --- eng/common/TestResources/New-TestResources.ps1 | 18 ++++++++++++++---- eng/common/TestResources/README.md | 12 +++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 2692fee56972..cb8a2f46a1db 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -200,7 +200,17 @@ try { # Make sure the user is logged in to create a service principal. $context = Get-AzContext; if (!$context) { - Log "You are not logged in; connecting to 'Azure SDK Developer Playground'" + $subscriptionName = $SubscriptionId + $wellKnownSubscriptions = @{ + 'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground' + 'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System' + } + + if ($wellKnownSubscriptions.ContainsKey($SubscriptionId)) { + $subscriptionName = '{0} ({1})' -f $wellKnownSubscriptions[$SubscriptionId], $SubscriptionId + } + + Log "You are not logged in; connecting to $subscriptionName" $context = (Connect-AzAccount -Subscription $SubscriptionId).Context } @@ -208,11 +218,11 @@ try { if (!$TestApplicationId) { # Cache the created service principal in this session for frequent reuse. - $servicePrincipal = if ($AzureTestPrincipal) { - Log "TestApplicationId was not specified; loading the cached service principal" + $servicePrincipal = if ($AzureTestPrincipal -and (Get-AzADServicePrincipal -ApplicationId $AzureTestPrincipal.ApplicationId)) { + Log "TestApplicationId was not specified; loading cached service principal '$($AzureTestPrincipal.ApplicationId)'" $AzureTestPrincipal } else { - Log "TestApplicationId was not specified; creating a new service principal" + Log 'TestApplicationId was not specified; creating a new service principal' $global:AzureTestPrincipal = New-AzADServicePrincipal -Role Owner Log "Created service principal '$AzureTestPrincipal'" diff --git a/eng/common/TestResources/README.md b/eng/common/TestResources/README.md index 3aa1720107e3..7502503aea48 100644 --- a/eng/common/TestResources/README.md +++ b/eng/common/TestResources/README.md @@ -19,8 +19,9 @@ scenarios as well as on hosted agents for continuous integration testing. ## On the Desktop To set up your Azure account to run live tests, you'll need to log into Azure, -create a service principal, and set up your resources defined in -test-resources.json as shown in the following example using Azure Search. +and set up your resources defined in test-resources.json as shown in the following +example using Azure Search. The script will create a service principal automatically, +or you may create a service principal you can save and reuse subsequently. Note that `-Subscription` is an optional parameter but recommended if your account is a member of multiple subscriptions. @@ -80,13 +81,6 @@ you can remove the test resources you created above by running: Remove-TestResources.ps1 -BaseName 'myusername' -Force ``` -If you created a new service principal as shown above, you might also remove it: - -```powershell -Remove-AzADServicePrincipal -ApplicationId $sp.ApplicationId -Force - -``` - If you persisted environment variables, you should also remove those as well. Some test-resources.json templates utilize the `AdditionalParameters` parameter to control additional resource configuration options. For example: