Skip to content

Commit 418a1f2

Browse files
author
maddieclayton
authored
Merge pull request #5004 from maddieclayton/ImproveTests
Convert New-TestCredential tests to Pester and add to CI build
2 parents d870065 + 76cee32 commit 418a1f2

File tree

4 files changed

+175
-252
lines changed

4 files changed

+175
-252
lines changed

build.proj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@
531531
<!-- Run checkin tests for each pull request -->
532532
<Target Name="Test" DependsOnTargets="BeforeRunTests">
533533
<Message Importance="high" Text="Running check in tests..." />
534+
<!-- Run Pester tests -->
535+
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;$(MSBuildProjectDirectory)\tools\Modules\Run-UnitTests.ps1&quot;"
536+
ContinueOnError="ErrorAndContinue"/>
534537
<CallTarget Targets="InvokeXUnit"/>
535538
</Target>
536539

tools/Modules/Run-UnitTests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Requires -Modules Pester
2+
$defaults = [System.IO.Path]::GetDirectoryName($PSScriptRoot)
3+
Start-Job -ScriptBlock {
4+
Import-Module $using:PSScriptRoot\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1;
5+
Import-Module $using:PSScriptRoot\..\..\src\Package\Debug\ResourceManager\AzureResourceManager\AzureRM.Resources\AzureRM.Resources.psd1;
6+
Set-Location $using:defaults;
7+
Invoke-Pester
8+
} | Receive-Job -Wait -AutoRemoveJob

