From f9ca007ae5faded62b347e778b0ab0105e88611e Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Mon, 18 Feb 2019 21:57:52 +0000 Subject: [PATCH 01/14] Added two pester tests for Get-VMCCommand and Connect-VMCVIServer. --- Modules/VMware.VMC/VMware.VMC.psm1 | 2 +- .../Functions/Connect-VMCVIServer.tests.ps1 | 57 +++++++++++++++++++ Pester/Functions/Get-VMCCommand.tests.ps1 | 20 +++++++ Pester/Functions/Shared.ps1 | 6 ++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 Pester/Functions/Connect-VMCVIServer.tests.ps1 create mode 100644 Pester/Functions/Get-VMCCommand.tests.ps1 create mode 100644 Pester/Functions/Shared.ps1 diff --git a/Modules/VMware.VMC/VMware.VMC.psm1 b/Modules/VMware.VMC/VMware.VMC.psm1 index 33f78a7..538501e 100644 --- a/Modules/VMware.VMC/VMware.VMC.psm1 +++ b/Modules/VMware.VMC/VMware.VMC.psm1 @@ -20,7 +20,7 @@ Function Get-VMCCommand { .NOTES You can either use this cmdlet or the Get-Command cmdlet as seen in Example 2 #> - Get-command -Module VMware.VimAutomation.Vmc + Get-command -Module VimppppppMware.VimAutomation.Vmc Get-Command -Module VMware.VMC } diff --git a/Pester/Functions/Connect-VMCVIServer.tests.ps1 b/Pester/Functions/Connect-VMCVIServer.tests.ps1 new file mode 100644 index 0000000..4a7feec --- /dev/null +++ b/Pester/Functions/Connect-VMCVIServer.tests.ps1 @@ -0,0 +1,57 @@ +#Requires -Modules Pester, VMware.VMC +$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") +Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $Org = 'MyOrg' + $Sddc = 'MySddc' + + $global:DefaultVMCServers = $true + + $secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force + $Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd) + $cloud_username = "MockedUserName" + $vc_public_ip = "MockedServer" + + Mock Get-VMCSDDCDefaultCredential { + $object = [PSCustomObject] @{ + 'vc_public_ip' = $vc_public_ip + 'cloud_username' = $cloud_username + 'cloud_password' = $Mockedcreds.Password + } + return $object + } + + Mock Connect-VIServer {} + + Mock Connect-CisServer {} + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Org' + defParam $command 'Sddc' + defParam $command 'Autologin' + } + + Context "Behavior testing" { + It "gets creds via Get-VMCSDDCDefaultCredential" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCSDDCDefaultCredential -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } + It "calls the Connect-VIServer" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Connect-VIServer -Times 1 -Scope It -ParameterFilter { ` + $Server -eq $vc_public_ip ` + -and $User -eq $cloud_username ` + -and $Password -eq $Mockedcreds.Password } + } + It "calls the Connect-CisServer" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Connect-CisServer -Times 1 -Scope It -ParameterFilter { ` + $Server -eq $vc_public_ip ` + -and $User -eq $cloud_username ` + -and $Password -eq $Mockedcreds.password } + } + } +} diff --git a/Pester/Functions/Get-VMCCommand.tests.ps1 b/Pester/Functions/Get-VMCCommand.tests.ps1 new file mode 100644 index 0000000..230594c --- /dev/null +++ b/Pester/Functions/Get-VMCCommand.tests.ps1 @@ -0,0 +1,20 @@ +#Requires -Modules Pester, VMware.VMC, VMware.VimAutomation.Vmc +$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") + +Describe "$functionName" -Tag 'Unit' { + Mock Get-Command { + "Mocked Command Response" + } + + Context "Behavior testing" { + It "should call get-command on VMware.VimAutomation.Vmc" { + { Get-VMCCommand } | Should Not Throw + Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VimAutomation.Vmc' } + + } + It "should call get-command on VMware.Vmc" { + { Get-VMCCommand } | Should Not Throw + Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VMC' } + } + } +} \ No newline at end of file diff --git a/Pester/Functions/Shared.ps1 b/Pester/Functions/Shared.ps1 new file mode 100644 index 0000000..d49d97c --- /dev/null +++ b/Pester/Functions/Shared.ps1 @@ -0,0 +1,6 @@ +#Requires -Modules Pester +function defParam($command, $name) { + It "Has a -$name parameter" { + $command.Parameters.Item($name) | Should Not BeNullOrEmpty + } +} \ No newline at end of file From 783319a624a296e391bd4c05b43ce9e44d94147c Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Wed, 20 Feb 2019 22:17:59 +0000 Subject: [PATCH 02/14] Finished org tests --- Pester/Functions/Get-VMCOrg.tests.1.ps1 | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Pester/Functions/Get-VMCOrg.tests.1.ps1 diff --git a/Pester/Functions/Get-VMCOrg.tests.1.ps1 b/Pester/Functions/Get-VMCOrg.tests.1.ps1 new file mode 100644 index 0000000..19d79fc --- /dev/null +++ b/Pester/Functions/Get-VMCOrg.tests.1.ps1 @@ -0,0 +1,73 @@ +#Requires -Modules Pester, VMware.VMC +$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") +Import-Module -Name VMware.VimAutomation.Cis.Core + +Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $display_name = "MockedDisplayName" + $user_name = "MockedUserName" + $OrgName = "MockedDisplayName" + $created = "MockedDate" + $id = "MockedId" + $Service = "com.vmware.vmc.orgs" + + $MockedList = [PSCustomObject]@{ + "display_name" = $display_name + "name" = $OrgName + "created" = $created + "user_name" = $user_name + "id" = $id + } + #$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + + $MockedArray = @() + $MockedArray += $MockedList + $MockedArray += $MockedList + + Mock -CommandName Get-VMCService -MockWith { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Name' + } + + Context "Behavior testing" { + It "calls get-service to com.vmware.vmc.orgs" { + { Get-VMCOrg -name $OrgName } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response + 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 + } + # Testing the multiple SDDC response + It "calls the Connect-CisServer" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCOrg -name $OrgName } | Should Not Throw + + $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id + + $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + } + } +} From 3a61850b41ac8927195a6694ba7e9c82825ad452 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Wed, 20 Feb 2019 22:26:26 +0000 Subject: [PATCH 03/14] Randomly wrote over import. undone. --- Modules/VMware.VMC/VMware.VMC.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/VMware.VMC/VMware.VMC.psm1 b/Modules/VMware.VMC/VMware.VMC.psm1 index 538501e..33f78a7 100644 --- a/Modules/VMware.VMC/VMware.VMC.psm1 +++ b/Modules/VMware.VMC/VMware.VMC.psm1 @@ -20,7 +20,7 @@ Function Get-VMCCommand { .NOTES You can either use this cmdlet or the Get-Command cmdlet as seen in Example 2 #> - Get-command -Module VimppppppMware.VimAutomation.Vmc + Get-command -Module VMware.VimAutomation.Vmc Get-Command -Module VMware.VMC } From 6c578f7b78899453bc1b5b62875d4f406cbae22d Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 16:37:03 +0000 Subject: [PATCH 04/14] Added mock write-host to clean up output. Added the general creation of the $object needed to mock the function. --- Pester/Functions/Connect-VMCVIServer.tests.ps1 | 2 ++ Pester/Functions/Get-VMCOrg.tests.1.ps1 | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Pester/Functions/Connect-VMCVIServer.tests.ps1 b/Pester/Functions/Connect-VMCVIServer.tests.ps1 index 4a7feec..2448ab3 100644 --- a/Pester/Functions/Connect-VMCVIServer.tests.ps1 +++ b/Pester/Functions/Connect-VMCVIServer.tests.ps1 @@ -22,6 +22,8 @@ Describe "$functionName" -Tag 'Unit' { return $object } + Mock Write-host {} + Mock Connect-VIServer {} Mock Connect-CisServer {} diff --git a/Pester/Functions/Get-VMCOrg.tests.1.ps1 b/Pester/Functions/Get-VMCOrg.tests.1.ps1 index 19d79fc..6f451aa 100644 --- a/Pester/Functions/Get-VMCOrg.tests.1.ps1 +++ b/Pester/Functions/Get-VMCOrg.tests.1.ps1 @@ -21,8 +21,9 @@ Describe "$functionName" -Tag 'Unit' { "user_name" = $user_name "id" = $id } - #$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } $MockedArray = @() $MockedArray += $MockedList @@ -37,6 +38,7 @@ Describe "$functionName" -Tag 'Unit' { } Context "Behavior testing" { + It "calls get-service to com.vmware.vmc.orgs" { { Get-VMCOrg -name $OrgName } | Should Not Throw Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } From a95e01066d920969371b3cde4d7b00d29b583410 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 16:40:31 +0000 Subject: [PATCH 05/14] Renamed VMC Org test file. Added VMC Sddc test file. --- ...MCOrg.tests.1.ps1 => Get-VMCOrg.tests.ps1} | 0 Pester/Functions/Get-VmcSddc.tests.ps1 | 75 +++++++++++++++++++ 2 files changed, 75 insertions(+) rename Pester/Functions/{Get-VMCOrg.tests.1.ps1 => Get-VMCOrg.tests.ps1} (100%) create mode 100644 Pester/Functions/Get-VmcSddc.tests.ps1 diff --git a/Pester/Functions/Get-VMCOrg.tests.1.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 similarity index 100% rename from Pester/Functions/Get-VMCOrg.tests.1.ps1 rename to Pester/Functions/Get-VMCOrg.tests.ps1 diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 new file mode 100644 index 0000000..6f451aa --- /dev/null +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -0,0 +1,75 @@ +#Requires -Modules Pester, VMware.VMC +$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") +Import-Module -Name VMware.VimAutomation.Cis.Core + +Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $display_name = "MockedDisplayName" + $user_name = "MockedUserName" + $OrgName = "MockedDisplayName" + $created = "MockedDate" + $id = "MockedId" + $Service = "com.vmware.vmc.orgs" + + $MockedList = [PSCustomObject]@{ + "display_name" = $display_name + "name" = $OrgName + "created" = $created + "user_name" = $user_name + "id" = $id + } + + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + $MockedArray = @() + $MockedArray += $MockedList + $MockedArray += $MockedList + + Mock -CommandName Get-VMCService -MockWith { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Name' + } + + Context "Behavior testing" { + + It "calls get-service to com.vmware.vmc.orgs" { + { Get-VMCOrg -name $OrgName } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response + 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 + } + # Testing the multiple SDDC response + It "calls the Connect-CisServer" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCOrg -name $OrgName } | Should Not Throw + + $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id + + $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + } + } +} From f642290b2d2548c182cf99b152e7e5f8838ce7f1 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 17:41:23 +0000 Subject: [PATCH 06/14] Tidied up the object creation in tests. --- Pester/Functions/Get-VMCOrg.tests.ps1 | 4 +- Pester/Functions/Get-VmcSddc.tests.ps1 | 78 ++++++++++++++++---------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Pester/Functions/Get-VMCOrg.tests.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 index 6f451aa..0726f5a 100644 --- a/Pester/Functions/Get-VMCOrg.tests.ps1 +++ b/Pester/Functions/Get-VMCOrg.tests.ps1 @@ -25,9 +25,7 @@ Describe "$functionName" -Tag 'Unit' { $object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $MockedArray = @() - $MockedArray += $MockedList - $MockedArray += $MockedList + $MockedArray = @($MockedList, $MockedList) Mock -CommandName Get-VMCService -MockWith { $object } diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 index 6f451aa..7d848a3 100644 --- a/Pester/Functions/Get-VmcSddc.tests.ps1 +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -9,67 +9,85 @@ Describe "$functionName" -Tag 'Unit' { $display_name = "MockedDisplayName" $user_name = "MockedUserName" - $OrgName = "MockedDisplayName" + $OrgName = "MockedOrgName" $created = "MockedDate" + $OrgId = "Mocked OrgID" + $name = "MockedSDDCName" + $Notname = "NotTheName" $id = "MockedId" - $Service = "com.vmware.vmc.orgs" + $Service = "com.vmware.vmc.orgs.sddcs" $MockedList = [PSCustomObject]@{ "display_name" = $display_name - "name" = $OrgName + "name" = $name + "created" = $created + "user_name" = $user_name + "id" = $id + } + $MockedList2 = [PSCustomObject]@{ + "display_name" = $display_name + "name" = $Notname "created" = $created "user_name" = $user_name "id" = $id } - $object = [PSCustomObject]@{} + $object = @( + @{"Id" = $Id} + ) $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $MockedArray = @() - $MockedArray += $MockedList - $MockedArray += $MockedList + $MockedArray = @($MockedList, $MockedList2) Mock -CommandName Get-VMCService -MockWith { $object } + Mock -CommandName Get-VMCOrg { $object } + Context "Sanity checking" { $command = Get-Command -Name $functionName defParam $command 'Name' + defParam $command 'Org' } Context "Behavior testing" { - It "calls get-service to com.vmware.vmc.orgs" { - { Get-VMCOrg -name $OrgName } | Should Not Throw + It "calls Get-VMCOrg" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It + } + It "calls Get-VMCOrg with the SDDC name supplied" { + { Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + } + # Testing with single "Org" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } } + + # Testing with two "Orgs" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + $object = @( + @{"Id" = 1} + @{"Id" = 2} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response - It "gets the orgs via list method and returns the properties" { + It "gets the SDDC details 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-VMCSDDC -Org $OrgId).display_name | Should -be $display_name } # Testing the multiple SDDC response - It "calls the Connect-CisServer" { - $object = [PSCustomObject]@{} + It "gets the SDDC details of the SDDC supplied and returns the properties" { + $object = @{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - { Get-VMCOrg -name $OrgName } | Should Not Throw - - $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id - - $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + $(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $name } } } From b90c83ac56f1440b671e08fcba95f80efb8dfd0c Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 18:06:09 +0000 Subject: [PATCH 07/14] Created tests for get VMC Task Trimed out excess fom get SDDC parameters in the object. --- Pester/Functions/Get-VmcSddc.tests.ps1 | 17 +----- Pester/Functions/Get-VmcTask.tests.ps1 | 78 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 Pester/Functions/Get-VmcTask.tests.ps1 diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 index 7d848a3..de27e79 100644 --- a/Pester/Functions/Get-VmcSddc.tests.ps1 +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -7,33 +7,20 @@ Describe "$functionName" -Tag 'Unit' { $global:DefaultVMCServers = $true - $display_name = "MockedDisplayName" - $user_name = "MockedUserName" - $OrgName = "MockedOrgName" - $created = "MockedDate" $OrgId = "Mocked OrgID" $name = "MockedSDDCName" $Notname = "NotTheName" - $id = "MockedId" $Service = "com.vmware.vmc.orgs.sddcs" $MockedList = [PSCustomObject]@{ - "display_name" = $display_name "name" = $name - "created" = $created - "user_name" = $user_name - "id" = $id } $MockedList2 = [PSCustomObject]@{ - "display_name" = $display_name "name" = $Notname - "created" = $created - "user_name" = $user_name - "id" = $id } $object = @( - @{"Id" = $Id} + @{"Id" = 1} ) $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } @@ -81,7 +68,7 @@ Describe "$functionName" -Tag 'Unit' { It "gets the SDDC details via list method and returns the properties" { $object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $(Get-VMCSDDC -Org $OrgId).display_name | Should -be $display_name + $(Get-VMCSDDC -Org $OrgId).name | Should -be $name } # Testing the multiple SDDC response It "gets the SDDC details of the SDDC supplied and returns the properties" { diff --git a/Pester/Functions/Get-VmcTask.tests.ps1 b/Pester/Functions/Get-VmcTask.tests.ps1 new file mode 100644 index 0000000..f54e944 --- /dev/null +++ b/Pester/Functions/Get-VmcTask.tests.ps1 @@ -0,0 +1,78 @@ +#Requires -Modules Pester, VMware.VMC +$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") +Import-Module -Name VMware.VimAutomation.Cis.Core + +Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $OrgId = "Mocked OrgID" + $name = "MockedSDDCName" + $Notname = "NotTheName" + $id = "MockedId" + $Service = "com.vmware.vmc.orgs.tasks" + + $MockedList = [PSCustomObject]@{ + "name" = $name + } + $MockedList2 = [PSCustomObject]@{ + "name" = $Notname + } + + $object = @( + @{"Id" = $Id} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + $MockedArray = @($MockedList, $MockedList2) + + Mock -CommandName Get-VMCService -MockWith { $object } + + Mock -CommandName Get-VMCOrg { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Org' + } + + Context "Behavior testing" { + + It "calls Get-VMCOrg with the Org name supplied" { + { Get-VMCTask -Org $name} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + } + + # Testing with single "Org" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.tasks" { + { Get-VMCTask -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing with two "Orgs" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.tasks" { + $object = @( + @{"Id" = 1} + @{"Id" = 2} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCTask -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing a single SDDC response + It "gets the task details via list method and returns the properties" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + $(Get-VMCTask -Org $OrgId).name | Should -be $name + } + # Testing the multiple SDDC response + It "gets the task details of the SDDC supplied and returns the properties" { + $object = @{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + $(Get-VMCTask -Org $OrgId)[0].name | Should -be $name + $(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname + } + } +} From 5c833d4ce5b93f7668d95be2a422f44e37edfdf2 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 18:56:44 +0000 Subject: [PATCH 08/14] Wraped all the tests inModuleScope for invoke-pester invocation. Moved function name into module scope. --- .../Functions/Connect-VMCVIServer.tests.ps1 | 95 ++++++------ Pester/Functions/Get-VMCCommand.tests.ps1 | 29 ++-- Pester/Functions/Get-VMCOrg.tests.ps1 | 124 ++++++++-------- Pester/Functions/Get-VmcSddc.tests.ps1 | 136 +++++++++--------- Pester/Functions/Get-VmcTask.tests.ps1 | 132 ++++++++--------- 5 files changed, 264 insertions(+), 252 deletions(-) diff --git a/Pester/Functions/Connect-VMCVIServer.tests.ps1 b/Pester/Functions/Connect-VMCVIServer.tests.ps1 index 2448ab3..b8d87f0 100644 --- a/Pester/Functions/Connect-VMCVIServer.tests.ps1 +++ b/Pester/Functions/Connect-VMCVIServer.tests.ps1 @@ -1,59 +1,62 @@ #Requires -Modules Pester, VMware.VMC -$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") -Describe "$functionName" -Tag 'Unit' { - . "$PSScriptRoot/Shared.ps1" - $Org = 'MyOrg' - $Sddc = 'MySddc' - - $global:DefaultVMCServers = $true +inModuleScope VMware.VMC { + $functionName = "Connect-VMCVIServer" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" - $secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force - $Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd) - $cloud_username = "MockedUserName" - $vc_public_ip = "MockedServer" + $Org = 'MyOrg' + $Sddc = 'MySddc' + + $global:DefaultVMCServers = $true - Mock Get-VMCSDDCDefaultCredential { - $object = [PSCustomObject] @{ - 'vc_public_ip' = $vc_public_ip - 'cloud_username' = $cloud_username - 'cloud_password' = $Mockedcreds.Password + $secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force + $Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd) + $cloud_username = "MockedUserName" + $vc_public_ip = "MockedServer" + + Mock Get-VMCSDDCDefaultCredential { + $object = [PSCustomObject] @{ + 'vc_public_ip' = $vc_public_ip + 'cloud_username' = $cloud_username + 'cloud_password' = $Mockedcreds.Password + } + return $object } - return $object - } - Mock Write-host {} + Mock Write-host {} - Mock Connect-VIServer {} + Mock Connect-VIServer {} - Mock Connect-CisServer {} + Mock Connect-CisServer {} - Context "Sanity checking" { - $command = Get-Command -Name $functionName + Context "Sanity checking" { + $command = Get-Command -Name $functionName - defParam $command 'Org' - defParam $command 'Sddc' - defParam $command 'Autologin' - } - - Context "Behavior testing" { - It "gets creds via Get-VMCSDDCDefaultCredential" { - { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCSDDCDefaultCredential -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + defParam $command 'Org' + defParam $command 'Sddc' + defParam $command 'Autologin' } - It "calls the Connect-VIServer" { - { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw - Assert-MockCalled -CommandName Connect-VIServer -Times 1 -Scope It -ParameterFilter { ` - $Server -eq $vc_public_ip ` - -and $User -eq $cloud_username ` - -and $Password -eq $Mockedcreds.Password } - } - It "calls the Connect-CisServer" { - { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw - Assert-MockCalled -CommandName Connect-CisServer -Times 1 -Scope It -ParameterFilter { ` - $Server -eq $vc_public_ip ` - -and $User -eq $cloud_username ` - -and $Password -eq $Mockedcreds.password } + + Context "Behavior testing" { + It "gets creds via Get-VMCSDDCDefaultCredential" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCSDDCDefaultCredential -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc } + } + It "calls the Connect-VIServer" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Connect-VIServer -Times 1 -Scope It -ParameterFilter { ` + $Server -eq $vc_public_ip ` + -and $User -eq $cloud_username ` + -and $Password -eq $Mockedcreds.Password } + } + It "calls the Connect-CisServer" { + { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw + Assert-MockCalled -CommandName Connect-CisServer -Times 1 -Scope It -ParameterFilter { ` + $Server -eq $vc_public_ip ` + -and $User -eq $cloud_username ` + -and $Password -eq $Mockedcreds.password } + } } } -} +} \ No newline at end of file diff --git a/Pester/Functions/Get-VMCCommand.tests.ps1 b/Pester/Functions/Get-VMCCommand.tests.ps1 index 230594c..eb84490 100644 --- a/Pester/Functions/Get-VMCCommand.tests.ps1 +++ b/Pester/Functions/Get-VMCCommand.tests.ps1 @@ -1,20 +1,23 @@ #Requires -Modules Pester, VMware.VMC, VMware.VimAutomation.Vmc -$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") -Describe "$functionName" -Tag 'Unit' { - Mock Get-Command { - "Mocked Command Response" - } - - Context "Behavior testing" { - It "should call get-command on VMware.VimAutomation.Vmc" { - { Get-VMCCommand } | Should Not Throw - Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VimAutomation.Vmc' } +inModuleScope VMware.VMC { + $functionName = "Get-VMCCommand" + Describe "$functionName" -Tag 'Unit' { + Mock Get-Command { + "Mocked Command Response" } - It "should call get-command on VMware.Vmc" { - { Get-VMCCommand } | Should Not Throw - Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VMC' } + + Context "Behavior testing" { + It "should call get-command on VMware.VimAutomation.Vmc" { + { Get-VMCCommand } | Should Not Throw + Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VimAutomation.Vmc' } + + } + It "should call get-command on VMware.Vmc" { + { Get-VMCCommand } | Should Not Throw + Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VMC' } + } } } } \ No newline at end of file diff --git a/Pester/Functions/Get-VMCOrg.tests.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 index 0726f5a..983703e 100644 --- a/Pester/Functions/Get-VMCOrg.tests.ps1 +++ b/Pester/Functions/Get-VMCOrg.tests.ps1 @@ -1,73 +1,75 @@ #Requires -Modules Pester, VMware.VMC -$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") Import-Module -Name VMware.VimAutomation.Cis.Core -Describe "$functionName" -Tag 'Unit' { - . "$PSScriptRoot/Shared.ps1" +inModuleScope VMware.VMC { + $functionName = "Get-VMCOrg" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" - $global:DefaultVMCServers = $true + $global:DefaultVMCServers = $true - $display_name = "MockedDisplayName" - $user_name = "MockedUserName" - $OrgName = "MockedDisplayName" - $created = "MockedDate" - $id = "MockedId" - $Service = "com.vmware.vmc.orgs" + $display_name = "MockedDisplayName" + $user_name = "MockedUserName" + $OrgName = "MockedDisplayName" + $created = "MockedDate" + $id = "MockedId" + $Service = "com.vmware.vmc.orgs" - $MockedList = [PSCustomObject]@{ - "display_name" = $display_name - "name" = $OrgName - "created" = $created - "user_name" = $user_name - "id" = $id - } - - $object = [PSCustomObject]@{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - - $MockedArray = @($MockedList, $MockedList) - - Mock -CommandName Get-VMCService -MockWith { $object } - - Context "Sanity checking" { - $command = Get-Command -Name $functionName - - defParam $command 'Name' - } - - Context "Behavior testing" { - - It "calls get-service to com.vmware.vmc.orgs" { - { Get-VMCOrg -name $OrgName } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + $MockedList = [PSCustomObject]@{ + "display_name" = $display_name + "name" = $OrgName + "created" = $created + "user_name" = $user_name + "id" = $id } - # Testing a single SDDC response - 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 + + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + $MockedArray = @($MockedList, $MockedList) + + Mock -CommandName Get-VMCService -MockWith { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Name' } - # Testing the multiple SDDC response - It "calls the Connect-CisServer" { - $object = [PSCustomObject]@{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - { Get-VMCOrg -name $OrgName } | Should Not Throw - $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id + Context "Behavior testing" { - $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name - $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName - $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name - $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created - $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + It "calls get-service to com.vmware.vmc.orgs" { + { Get-VMCOrg -name $OrgName } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response + 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 + } + # Testing the multiple SDDC response + It "calls the Connect-CisServer" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCOrg -name $OrgName } | Should Not Throw + + $(Get-VMCOrg -name $OrgName)[0].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[0].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[0].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[0].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[0].id | Should -be $id + + $(Get-VMCOrg -name $OrgName)[1].display_name | Should -be $display_name + $(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName + $(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name + $(Get-VMCOrg -name $OrgName)[1].created | Should -be $created + $(Get-VMCOrg -name $OrgName)[1].id | Should -be $id + } } } -} +} \ No newline at end of file diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 index de27e79..3bc7d09 100644 --- a/Pester/Functions/Get-VmcSddc.tests.ps1 +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -1,80 +1,82 @@ #Requires -Modules Pester, VMware.VMC -$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") Import-Module -Name VMware.VimAutomation.Cis.Core -Describe "$functionName" -Tag 'Unit' { - . "$PSScriptRoot/Shared.ps1" +inModuleScope VMware.VMC { + $functionName ="Get-VmcSddc" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" - $global:DefaultVMCServers = $true + $global:DefaultVMCServers = $true - $OrgId = "Mocked OrgID" - $name = "MockedSDDCName" - $Notname = "NotTheName" - $Service = "com.vmware.vmc.orgs.sddcs" + $OrgId = "Mocked OrgID" + $name = "MockedSDDCName" + $Notname = "NotTheName" + $Service = "com.vmware.vmc.orgs.sddcs" - $MockedList = [PSCustomObject]@{ - "name" = $name - } - $MockedList2 = [PSCustomObject]@{ - "name" = $Notname - } - - $object = @( - @{"Id" = 1} - ) - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - - $MockedArray = @($MockedList, $MockedList2) - - Mock -CommandName Get-VMCService -MockWith { $object } - - Mock -CommandName Get-VMCOrg { $object } - - Context "Sanity checking" { - $command = Get-Command -Name $functionName - - defParam $command 'Name' - defParam $command 'Org' - } - - Context "Behavior testing" { - - It "calls Get-VMCOrg" { - { Get-VMCSDDC -Org $OrgId } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It + $MockedList = [PSCustomObject]@{ + "name" = $name } - It "calls Get-VMCOrg with the SDDC name supplied" { - { Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw - Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } - } - # Testing with single "Org" so assert call twice. - It "calls get-service to com.vmware.vmc.orgs.sddcs" { - { Get-VMCSDDC -Org $OrgId } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + $MockedList2 = [PSCustomObject]@{ + "name" = $Notname } - # Testing with two "Orgs" so assert call twice. - It "calls get-service to com.vmware.vmc.orgs.sddcs" { - $object = @( - @{"Id" = 1} - @{"Id" = 2} - ) - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - { Get-VMCSDDC -Org $OrgId } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + $object = @( + @{"Id" = 1} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + $MockedArray = @($MockedList, $MockedList2) + + Mock -CommandName Get-VMCService -MockWith { $object } + + Mock -CommandName Get-VMCOrg { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Name' + defParam $command 'Org' } - # Testing a single SDDC response - It "gets the SDDC details via list method and returns the properties" { - $object = [PSCustomObject]@{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $(Get-VMCSDDC -Org $OrgId).name | Should -be $name - } - # Testing the multiple SDDC response - It "gets the SDDC details of the SDDC supplied and returns the properties" { - $object = @{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - $(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $name + Context "Behavior testing" { + + It "calls Get-VMCOrg" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It + } + It "calls Get-VMCOrg with the SDDC name supplied" { + { Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + } + # Testing with single "Org" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing with two "Orgs" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + $object = @( + @{"Id" = 1} + @{"Id" = 2} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing a single SDDC response + It "gets the SDDC details via list method and returns the properties" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + $(Get-VMCSDDC -Org $OrgId).name | Should -be $name + } + # Testing the multiple SDDC response + It "gets the SDDC details of the SDDC supplied and returns the properties" { + $object = @{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + $(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $name + } } } -} +} \ No newline at end of file diff --git a/Pester/Functions/Get-VmcTask.tests.ps1 b/Pester/Functions/Get-VmcTask.tests.ps1 index f54e944..0ee9f19 100644 --- a/Pester/Functions/Get-VmcTask.tests.ps1 +++ b/Pester/Functions/Get-VmcTask.tests.ps1 @@ -1,78 +1,80 @@ #Requires -Modules Pester, VMware.VMC -$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1") Import-Module -Name VMware.VimAutomation.Cis.Core -Describe "$functionName" -Tag 'Unit' { - . "$PSScriptRoot/Shared.ps1" +inModuleScope VMware.VMC { + $functionName = "Get-VMCTask" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" - $global:DefaultVMCServers = $true + $global:DefaultVMCServers = $true - $OrgId = "Mocked OrgID" - $name = "MockedSDDCName" - $Notname = "NotTheName" - $id = "MockedId" - $Service = "com.vmware.vmc.orgs.tasks" + $OrgId = "Mocked OrgID" + $name = "MockedSDDCName" + $Notname = "NotTheName" + $id = "MockedId" + $Service = "com.vmware.vmc.orgs.tasks" - $MockedList = [PSCustomObject]@{ - "name" = $name - } - $MockedList2 = [PSCustomObject]@{ - "name" = $Notname - } - - $object = @( - @{"Id" = $Id} - ) - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - - $MockedArray = @($MockedList, $MockedList2) - - Mock -CommandName Get-VMCService -MockWith { $object } - - Mock -CommandName Get-VMCOrg { $object } - - Context "Sanity checking" { - $command = Get-Command -Name $functionName - - defParam $command 'Org' - } - - Context "Behavior testing" { - - It "calls Get-VMCOrg with the Org name supplied" { - { Get-VMCTask -Org $name} | Should Not Throw - Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + $MockedList = [PSCustomObject]@{ + "name" = $name + } + $MockedList2 = [PSCustomObject]@{ + "name" = $Notname } - # Testing with single "Org" so assert call twice. - It "calls get-service to com.vmware.vmc.orgs.tasks" { - { Get-VMCTask -Org $OrgId } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + $object = @( + @{"Id" = $Id} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + + $MockedArray = @($MockedList, $MockedList2) + + Mock -CommandName Get-VMCService -MockWith { $object } + + Mock -CommandName Get-VMCOrg { $object } + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Org' } - # Testing with two "Orgs" so assert call twice. - It "calls get-service to com.vmware.vmc.orgs.tasks" { - $object = @( - @{"Id" = 1} - @{"Id" = 2} - ) - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - { Get-VMCTask -Org $OrgId } | Should Not Throw - Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } - } + Context "Behavior testing" { - # Testing a single SDDC response - It "gets the task details via list method and returns the properties" { - $object = [PSCustomObject]@{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } - $(Get-VMCTask -Org $OrgId).name | Should -be $name - } - # Testing the multiple SDDC response - It "gets the task details of the SDDC supplied and returns the properties" { - $object = @{} - $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } - $(Get-VMCTask -Org $OrgId)[0].name | Should -be $name - $(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname + It "calls Get-VMCOrg with the Org name supplied" { + { Get-VMCTask -Org $name} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name } + } + + # Testing with single "Org" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.tasks" { + { Get-VMCTask -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing with two "Orgs" so assert call twice. + It "calls get-service to com.vmware.vmc.orgs.tasks" { + $object = @( + @{"Id" = 1} + @{"Id" = 2} + ) + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + { Get-VMCTask -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service } + } + + # Testing a single SDDC response + It "gets the task details via list method and returns the properties" { + $object = [PSCustomObject]@{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } + $(Get-VMCTask -Org $OrgId).name | Should -be $name + } + # Testing the multiple SDDC response + It "gets the task details of the SDDC supplied and returns the properties" { + $object = @{} + $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } + $(Get-VMCTask -Org $OrgId)[0].name | Should -be $name + $(Get-VMCTask -Org $OrgId)[1].name | Should -be $Notname + } } } -} +} \ No newline at end of file From b5111e1fab1fa2c919556c7adcad4f8e6b373d8c Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sat, 23 Feb 2019 19:33:24 +0000 Subject: [PATCH 09/14] Adding a VMC code coverage script to invoke the coverage report. --- Pester/VMCCode-Coverage.ps1 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Pester/VMCCode-Coverage.ps1 diff --git a/Pester/VMCCode-Coverage.ps1 b/Pester/VMCCode-Coverage.ps1 new file mode 100644 index 0000000..28aa114 --- /dev/null +++ b/Pester/VMCCode-Coverage.ps1 @@ -0,0 +1,4 @@ +Remove-Module VMware.VMC +import-module ../Modules/VMware.VMC/VMware.VMC.psm1 + +invoke-pester ./Functions -CodeCoverage ..\Modules\VMware.VMC\VMware.VMC.psm1 \ No newline at end of file From 2bb82249cab5cd86a2ba46deabd0554139e62830 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sun, 24 Feb 2019 14:41:16 +0000 Subject: [PATCH 10/14] 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 From c01427f6f4a75c83fd5c271ccb0d856332ef74fd Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Sun, 24 Feb 2019 16:06:47 +0000 Subject: [PATCH 11/14] Added new tests. --- .../Functions/Get-VMCSDDCPublicIP.tests.ps1 | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Pester/Functions/Get-VMCSDDCPublicIP.tests.ps1 diff --git a/Pester/Functions/Get-VMCSDDCPublicIP.tests.ps1 b/Pester/Functions/Get-VMCSDDCPublicIP.tests.ps1 new file mode 100644 index 0000000..16c2e15 --- /dev/null +++ b/Pester/Functions/Get-VMCSDDCPublicIP.tests.ps1 @@ -0,0 +1,72 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCSDDCPublicIP" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $OrgId = "Mocked OrgID" + $SddcName = "MockedSDDCName" + $public_ip_pool = "MockedPublic_ip_pool" + + $MockedList = [PSCustomObject]@{ + "public_ip_pool" = $public_ip_pool + } + $MockedList2 = [PSCustomObject]@{ + "public_ip_pool" = $public_ip_pool + } + + $object = [PSCustomObject]@{ + "resource_config" = @($MockedList) + } + + $MockedArray = @{ + "resource_config" = @($MockedList, $MockedList2) + } + + Mock -CommandName Get-VMCSDDC -MockWith { $object } + + Mock -CommandName Write-Error -MockWith {} + + 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-VMCSDDCPublicIP -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-VMCSDDCPublicIP -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-VMCSDDCPublicIP -Org $OrgId) | Should -be $Public_ip_pool + #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-VMCSDDCPublicIP -Org $OrgId)[0] | Should -be $Public_ip_pool + + $(Get-VMCSDDCPublicIP -Org $OrgId)[1] | Should -be $Public_ip_pool + #Assert-MockCalled -CommandName Select-Object -Times 2 -Scope It + } + It "writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCSDDCPublicIP -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 From f926508d1f9386f637362fa22a4c16c4757cead7 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Mon, 25 Feb 2019 21:09:18 +0000 Subject: [PATCH 12/14] Added test for Get-VMCVMHost, complicated object mocking. --- Pester/Functions/Get-VMCHost.tests.ps1 | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Pester/Functions/Get-VMCHost.tests.ps1 diff --git a/Pester/Functions/Get-VMCHost.tests.ps1 b/Pester/Functions/Get-VMCHost.tests.ps1 new file mode 100644 index 0000000..40bde0c --- /dev/null +++ b/Pester/Functions/Get-VMCHost.tests.ps1 @@ -0,0 +1,88 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCVMHost" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $OrgId = "Mocked OrgID" + $SddcId = "MockedSDDCName" + $VMhostName = "Mockedvc_url" + $VMHost_name = "MockedVCmanage_ip" + $esx_state = "MockedVCpublic_ip" + $esx_id = "Mocked_esx_id" + + $object = @([PSCustomObject]@{ + "resource_config" = @{ + esx_hosts = @(@{ + esx_id = $esx_id + name = $VMHost_name + hostname = $VMhostName + esx_state = $esx_state + }) + } + "id" = $SddcId + "Org_Id" = $OrgId + }) + + $MockedArray = @($object, $object) + + Mock -CommandName Get-VMCSDDC -MockWith { $object } + + Mock -CommandName Write-Error -MockWith {} + + 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-VMCVMHost with the Org name supplied" { + { Get-VMCVMHost -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-VMCVMHost without SDDC name supplied" { + { Get-VMCVMHost -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-VMCVMHost -Org $OrgId).esx_id | Should -be "Mocked_esx_id"#$esx_id + $(Get-VMCVMHost -Org $OrgId).name | Should -be $VMHost_name + $(Get-VMCVMHost -Org $OrgId).hostname | Should -be $VMhostName + $(Get-VMCVMHost -Org $OrgId).esx_state | Should -be $esx_state + $(Get-VMCVMHost -Org $OrgId).sddc_id | Should -be $SddcId + $(Get-VMCVMHost -Org $OrgId).org_id | Should -be $OrgId + } + # 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-VMCVMHost -Org $OrgId)[0].esx_id | Should -be $esx_id + $(Get-VMCVMHost -Org $OrgId)[0].name | Should -be $VMHost_name + $(Get-VMCVMHost -Org $OrgId)[0].hostname | Should -be $VMhostName + $(Get-VMCVMHost -Org $OrgId)[0].esx_state | Should -be $esx_state + $(Get-VMCVMHost -Org $OrgId)[0].sddc_id | Should -be $SddcId + $(Get-VMCVMHost -Org $OrgId)[0].org_id | Should -be $OrgId + + $(Get-VMCVMHost -Org $OrgId)[1].esx_id | Should -be $esx_id + $(Get-VMCVMHost -Org $OrgId)[1].name | Should -be $VMHost_name + $(Get-VMCVMHost -Org $OrgId)[1].hostname | Should -be $VMhostName + $(Get-VMCVMHost -Org $OrgId)[1].esx_state | Should -be $esx_state + $(Get-VMCVMHost -Org $OrgId)[1].sddc_id | Should -be $SddcId + $(Get-VMCVMHost -Org $OrgId)[1].org_id | Should -be $OrgId + } + It "writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCVMHost -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 From 3e09f0a601f43ffae443b1f33f228e193f56c7f2 Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Mon, 25 Feb 2019 22:28:23 +0000 Subject: [PATCH 13/14] Wrote tests covering get VMC SDDC Version Fixed up get VMC Hosts a bit. --- Pester/Functions/Get-VMCHost.tests.ps1 | 2 +- Pester/Functions/Get-VMCSDDCVersion.tests.ps1 | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Pester/Functions/Get-VMCSDDCVersion.tests.ps1 diff --git a/Pester/Functions/Get-VMCHost.tests.ps1 b/Pester/Functions/Get-VMCHost.tests.ps1 index 40bde0c..921b2a0 100644 --- a/Pester/Functions/Get-VMCHost.tests.ps1 +++ b/Pester/Functions/Get-VMCHost.tests.ps1 @@ -54,7 +54,7 @@ inModuleScope VMware.VMC { } # Testing a single SDDC response It "gets the task details via list method and returns the properties" { - $(Get-VMCVMHost -Org $OrgId).esx_id | Should -be "Mocked_esx_id"#$esx_id + $(Get-VMCVMHost -Org $OrgId).esx_id | Should -be $esx_id $(Get-VMCVMHost -Org $OrgId).name | Should -be $VMHost_name $(Get-VMCVMHost -Org $OrgId).hostname | Should -be $VMhostName $(Get-VMCVMHost -Org $OrgId).esx_state | Should -be $esx_state diff --git a/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 b/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 new file mode 100644 index 0000000..52da5ee --- /dev/null +++ b/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 @@ -0,0 +1,75 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCSDDCVersion" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $Service = "com.vmware.vmc.orgs.sddcs" + $OrgId = "Mocked OrgID" + $SddcName = "MockedSDDCName" + $version = "MockedVersion" + + $orgs = @( + [PSCustomObject]@{ + "Id" = $OrgId + "Org_Id" = $OrgId + }) + + $MockedServiceObj = [PSCustomObject]{} + + $ServicesObject = @([PSCustomObject]@{ + "resource_config" = @{ + sddc_manifest = [PSCustomObject]@{ + version = $version + } + } + }) + + Mock -CommandName Get-VMCOrg -MockWith { $orgs } + + Mock -CommandName Get-VMCService -MockWith { $MockedServiceObj } + + $MockedServiceObj | Add-Member -MemberType ScriptMethod -Name "list" -Value { $ServicesObject } + + Mock -CommandName Write-Error -MockWith {} + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'Org' + defParam $command 'Name' + } + + Context "Behavior testing" { + # Testing single Org with optional SDDC parameter + It "calls Get-VMCSDDCVersion with the Org name supplied" { + { Get-VMCSDDCVersion -Org $OrgId -Name $SddcName} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $Name -eq $OrgId } + } + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + { Get-VMCSDDC -Org $OrgId } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response + It "gets the task details via list method and returns the properties" { + $(Get-VMCSDDCVersion -Org $OrgId).version | Should -be $version + } + # Testing the multiple SDDC response + It "gets the task details of the Org supplied and returns the properties" { + Mock -CommandName Get-VMCOrg -MockWith { @($orgs, $orgs) } + + $(Get-VMCSDDCVersion -Org $OrgId)[0].version | Should -be $version + $(Get-VMCSDDCVersion -Org $OrgId)[1].version | Should -be $version + } + It "writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCSDDCVersion -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 From bbc6d0f77550bdf688c25746df10db038b4f541c Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Wed, 27 Feb 2019 22:23:05 +0000 Subject: [PATCH 14/14] Started tests for VMCFirewall. Fixed incorrect function call in sddc version test. --- .../Functions/Get-VMCFirewallRule.tests.ps1 | 75 +++++++++++++++++++ Pester/Functions/Get-VMCSDDCVersion.tests.ps1 | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Pester/Functions/Get-VMCFirewallRule.tests.ps1 diff --git a/Pester/Functions/Get-VMCFirewallRule.tests.ps1 b/Pester/Functions/Get-VMCFirewallRule.tests.ps1 new file mode 100644 index 0000000..b508dbd --- /dev/null +++ b/Pester/Functions/Get-VMCFirewallRule.tests.ps1 @@ -0,0 +1,75 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCFirewallRule" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $Service = "com.vmware.vmc.orgs.sddcs.networks.edges.firewall.config" + $OrgId = "Mocked OrgID" + $GatewayType = "MGW" + $SddcName = "MockedSDDCName" + $OrgName = "MockedOrgName" + $version = "MockedVersion" + + $orgs = @( + [PSCustomObject]@{ + "Id" = $OrgId + "Org_Id" = $OrgId + }) + + $MockedServiceObj = [PSCustomObject]{} + + $ServicesObject = @([PSCustomObject]@{ + "resource_config" = @{ + sddc_manifest = [PSCustomObject]@{ + version = $version + } + } + }) + + Mock -CommandName Get-VMCOrg -MockWith { $orgs } + Mock -CommandName Get-VMCSDDC -MockWith { $orgs } + + Mock -CommandName Get-VMCService -MockWith { $MockedServiceObj } + + $MockedServiceObj | Add-Member -MemberType ScriptMethod -Name "Get" -Value { $ServicesObject } + $MockedServiceObj | Add-Member -MemberType ScriptMethod -Name "list" -Value { $ServicesObject } + + Mock -CommandName Write-Error -MockWith {} + + Context "Sanity checking" { + $command = Get-Command -Name $functionName + + defParam $command 'OrgName' + defParam $command 'SDDCName' + defParam $command 'ShowAll' + defParam $command 'GatewayType' + } + + Context "Behavior testing" { + # Testing single Org with optional SDDC parameter + It "calls Get-VMCFirewallRule with the Org name supplied" { + { Get-VMCFirewallRule -GatewayType $GatewayType -SDDCName $SddcName -OrgName $OrgName} | Should Not Throw + Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It #-ParameterFilter { $Name -eq $SddcName } + Assert-MockCalled -CommandName Get-VMCSDDC -Times 1 -Scope It -ParameterFilter { $Name -eq $SddcName -and $Org -eq $OrgName } + } + It "calls get-service to com.vmware.vmc.orgs.sddcs" { + { Get-VMCFirewallRule -GatewayType $GatewayType } | Should Not Throw + Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } + } + # Testing a single SDDC response + It "gets the task details via list method and returns the properties" { + $(Get-VMCFirewallRule -GatewayType $GatewayType).version | Should -be $version + } + It "writes an error if not connected" { + $global:DefaultVMCServers = $false + { Get-VMCFirewallRule -GatewayType $GatewayType } | 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-VMCSDDCVersion.tests.ps1 b/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 index 52da5ee..203801a 100644 --- a/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 +++ b/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 @@ -51,7 +51,7 @@ inModuleScope VMware.VMC { Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $Name -eq $OrgId } } It "calls get-service to com.vmware.vmc.orgs.sddcs" { - { Get-VMCSDDC -Org $OrgId } | Should Not Throw + { Get-VMCSDDCVersion -Org $OrgId } | Should Not Throw Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } } # Testing a single SDDC response