Skip to content

Commit

Permalink
Remove dependency on track 1 Storage mgmt lib (Azure#11188)
Browse files Browse the repository at this point in the history
* Remove dependency on track 1 Search mgmt lib

Fixes Azure#11088

* Centrally document live test resource management

Resolves PR feedback

* Sanitize (fake) secrets from output example
  • Loading branch information
heaths authored Apr 10, 2020
1 parent 203518c commit f24f39c
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 423 deletions.
20 changes: 7 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,13 @@ If for any reason there is an update to the build tools, you will then need to f

### Live testing

Live tests assume a live resource has been created and appropriate environment
variables have been set for the test process. To automate setting up live
resources we use created a script called `New-TestResources.ps1` that deploys
resources for a given service.
Before running or recording live tests you need to create
[live test resources](https://github.com/Azure/azure-sdk-for-net/blob/master/eng/common/TestResources/README.md).
If recording tests, secrets will be sanitized from saved recordings.
If you will be working on contributions over time, you should consider
persisting these variables.

To see what resources will be deployed for a live service, check the
`test-resources.json` ARM template files in the service you wish to deploy for
testing, for example `sdk\keyvault\test-resources.json`.

To deploy live resources for testing use the steps documented in [`Example 1 of New-TestResources.ps1`](eng/common/TestResources/New-TestResources.ps1.md#example-1)
to set up a service principal and deploy live testing resources.

To run live tests after deploying live resources:
To run live tests after creating live resources:

1. Open Developer Command Prompt
2. Navigate to service directory e.g. _"sdk\keyvault"_
Expand Down Expand Up @@ -168,7 +162,7 @@ Follow instructions provided [here](https://docs.microsoft.com/en-us/nuget/consu

You can also achieve this from the command line.

```nuget.exe sources add -Name Azure SDK for Net Dev Feed -Source https://azuresdkartifacts.blob.core.windows.net/azure-sdk-for-net/index.json```
```nuget.exe sources add -Name "Azure SDK for Net Dev Feed" -Source "https://azuresdkartifacts.blob.core.windows.net/azure-sdk-for-net/index.json"```

You can then consume packages from this package source, remember to check the [Include prerelease](https://docs.microsoft.com/en-us/nuget/create-packages/prerelease-packages#installing-and-updating-pre-release-packages) box in Visual Studio when searching for the packages.

Expand Down
10 changes: 1 addition & 9 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ Force creation of resources instead of being prompted.
Connect-AzAccount -Subscription "REPLACE_WITH_SUBSCRIPTION_ID"
$testAadApp = New-AzADServicePrincipal -Role Owner -DisplayName 'azure-sdk-live-test-app'
New-TestResources.ps1 `
-BaseName 'myalias' `
-BaseName 'uuid123' `
-ServiceDirectory 'keyvault' `
-TestApplicationId $testAadApp.ApplicationId.ToString() `
-TestApplicationSecret (ConvertFrom-SecureString $testAadApp.Secret -AsPlainText)
Expand Down Expand Up @@ -475,14 +475,6 @@ Run this in an Azure DevOps CI (with approrpiate variables configured) before
executing live tests. The script will output variables as secrets (to enable
log redaction).
.OUTPUTS
Entries from the ARM templates' "output" section in environment variable syntax
(e.g. $env:RESOURCE_NAME='<< resource name >>') that can be used for running
live tests.
If run in -CI mode the environment variables will be output in syntax that Azure
DevOps can consume.
.LINK
Remove-TestResources.ps1
#>
21 changes: 8 additions & 13 deletions eng/common/TestResources/New-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ specified in $ProvisionerApplicationId and $ProvisionerApplicationSecret.

### EXAMPLE 1
```
$subscriptionId = "REPLACE_WITH_SUBSCRIPTION_ID"
Connect-AzAccount -Subscription $subscriptionId
Connect-AzAccount -Subscription "REPLACE_WITH_SUBSCRIPTION_ID"
$testAadApp = New-AzADServicePrincipal -Role Owner -DisplayName 'azure-sdk-live-test-app'
.\eng\common\LiveTestResources\New-TestResources.ps1 `
-BaseName 'myalias' `
New-TestResources.ps1 `
-BaseName 'uuid123' `
-ServiceDirectory 'keyvault' `
-TestApplicationId $testAadApp.ApplicationId.ToString() `
-TestApplicationSecret (ConvertFrom-SecureString $testAadApp.Secret -AsPlainText)
Expand All @@ -71,7 +70,7 @@ the SecureString to plaintext by another means.

### EXAMPLE 2
```
eng/New-TestResources.ps1 `
New-TestResources.ps1 `
-BaseName 'Generated' `
-ServiceDirectory '$(ServiceDirectory)' `
-TenantId '$(TenantId)' `
Expand Down Expand Up @@ -215,7 +214,7 @@ Accept wildcard characters: False
```
### -SubscriptionId
Optional subscription ID to use for new resources when logging in as a
Optional subscription ID to use for new resources when logging in as a
provisioner.
You can also use Set-AzContext if not provisioning.
Expand Down Expand Up @@ -416,16 +415,12 @@ Accept wildcard characters: False
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
## OUTPUTS
### Entries from the ARM templates' "output" section in environment variable syntax
### (e.g. $env:RESOURCE_NAME='<< resource name >>') that can be used for running
### live tests.
### If run in -CI mode the environment variables will be output in syntax that Azure
### DevOps can consume.
## NOTES
## RELATED LINKS
[Remove-TestResources.ps1](./New-TestResources.ps1.md)
[Remove-TestResources.ps1](./Remove-TestResources.ps1.md)
117 changes: 100 additions & 17 deletions eng/common/TestResources/README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,112 @@
# Live Test Resource Management

Live test runs require pre-existing resources in Azure. This set of PowerShell
commands automates creation and teardown of live test resources for Desktop and
CI scenarios.
Running and recording live tests often requires first creating some resources
in Azure. Service directories that include a test-resources.yml file require
running [New-TestResources.ps1][] to create these resources and output
environment variables you must set.

* [New-TestResources.ps1](./New-TestResources.ps1.md) - Create new test resources
for the given service.
* [Remove-TestResources.ps1](./New-TestResources.ps1.md) - Deletes resources
The following scripts can be used both in on your desktop for developer
scenarios as well as on hosted agents for continuous integration testing.

* [New-TestResources.ps1][] - Creates new test resources for a given service.
* [Remove-TestResources.ps1][] - Deletes previously created resources.

## On the Desktop

Run `New-TestResources.ps1` on your desktop to create live test resources for a
given service (e.g. Storage, Key Vault, etc.). The command will output
environment variables that need to be set when running the live tests.
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.

Note that `-Subscription` is an optional parameter but recommended if your account
is a member of multiple subscriptions.

```powershell
Connect-AzAccount -Subscription 'YOUR SUBSCRIPTION ID'
$sp = New-AzADServicePrincipal -Role Owner
eng\common\TestResources\New-TestResources.ps1 `
-BaseName 'myusername' `
-ServiceDirectory 'search' `
-TestApplicationId $sp.ApplicationId `
-TestApplicationSecret (ConvertFrom-SecureString $sp.Secret -AsPlainText)
```

Along with some log messages, this will output environment variables based on
your current shell like in the following example:

```powershell
$env:AZURE_TENANT_ID = '<<secret>>'
$env:AZURE_CLIENT_ID = '<<secret>>'
$env:AZURE_CLIENT_SECRET = '<<secret>>'
$env:AZURE_SUBSCRIPTION_ID = 'YOUR SUBSCRIPTION ID'
$env:AZURE_RESOURCE_GROUP = 'rg-myusername'
$env:AZURE_LOCATION = 'westus2'
$env:AZURE_SEARCH_STORAGE_NAME = 'myusernamestg'
$env:AZURE_SEARCH_STORAGE_KEY = '<<secret>>'
```

For security reasons we do not set these environment variables automatically
for either the current process or persistently for future sessions. You must
do that yourself based on your current platform and shell.

If your current shell was detected properly, you should be able to copy and
paste the output directly in your terminal and add to your profile script.
For example, in PowerShell on Windows you can copy the output above and paste
it back into the terminal to set those environment variables for the current
process. To persist these variables for future terminal sessions or for
applications started outside the terminal, you could copy and paste the
following commands:

```powershell
setx AZURE_TENANT_ID $env:AZURE_TENANT_ID
setx AZURE_CLIENT_ID $env:AZURE_CLIENT_ID
setx AZURE_CLIENT_SECRET $env:AZURE_CLIENT_SECRET
setx AZURE_SUBSCRIPTION_ID $env:AZURE_SUBSCRIPTION_ID
setx AZURE_RESOURCE_GROUP $env:AZURE_RESOURCE_GROUP
setx AZURE_LOCATION $env:AZURE_LOCATION
setx AZURE_SEARCH_STORAGE_NAME $env:AZURE_SEARCH_STORAGE_NAME
setx AZURE_SEARCH_STORAGE_KEY $env:AZURE_SEARCH_STORAGE_KEY
```

After running or recording live tests, if you do not plan on further testing
you can remove the test resources you created above by running
[Remove-TestResources.ps1][]:

See examples for how to create the needed Service Principals and execute live
tests.
```powershell
Remove-TestResources.ps1 -BaseName 'myusername' -Force
```

If you persisted environment variables, you should also remove those as well.

## In CI

The `New-TestResources.ps1` script is invoked on each test job to create an
isolated environment for live tests. Test resource isolation makes it easier to
parallelize test runs.
Test pipelines should include deploy-test-resources.yml and
remove-test-resources.yml like in the following examples:

```yml
- template: /eng/common/TestResources/deploy-test-resources.yml
parameters:
ServiceDirectory: '${{ parameters.ServiceDirectory }}'

# Run tests

- template: /eng/common/TestResources/remove-test-resources.yml
```
Be sure to link the **Secrets for Resource Provisioner** variable group
into the test pipeline for these scripts to work.
## Documentation
To regenerate documentation for scripts within this directory, you can install
[platyPS][] and run it like in the following example:
```powershell
Install-Module platyPS -Scope CurrentUser -Force
New-MarkdownHelp -Command .\New-TestResources.ps1 -OutputFolder . -Force
```

## Other
PowerShell markdown documentation created with [platyPS][].

PowerShell markdown documentation created with
[PlatyPS](https://github.com/PowerShell/platyPS)
[New-TestResources.ps1]: ./New-TestResources.ps1.md
[Remove-TestResources.ps1]: ./Remove-TestResources.ps1.md
[platyPS]: https://github.com/PowerShell/platyPS
2 changes: 1 addition & 1 deletion eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Name of the cloud environment. The default is the Azure Public Cloud
Force removal of resource group without asking for user confirmation
.EXAMPLE
./Remove-TestResources.ps1 -BaseName uuid123 -Force
Remove-TestResources.ps1 -BaseName 'uuid123' -Force
Use the currently logged-in account to delete the resource group by the name of
'rg-uuid123'
Expand Down
13 changes: 9 additions & 4 deletions eng/common/TestResources/Remove-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ create resources.

### EXAMPLE 1
```
./Remove-TestResources.ps1 -BaseName uuid123 -Force
Remove-TestResources.ps1 -BaseName 'uuid123' -Force
```

Use the currently logged-in account to delete the resource group by the name of
'rg-uuid123'

### EXAMPLE 2
```
eng/Remove-TestResources.ps1 `
Remove-TestResources.ps1 `
-ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" `
-TenantId '$(TenantId)' `
-ProvisionerApplicationId '$(AppId)' `
Expand Down Expand Up @@ -123,7 +123,7 @@ Accept wildcard characters: False
```
### -SubscriptionId
Optional subscription ID to use for new resources when logging in as a
Optional subscription ID to use for new resources when logging in as a
provisioner.
You can also use Set-AzContext if not provisioning.
Expand Down Expand Up @@ -235,7 +235,12 @@ Accept wildcard characters: False
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## INPUTS
## OUTPUTS
## NOTES
## RELATED LINKS
[New-TestResources.ps1](./New-TestResources.ps1.md)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Microsoft.Azure.Management.Search" />
<PackageReference Include="Microsoft.Azure.Management.Storage" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" />

<!-- TODO: Remove this when https://github.com/Azure/azure-sdk-for-net/issues/10592 is resolved. -->
Expand Down
Loading

0 comments on commit f24f39c

Please sign in to comment.