• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

AzurePowerShellV4/H18-Mar-2021-154134

Import-AzModules.ps1H A D18-Mar-2021525 128

New-TestResources.cmdH A D18-Mar-2021408 1812

New-TestResources.ps1H A D18-Mar-202126.9 KiB691376

New-TestResources.ps1.mdH A D18-Mar-202111.5 KiB489390

README.mdH A D18-Mar-20215.1 KiB12795

Remove-TestResources.cmdH A D18-Mar-2021408 1812

Remove-TestResources.ps1H A D18-Mar-20218 KiB218120

Remove-TestResources.ps1.mdH A D18-Mar-20216.4 KiB275221

deploy-test-resources.ymlH A D18-Mar-20212.3 KiB5952

remove-test-resources.ymlH A D18-Mar-20211.5 KiB3933

setup-az-modules.ymlH A D18-Mar-20211.3 KiB3531

README.md

1# Live Test Resource Management
2
3Running and recording live tests often requires first creating some resources
4in Azure. Service directories that include a test-resources.json file require
5running [New-TestResources.ps1][] to create these resources and output
6environment variables you must set.
7
8The following scripts can be used both in on your desktop for developer
9scenarios as well as on hosted agents for continuous integration testing.
10
11* [New-TestResources.ps1][] - Creates new test resources for a given service.
12* [Remove-TestResources.ps1][] - Deletes previously created resources.
13
14## Prerequisites
15
161. Install [PowerShell][] version 7.0 or newer.
172. Install the [Azure PowerShell][PowerShellAz].
18
19## On the Desktop
20
21To set up your Azure account to run live tests, you'll need to log into Azure,
22and set up your resources defined in test-resources.json as shown in the following
23example using Azure Search. The script will create a service principal automatically,
24or you may create a service principal you can save and reuse subsequently.
25
26Note that `-Subscription` is an optional parameter but recommended if your account
27is a member of multiple subscriptions.
28
29```powershell
30Connect-AzAccount -Subscription 'YOUR SUBSCRIPTION ID'
31eng\common\TestResources\New-TestResources.ps1 -ServiceDirectory 'search'
32```
33
34The `OutFile` switch will be set by default if you are running this for a .NET project on Windows. This will save test environment settings
35into a test-resources.json.env file next to test-resources.json. The file is protected via DPAPI.
36The environment file would be scoped to the current repository directory and avoids the need to
37set environment variables or restart your IDE to recognize them.
38
39Along with some log messages, this will output environment variables based on
40your current shell like in the following example:
41
42```powershell
43${env:AZURE_TENANT_ID} = '<<secret>>'
44${env:AZURE_CLIENT_ID} = '<<secret>>'
45${env:AZURE_CLIENT_SECRET} = '<<secret>>'
46${env:AZURE_SUBSCRIPTION_ID} = 'YOUR SUBSCRIPTION ID'
47${env:AZURE_RESOURCE_GROUP} = 'rg-myusername'
48${env:AZURE_LOCATION} = 'westus2'
49${env:AZURE_SEARCH_STORAGE_NAME} = 'myusernamestg'
50${env:AZURE_SEARCH_STORAGE_KEY} = '<<secret>>'
51```
52
53For security reasons we do not set these environment variables automatically
54for either the current process or persistently for future sessions. You must
55do that yourself based on your current platform and shell.
56
57If your current shell was detected properly, you should be able to copy and
58paste the output directly in your terminal and add to your profile script.
59For example, in PowerShell on Windows you can copy the output above and paste
60it back into the terminal to set those environment variables for the current
61process. To persist these variables for future terminal sessions or for
62applications started outside the terminal, you could copy and paste the
63following commands:
64
65```powershell
66setx AZURE_TENANT_ID ${env:AZURE_TENANT_ID}
67setx AZURE_CLIENT_ID ${env:AZURE_CLIENT_ID}
68setx AZURE_CLIENT_SECRET ${env:AZURE_CLIENT_SECRET}
69setx AZURE_SUBSCRIPTION_ID ${env:AZURE_SUBSCRIPTION_ID}
70setx AZURE_RESOURCE_GROUP ${env:AZURE_RESOURCE_GROUP}
71setx AZURE_LOCATION ${env:AZURE_LOCATION}
72setx AZURE_SEARCH_STORAGE_NAME ${env:AZURE_SEARCH_STORAGE_NAME}
73setx AZURE_SEARCH_STORAGE_KEY ${env:AZURE_SEARCH_STORAGE_KEY}
74```
75
76After running or recording live tests, if you do not plan on further testing
77you can remove the test resources you created above by running:
78[Remove-TestResources.ps1][]:
79
80```powershell
81Remove-TestResources.ps1 -BaseName 'myusername' -Force
82```
83
84If you persisted environment variables, you should also remove those as well.
85
86Some test-resources.json templates utilize the `AdditionalParameters` parameter to control additional resource configuration options. For example:
87
88```powershell
89New-TestResources.ps1 keyvault -AdditionalParameters @{enableHsm = $true}
90```
91
92## In CI
93
94Test pipelines should include deploy-test-resources.yml and
95remove-test-resources.yml like in the following examples:
96
97```yml
98- template: /eng/common/TestResources/deploy-test-resources.yml
99  parameters:
100    ServiceDirectory: '${{ parameters.ServiceDirectory }}'
101
102# Run tests
103
104- template: /eng/common/TestResources/remove-test-resources.yml
105```
106
107Be sure to link the **Secrets for Resource Provisioner** variable group
108into the test pipeline for these scripts to work.
109
110## Documentation
111
112To regenerate documentation for scripts within this directory, you can install
113[platyPS][] and run it like in the following example:
114
115```powershell
116Install-Module platyPS -Scope CurrentUser -Force
117New-MarkdownHelp -Command .\New-TestResources.ps1 -OutputFolder . -Force
118```
119
120PowerShell markdown documentation created with [platyPS][].
121
122  [New-TestResources.ps1]: https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/TestResources/New-TestResources.ps1.md
123  [Remove-TestResources.ps1]: https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/TestResources/Remove-TestResources.ps1.md
124  [PowerShell]: https://github.com/PowerShell/PowerShell
125  [PowerShellAz]: https://docs.microsoft.com/powershell/azure/install-az-ps
126  [platyPS]: https://github.com/PowerShell/platyPS
127