From 2bb82249cab5cd86a2ba46deabd0554139e62830 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sun, 24 Feb 2019 14:41:16 +0000 Subject: [PATCH] Added new tests for get default creds Added test for not connected route in functions to increase coverage. --- .../Functions/Connect-VMCVIServer.tests.ps1 | 7 ++ Pester/Functions/Get-VMCOrg.tests.ps1 | 17 +++- .../Get-VMCSDDCDefaultCredential.tests.ps1 | 98 +++++++++++++++++++ Pester/Functions/Get-VmcSddc.tests.ps1 | 7 ++ Pester/Functions/Get-VmcTask.tests.ps1 | 7 ++ 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 Pester/Functions/Get-VMCSDDCDefaultCredential.tests.ps1 diff --git a/Pester/Functions/Connect-VMCVIServer.tests.ps1 b/Pester/Functions/Connect-VMCVIServer.tests.ps1 index b8d87f0..a98a277 100644 --- a/Pester/Functions/Connect-VMCVIServer.tests.ps1 +++ b/Pester/Functions/Connect-VMCVIServer.tests.ps1 @@ -30,6 +30,8 @@ inModuleScope VMware.VMC { Mock Connect-CisServer {} + Mock Write-Error + Context "Sanity checking" { $command = Get-Command -Name $functionName @@ -57,6 +59,11 @@ inModuleScope VMware.VMC { -and $User -eq $cloud_username ` -and $Password -eq $Mockedcreds.password } } + It "gets writes an error if not connected" { + $global:DefaultVMCServers = $false + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Write-Error -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } } } } \ No newline at end of file diff --git a/Pester/Functions/Get-VMCOrg.tests.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 index 983703e..2a8c589 100644 --- a/Pester/Functions/Get-VMCOrg.tests.ps1 +++ b/Pester/Functions/Get-VMCOrg.tests.ps1 @@ -30,6 +30,8 @@ inModuleScope VMware.VMC { Mock -CommandName Get-VMCService -MockWith { $object } + Mock -CommandName Write-Error -MockWith {} + Context "Sanity checking" { $command = Get-Command -Name $functionName @@ -46,11 +48,11 @@ inModuleScope VMware.VMC { It "gets the orgs via list method and returns the properties" { $object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $(Get-VMCOrg -name $OrgName).display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName).name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName).user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName).created | Should -be $created - $(Get-VMCOrg -name $OrgName).id | Should -be $id + $(Get-VMCOrg).display_name | Should -be $display_name + $(Get-VMCOrg).name | Should -be $OrgName + $(Get-VMCOrg).user_name | Should -be $user_name + $(Get-VMCOrg).created | Should -be $created + $(Get-VMCOrg).id | Should -be $id } # Testing the multiple SDDC response It "calls the Connect-CisServer" { @@ -70,6 +72,11 @@ inModuleScope VMware.VMC { $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id } + It "gets writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCOrg -name $OrgName } | Should Not Throw + Assert-MockCalled -CommandName Write-Error -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } } } } \ No newline at end of file diff --git a/Pester/Functions/Get-VMCSDDCDefaultCredential.tests.ps1 b/Pester/Functions/Get-VMCSDDCDefaultCredential.tests.ps1 new file mode 100644 index 0000000..c0a1393 --- /dev/null +++ b/Pester/Functions/Get-VMCSDDCDefaultCredential.tests.ps1 @@ -0,0 +1,98 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCSDDCDefaultCredential" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $OrgId = "Mocked OrgID" + $SddcName = "MockedSDDCName" + $vc_url = "Mockedvc_url" + $vc_management_ip = "MockedVCmanage_ip" + $vc_public_ip = "MockedVCpublic_ip" + $cloud_username = "MockedCloudUser" + $cloud_password = "MockedCloudPass" + + $MockedList = [PSCustomObject]@{ + "vc_url" = $vc_url + "vc_management_ip" = $vc_management_ip + "vc_public_ip" = $vc_public_ip + "cloud_username" = $cloud_username + "cloud_password" = $cloud_password + } + $MockedList2 = [PSCustomObject]@{ + "vc_url" = $vc_url + "vc_management_ip" = $vc_management_ip + "vc_public_ip" = $vc_public_ip + "cloud_username" = $cloud_username + "cloud_password" = $cloud_password + } + + $object = [PSCustomObject]@{ + "resource_config" = @($MockedList) + } + + $MockedArray = @{ + "resource_config" = @($MockedList, $MockedList2) + } + + Mock -CommandName Get-VMCSDDC -MockWith { $object } + + Mock -CommandName Write-Error -MockWith {} + + Mock -CommandName Select-Object { $MockedList } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Org' + defParam $command 'Sddc' + } + + Context "Behavior testing" { + # Testing single Org with optional SDDC parameter + It "calls Get-VMCSDDC with the Org name supplied" { + { Get-VMCSDDCDefaultCredential -Org $OrgId -sddc $SddcName} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCSDDC -Times 1 -Scope It -ParameterFilter { $Org -eq $OrgId -and $name -eq $SddcName } + } + # Testing single Org without SDDC parameter. + It "calls get-VMCSDDC without SDDC name supplied" { + { Get-VMCSDDCDefaultCredential -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCSDDC -Times 1 -Scope It -ParameterFilter { $org -eq $OrgId } + } + # Testing a single SDDC response + It "gets the task details via list method and returns the properties" { + $(Get-VMCSDDCDefaultCredential -Org $OrgId).vc_url | Should -be $vc_url + $(Get-VMCSDDCDefaultCredential -Org $OrgId).vc_management_ip | Should -be $vc_management_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId).vc_public_ip | Should -be $vc_public_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId).cloud_username | Should -be $cloud_username + $(Get-VMCSDDCDefaultCredential -Org $OrgId).cloud_password | Should -be $cloud_password + Assert-MockCalled -CommandName Select-Object -Times 1 -Scope It + } + # Testing the multiple SDDC response + It "gets the task details of the Org supplied and returns the properties" { + Mock -CommandName Get-VMCSDDC -MockWith { $MockedArray } + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[0].vc_url | Should -be $vc_url + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[0].vc_management_ip | Should -be $vc_management_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[0].vc_public_ip | Should -be $vc_public_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[0].cloud_username | Should -be $cloud_username + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[0].cloud_password | Should -be $cloud_password + + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[1].vc_url | Should -be $vc_url + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[1].vc_management_ip | Should -be $vc_management_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[1].vc_public_ip | Should -be $vc_public_ip + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[1].cloud_username | Should -be $cloud_username + $(Get-VMCSDDCDefaultCredential -Org $OrgId)[1].cloud_password | Should -be $cloud_password + Assert-MockCalled -CommandName Select-Object -Times 2 -Scope It + } + It "writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCSDDCDefaultCredential -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Write-Error -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } + } + } +} \ No newline at end of file diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 index 3bc7d09..f5182b9 100644 --- a/Pester/Functions/Get-VmcSddc.tests.ps1 +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -31,6 +31,8 @@ inModuleScope VMware.VMC { Mock -CommandName Get-VMCOrg { $object } + Mock -CommandName Write-Error -MockWith {} + Context "Sanity checking" { $command = Get-Command -Name $functionName @@ -77,6 +79,11 @@ inModuleScope VMware.VMC { $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } $(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $name } + It "gets writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Write-Error -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } } } } \ No newline at end of file diff --git a/Pester/Functions/Get-VmcTask.tests.ps1 b/Pester/Functions/Get-VmcTask.tests.ps1 index 0ee9f19..5f0ab11 100644 --- a/Pester/Functions/Get-VmcTask.tests.ps1 +++ b/Pester/Functions/Get-VmcTask.tests.ps1 @@ -32,6 +32,8 @@ inModuleScope VMware.VMC { Mock -CommandName Get-VMCOrg { $object } + Mock -CommandName Write-Error -MockWith {} + Context "Sanity checking" { $command = Get-Command -Name $functionName @@ -75,6 +77,11 @@ inModuleScope VMware.VMC { $(Get-VMCTask -Org $OrgId)[0].name | Should -be $name $(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname } + It "gets writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCTask -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Write-Error -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } } } } \ No newline at end of file