Skip to content

Commit bcf5350

Browse files
authored
Merge pull request #8271 from maddieclayton/fixtestdocs
Fix test cmdlets
2 parents 2f68db7 + 28b999e commit bcf5350

File tree

3 files changed

+32
-88
lines changed

3 files changed

+32
-88
lines changed

documentation/testing-docs/using-azure-test-framework.md

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
- [New-TestCredential](#new-testcredential)
77
- [Create New Service Principal](#create-new-service-principal)
88
- [Use Existing Service Principal](#use-existing-service-principal)
9-
- [UserId](#userid)
109
- [Set-TestEnvironment](#set-testenvironment)
1110
- [Existing Service Principal](#existing-service-principal)
12-
- [UserId](#userid)
1311
- [Manually Set Environment Variables](#manually-set-environment-variables)
1412
- [Environment Variables](#environment-variables)
1513
- [Playback Test](#playback-test)
@@ -51,11 +49,11 @@ This cmdlet, located in the [`Repo-Tasks` module](/tools/Repo-Tasks.psd1), which
5149
Using a service principal is the preferred option for recording tests because it works with both .NET Framework and .NET Core. In order to create a new service principal, run this command with an unused service principal display name:
5250

5351
```powershell
54-
New-TestCredential -ServicePrincipalDisplayName "ScenarioTestCredentials" -ServicePrincipalSecret `
55-
"testpassword" -SubscriptionId <subscriptionId> -TenantId <tenantId> -RecordMode "Record"
52+
New-TestCredential -ServicePrincipalDisplayName "ScenarioTestCredentials" -SubscriptionId `
53+
<subscriptionId> -TenantId <tenantId> -RecordMode "Record"
5654
```
5755

58-
This command will create a new service principal, set the correct role assignment for this service principal based upon the subscription provided, and place the service principal id and secret into the credentials file.
56+
This command will create a new service principal, set the correct role assignment for this service principal based upon the subscription provided, and place the service principal id and automatically generated secret into the credentials file.
5957

6058
Alternatively, to create a service principal, follow the [Azure AD guide to create a Application Service Principal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#create-an-active-directory-application). The application type should be `Web app / API` and the sign-on URL value is irrelevant (you can set any value).
6159

@@ -68,15 +66,6 @@ New-TestCredential -ServicePrincipalDisplayName "Existing Service Principal" -Se
6866
"testpassword" -SubscriptionId <subscriptionId> -TenantId <tenantId> -RecordMode "Record"
6967
```
7068

71-
#### UserId
72-
73-
This is no longer the preferred option because it only works when running on .NET Framework. When running on .NET Core you may get an error like `Interactive Login is supported only in NET45 projects`. Additionally, you will have to manually log in when running the scenario tests rather than being automatically validated.
74-
75-
```powershell
76-
New-TestCredential -UserId "[email protected]" -SubscriptionId <subscriptionId> `
77-
-TenantId <tenantId> -RecordMode "Record"
78-
```
79-
8069
### Set-TestEnvironment
8170

8271
This cmdlet, located in the [`Repo-Tasks` module](/tools/Repo-Tasks.psd1), will directly set the environment variable for the session.
@@ -90,15 +79,6 @@ Set-TestEnvironment -ServicePrincipalId <servicePrincipalId> -ServicePrincipalSe
9079
"testpassword" -SubscriptionId <subscriptionId> -TenantId <tenantId> -RecordMode "Record"
9180
```
9281

93-
#### UserId
94-
95-
This is no longer the preferred option because it only works when running on .NET Framework. When running on .NET Core you may get an error like `Interactive Login is supported only in NET45 projects`.
96-
97-
```powershell
98-
Set-TestEnvironment -UserId "[email protected]" -SubscriptionId <subscriptionId> `
99-
-TenantId <tenantId> -RecordMode "Record"
100-
```
101-
10282
### Manually Set Environment Variables
10383

10484
#### Environment Variables
@@ -118,21 +98,8 @@ TEST_CSM_ORGID_AUTHENTICATION=
11898
AZURE_TEST_MODE=Playback
11999
```
120100

121-
#### Record Test with Interactive login using OrgId
122-
123-
This is no longer the preferred option because it only works when running on .NET Framework. When running on .NET Core you may get an error like `Interactive Login is supported only in NET45 projects`.
124-
125-
To use this option, set the following environment variables before starting Visual Studio:
126-
127-
```
128-
TEST_CSM_ORGID_AUTHENTICATION=SubscriptionId={SubId};UserId={orgId};AADTenant={tenantId};Environment={env};HttpRecorderMode=Record;
129-
AZURE_TEST_MODE=Record
130-
```
131-
132101
#### Record Test with service principal
133102

134-
This is the preferred option for recording tests because it works with both .NET Framework and .NET Core.
135-
136103
After the service principal is created, you will need to give it access to Azure resources. This can be done with the following PowerShell command, with the [Service Principal Application ID](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal#get-application-id-and-authentication-key) (this is a guid, not the display name of the service principal) substituted in for `{clientId}`.
137104

138105
```powershell

tools/Modules/TestFx-Tasks.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Copyright = '(c) 2016 ShahAbhijeet. All rights reserved.'
5151
# ProcessorArchitecture = ''
5252

5353
# Modules that must be imported into the global environment prior to importing this module
54-
RequiredModules = @(@{ModuleName = 'AzureRM.Resources'; ModuleVersion = '5.0.0'; })
54+
RequiredModules = @(@{ModuleName = 'Az.Resources'; ModuleVersion = '1.0.0'; })
5555

5656
# Assemblies that must be loaded prior to importing this module
5757
# RequiredAssemblies = @()

tools/Modules/TestFx-Tasks.psm1

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
Function New-TestCredential
22
{
33
[CmdletBinding(
4-
DefaultParameterSetName='SpnParamSet',
54
SupportsShouldProcess=$true
65
)]
76
param(
8-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='ServicePrincipal/ClientId you would like to use')]
97
[ValidateNotNullOrEmpty()]
108
[string]$ServicePrincipalDisplayName,
119

12-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='ServicePrincipal Secret/ClientId Secret you would like to use')]
13-
[ValidateNotNullOrEmpty()]
14-
[securestring]$ServicePrincipalSecret,
15-
16-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "UserId (OrgId) you would like to use")]
17-
[ValidateNotNullOrEmpty()]
18-
[string]$UserId,
19-
20-
[Parameter(ParameterSetName='CreateSpnParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
21-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
22-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
10+
[Parameter(Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
2311
[ValidateNotNullOrEmpty()]
2412
[string]$SubscriptionId,
2513

26-
[Parameter(ParameterSetName='CreateSpnParamSet', Mandatory=$true, HelpMessage='AADTenant/TenantId you would like to use')]
27-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='AADTenant/TenantId you would like to use')]
14+
[Parameter(Mandatory=$true, HelpMessage='AADTenant/TenantId you would like to use')]
2815
[ValidateNotNullOrEmpty()]
2916
[string]$TenantId,
3017

31-
[Parameter(ParameterSetName='CreateSpnParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
32-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage = "Record tests, Playback tests, or neither")]
33-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "Record tests, Playback tests, or neither")]
18+
[Parameter(Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
3419
[ValidateSet("Playback", "Record", "None")]
3520
[string]$RecordMode,
3621

22+
[Parameter(Mandatory=$false, HelpMessage='ServicePrincipal Secret/ClientId Secret you would like to use (required for existing Service Principal)')]
23+
[ValidateNotNullOrEmpty()]
24+
[securestring]$ServicePrincipalSecret,
25+
3726
[Parameter(Mandatory=$false, HelpMessage="Environment you would like to run in")]
3827
[ValidateSet("Prod", "Dogfood", "Current", "Next", "Custom")]
3928
[string]$TargetEnvironment='Prod',
@@ -81,15 +70,20 @@
8170
$credentials.Environment = $TargetEnvironment
8271

8372
if ([string]::IsNullOrEmpty($ServicePrincipalDisplayName) -eq $false) {
84-
$existingServicePrincipal = Get-AzureRmADServicePrincipal -SearchString $ServicePrincipalDisplayName | Where-Object {$_.DisplayName -eq $ServicePrincipalDisplayName}
73+
$existingServicePrincipal = Get-AzADServicePrincipal -SearchString $ServicePrincipalDisplayName | Where-Object {$_.DisplayName -eq $ServicePrincipalDisplayName}
8574
if ($existingServicePrincipal -eq $null -and ($Force -or $PSCmdlet.ShouldContinue("ServicePrincipal `"" + $ServicePrincipalDisplayName + "`" does not exist, would you like to create a new ServicePrincipal with this name?", "Create ServicePrincipal?")))
8675
{
76+
if (![string]::IsNullOrEmpty($ServicePrincipalSecret))
77+
{
78+
Write-Warning "Service Principal secrets are randomly generated, so provided secret value will not be used during creation."
79+
}
80+
8781
if ($TargetEnvironment -ne 'Prod')
8882
{
8983
throw "To create a new Service Principal you must be in Prod. Please run again with `$TargetEnvironment set to 'Prod'"
9084
}
9185
$Scope = "/subscriptions/" + $SubscriptionId
92-
$NewServicePrincipal = New-AzureRMADServicePrincipal -DisplayName $ServicePrincipalDisplayName -Password $ServicePrincipalSecret
86+
$NewServicePrincipal = New-AzADServicePrincipal -DisplayName $ServicePrincipalDisplayName
9387
Write-Host "New ServicePrincipal created: " + $NewServicePrincipal.ApplicationId
9488

9589
$NewRole = $null
@@ -98,30 +92,31 @@
9892
{
9993
# Sleep here for a few seconds to allow the service principal application to become active (should only take a couple of seconds normally)
10094
Start-Sleep 5
101-
New-AzureRMRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $NewServicePrincipal.ApplicationId -Scope $Scope | Write-Verbose -ErrorAction SilentlyContinue
102-
$NewRole = Get-AzureRMRoleAssignment -ObjectId $NewServicePrincipal.Id -ErrorAction SilentlyContinue
95+
New-AzRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $NewServicePrincipal.ApplicationId -Scope $Scope | Write-Verbose -ErrorAction SilentlyContinue
96+
$NewRole = Get-AzRoleAssignment -ObjectId $NewServicePrincipal.Id -ErrorAction SilentlyContinue
10397
$Retries++;
10498
}
10599

106100
$credentials.ServicePrincipal = $NewServicePrincipal.ApplicationId
101+
$ServicePrincipalSecret = $NewServicePrincipal.Secret
107102
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ServicePrincipalSecret)
108103
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
109104
$credentials.ServicePrincipalSecret = $UnsecurePassword
110105
}
111106

112107
else
113108
{
109+
if ([string]::IsNullOrEmpty($ServicePrincipalSecret))
110+
{
111+
throw "Service Principal secret required for existing Service Principal."
112+
}
114113
$credentials.ServicePrincipal = $existingServicePrincipal.ApplicationId
115114
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ServicePrincipalSecret)
116115
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
117116
$credentials.ServicePrincipalSecret = $UnsecurePassword
118117
}
119118
}
120119

121-
if ([string]::IsNullOrEmpty($UserId) -eq $false) {
122-
$credentials.UserId = $UserId
123-
}
124-
125120
if ([string]::IsNullOrEmpty($TenantId) -eq $false) {
126121
$credentials.TenantId = $TenantId
127122
}
@@ -191,31 +186,25 @@ In order to successfully run a test, you will need SubscriptionId, TenantId
191186
This cmdlet will only prompt you for Subscription and Tenant information, rest all other parameters are optional
192187
193188
#>
194-
[CmdletBinding(DefaultParameterSetName='UserIdParamSet')]
189+
[CmdletBinding()]
195190
param(
196-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "UserId (OrgId) you would like to use")]
197-
[ValidateNotNullOrEmpty()]
198-
[string]$UserId,
199-
200-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='ServicePrincipal/ClientId you would like to use')]
191+
[Parameter(Mandatory=$true, HelpMessage='ServicePrincipal/ClientId you would like to use')]
201192
[ValidateNotNullOrEmpty()]
202193
[string]$ServicePrincipalId,
203194

204-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='ServicePrincipal Secret/ClientId Secret you would like to use')]
195+
[Parameter(Mandatory=$true, HelpMessage='ServicePrincipal Secret/ClientId Secret you would like to use')]
205196
[ValidateNotNullOrEmpty()]
206197
[string]$ServicePrincipalSecret,
207198

208-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
209-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
199+
[Parameter(Mandatory=$true, HelpMessage = "SubscriptionId you would like to use")]
210200
[ValidateNotNullOrEmpty()]
211201
[string]$SubscriptionId,
212202

213-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage='AADTenant/TenantId you would like to use')]
203+
[Parameter(Mandatory=$true, HelpMessage='AADTenant/TenantId you would like to use')]
214204
[ValidateNotNullOrEmpty()]
215205
[string]$TenantId,
216206

217-
[Parameter(ParameterSetName='SpnParamSet', Mandatory=$true, HelpMessage = "Would you like to record or playback your tests?")]
218-
[Parameter(ParameterSetName='UserIdParamSet', Mandatory=$true, HelpMessage = "Would you like to record or playback your tests?")]
207+
[Parameter(Mandatory=$true, HelpMessage = "Would you like to record or playback your tests?")]
219208
[ValidateSet("Playback", "Record", "None")]
220209
[string]$RecordMode='Playback',
221210

@@ -237,11 +226,6 @@ This cmdlet will only prompt you for Subscription and Tenant information, rest a
237226

238227
$formattedConnStr = [string]::Format("SubscriptionId={0};HttpRecorderMode={1};Environment={2}", $SubscriptionId, $RecordMode, $TargetEnvironment)
239228

240-
if([string]::IsNullOrEmpty($UserId) -eq $false)
241-
{
242-
$formattedConnStr = [string]::Format([string]::Concat($formattedConnStr, ";UserId={0}"), $UserId)
243-
}
244-
245229
if([string]::IsNullOrEmpty($TenantId) -eq $false)
246230
{
247231
$formattedConnStr = [string]::Format([string]::Concat($formattedConnStr, ";AADTenant={0}"), $TenantId)
@@ -314,7 +298,7 @@ This cmdlet will only prompt you for Subscription and Tenant information, rest a
314298
}
315299

316300
Write-Host "Below connection string is ready to be set"
317-
Print-ConnectionString $UserId $SubscriptionId $TenantId $ServicePrincipal $ServicePrincipalSecret $RecordMode $TargetEnvironment
301+
Print-ConnectionString $SubscriptionId $TenantId $ServicePrincipal $ServicePrincipalSecret $RecordMode $TargetEnvironment
318302

319303
#Set connection string to Environment variable
320304
$env:TEST_CSM_ORGID_AUTHENTICATION=$formattedConnStr
@@ -333,13 +317,6 @@ This cmdlet will only prompt you for Subscription and Tenant information, rest a
333317

334318
Function Print-ConnectionString([string]$uid, [string]$subId, [string]$aadTenant, [string]$spn, [string]$spnSecret, [string]$recordMode, [string]$targetEnvironment)
335319
{
336-
337-
if([string]::IsNullOrEmpty($uid) -eq $false)
338-
{
339-
Write-Host "UserId=" -ForegroundColor Green -NoNewline
340-
Write-Host $uid";" -NoNewline
341-
}
342-
343320
if([string]::IsNullOrEmpty($subId) -eq $false)
344321
{
345322
Write-Host "SubscriptionId=" -ForegroundColor Green -NoNewline

0 commit comments

Comments
 (0)