tools/Modules/TestFx-Tasks.Tests.ps1

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
# ----------------------------------------------------------------------------------
3+
#
4+
# Copyright Microsoft Corporation
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ----------------------------------------------------------------------------------
15+
16+
Import-Module $PSScriptRoot/TestFx-Tasks
17+
Describe "Test-NewCredentialNewServicePrincipal"{
18+
Context "Creates a correctly formatted ServicePrincipal"{
19+
Login-AzureRmAccount -AccessToken "12345678" -SkipValidation -AccountId "testaccount" -TenantId "8bc48661-1801-4b7a-8ca1-6a3cadfb4870" -Subscription "8bc48661-1801-4b7a-8ca1-6a3cadfb4870" -GraphAccessToken "12345678"
20+
Mock -ModuleName TestFx-Tasks Get-AzureRmADServicePrincipal { return @() }
21+
Mock -ModuleName TestFx-Tasks New-AzureRMADServicePrincipal { return @{"ApplicationId" = "8bc48661-1801-4b7a-8ca1-6a3cadfb4870"; "Id" = "8bc48661-1801-4b7a-8ca1-6a3cadfb4870"} }
22+
Mock -ModuleName TestFx-Tasks New-AzureRMRoleAssignment { return $true }
23+
Mock -ModuleName TestFx-Tasks Get-AzureRMRoleAssignment { return $true }
24+
$context = Get-AzureRmContext
25+
$secureSecret = ConvertTo-SecureString -String "testpassword" -AsPlainText -Force
26+
New-TestCredential -ServicePrincipalDisplayName "credentialtestserviceprincipal" -ServicePrincipalSecret $secureSecret -SubscriptionId $context.Subscription.Id -TenantId $context.Tenant.Id -RecordMode "Playback" -Force
27+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
28+
It "writes correct information to file" {
29+
$filePath | Should Contain "ServicePrincipal": "8bc48661-1801-4b7a-8ca1-6a3cadfb4870"
30+
$filePath | Should Contain "ServicePrincipalSecret": "testpassword"
31+
}
32+
}
33+
34+
Context "Connection string is properly set" {
35+
$filePath = Join-Path -Path $PSScriptRoot -ChildPath "\..\..\src\ResourceManager\Common\Commands.ScenarioTests.ResourceManager.Common\bin\Debug\Microsoft.Azure.Commands.ScenarioTest.Common.dll"
36+
$assembly = [System.Reflection.Assembly]::LoadFrom($filePath)
37+
$envHelper = New-Object Microsoft.WindowsAzure.Commands.ScenarioTest.EnvironmentSetupHelper -ArgumentList @()
38+
$envHelper.SetEnvironmentVariableFromCredentialFile()
39+
It "creates correctly formatted environment string" {
40+
$Env:AZURE_TEST_MODE | Should Match "Playback"
41+
$Env:TEST_CSM_ORGID_AUTHENTICATION | Should Match "SubscriptionId=" + $context.Subscription.Id + ";HttpRecorderMode=Playback;Environment=Prod;ServicePrincipal=" +
42+
"8bc48661-1801-4b7a-8ca1-6a3cadfb4870" + ";ServicePrincipalSecret=testpassword;" + "AADTenant=" + $context.Tenant.Id
43+
}
44+
}
45+
46+
# Clean-up
47+
Remove-Item Env:AZURE_TEST_MODE
48+
Remove-Item Env:TEST_CSM_ORGID_AUTHENTICATION
49+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
50+
Remove-Item -LiteralPath $filePath -Force
51+
$directoryPath = $Env:USERPROFILE + "\.azure\"
52+
$directoryEmpty = Get-ChildItem $directoryPath | Measure-Object
53+
if ($directoryEmpty.Count -eq 0)
54+
{
55+
Remove-Item $directoryPath -Recurse -Force
56+
}
57+
}
58+
59+
Describe "Test-NewCredentialExistingServicePrincipal" {
60+
Context "Finds and uses a ServicePrincipal"{
61+
Login-AzureRmAccount -AccessToken "12345678" -SkipValidation -AccountId "testaccount" -TenantId "8bc48661-1801-4b7a-8ca1-6a3cadfb4870" -Subscription "8bc48661-1801-4b7a-8ca1-6a3cadfb4870" -GraphAccessToken "12345678"
62+
Mock -ModuleName TestFx-Tasks Get-AzureRmADServicePrincipal { return @(@{"ApplicationId" = "1234"; "Id" = "5678"; "DisplayName" = "credentialtestserviceprincipal"}) }
63+
$context = Get-AzureRmContext
64+
$secureSecret = ConvertTo-SecureString -String "testpassword" -AsPlainText -Force
65+
New-TestCredential -ServicePrincipalDisplayName "credentialtestserviceprincipal" -ServicePrincipalSecret $secureSecret -SubscriptionId $context.Subscription.Id -TenantId $context.Tenant.Id -RecordMode "Record" -Force
66+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
67+
It "writes correct information to file" {
68+
$filePath | Should Contain "ServicePrincipal": "1234"
69+
$filePath | Should Contain "ServicePrincipalSecret": "testpassword"
70+
}
71+
}
72+
73+
Context "Connection string is properly set" {
74+
$filePath = Join-Path -Path $PSScriptRoot -ChildPath "\..\..\src\ResourceManager\Common\Commands.ScenarioTests.ResourceManager.Common\bin\Debug\Microsoft.Azure.Commands.ScenarioTest.Common.dll"
75+
$assembly = [System.Reflection.Assembly]::LoadFrom($filePath)
76+
$envHelper = New-Object Microsoft.WindowsAzure.Commands.ScenarioTest.EnvironmentSetupHelper -ArgumentList @()
77+
$envHelper.SetEnvironmentVariableFromCredentialFile()
78+
It "creates correctly formatted environment string" {
79+
$Env:AZURE_TEST_MODE | Should Match "Record"
80+
$Env:TEST_CSM_ORGID_AUTHENTICATION | Should Match "SubscriptionId=" + $context.Subscription.Id + ";HttpRecorderMode=Record;Environment=Prod;ServicePrincipal=" +
81+
"1234" + ";ServicePrincipalSecret=testpassword;" + "AADTenant=" + $context.Tenant.Id
82+
}
83+
}
84+
85+
# Clean-up
86+
Remove-Item Env:AZURE_TEST_MODE
87+
Remove-Item Env:TEST_CSM_ORGID_AUTHENTICATION
88+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
89+
Remove-Item -LiteralPath $filePath -Force
90+
$directoryPath = $Env:USERPROFILE + "\.azure\"
91+
$directoryEmpty = Get-ChildItem $directoryPath | Measure-Object
92+
if ($directoryEmpty.Count -eq 0)
93+
{
94+
Remove-Item $directoryPath -Recurse -Force
95+
}
96+
}
97+
98+
Describe "Test-NewCredentialUserId" {
99+
Context "Creates correct file" {
100+
$context = Get-AzureRmContext
101+
New-TestCredential -UserId "testuser" -SubscriptionId $context.Subscription.Id -RecordMode "Playback" -Force
102+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
103+
It "writes correct information to file" {
104+
$filePath | Should Contain "UserId": "testuser"
105+
$filePath | Should Contain "HttpRecorderMode": "Playback"
106+
}
107+
}
108+
109+
Context "Connection string is properly set" {
110+
$filePath = Join-Path -Path $PSScriptRoot -ChildPath "\..\..\src\ResourceManager\Common\Commands.ScenarioTests.ResourceManager.Common\bin\Debug\Microsoft.Azure.Commands.ScenarioTest.Common.dll"
111+
$assembly = [System.Reflection.Assembly]::LoadFrom($filePath)
112+
$envHelper = New-Object Microsoft.WindowsAzure.Commands.ScenarioTest.EnvironmentSetupHelper -ArgumentList @()
113+
$envHelper.SetEnvironmentVariableFromCredentialFile()
114+
It "creates correctly formatted environment string" {
115+
$Env:AZURE_TEST_MODE | Should Match "Playback"
116+
$Env:TEST_CSM_ORGID_AUTHENTICATION | Should Match "SubscriptionId=" + $context.Subscription.Id + ";HttpRecorderMode=Playback;Environment=Prod;UserId=testuser"
117+
}
118+
}
119+
120+
# Clean-up
121+
Remove-Item Env:AZURE_TEST_MODE
122+
Remove-Item Env:TEST_CSM_ORGID_AUTHENTICATION
123+
$filePath = $Env:USERPROFILE + "\.azure\testcredentials.json"
124+
Remove-Item -LiteralPath $filePath -Force
125+
$directoryPath = $Env:USERPROFILE + "\.azure\"
126+
$directoryEmpty = Get-ChildItem $directoryPath | Measure-Object
127+
if ($directoryEmpty.Count -eq 0)
128+
{
129+
Remove-Item $directoryPath -Recurse -Force
130+
}
131+
}
132+
133+
Describe "Test-SetEnvironmentServicePrincipal" {
134+
Context "Connection string is properly set" {
135+
$context = Get-AzureRmContext
136+
$NewServicePrincipal = @{"ApplicationId" = "1234"; "Id" = "5678"; "DisplayName" = "credentialtestserviceprincipal"}
137+
Set-TestEnvironment -ServicePrincipalId $NewServicePrincipal.ApplicationId -ServicePrincipalSecret "testpassword" -SubscriptionId $context.Subscription.Id -TenantId $context.Tenant.Id -RecordMode "Record"
138+
It "creates correctly formatted environment string" {
139+
$Env:AZURE_TEST_MODE | Should Match "Record"
140+
$Env:TEST_CSM_ORGID_AUTHENTICATION | Should Match "SubscriptionId=" + $context.Subscription.Id + ";HttpRecorderMode=Record;Environment=Prod;AADTenant=" +
141+
$context.Tenant.Id + ";ServicePrincipal=" + "1234" + ";ServicePrincipalSecret=testpassword"
142+
}
143+
}
144+
145+
# Clean-up
146+
Remove-Item Env:AZURE_TEST_MODE
147+
Remove-Item Env:TEST_CSM_ORGID_AUTHENTICATION
148+
}
149+
150+
Describe "Test-SetEnvironmentUserId" {
151+
Context "Connection string is properly set" {
152+
$context = Get-AzureRmContext
153+
Set-TestEnvironment -UserId "testuser" -SubscriptionId $context.Subscription.Id -RecordMode "Playback"
154+
It "creates correctly formatted environment string" {
155+
$Env:AZURE_TEST_MODE | Should Match "Playback"
156+
$Env:TEST_CSM_ORGID_AUTHENTICATION | Should Match "SubscriptionId=" + $context.Subscription.Id + ";HttpRecorderMode=Playback;Environment=Prod;UserId=testuser"
157+
}
158+
}
159+
160+
# Clean-up
161+
Remove-Item Env:AZURE_TEST_MODE
162+
Remove-Item Env:TEST_CSM_ORGID_AUTHENTICATION
163+
}

0 commit comments

Comments
 (0)