Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 1408 (#14304)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
azure-sdk and heaths committed Feb 13, 2021
1 parent ce7b7b4 commit 3ddb9e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 17 additions & 4 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,32 @@ 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

# Use cache of well-known team subs without having to be authenticated.
$wellKnownSubscriptions = @{
'faa080af-c1d8-40ad-9cce-e1a450ca5b57' = 'Azure SDK Developer Playground'
'a18897a6-7e44-457d-9260-f2854c0aca42' = 'Azure SDK Engineering System'
'2cd617ea-1866-46b1-90e3-fffb087ebf9b' = 'Azure SDK Test Resources'
}

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
}

# If no test application ID is specified during an interactive session, create a new service principal.
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'"
Expand Down
12 changes: 3 additions & 9 deletions eng/common/TestResources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3ddb9e2

Please sign in to comment.