From f9ca007ae5faded62b347e778b0ab0105e88611e Mon Sep 17 00:00:00 2001 From: Conor Tolan Date: Mon, 18 Feb 2019 21:57:52 +0000 Subject: [PATCH] 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