diff --git a/Pester/Functions/Connect-VMCVIServer.tests.ps1 b/Pester/Functions/Connect-VMCVIServer.tests.ps1 new file mode 100644 index 0000000..a98a277 --- /dev/null +++ b/Pester/Functions/Connect-VMCVIServer.tests.ps1 @@ -0,0 +1,69 @@ +#Requires -Modules Pester, VMware.VMC + +inModuleScope VMware.VMC { + $functionName = "Connect-VMCVIServer" + 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 Write-host {} + + Mock Connect-VIServer {} + + Mock Connect-CisServer {} + + Mock Write-Error + + 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 } + } + 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-VMCCommand.tests.ps1 b/Pester/Functions/Get-VMCCommand.tests.ps1 new file mode 100644 index 0000000..eb84490 --- /dev/null +++ b/Pester/Functions/Get-VMCCommand.tests.ps1 @@ -0,0 +1,23 @@ +#Requires -Modules Pester, VMware.VMC, VMware.VimAutomation.Vmc + + +inModuleScope VMware.VMC { + $functionName = "Get-VMCCommand" + 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/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-VMCHost.tests.ps1 b/Pester/Functions/Get-VMCHost.tests.ps1 new file mode 100644 index 0000000..921b2a0 --- /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 $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 diff --git a/Pester/Functions/Get-VMCOrg.tests.ps1 b/Pester/Functions/Get-VMCOrg.tests.ps1 new file mode 100644 index 0000000..2a8c589 --- /dev/null +++ b/Pester/Functions/Get-VMCOrg.tests.ps1 @@ -0,0 +1,82 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCOrg" + 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 = @($MockedList, $MockedList) + + Mock -CommandName Get-VMCService -MockWith { $object } + + Mock -CommandName Write-Error -MockWith {} + + 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).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" { + $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 + } + 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-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 diff --git a/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 b/Pester/Functions/Get-VMCSDDCVersion.tests.ps1 new file mode 100644 index 0000000..203801a --- /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-VMCSDDCVersion -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 diff --git a/Pester/Functions/Get-VmcSddc.tests.ps1 b/Pester/Functions/Get-VmcSddc.tests.ps1 new file mode 100644 index 0000000..f5182b9 --- /dev/null +++ b/Pester/Functions/Get-VmcSddc.tests.ps1 @@ -0,0 +1,89 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName ="Get-VmcSddc" + Describe "$functionName" -Tag 'Unit' { + . "$PSScriptRoot/Shared.ps1" + + $global:DefaultVMCServers = $true + + $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 } + + Mock -CommandName Write-Error -MockWith {} + + 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 + } + 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 + } + 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 new file mode 100644 index 0000000..5f0ab11 --- /dev/null +++ b/Pester/Functions/Get-VmcTask.tests.ps1 @@ -0,0 +1,87 @@ +#Requires -Modules Pester, VMware.VMC +Import-Module -Name VMware.VimAutomation.Cis.Core + +inModuleScope VMware.VMC { + $functionName = "Get-VMCTask" + 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 } + + Mock -CommandName Write-Error -MockWith {} + + 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 + } + 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 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 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