Added two pester tests for Get-VMCCommand and Connect-VMCVIServer.

This commit is contained in:
Conor Tolan
2019-02-18 21:57:52 +00:00
parent 71915cde79
commit f9ca007ae5
4 changed files with 84 additions and 1 deletions

View 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 }
}
}
}

View 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' }
}
}
}

View File

@@ -0,0 +1,6 @@
#Requires -Modules Pester
function defParam($command, $name) {
It "Has a -$name parameter" {
$command.Parameters.Item($name) | Should Not BeNullOrEmpty
}
}