From 9727f119228d22445c7d6c47d18b47605fa621a2 Mon Sep 17 00:00:00 2001 From: Vishnu Priya Ananthu Sundaram Date: Tue, 8 Nov 2016 10:34:42 -0800 Subject: [PATCH 1/3] Create AzureRM.BootStrapper.Tests.ps1 Unit-tests for Bootstrapper cmdlets. --- .../AzureRM.BootStrapper.Tests.ps1 | 231 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 diff --git a/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 new file mode 100644 index 000000000000..5c9f1d2e1dc1 --- /dev/null +++ b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 @@ -0,0 +1,231 @@ +Import-Module -Name AzureRM.Bootstrapper +$global:testProfileMap = "{`"Profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"Profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" + +Describe "Get-ProfileCachePath" { + Context "Gets the correct profile cache path" { + It "Should return proper path" { + Get-ProfileCachePath | Should Match "(.*)ProfileCache$" + } + } +} + +Describe "Get-AzureProfileMap" { + InModuleScope AzureRM.Bootstrapper { + Mock New-Item -Verifiable { return "Creating ProfilePath"} + Mock Get-ProfileCachePath -Verifiable { return "MockPath\ProfileCache"} + Mock Invoke-RestMethod -Verifiable { return "Invoking ProfileMapEndPoint... Receiving ProfileMap.json"} + Mock Get-Content -Verifiable { return $testProfileMap } + + Context "ProfilePath does not exist" { + It "Returns the proper json file" { + Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" + } + It "Checks all the verifiable Mock calls" { + Assert-VerifiableMocks + } + } + + Context "ProfilePath Exists" { + Mock Test-Path { $true } + Mock New-Item {} + It "Returns Correct ProfileMap" { + Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" + } + It "Should not call New-Item" { + Assert-MockCalled New-Item -Exactly 0 + } + } + + Context "Invoke-RestMethod throws exception" { + Mock Invoke-RestMethod { throw [System.Net.WebException] } + + It "Throws Web Exception" { + { Get-AzureProfileMap } | Should throw + } + } + } +} + +Describe Get-AzProfile { + InModuleScope AzureRM.Bootstrapper { + + Context "Forces update from Azure Endpoint" { + Mock Get-AzureProfileMap { $global:testProfileMap } + + It "Should get ProfileMap from Azure Endpoint" { + Get-AzProfile -Force | Should Be "{`"profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" + } + It "Checks Mock calls to Get-AzureProfileMap" { + Assert-MockCalled Get-AzureProfileMap -Exactly 1 + } + } + + Context "Gets Azure ProfileMap from Cache" { + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } + Mock Test-Path -Verifiable { $true } + Mock Get-Content -Verifiable { return $global:testProfileMap } + + It "Should get ProfileMap from Cache" { + Get-AzProfile | Should Be "@{profile1=; profile2=}" + } + It "Checks all verifiable mocks" { + Assert-VerifiableMocks + } + } + + Context "ProfileMap is not available from cache" { + Mock Test-Path -Verifiable { $false } + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } + Mock Get-Content -Verifiable { return $global:testProfileMap } + + It "Should get ProfileMap from Embedded source" { + Get-AzProfile | Should Be "@{profile1=; profile2=}" + } + It "Checks all verifiable mocks" { + Assert-VerifiableMocks + } + } + + Context "ProfileMap is not available in cache or Embedded source" { + Mock Test-Path -Verifiable { $false } + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } + Mock Get-Content -Verifiable {} + + It "Should throw FileNotFound Exception" { + { Get-AzProfile } | Should Throw + } + It "Checks all verifiable mocks" { + Assert-VerifiableMocks + } + } + } +} + +Describe "Add-ProfileParam" { + InModuleScope AzureRM.Bootstrapper { + $params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + + It "Should return Profile parameter object" { + (Add-ProfileParam $params) + $params.ContainsKey("Profile") | Should Be $true + Assert-VerifiableMocks + } + } +} + +Describe "Add-ForceParam" { + InModuleScope AzureRM.Bootstrapper { + $params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary + + It "Should return Force parameter object" { + Add-ForceParam $params + $params.ContainsKey("Force") | Should Be $true + } + } +} + +Describe "Get-AzureRmModule" { + InModuleScope AzureRM.Bootstrapper { + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + + Context "Module is installed" { + Mock Get-Module -Verifiable { @( [PSCustomObject] @{ Name='Module1'; Version='1.0'; RepositorySourceLocation='foo\bar' }, [PSCustomObject] @{ Name='Module1'; Version='2.0'}) } + It "Should return installed version" { + Get-AzureRmModule -Profile 'Profile1' -Module 'Module1' | Should Be "1.0" + Assert-VerifiableMocks + } + } + + Context "Module is not installed" { + Mock Get-Module -Verifiable {} + It "Should return null" { + Get-AzureRmModule -Profile 'Profile1' -Module 'Module1' | Should be $null + Assert-VerifiableMocks + } + } + } +} + +Describe "Get-AzureRmProfile" { + InModuleScope AzureRM.Bootstrapper { + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + + Context "With ListAvailable Switch" { + It "Should return available profiles" { + $Result = (Get-AzureRmProfile -ListAvailable) + $Result.Length | Should Be 14 + $Result[0] | Should Be "Profile: Profile1" + Assert-VerifiableMocks + } + } + + Context "With ListAvailable and Force Switches" { + It "Should return available profiles" { + $Result = (Get-AzureRmProfile -ListAvailable -Force) + $Result.Length | Should Be 14 + $Result[0] | Should Be "Profile: Profile1" + Assert-VerifiableMocks + } + } + + Context "Without ListAvailable Switch" { + $RollupModule = 'Module1' + Mock Get-AzureRmModule -Verifiable { "1.0"} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} + It "Returns installed Profile" { + (Get-AzureRmProfile) | Should Be "Profile1" + Assert-VerifiableMocks + } + } + + Context "No profiles installed" { + $RollupModule = 'Module1' + Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} + It "Returns null" { + (Get-AzureRmProfile) | Should Be $null + Assert-VerifiableMocks + } + } + } +} + +Describe "Use-AzureRmProfile" { + InModuleScope AzureRM.Bootstrapper { + $RollupModule = 'Module1' + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + Mock Install-Module { "Installing module..."} + Mock Import-Module -Verifiable { "Importing Module..."} + + Context "Modules not installed" { + Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} + It "Should install modules" { + $Result = (Use-AzureRmProfile -Profile 'Profile1' -Force) + $Result.Length | Should Be 2 + $Result[0] | Should Be "Installing module..." + $Result[1] | Should Be "Importing Module..." + Assert-VerifiableMocks + } + } + + Context "Modules are installed" { + Mock Get-AzureRmModule -Verifiable { "1.0" } -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} + It "Should skip installing modules" { + (Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Importing Module..." + Assert-MockCalled Install-Module -Exactly 0 + Assert-VerifiableMocks + } + } + } +} + +Describe "Install-AzureRmProfile" { + InModuleScope AzureRM.Bootstrapper { + $RollupModule = 'Module1' + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + Mock Install-Module -Verifiable { "Installing module..."} + It "Should install RollupModule" { + (Install-AzureRmProfile -Profile 'profile1') | Should be "Installing module..." + Assert-VerifiableMocks + } + } +} From d68a7641d21f5aba051d1020bf03b055e634eed3 Mon Sep 17 00:00:00 2001 From: Vishnu Priya Ananthu Sundaram Date: Mon, 14 Nov 2016 11:35:38 -0800 Subject: [PATCH 2/3] Updating tests with CR Comments (I) Changed '-Force' to '-Update' (ii) Added test for 'Module not in list' scenario in Get-AzureRmModule (iii) Added filtered test for checking version of the module being imported under 'Use-AzureRmProfile' (iv) 'Assert-VerifiableMocks' is part of the test being done above each scenario. So removed the "It" block for 'Assert-VerifiableMocks' --- .../AzureRM.BootStrapper.Tests.ps1 | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 index 5c9f1d2e1dc1..9105aaa90ea3 100644 --- a/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 +++ b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 @@ -19,8 +19,6 @@ Describe "Get-AzureProfileMap" { Context "ProfilePath does not exist" { It "Returns the proper json file" { Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" - } - It "Checks all the verifiable Mock calls" { Assert-VerifiableMocks } } @@ -53,7 +51,7 @@ Describe Get-AzProfile { Mock Get-AzureProfileMap { $global:testProfileMap } It "Should get ProfileMap from Azure Endpoint" { - Get-AzProfile -Force | Should Be "{`"profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" + Get-AzProfile -Update | Should Be "{`"profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" } It "Checks Mock calls to Get-AzureProfileMap" { Assert-MockCalled Get-AzureProfileMap -Exactly 1 @@ -67,8 +65,6 @@ Describe Get-AzProfile { It "Should get ProfileMap from Cache" { Get-AzProfile | Should Be "@{profile1=; profile2=}" - } - It "Checks all verifiable mocks" { Assert-VerifiableMocks } } @@ -80,8 +76,6 @@ Describe Get-AzProfile { It "Should get ProfileMap from Embedded source" { Get-AzProfile | Should Be "@{profile1=; profile2=}" - } - It "Checks all verifiable mocks" { Assert-VerifiableMocks } } @@ -93,8 +87,6 @@ Describe Get-AzProfile { It "Should throw FileNotFound Exception" { { Get-AzProfile } | Should Throw - } - It "Checks all verifiable mocks" { Assert-VerifiableMocks } } @@ -144,6 +136,14 @@ Describe "Get-AzureRmModule" { Assert-VerifiableMocks } } + + Context "Module not in the list" { + Mock Get-Module -Verifiable { @( [PSCustomObject] @{ Name='Module1'; Version='1.0'; RepositorySourceLocation='foo\bar' }, [PSCustomObject] @{ Name='Module1'; Version='2.0'}) } + It "Should return null" { + Get-AzureRmModule -Profile 'Profile2' -Module 'Module2' | Should be $null + Assert-VerifiableMocks + } + } } } @@ -160,9 +160,9 @@ Describe "Get-AzureRmProfile" { } } - Context "With ListAvailable and Force Switches" { + Context "With ListAvailable and update Switches" { It "Should return available profiles" { - $Result = (Get-AzureRmProfile -ListAvailable -Force) + $Result = (Get-AzureRmProfile -ListAvailable -Update) $Result.Length | Should Be 14 $Result[0] | Should Be "Profile: Profile1" Assert-VerifiableMocks @@ -209,9 +209,11 @@ Describe "Use-AzureRmProfile" { Context "Modules are installed" { Mock Get-AzureRmModule -Verifiable { "1.0" } -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} + Mock Import-Module { "Module1 1.0 Imported"} -ParameterFilter { $Name -eq "Module1" -and $RequiredVersion -eq "1.0"} It "Should skip installing modules" { - (Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Importing Module..." + (Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Module1 1.0 Imported" Assert-MockCalled Install-Module -Exactly 0 + Assert-MockCalled Import-Module -Exactly 1 Assert-VerifiableMocks } } From e31c14afe566f1cfdb4de3f647a5c1e273825172 Mon Sep 17 00:00:00 2001 From: Vishnu Priya Ananthu Sundaram Date: Wed, 16 Nov 2016 16:10:44 -0800 Subject: [PATCH 3/3] Adding more unit tests. This closes #14 --- .../AzureRM.BootStrapper.Tests.ps1 | 141 +++++++++++++++++- 1 file changed, 134 insertions(+), 7 deletions(-) diff --git a/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 index 9105aaa90ea3..6649e3b51858 100644 --- a/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 +++ b/tools/AzureRM.BootStrapper/AzureRM.BootStrapper.Tests.ps1 @@ -15,7 +15,7 @@ Describe "Get-AzureProfileMap" { Mock Get-ProfileCachePath -Verifiable { return "MockPath\ProfileCache"} Mock Invoke-RestMethod -Verifiable { return "Invoking ProfileMapEndPoint... Receiving ProfileMap.json"} Mock Get-Content -Verifiable { return $testProfileMap } - + Context "ProfilePath does not exist" { It "Returns the proper json file" { Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" @@ -41,6 +41,23 @@ Describe "Get-AzureProfileMap" { { Get-AzureProfileMap } | Should throw } } + + Context "Invoke-RestMethod returns an error" { + $Response = New-Object System.Net.HttpWebResponse + $Exception = New-Object System.Net.WebException "Test Error Occurred.", (New-Object System.Exception), ConnectFailure, $Response + $ErrorRecord = new-object System.Management.Automation.ErrorRecord $Exception, "TestError", ConnectionError, $null + + $RestError = @() + $object = New-Object -TypeName psobject + $object | Add-Member -Name ErrorRecord -MemberType NoteProperty -value $ErrorRecord + $RestError += $object + + Mock Invoke-RestMethod {} + Mock Get-RestResponse { return $RestError } + It "Throws custom exception" { + { Get-AzureProfileMap } | Should throw "Http Status Code:" + } + } } } @@ -117,6 +134,17 @@ Describe "Add-ForceParam" { } } +Describe "Add-SwitchParam" { + InModuleScope AzureRM.Bootstrapper { + $params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary + + It "Should return Switch parameter object" { + Add-SwitchParam $params "TestParam" + $params.ContainsKey("TestParam") | Should Be $true + } + } +} + Describe "Get-AzureRmModule" { InModuleScope AzureRM.Bootstrapper { Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } @@ -144,6 +172,18 @@ Describe "Get-AzureRmModule" { Assert-VerifiableMocks } } + + Context "Invoke with invalid parameters" { + It "Should throw" { + { Get-AzureRmModule -Profile 'XYZ' -Module 'ABC' } | Should Throw + } + } + + Context "Invoke with null parameters" { + It "Should throw" { + { Get-AzureRmModule -Profile $null -Module $null } | Should Throw + } + } } } @@ -195,7 +235,7 @@ Describe "Use-AzureRmProfile" { Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } Mock Install-Module { "Installing module..."} Mock Import-Module -Verifiable { "Importing Module..."} - + Context "Modules not installed" { Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} It "Should install modules" { @@ -210,13 +250,25 @@ Describe "Use-AzureRmProfile" { Context "Modules are installed" { Mock Get-AzureRmModule -Verifiable { "1.0" } -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} Mock Import-Module { "Module1 1.0 Imported"} -ParameterFilter { $Name -eq "Module1" -and $RequiredVersion -eq "1.0"} - It "Should skip installing modules" { + It "Should skip installing modules, imports the right version module" { (Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Module1 1.0 Imported" Assert-MockCalled Install-Module -Exactly 0 Assert-MockCalled Import-Module -Exactly 1 Assert-VerifiableMocks } } + + Context "Invoke with invalid profile" { + It "Should throw" { + { Use-AzureRmProfile -Profile 'WrongProfileName'} | Should Throw + } + } + + Context "Invoke with $null profile" { + It "Should throw" { + { Use-AzureRmProfile -Profile $null} | Should Throw + } + } } } @@ -224,10 +276,85 @@ Describe "Install-AzureRmProfile" { InModuleScope AzureRM.Bootstrapper { $RollupModule = 'Module1' Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } - Mock Install-Module -Verifiable { "Installing module..."} - It "Should install RollupModule" { - (Install-AzureRmProfile -Profile 'profile1') | Should be "Installing module..." - Assert-VerifiableMocks + + Context "Invoke with valid profile name" { + Mock Install-Module -Verifiable { "Installing module $RollupModule... Version 1.0"} -ParameterFilter { $Name -eq $RollupModule -and $RequiredVersion -eq '1.0' } + It "Should install RollupModule" { + (Install-AzureRmProfile -Profile 'Profile1') | Should be "Installing module $RollupModule... Version 1.0" + Assert-VerifiableMocks + } + } + + Context "Invoke with invalid profile name" { + It "Should throw" { + { Install-AzureRmProfile -Profile 'WrongProfileName'} | Should Throw + } + } + + Context "Invoke with null profile name" { + It "Should throw" { + { Install-AzureRmProfile -Profile $null } | Should Throw + } + } + } +} + +Describe "Uninstall-AzureRmProfile" { + InModuleScope AzureRM.Bootstrapper { + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } + Mock Remove-Module -Verifiable { "Removing module from session..." } + Mock Uninstall-Module -Verifiable { "Uninstalling module..." } + + Context "Modules are installed" { + It "Should remove and uninstall modules" { + # Arrange + $Script:mockCalled = 0 + $mockTestPath = { + $Script:mockCalled++ + if ($Script:mockCalled -eq 1) + { + return "1.0" + } + else { + return $null + } + } + + Mock -CommandName Get-AzureRmModule -MockWith $mockTestPath + + # Act + $callResult = (Uninstall-AzureRmProfile -Profile 'Profile1' -Force) + + # Assert + $Script:mockCalled | Should Be 3 + $callResult[0] | Should Be "Removing module from session..." + $callResult[1] | Should Be "Uninstalling module..." + Assert-VerifiableMocks + } + } + + Context "Modules are not installed" { + It "Should not call Uninstall-Module" { + Mock Get-AzureRmModule -Verifiable {} + $callResult = (Uninstall-AzureRmProfile -Profile 'Profile1' -Force) + + Assert-MockCalled Get-AzureRmModule -Exactly 2 + Assert-MockCalled Remove-Module -Exactly 0 + Assert-MockCalled Uninstall-Module -Exactly 0 + Assert-VerifiableMocks + } + } + + Context "Invoke with invalid profile name" { + It "Should throw" { + { Uninstall-AzureRmProfile -Profile 'WrongProfileName' } | Should Throw + } + } + + Context "Invoke with null profile name" { + It "Should throw" { + { Uninstall-AzureRmProfile -Profile $null } | Should Throw + } } } }