|
| 1 | +Import-Module -Name AzureRM.Bootstrapper |
| 2 | +$global:testProfileMap = "{`"Profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"Profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" |
| 3 | + |
| 4 | +Describe "Get-ProfileCachePath" { |
| 5 | + Context "Gets the correct profile cache path" { |
| 6 | + It "Should return proper path" { |
| 7 | + Get-ProfileCachePath | Should Match "(.*)ProfileCache$" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +Describe "Get-AzureProfileMap" { |
| 13 | + InModuleScope AzureRM.Bootstrapper { |
| 14 | + Mock New-Item -Verifiable { return "Creating ProfilePath"} |
| 15 | + Mock Get-ProfileCachePath -Verifiable { return "MockPath\ProfileCache"} |
| 16 | + Mock Invoke-RestMethod -Verifiable { return "Invoking ProfileMapEndPoint... Receiving ProfileMap.json"} |
| 17 | + Mock Get-Content -Verifiable { return $testProfileMap } |
| 18 | + |
| 19 | + Context "ProfilePath does not exist" { |
| 20 | + It "Returns the proper json file" { |
| 21 | + Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" |
| 22 | + Assert-VerifiableMocks |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + Context "ProfilePath Exists" { |
| 27 | + Mock Test-Path { $true } |
| 28 | + Mock New-Item {} |
| 29 | + It "Returns Correct ProfileMap" { |
| 30 | + Get-AzureProfileMap | Should Be "@{profile1=; profile2=}" |
| 31 | + } |
| 32 | + It "Should not call New-Item" { |
| 33 | + Assert-MockCalled New-Item -Exactly 0 |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + Context "Invoke-RestMethod throws exception" { |
| 38 | + Mock Invoke-RestMethod { throw [System.Net.WebException] } |
| 39 | + |
| 40 | + It "Throws Web Exception" { |
| 41 | + { Get-AzureProfileMap } | Should throw |
| 42 | + } |
| 43 | + } |
| 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 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +Describe Get-AzProfile { |
| 65 | + InModuleScope AzureRM.Bootstrapper { |
| 66 | + |
| 67 | + Context "Forces update from Azure Endpoint" { |
| 68 | + Mock Get-AzureProfileMap { $global:testProfileMap } |
| 69 | + |
| 70 | + It "Should get ProfileMap from Azure Endpoint" { |
| 71 | + Get-AzProfile -Update | Should Be "{`"profile1`": { `"Module1`": [`"1.0`"], `"Module2`": [`"1.0`"] }, `"profile2`": { `"Module1`": [`"2.0`"], `"Module2`": [`"2.0`"] }}" |
| 72 | + } |
| 73 | + It "Checks Mock calls to Get-AzureProfileMap" { |
| 74 | + Assert-MockCalled Get-AzureProfileMap -Exactly 1 |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Context "Gets Azure ProfileMap from Cache" { |
| 79 | + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } |
| 80 | + Mock Test-Path -Verifiable { $true } |
| 81 | + Mock Get-Content -Verifiable { return $global:testProfileMap } |
| 82 | + |
| 83 | + It "Should get ProfileMap from Cache" { |
| 84 | + Get-AzProfile | Should Be "@{profile1=; profile2=}" |
| 85 | + Assert-VerifiableMocks |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Context "ProfileMap is not available from cache" { |
| 90 | + Mock Test-Path -Verifiable { $false } |
| 91 | + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } |
| 92 | + Mock Get-Content -Verifiable { return $global:testProfileMap } |
| 93 | + |
| 94 | + It "Should get ProfileMap from Embedded source" { |
| 95 | + Get-AzProfile | Should Be "@{profile1=; profile2=}" |
| 96 | + Assert-VerifiableMocks |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + Context "ProfileMap is not available in cache or Embedded source" { |
| 101 | + Mock Test-Path -Verifiable { $false } |
| 102 | + Mock Get-ProfileCachePath -Verifiable { "MockPath\ProfileCache" } |
| 103 | + Mock Get-Content -Verifiable {} |
| 104 | + |
| 105 | + It "Should throw FileNotFound Exception" { |
| 106 | + { Get-AzProfile } | Should Throw |
| 107 | + Assert-VerifiableMocks |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +Describe "Add-ProfileParam" { |
| 114 | + InModuleScope AzureRM.Bootstrapper { |
| 115 | + $params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary |
| 116 | + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } |
| 117 | + |
| 118 | + It "Should return Profile parameter object" { |
| 119 | + (Add-ProfileParam $params) |
| 120 | + $params.ContainsKey("Profile") | Should Be $true |
| 121 | + Assert-VerifiableMocks |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +Describe "Add-ForceParam" { |
| 127 | + InModuleScope AzureRM.Bootstrapper { |
| 128 | + $params = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary |
| 129 | + |
| 130 | + It "Should return Force parameter object" { |
| 131 | + Add-ForceParam $params |
| 132 | + $params.ContainsKey("Force") | Should Be $true |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 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 | + |
| 148 | +Describe "Get-AzureRmModule" { |
| 149 | + InModuleScope AzureRM.Bootstrapper { |
| 150 | + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } |
| 151 | + |
| 152 | + Context "Module is installed" { |
| 153 | + Mock Get-Module -Verifiable { @( [PSCustomObject] @{ Name='Module1'; Version='1.0'; RepositorySourceLocation='foo\bar' }, [PSCustomObject] @{ Name='Module1'; Version='2.0'}) } |
| 154 | + It "Should return installed version" { |
| 155 | + Get-AzureRmModule -Profile 'Profile1' -Module 'Module1' | Should Be "1.0" |
| 156 | + Assert-VerifiableMocks |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + Context "Module is not installed" { |
| 161 | + Mock Get-Module -Verifiable {} |
| 162 | + It "Should return null" { |
| 163 | + Get-AzureRmModule -Profile 'Profile1' -Module 'Module1' | Should be $null |
| 164 | + Assert-VerifiableMocks |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + Context "Module not in the list" { |
| 169 | + Mock Get-Module -Verifiable { @( [PSCustomObject] @{ Name='Module1'; Version='1.0'; RepositorySourceLocation='foo\bar' }, [PSCustomObject] @{ Name='Module1'; Version='2.0'}) } |
| 170 | + It "Should return null" { |
| 171 | + Get-AzureRmModule -Profile 'Profile2' -Module 'Module2' | Should be $null |
| 172 | + Assert-VerifiableMocks |
| 173 | + } |
| 174 | + } |
| 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 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +Describe "Get-AzureRmProfile" { |
| 191 | + InModuleScope AzureRM.Bootstrapper { |
| 192 | + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } |
| 193 | + |
| 194 | + Context "With ListAvailable Switch" { |
| 195 | + It "Should return available profiles" { |
| 196 | + $Result = (Get-AzureRmProfile -ListAvailable) |
| 197 | + $Result.Length | Should Be 14 |
| 198 | + $Result[0] | Should Be "Profile: Profile1" |
| 199 | + Assert-VerifiableMocks |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + Context "With ListAvailable and update Switches" { |
| 204 | + It "Should return available profiles" { |
| 205 | + $Result = (Get-AzureRmProfile -ListAvailable -Update) |
| 206 | + $Result.Length | Should Be 14 |
| 207 | + $Result[0] | Should Be "Profile: Profile1" |
| 208 | + Assert-VerifiableMocks |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + Context "Without ListAvailable Switch" { |
| 213 | + $RollupModule = 'Module1' |
| 214 | + Mock Get-AzureRmModule -Verifiable { "1.0"} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} |
| 215 | + It "Returns installed Profile" { |
| 216 | + (Get-AzureRmProfile) | Should Be "Profile1" |
| 217 | + Assert-VerifiableMocks |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + Context "No profiles installed" { |
| 222 | + $RollupModule = 'Module1' |
| 223 | + Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} |
| 224 | + It "Returns null" { |
| 225 | + (Get-AzureRmProfile) | Should Be $null |
| 226 | + Assert-VerifiableMocks |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +Describe "Use-AzureRmProfile" { |
| 233 | + InModuleScope AzureRM.Bootstrapper { |
| 234 | + $RollupModule = 'Module1' |
| 235 | + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } |
| 236 | + Mock Install-Module { "Installing module..."} |
| 237 | + Mock Import-Module -Verifiable { "Importing Module..."} |
| 238 | + |
| 239 | + Context "Modules not installed" { |
| 240 | + Mock Get-AzureRmModule -Verifiable {} -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} |
| 241 | + It "Should install modules" { |
| 242 | + $Result = (Use-AzureRmProfile -Profile 'Profile1' -Force) |
| 243 | + $Result.Length | Should Be 2 |
| 244 | + $Result[0] | Should Be "Installing module..." |
| 245 | + $Result[1] | Should Be "Importing Module..." |
| 246 | + Assert-VerifiableMocks |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + Context "Modules are installed" { |
| 251 | + Mock Get-AzureRmModule -Verifiable { "1.0" } -ParameterFilter {$Profile -eq "Profile1" -and $Module -eq "Module1"} |
| 252 | + Mock Import-Module { "Module1 1.0 Imported"} -ParameterFilter { $Name -eq "Module1" -and $RequiredVersion -eq "1.0"} |
| 253 | + It "Should skip installing modules, imports the right version module" { |
| 254 | + (Use-AzureRmProfile -Profile 'Profile1' -Force) | Should Be "Module1 1.0 Imported" |
| 255 | + Assert-MockCalled Install-Module -Exactly 0 |
| 256 | + Assert-MockCalled Import-Module -Exactly 1 |
| 257 | + Assert-VerifiableMocks |
| 258 | + } |
| 259 | + } |
| 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 | + } |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +Describe "Install-AzureRmProfile" { |
| 276 | + InModuleScope AzureRM.Bootstrapper { |
| 277 | + $RollupModule = 'Module1' |
| 278 | + Mock Get-AzProfile -Verifiable { ($global:testProfileMap | ConvertFrom-Json) } |
| 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 | + } |
| 358 | + } |
| 359 | + } |
| 360 | +} |
0 commit comments