From 83e95516925fb37a96f82b01a3dcb42d62e18a44 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Thu, 9 Apr 2020 12:48:33 -0400 Subject: [PATCH] Add tests for function --- .../Get-ZertoVirtualizationSite.Tests.ps1 | 234 ++++++++++++++++++ 1 file changed, 234 insertions(+) diff --git a/Tests/Public/Get-ZertoVirtualizationSite.Tests.ps1 b/Tests/Public/Get-ZertoVirtualizationSite.Tests.ps1 index e6e6083..436928b 100644 --- a/Tests/Public/Get-ZertoVirtualizationSite.Tests.ps1 +++ b/Tests/Public/Get-ZertoVirtualizationSite.Tests.ps1 @@ -3,13 +3,247 @@ $global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) $global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] Describe $global:function -Tag 'Unit', 'Source', 'Built' { + BeforeAll { + $script:ScriptBlock = (Get-Command $global:function).ScriptBlock + } Context "$global:function::Parameter Unit Tests" { + It "$global:function should have exactly 23 parameters defined" { + (Get-Command $global:function).Parameters.Count | Should -Be 23 + } + + $ParameterTestCases = @( + @{ParameterName = 'siteIdentifier'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'hostIdentifier'; Type = 'String'; Mandatory = $false; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'folders'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'hostClusters'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'hosts'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'networks'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'resourcePools'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'vms'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + @{ParameterName = 'repositories'; Type = 'Switch'; Mandatory = $true; Validation = $Null } + ) + + It " parameter is of type" -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Mandatory) + Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type + } + + It " parameter has correct validation setting of " -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Validation) + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + Switch ($Validation) { + + 'NotNullOrEmpty' { + $attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 + } + + $Null { + $Type -match 'Switch' | Should -BeTrue -Because "Only Switch Parameters should not have validation" + } + + default { + $true | Should -BeFalse -Because "No Validation Selected. Review test cases" + } + + } + } + + It "$($global:function) does not have 'SupportsShouldProcess'" { + Get-Command $global:function | Should -not -HaveParameter WhatIf + Get-Command $global:function | Should -not -HaveParameter Confirm + $script:ScriptBlock | Should -not -match 'SupportsShouldProcess' + $script:ScriptBlock | Should -not -match '\$PSCmdlet\.ShouldProcess\(.+\)' + } } Context "$global:function::Parameter Functional Tests" { + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-NoParams.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/devices' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-devices.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/devices?hostIdentifier=4567' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-devices-hostid.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hosts' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-hosts.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hosts/4567' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-hosts-hostid.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-siteId.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/datastores' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-datastores.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/datastoreclusters' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-datastoreClusters.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/networks' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-Networks.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/folders' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-Folders.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hostclusters' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-hostClusters.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/resourcepools' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-ResourcePools.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/vms' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-VMs.json" -Raw) | ConvertFrom-Json + } -Verifiable + Mock -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/repositories' + } { + return (Get-Content "$global:here\Mocks\VirtualSite-repositories.json" -Raw) | ConvertFrom-Json + } -Verifiable + It "Should return all known sites when called without parameters" { + $results = Get-ZertoVirtualizationSite + $results.Count | Should -BeExactly 2 + } + + It "Should return a single site when a siteIdentifier is provided" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of devices with the '-devices' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -devices + $results.Count | Should -BeExactly 5 + } + + It "Should return a list of devices with the '-devices' switch and hostIdentifier provided" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -devices -hostIdentifier '4567' + $results.Count | Should -BeExactly 5 + } + + It "Should return a list of hosts with the '-hosts' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -hosts + $results.Count | Should -BeExactly 3 + } + + It "Should return a single host with the '-hosts' switch and hostIdentifier provided" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -hosts -hostIdentifier '4567' + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of datastores with the '-datastores' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -datastores + $results.Count | Should -BeExactly 8 + } + + It "Should return a list of datastores with the '-datastoreClusters' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -datastoreClusters + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of Networks with the '-networks' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -networks + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of folders with the '-folders' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -folders + $results.Count | Should -BeExactly 3 + } + + It "Should return a list of Host Clusters with the '-hostClusters' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -hostClusters + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of Resource Pools with the '-resourcePools' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -resourcePools + $results.Count | Should -BeExactly 1 + } + + It "Should return a list of VMs with the '-VMs' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -vms + $results.Count | Should -BeExactly 4 + } + + It "Should return a list of LTR Repositories with the '-repositories' switch" { + $results = Get-ZertoVirtualizationSite -siteIdentifier '1234' -repositories + $results.Count | Should -BeExactly 1 + } + + Assert-VerifiableMock + + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/devices' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/devices?hostIdentifier=4567' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hosts' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hosts/4567' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/datastores' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/datastoreclusters' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/networks' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/folders' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/hostclusters' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/resourcepools' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/vms' + } -Exactly 1 + Assert-MockCalled -ModuleName ZertoApiWrapper -CommandName Invoke-ZertoRestRequest -ParameterFilter { + $uri -eq 'virtualizationsites/1234/repositories' + } -Exactly 1 } }