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
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$Org = 'MyOrg'
$Sddc = 'MySddc'
$global:DefaultVMCServers = $true
inModuleScope VMware.VMC {
$functionName = "Connect-VMCVIServer"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
$Mockedcreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
$cloud_username = "MockedUserName"
$vc_public_ip = "MockedServer"
$Org = 'MyOrg'
$Sddc = 'MySddc'
$global:DefaultVMCServers = $true
Mock Get-VMCSDDCDefaultCredential {
$object = [PSCustomObject] @{
'vc_public_ip' = $vc_public_ip
'cloud_username' = $cloud_username
'cloud_password' = $Mockedcreds.Password
$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
}
return $object
}
Mock Write-host {}
Mock Write-host {}
Mock Connect-VIServer {}
Mock Connect-VIServer {}
Mock Connect-CisServer {}
Mock Connect-CisServer {}
Context "Sanity checking" {
$command = Get-Command -Name $functionName
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 }
defParam $command 'Org'
defParam $command 'Sddc'
defParam $command 'Autologin'
}
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 }
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

@@ -1,20 +1,23 @@
#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
Assert-MockCalled -CommandName Get-command -Times 1 -Scope It -ParameterFilter { $Module -eq 'VMware.VMC' }
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

@@ -1,73 +1,75 @@
#Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
inModuleScope VMware.VMC {
$functionName = "Get-VMCOrg"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true
$global:DefaultVMCServers = $true
$display_name = "MockedDisplayName"
$user_name = "MockedUserName"
$OrgName = "MockedDisplayName"
$created = "MockedDate"
$id = "MockedId"
$Service = "com.vmware.vmc.orgs"
$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 }
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 }
$MockedList = [PSCustomObject]@{
"display_name" = $display_name
"name" = $OrgName
"created" = $created
"user_name" = $user_name
"id" = $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
$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'
}
# 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
Context "Behavior testing" {
$(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 "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 -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
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
inModuleScope VMware.VMC {
$functionName ="Get-VmcSddc"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true
$global:DefaultVMCServers = $true
$OrgId = "Mocked OrgID"
$name = "MockedSDDCName"
$Notname = "NotTheName"
$Service = "com.vmware.vmc.orgs.sddcs"
$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 }
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
$MockedList = [PSCustomObject]@{
"name" = $name
}
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 }
$MockedList2 = [PSCustomObject]@{
"name" = $Notname
}
# 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 }
$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'
}
# 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
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
}
}
}
}
}

View File

@@ -1,78 +1,80 @@
#Requires -Modules Pester, VMware.VMC
$functionName = $MyInvocation.MyCommand.Name.TrimEnd(".Tests.ps1")
Import-Module -Name VMware.VimAutomation.Cis.Core
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
inModuleScope VMware.VMC {
$functionName = "Get-VMCTask"
Describe "$functionName" -Tag 'Unit' {
. "$PSScriptRoot/Shared.ps1"
$global:DefaultVMCServers = $true
$global:DefaultVMCServers = $true
$OrgId = "Mocked OrgID"
$name = "MockedSDDCName"
$Notname = "NotTheName"
$id = "MockedId"
$Service = "com.vmware.vmc.orgs.tasks"
$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 }
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 }
$MockedList = [PSCustomObject]@{
"name" = $name
}
$MockedList2 = [PSCustomObject]@{
"name" = $Notname
}
# 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 }
$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'
}
# 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 }
}
Context "Behavior testing" {
# 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 "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
}
}
}
}
}