Wraped all the tests inModuleScope for invoke-pester invocation.

Moved function name into module scope.
This commit is contained in:
Conor Tolan
2019-02-23 18:56:44 +00:00
parent b90c83ac56
commit 5c833d4ce5
5 changed files with 264 additions and 252 deletions

View File

@@ -1,59 +1,62 @@
#Requires -Modules Pester, VMware.VMC #Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$Org = 'MyOrg' inModuleScope VMware.VMC {
$Sddc = 'MySddc' $functionName = "Connect-VMCVIServer"
Describe "$functionName" -Tag 'Unit' {
$global:DefaultVMCServers = $true . "$PSScriptRoot/Shared.ps1"
$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force $Org = 'MyOrg'
$Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd) $Sddc = 'MySddc'
$cloud_username = "MockedUserName"
$vc_public_ip = "MockedServer" $global:DefaultVMCServers = $true
Mock Get-VMCSDDCDefaultCredential { $secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$object = [PSCustomObject] @{ $Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
'vc_public_ip' = $vc_public_ip $cloud_username = "MockedUserName"
'cloud_username' = $cloud_username $vc_public_ip = "MockedServer"
'cloud_password' = $Mockedcreds.Password
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" { Context "Sanity checking" {
$command = Get-Command -Name $functionName $command = Get-Command -Name $functionName
defParam $command 'Org' defParam $command 'Org'
defParam $command 'Sddc' defParam $command 'Sddc'
defParam $command 'Autologin' 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 Context "Behavior testing" {
Assert-MockCalled -CommandName Connect-VIServer -Times 1 -Scope It -ParameterFilter { ` It "gets creds via Get-VMCSDDCDefaultCredential" {
$Server -eq $vc_public_ip ` { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw
-and $User -eq $cloud_username ` Assert-MockCalled -CommandName Get-VMCSDDCDefaultCredential -Times 1 -Scope It -ParameterFilter { $org -eq $Org -and $Sddc -eq $Sddc }
-and $Password -eq $Mockedcreds.Password } }
} It "calls the Connect-VIServer" {
It "calls the Connect-CisServer" { { Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw
{ Connect-VMCVIServer -org $Org -Sddc $Sddc } | Should Not Throw Assert-MockCalled -CommandName Connect-VIServer -Times 1 -Scope It -ParameterFilter { `
Assert-MockCalled -CommandName Connect-CisServer -Times 1 -Scope It -ParameterFilter { ` $Server -eq $vc_public_ip `
$Server -eq $vc_public_ip ` -and $User -eq $cloud_username `
-and $User -eq $cloud_username ` -and $Password -eq $Mockedcreds.Password }
-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

@@ -1,20 +1,23 @@
#Requires -Modules Pester, VMware.VMC, VMware.VimAutomation.Vmc #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 Context "Behavior testing" {
Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VMC' } 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

@@ -1,73 +1,75 @@
#Requires -Modules Pester, VMware.VMC #Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' { inModuleScope VMware.VMC {
. "$PSScriptRoot/Shared.ps1" $functionName = "Get-VMCOrg"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true $global:DefaultVMCServers = $true
$display_name = "MockedDisplayName" $display_name = "MockedDisplayName"
$user_name = "MockedUserName" $user_name = "MockedUserName"
$OrgName = "MockedDisplayName" $OrgName = "MockedDisplayName"
$created = "MockedDate" $created = "MockedDate"
$id = "MockedId" $id = "MockedId"
$Service = "com.vmware.vmc.orgs" $Service = "com.vmware.vmc.orgs"
$MockedList = [PSCustomObject]@{ $MockedList = [PSCustomObject]@{
"display_name" = $display_name "display_name" = $display_name
"name" = $OrgName "name" = $OrgName
"created" = $created "created" = $created
"user_name" = $user_name "user_name" = $user_name
"id" = $id "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 }
} }
# Testing a single SDDC response
It "gets the orgs via list method and returns the properties" { $object = [PSCustomObject]@{}
$object = [PSCustomObject]@{} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList }
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList }
$(Get-VMCOrg -name $OrgName).display_name | Should -be $display_name $MockedArray = @($MockedList, $MockedList)
$(Get-VMCOrg -name $OrgName).name | Should -be $OrgName
$(Get-VMCOrg -name $OrgName).user_name | Should -be $user_name Mock -CommandName Get-VMCService -MockWith { $object }
$(Get-VMCOrg -name $OrgName).created | Should -be $created
$(Get-VMCOrg -name $OrgName).id | Should -be $id 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 Context "Behavior testing" {
$(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 It "calls get-service to com.vmware.vmc.orgs" {
$(Get-VMCOrg -name $OrgName)[1].name | Should -be $OrgName { Get-VMCOrg -name $OrgName } | Should Not Throw
$(Get-VMCOrg -name $OrgName)[1].user_name | Should -be $user_name Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service }
$(Get-VMCOrg -name $OrgName)[1].created | Should -be $created }
$(Get-VMCOrg -name $OrgName)[1].id | Should -be $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
}
# 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
}
} }
} }
} }

View File

@@ -1,80 +1,82 @@
#Requires -Modules Pester, VMware.VMC #Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' { inModuleScope VMware.VMC {
. "$PSScriptRoot/Shared.ps1" $functionName ="Get-VmcSddc"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true $global:DefaultVMCServers = $true
$OrgId = "Mocked OrgID" $OrgId = "Mocked OrgID"
$name = "MockedSDDCName" $name = "MockedSDDCName"
$Notname = "NotTheName" $Notname = "NotTheName"
$Service = "com.vmware.vmc.orgs.sddcs" $Service = "com.vmware.vmc.orgs.sddcs"
$MockedList = [PSCustomObject]@{ $MockedList = [PSCustomObject]@{
"name" = $name "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
} }
It "calls Get-VMCOrg with the SDDC name supplied" { $MockedList2 = [PSCustomObject]@{
{ Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw "name" = $Notname
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. $object = @(
It "calls get-service to com.vmware.vmc.orgs.sddcs" { @{"Id" = 1}
$object = @( )
@{"Id" = 1} $object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList }
@{"Id" = 2}
) $MockedArray = @($MockedList, $MockedList2)
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray }
{ Get-VMCSDDC -Org $OrgId } | Should Not Throw Mock -CommandName Get-VMCService -MockWith { $object }
Assert-MockCalled -CommandName Get-VMCService -Times 2 -Scope It -ParameterFilter { $name -eq $Service }
Mock -CommandName Get-VMCOrg { $object }
Context "Sanity checking" {
$command = Get-Command -Name $functionName
defParam $command 'Name'
defParam $command 'Org'
} }
# Testing a single SDDC response Context "Behavior testing" {
It "gets the SDDC details via list method and returns the properties" {
$object = [PSCustomObject]@{} It "calls Get-VMCOrg" {
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } { Get-VMCSDDC -Org $OrgId } | Should Not Throw
$(Get-VMCSDDC -Org $OrgId).name | Should -be $name Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It
} }
# Testing the multiple SDDC response It "calls Get-VMCOrg with the SDDC name supplied" {
It "gets the SDDC details of the SDDC supplied and returns the properties" { { Get-VMCSDDC -Org $OrgId -name $name} | Should Not Throw
$object = @{} Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name }
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedArray } }
$(Get-VMCSDDC -Org $OrgId -name $name).name | Should -be $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
}
} }
} }
} }

View File

@@ -1,78 +1,80 @@
#Requires -Modules Pester, VMware.VMC #Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' { inModuleScope VMware.VMC {
. "$PSScriptRoot/Shared.ps1" $functionName = "Get-VMCTask"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true $global:DefaultVMCServers = $true
$OrgId = "Mocked OrgID" $OrgId = "Mocked OrgID"
$name = "MockedSDDCName" $name = "MockedSDDCName"
$Notname = "NotTheName" $Notname = "NotTheName"
$id = "MockedId" $id = "MockedId"
$Service = "com.vmware.vmc.orgs.tasks" $Service = "com.vmware.vmc.orgs.tasks"
$MockedList = [PSCustomObject]@{ $MockedList = [PSCustomObject]@{
"name" = $name "name" = $name
} }
$MockedList2 = [PSCustomObject]@{ $MockedList2 = [PSCustomObject]@{
"name" = $Notname "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. $object = @(
It "calls get-service to com.vmware.vmc.orgs.tasks" { @{"Id" = $Id}
{ Get-VMCTask -Org $OrgId } | Should Not Throw )
Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service } $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. Context "Behavior testing" {
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 "calls Get-VMCOrg with the Org name supplied" {
It "gets the task details via list method and returns the properties" { { Get-VMCTask -Org $name} | Should Not Throw
$object = [PSCustomObject]@{} Assert-MockCalled -CommandName Get-VMCOrg -Times 1 -Scope It -ParameterFilter { $name -eq $name }
$object | Add-Member -MemberType ScriptMethod -Name "list" -Value { $MockedList } }
$(Get-VMCTask -Org $OrgId).name | Should -be $name
} # Testing with single "Org" so assert call twice.
# Testing the multiple SDDC response It "calls get-service to com.vmware.vmc.orgs.tasks" {
It "gets the task details of the SDDC supplied and returns the properties" { { Get-VMCTask -Org $OrgId } | Should Not Throw
$object = @{} Assert-MockCalled -CommandName Get-VMCService -Times 1 -Scope It -ParameterFilter { $name -eq $Service }
$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 # 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
}
} }
} }
} }