Added two pester tests for Get-VMCCommand and Connect-VMCVIServer.
This commit is contained in:
57
Pester/Functions/Connect-VMCVIServer.tests.ps1
Normal file
57
Pester/Functions/Connect-VMCVIServer.tests.ps1
Normal file
@@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Pester/Functions/Get-VMCCommand.tests.ps1
Normal file
20
Pester/Functions/Get-VMCCommand.tests.ps1
Normal file
@@ -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' }
|
||||
}
|
||||
}
|
||||
}
|
||||
6
Pester/Functions/Shared.ps1
Normal file
6
Pester/Functions/Shared.ps1
Normal file
@@ -0,0 +1,6 @@
|
||||
#Requires -Modules Pester
|
||||
function defParam($command, $name) {
|
||||
It "Has a -$name parameter" {
|
||||
$command.Parameters.Item($name) | Should Not BeNullOrEmpty
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user