From 37016076fde9c710f5bd67a1a9bdc0ed4b75839c Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Sat, 20 Jul 2019 20:56:23 -0400 Subject: [PATCH] Update to the new format --- Tests/Public/Install-ZertoVra.Tests.ps1 | 143 ++++++-------------- Tests/Public/Invoke-ZertoFailover.Tests.ps1 | 124 +++++++++-------- 2 files changed, 113 insertions(+), 154 deletions(-) diff --git a/Tests/Public/Install-ZertoVra.Tests.ps1 b/Tests/Public/Install-ZertoVra.Tests.ps1 index 598a6e4..e14e10f 100644 --- a/Tests/Public/Install-ZertoVra.Tests.ps1 +++ b/Tests/Public/Install-ZertoVra.Tests.ps1 @@ -1,108 +1,51 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 + Context "$global:function::Parameter Unit Tests" { + $ParameterTestCases = @( + @{ParameterName = 'hostName'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'datastoreName'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'networkName'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'Dhcp'; Type = 'Switch'; Mandatory = $true; Validation = $null } + @{ParameterName = 'vraIpAddress'; Type = 'String'; Mandatory = $true; Validation = 'IpAddr' } + @{ParameterName = 'subnetMask'; Type = 'String'; Mandatory = $true; Validation = 'IpAddr' } + @{ParameterName = 'defaultGateway'; Type = 'String'; Mandatory = $true; Validation = 'IpAddr' } + ) + + It " parameter is of type" -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Mandatory, $Validation) + Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type + } + + It " parameter has correct validation setting" -TestCases $ParameterTestCases { + param($ParameterName, $Validation) + Switch ($Validation) { + 'NotNullOrEmpty' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 + } + + 'IpAddr' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateScript] }.Count | Should -Be 1 + $attrs.Where{ $_ -is [ValidateScript] }.ScriptBlock | Should -Match '^\$_ \-match \[IPAddress\]\$_' + } + + $null { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.TypeId.Count | Should -Be 2 + } + } + } } - Context "$($file.BaseName)::Parameter Unit Tests" { + Context "$global:function::Parameter Functional Tests" { - It "Has a mandatory string host name parameter" { - Get-Command $file.BaseName | Should -HaveParameter hostName - Get-Command $file.BaseName | Should -HaveParameter hostName -Mandatory - Get-Command $file.BaseName | Should -HaveParameter hostName -Type String - } - - It "Will not accecpt a Null or Empty string for the host name" { - {Install-ZertoVra -hostName $null -datastoreName "DS01" -networkName "MyNetwork" -Dhcp } | Should -Throw "The argument is null or empty" - {Install-ZertoVra -hostName "" -datastoreName "DS01" -networkName "MyNetwork" -Dhcp } | Should -Throw "The argument is null or empty" - } - - It "Has a mandatory string datastore parameter" { - Get-Command $file.BaseName | Should -HaveParameter datastoreName - Get-Command $file.BaseName | Should -HaveParameter datastoreName -Mandatory - Get-Command $file.BaseName | Should -HaveParameter datastoreName -Type String - } - - It "Will not accecpt a Null or Empty string for the datastore" { - {Install-ZertoVra -hostName "MyfirstHost" -datastoreName $null -networkName "MyNetwork" -Dhcp } | Should -Throw "The argument is null or empty" - {Install-ZertoVra -hostName "MyfirstHost" -datastoreName "" -networkName "MyNetwork" -Dhcp } | Should -Throw "The argument is null or empty" - } - - It "Has a mandatory string network parameter" { - Get-Command $file.BaseName | Should -HaveParameter networkName - Get-Command $file.BaseName | Should -HaveParameter networkName -Mandatory - Get-Command $file.BaseName | Should -HaveParameter networkName -Type String - } - - It "Will not accecpt a Null or Empty string for the datastore" { - {Install-ZertoVra -hostName "MyfirstHost" -datastoreName "DS01" -networkName $null -Dhcp } | Should -Throw "The argument is null or empty" - {Install-ZertoVra -hostName "MyfirstHost" -datastoreName "DS01" -networkName "" -Dhcp } | Should -Throw "The argument is null or empty" - } - - it "Has a switch parameter for setting DHCP" { - Get-Command $file.BaseName | Should -HaveParameter Dhcp - Get-Command $file.BaseName | Should -HaveParameter Dhcp -Mandatory - Get-Command $file.BaseName | Should -HaveParameter Dhcp -Type 'Switch' - - } - - it "Has a mandatory string parameter for the static IP address" { - Get-Command $file.BaseName | Should -HaveParameter vraIpAddress - Get-Command $file.BaseName | Should -HaveParameter vraIpAddress -Mandatory - Get-Command $file.BaseName | Should -HaveParameter vraIpAddress -Type String - } - - it "Has a mandatory string parameter for the subnet mask" { - Get-Command $file.BaseName | Should -HaveParameter subnetMask - Get-Command $file.BaseName | Should -HaveParameter subnetMask -Mandatory - Get-Command $file.BaseName | Should -HaveParameter subnetMask -Type String - } - - it "Has a mandatory string parameter for the default gateway" { - Get-Command $file.BaseName | Should -HaveParameter defaultGateway - Get-Command $file.BaseName | Should -HaveParameter defaultGateway -Mandatory - Get-Command $file.BaseName | Should -HaveParameter defaultGateway -Type String - } - - $cases = ` - @{invalidIpAddress = "192.168.1.256"}, ` - @{invalidIpAddress = "192.168.1"}, ` - @{invalidIpAddress = "String"}, ` - @{invalidIpAddress = 192.168.1.246}, ` - @{invalidIpAddress = 32}, ` - @{invalidIpAddress = ""}, ` - @{invalidIpAddress = $null} - It "IpAddress field require valid IP addresses as a String: " -TestCases $cases { - param ( $invalidIpAddress ) - {Install-ZertoVra -hostName "MyFirstHost" -datastoreName "DS01" -networkName "MyNetwork" -vraIpAddress $invalidIpAddress -subnetMask "255.255.255.0" -defaultGateway "192.168.1.254"} | Should -Throw - } - - It "Default Gateway field require valid IP addresses as a String: " -TestCases $cases { - param ( $invalidIpAddress ) - {Install-ZertoVra -hostName "MyFirstHost" -datastoreName "DS01" -networkName "MyNetwork" -vraIpAddress '192.168.1.100' -subnetMask "255.255.255.0" -defaultGateway $invalidIpAddress} | Should -Throw - } - - It "Subnet Mask field require valid IP addresses as a String: " -TestCases $cases { - param ( $invalidIpAddress ) - {Install-ZertoVra -hostName "MyFirstHost" -datastoreName "DS01" -networkName "MyNetwork" -vraIpAddress '192.168.1.100' -subnetMask $invalidIpAddress -defaultGateway "192.168.1.254"} | Should -Throw - } - - } - - Context "$($file.BaseName)::Function Unit Tests" { - #TODO } } + +Remove-Variable -Name here -Scope Global +Remove-Variable -Name function -Scope Global diff --git a/Tests/Public/Invoke-ZertoFailover.Tests.ps1 b/Tests/Public/Invoke-ZertoFailover.Tests.ps1 index 8582291..fab5802 100644 --- a/Tests/Public/Invoke-ZertoFailover.Tests.ps1 +++ b/Tests/Public/Invoke-ZertoFailover.Tests.ps1 @@ -1,77 +1,93 @@ #Requires -Modules Pester -$moduleFileName = "ZertoApiWrapper.psd1" -$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") -$file = Get-ChildItem "$here\$sut" -$modulePath = $here -replace "Public", "" -$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" -Get-Module -Name ZertoApiWrapper | Remove-Module -Force -Import-Module $moduleFile -Force +$global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path) +$global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0] -Describe $file.BaseName -Tag 'Unit' { +Describe $global:function -Tag 'Unit', 'Source', 'Built' { - It "is valid Powershell (Has no script errors)" { - $contents = Get-Content -Path $file -ErrorAction Stop - $errors = $null - $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) - $errors | Should -HaveCount 0 - } + Context "$global:function::Parameter Unit Tests" { + $ParameterTestCases = @( + @{ParameterName = 'vpgName'; Type = 'String'; Mandatory = $true; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'checkpointIdentifier'; Type = 'String'; Mandatory = $false; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'commitPolicy'; Type = 'String'; Mandatory = $false; Validation = 'Set' } + @{ParameterName = 'shutdownPolicy'; Type = 'Int'; Mandatory = $false; Validation = 'Set' } + @{ParameterName = 'timeToWaitBeforeShutdownInSec'; Type = 'Int'; Mandatory = $false; Validation = 'Range' } + @{ParameterName = 'reverseProtection'; Type = 'bool'; Mandatory = $false; Validation = $null } + @{ParameterName = 'vmName'; Type = 'String[]'; Mandatory = $false; Validation = 'NotNullOrEmpty' } + @{ParameterName = 'whatIf'; Type = 'Switch'; Mandatory = $false; Validation = 'ShouldProcess' } + ) - Context "$($file.BaseName)::Parameter Unit Tests" { - it "has a mandatory string parameter for the vpgName" { - Get-Command $file.BaseName | Should -HaveParameter vpgName - Get-Command $file.BaseName | Should -HaveParameter vpgName -Type string - Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory + It " parameter is of type" -TestCases $ParameterTestCases { + param($ParameterName, $Type, $Mandatory, $Validation) + Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type } - it "has a non-mandatory string parameter for the checkpoint" { - Get-Command $file.BaseName | Should -HaveParameter checkpointIdentifier - Get-Command $file.BaseName | Should -HaveParameter checkpointIdentifier -Type string - Get-Command $file.BaseName | Should -HaveParameter checkpointIdentifier -Not -Mandatory + It " parameter has correct validation setting" -TestCases $ParameterTestCases { + param($ParameterName, $Validation) + Switch ($Validation) { + 'NotNullOrEmpty' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1 + } + + 'Set' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateSet] }.Count | Should -Be 1 + } + + 'Range' { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.Where{ $_ -is [ValidateRange] }.Count | Should -Be 1 + } + + 'Range' { + $scriptBlock = (Get-Command $global:function).ScriptBlock + $scriptBlock | Should -match 'SupportsShouldProcess' + $scriptBlock | Should -match '\$PSCmdlet\.ShouldProcess\(.+\)' + } + + $null { + $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes + $attrs.TypeId.Count | Should -Be 2 + } + } } - it "has a non-mandatory string parameter for the commit policy" { - Get-Command $file.BaseName | Should -HaveParameter commitPolicy - Get-Command $file.BaseName | Should -HaveParameter commitPolicy -Type string - Get-Command $file.BaseName | Should -HaveParameter commitPolicy -Not -Mandatory - Get-Command $file.BaseName | Should -HaveParameter commitPolicy -DefaultValue "Rollback" + It "Commit Policy Default Value is 'RollBack'" { + Get-Command $global:function | Should -HaveParameter commitPolicy -DefaultValue "Rollback" } - it "has a non-mandatory int parameter for the shutdown policy" { - Get-Command $file.BaseName | Should -HaveParameter shutdownPolicy - Get-Command $file.BaseName | Should -HaveParameter shutdownPolicy -Type int - Get-Command $file.BaseName | Should -HaveParameter shutdownPolicy -Not -Mandatory - Get-Command $file.BaseName | Should -HaveParameter shutdownPolicy -DefaultValue 0 + It "Commit Policy Only Accecpts 'RollBack', 'Commit', or 'None'" { + (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -HaveCount 3 + (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 'RollBack' + (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 'Commit' + (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 'None' } - it "has a non-mandatory int parameter for the time to wait before force shutdown" { - Get-Command $file.BaseName | Should -HaveParameter timeToWaitBeforeShutdownInSec - Get-Command $file.BaseName | Should -HaveParameter timeToWaitBeforeShutdownInSec -Type int - Get-Command $file.BaseName | Should -HaveParameter timeToWaitBeforeShutdownInSec -Not -Mandatory - Get-Command $file.BaseName | Should -HaveParameter timeToWaitBeforeShutdownInSec -DefaultValue 3600 + It "Shutdown Policy Default Value is '0'" { + Get-Command $global:function | Should -HaveParameter shutdownPolicy -DefaultValue 0 } - it "has a non-mandatory bool parameter for the reverse protection policy" { - Get-Command $file.BaseName | Should -HaveParameter reverseProtection - Get-Command $file.BaseName | Should -HaveParameter reverseProtection -Type bool - Get-Command $file.BaseName | Should -HaveParameter reverseProtection -Not -Mandatory + It "Shutdown Policy Only Accecpts 'RollBack', 'Commit', or 'None'" { + (Get-Command $global:function).Parameters['shutdownPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -HaveCount 3 + (Get-Command $global:function).Parameters['shutdownPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 0 + (Get-Command $global:function).Parameters['shutdownPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 1 + (Get-Command $global:function).Parameters['shutdownPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 2 } - it "has a non-mandatory array string parameter for the named VMs to be failed over" { - Get-Command $file.BaseName | Should -HaveParameter vmName - Get-Command $file.BaseName | Should -HaveParameter vmName -Type string[] - Get-Command $file.BaseName | Should -HaveParameter vmName -Not -Mandatory + it "Time to wait before shutdown in sec should have a default value of 3600" { + Get-Command $global:function | Should -HaveParameter timeToWaitBeforeShutdownInSec -DefaultValue 3600 } - it "Supports 'SupportsShouldProcess'" { - Get-Command $file.BaseName | Should -HaveParameter WhatIf - Get-Command $file.BaseName | Should -HaveParameter Confirm - $file | Should -FileContentMatch 'SupportsShouldProcess' - $file | Should -FileContentMatch '\$PSCmdlet\.ShouldProcess\(.+\)' + it "Time to wait before shutdown in sec should have a minimum value of 300 and max value of 86400" { + (Get-Command $global:function).Parameters['timeToWaitBeforeShutdownInSec'].Attributes.Where{ $_ -is [ValidateRange] }.MinRange | Should -Be 300 + (Get-Command $global:function).Parameters['timeToWaitBeforeShutdownInSec'].Attributes.Where{ $_ -is [ValidateRange] }.MaxRange | Should -Be 86400 } } - Context "$($file.BaseName)::Function Unit Tests" { - #TODO + Context "$global:function::Parameter Functional Tests" { + } } + +Remove-Variable -Name here -Scope Global +Remove-Variable -Name function -Scope Global