Skip to content

Commit f483d3b

Browse files
viananthmarkcowl
authored andcommitted
Adding more unit tests. This closes #14
1 parent 1881ad9 commit f483d3b

File tree

1 file changed

+134
-7
lines changed

1 file changed

+134
-7
lines changed

tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1

Lines changed: 134 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Describe "Get-AzureProfileMap" {
1515
Mock Get-ProfileCachePath -Verifiable { return "MockPath\ProfileCache"}
1616
Mock Invoke-RestMethod -Verifiable { return "Invoking ProfileMapEndPoint... Receiving ProfileMap.json"}
1717
Mock Get-Content -Verifiable { return $testProfileMap }
18-
18+
1919
Context "ProfilePath does not exist" {
2020
It "Returns the proper json file" {
2121
Get-AzureProfileMap | Should Be "@{profile1=; profile2=}"
@@ -41,6 +41,23 @@ Describe "Get-AzureProfileMap" {
4141
{ Get-AzureProfileMap } | Should throw
4242
}
4343
}
44+
45+
Context "Invoke-RestMethod returns an error" {
46+
$Response = New-Object System.Net.HttpWebResponse
47+
$Exception = New-Object System.Net.WebException "Test Error Occurred.", (New-Object System.Exception), ConnectFailure, $Response
48+
$ErrorRecord = new-object System.Management.Automation.ErrorRecord $Exception, "TestError", ConnectionError, $null
49+
50+
$RestError = @()
51+
$object = New-Object -TypeName psobject
52+
$object | Add-Member -Name ErrorRecord -MemberType NoteProperty -value $ErrorRecord
53+
$RestError += $object
54+
55+
Mock Invoke-RestMethod {}
56+
Mock Get-RestResponse { return $RestError }
57+
It "Throws custom exception" {
58+
{ Get-AzureProfileMap } | Should throw "Http Status Code:"
59+
}
60+
}
4461
}
4562
}
4663

@@ -117,6 +134,17 @@ Describe "Add-ForceParam" {
117134
}
118135
}
119136

137+
Describe "Add-SwitchParam" {
138+
InModuleScope AzureRM.Bootstrapper {
139+
$params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
140+
141+
It "Should return Switch parameter object" {
142+
Add-SwitchParam $params "TestParam"
143+
$params.ContainsKey("TestParam") | Should Be $true
144+
}
145+
}
146+
}
147+
120148
Describe "Get-AzureRmModule" {
121149
InModuleScope AzureRM.Bootstrapper {
122150
Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) }
@@ -144,6 +172,18 @@ Describe "Get-AzureRmModule" {
144172
Assert-VerifiableMocks
145173
}
146174
}
175+
176+
Context "Invoke with invalid parameters" {
177+
It "Should throw" {
178+
{ Get-AzureRmModule -Profile 'XYZ' -Module 'ABC' } | Should Throw
179+
}
180+
}
181+
182+
Context "Invoke with null parameters" {
183+
It "Should throw" {
184+
{ Get-AzureRmModule -Profile $null -Module $null } | Should Throw
185+
}
186+
}
147187
}
148188
}
149189

@@ -195,7 +235,7 @@ Describe "Use-AzureRmProfile" {
195235
Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) }
196236
Mock Install-Module { "Installing module..."}
197237
Mock Import-Module -Verifiable { "Importing Module..."}
198-
238+
199239
Context "Modules not installed" {
200240
Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"}
201241
It "Should install modules" {
@@ -210,24 +250,111 @@ Describe "Use-AzureRmProfile" {
210250
Context "Modules are installed" {
211251
Mock Get-AzureRmModule -Verifiable { "1.0" } -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"}
212252
Mock Import-Module { "Module1 1.0 Imported"} -ParameterFilter { $Name -eq "Module1" -and $RequiredVersion -eq "1.0"}
213-
It "Should skip installing modules" {
253+
It "Should skip installing modules, imports the right version module" {
214254
(Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Module1 1.0 Imported"
215255
Assert-MockCalled Install-Module -Exactly 0
216256
Assert-MockCalled Import-Module -Exactly 1
217257
Assert-VerifiableMocks
218258
}
219259
}
260+
261+
Context "Invoke with invalid profile" {
262+
It "Should throw" {
263+
{ Use-AzureRmProfile -Profile 'WrongProfileName'} | Should Throw
264+
}
265+
}
266+
267+
Context "Invoke with $null profile" {
268+
It "Should throw" {
269+
{ Use-AzureRmProfile -Profile $null} | Should Throw
270+
}
271+
}
220272
}
221273
}
222274

223275
Describe "Install-AzureRmProfile" {
224276
InModuleScope AzureRM.Bootstrapper {
225277
$RollupModule = 'Module1'
226278
Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) }
227-
Mock Install-Module -Verifiable { "Installing module..."}
228-
It "Should install RollupModule" {
229-
(Install-AzureRmProfile -Profile 'profile1') | Should be "Installing module..."
230-
Assert-VerifiableMocks
279+
280+
Context "Invoke with valid profile name" {
281+
Mock Install-Module -Verifiable { "Installing module $RollupModule... Version 1.0"} -ParameterFilter { $Name -eq $RollupModule -and $RequiredVersion -eq '1.0' }
282+
It "Should install RollupModule" {
283+
(Install-AzureRmProfile -Profile 'Profile1') | Should be "Installing module $RollupModule... Version 1.0"
284+
Assert-VerifiableMocks
285+
}
286+
}
287+
288+
Context "Invoke with invalid profile name" {
289+
It "Should throw" {
290+
{ Install-AzureRmProfile -Profile 'WrongProfileName'} | Should Throw
291+
}
292+
}
293+
294+
Context "Invoke with null profile name" {
295+
It "Should throw" {
296+
{ Install-AzureRmProfile -Profile $null } | Should Throw
297+
}
298+
}
299+
}
300+
}
301+
302+
Describe "Uninstall-AzureRmProfile" {
303+
InModuleScope AzureRM.Bootstrapper {
304+
Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) }
305+
Mock Remove-Module -Verifiable { "Removing module from session..." }
306+
Mock Uninstall-Module -Verifiable { "Uninstalling module..." }
307+
308+
Context "Modules are installed" {
309+
It "Should remove and uninstall modules" {
310+
# Arrange
311+
$Script:mockCalled = 0
312+
$mockTestPath = {
313+
$Script:mockCalled++
314+
if ($Script:mockCalled -eq 1)
315+
{
316+
return "1.0"
317+
}
318+
else {
319+
return $null
320+
}
321+
}
322+
323+
Mock -CommandName Get-AzureRmModule -MockWith $mockTestPath
324+
325+
# Act
326+
$callResult = (Uninstall-AzureRmProfile -Profile 'Profile1' -Force)
327+
328+
# Assert
329+
$Script:mockCalled | Should Be 3
330+
$callResult[0] | Should Be "Removing module from session..."
331+
$callResult[1] | Should Be "Uninstalling module..."
332+
Assert-VerifiableMocks
333+
}
334+
}
335+
336+
Context "Modules are not installed" {
337+
It "Should not call Uninstall-Module" {
338+
Mock Get-AzureRmModule -Verifiable {}
339+
$callResult = (Uninstall-AzureRmProfile -Profile 'Profile1' -Force)
340+
341+
Assert-MockCalled Get-AzureRmModule -Exactly 2
342+
Assert-MockCalled Remove-Module -Exactly 0
343+
Assert-MockCalled Uninstall-Module -Exactly 0
344+
Assert-VerifiableMocks
345+
}
346+
}
347+
348+
Context "Invoke with invalid profile name" {
349+
It "Should throw" {
350+
{ Uninstall-AzureRmProfile -Profile 'WrongProfileName' } | Should Throw
351+
}
352+
}
353+
354+
Context "Invoke with null profile name" {
355+
It "Should throw" {
356+
{ Uninstall-AzureRmProfile -Profile $null } | Should Throw
357+
}
231358
}
232359
}
233360
}

0 commit comments

Comments
 (0)