Update tests to new format

This commit is contained in:
Wes Carroll
2019-07-21 12:09:15 -04:00
parent e1720dd4f7
commit 928d6ef4a1
+57 -49
View File
@@ -1,74 +1,82 @@
#Requires -Modules Pester #Requires -Modules Pester
$moduleFileName = "ZertoApiWrapper.psd1" $global:here = (Split-Path -Parent $MyInvocation.MyCommand.Path)
$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") $global:function = ((Split-Path -leaf $MyInvocation.MyCommand.Path).Split('.'))[0]
$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
Describe $file.BaseName -Tag 'Unit' { Describe $global:function -Tag 'Unit', 'Source', 'Built' {
It "is valid Powershell (Has no script errors)" { Context "$global:function::Parameter Unit Tests" {
$contents = Get-Content -Path $file -ErrorAction Stop it "$global:function should have exactly 20 parameters defined" {
$errors = $null (get-command $global:function).Parameters.Count | Should -Be 20
$null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
$errors | Should -HaveCount 0
} }
Context "$($file.BaseName)::Parameter Unit Tests" { $ParameterTestCases = @(
@{ParameterName = 'vpgName'; Type = 'String[]'; Mandatory = $true; Validation = 'NotNullOrEmpty' }
@{ParameterName = 'commitPolicy'; Type = 'String'; Mandatory = $false; Validation = 'Set' }
@{ParameterName = 'commitPolicyTimeout'; Type = 'Int'; Mandatory = $false; Validation = 'Range' }
@{ParameterName = 'forceShutdown'; Type = 'Switch'; Mandatory = $false; Validation = $null }
@{ParameterName = 'disableReverseProtection'; Type = 'Switch'; Mandatory = $true; Validation = $null }
@{ParameterName = 'keepSourceVms'; Type = 'Switch'; Mandatory = $true; Validation = $null }
@{ParameterName = 'ContinueOnPreScriptFailure'; Type = 'Switch'; Mandatory = $false; Validation = $null }
@{ParameterName = 'whatIf'; Type = 'Switch'; Mandatory = $false; Validation = 'ShouldProcess' }
)
it "has a mandatory string parameter for the vpgName" { It "<ParameterName> parameter is of <Type> type" -TestCases $ParameterTestCases {
Get-Command $file.BaseName | Should -HaveParameter vpgName param($ParameterName, $Type, $Mandatory, $Validation)
Get-Command $file.BaseName | Should -HaveParameter vpgName -Type string[] Get-Command $global:function | Should -HaveParameter $ParameterName -Mandatory:$Mandatory -Type $Type
Get-Command $file.BaseName | Should -HaveParameter vpgName -Mandatory
} }
it "has a non-mandatory string parameter for commitPolicy" { It "<ParameterName> parameter has correct validation setting" -TestCases $ParameterTestCases {
Get-Command $file.BaseName | Should -HaveParameter commitPolicy param($ParameterName, $Validation)
Get-Command $file.BaseName | Should -HaveParameter commitPolicy -Type string Switch ($Validation) {
'NotNullOrEmpty' {
$attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes
$attrs.Where{ $_ -is [ValidateNotNullOrEmpty] }.Count | Should -Be 1
} }
it "CommitPolicy only accecpts 'Rollback', 'Commit', or 'None'" { 'Set' {
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicy "Rollbackk" } | Should -Throw $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicy "" } | Should -Throw $attrs.Where{ $_ -is [ValidateSet] }.Count | Should -Be 1
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicy $null } | Should -Throw
} }
it "has a non-mandatory int parameter for commitPolicyTimeout" { 'Range' {
Get-Command $file.BaseName | Should -HaveParameter commitPolicyTimeout $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes
Get-Command $file.BaseName | Should -HaveParameter commitPolicyTimeout -Type Int $attrs.Where{ $_ -is [ValidateRange] }.Count | Should -Be 1
} }
it "Commit Policy Timeout will only accecpt an int value between 5 minutes and 24 Hours" { 'ShouldProcess' {
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicyTimeout 150 } | Should -Throw $scriptBlock = (Get-Command $global:function).ScriptBlock
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicyTimeout 15000000 } | Should -Throw $scriptBlock | Should -match 'SupportsShouldProcess'
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicyTimeout -1350 } | Should -Throw $scriptBlock | Should -match '\$PSCmdlet\.ShouldProcess\(.+\)'
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicyTimeout $null } | Should -Throw
{ Invoke-ZertoMove -vpgName "MyVpg" -commitPolicyTimeout "" } | Should -Throw
} }
it "has a non-mandatory switch parameter for forceShutdown" { $null {
Get-Command $file.BaseName | Should -HaveParameter forceShutdown $attrs = (Get-Command $global:function).Parameters[$ParameterName].Attributes
Get-Command $file.BaseName | Should -HaveParameter forceShutdown -Type Switch $attrs.TypeId.Count | Should -Be 2
} }
it "has a mandatory switch parameter for disableReverseProtection" { default {
Get-Command $file.BaseName | Should -HaveParameter disableReverseProtection $true | should be $false -Because "No Validation Selected. Review test cases"
Get-Command $file.BaseName | Should -HaveParameter disableReverseProtection -Type Switch }
Get-Command $file.BaseName | Should -HaveParameter disableReverseProtection -Mandatory }
} }
it "has a non-mandatory switch parameter for keepSourceVms" { It "Commit Policy Only Accecpts 'RollBack', 'Commit', or 'None'" {
Get-Command $file.BaseName | Should -HaveParameter keepSourceVms (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -HaveCount 3
Get-Command $file.BaseName | Should -HaveParameter keepSourceVms -Type Switch (Get-Command $global:function).Parameters['commitPolicy'].Attributes.Where{ $_ -is [ValidateSet] }.ValidValues | Should -Contain 'RollBack'
Get-Command $file.BaseName | Should -HaveParameter keepSourceVms -Mandatory (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 switch parameter for ContinueOnPreScriptFailure" { it "Commit Policy Timeout should have a minimum value of 300 and max value of 86400" {
Get-Command $file.BaseName | Should -HaveParameter ContinueOnPreScriptFailure (Get-Command $global:function).Parameters['commitPolicyTimeout'].Attributes.Where{ $_ -is [ValidateRange] }.MinRange | Should -Be 300
Get-Command $file.BaseName | Should -HaveParameter ContinueOnPreScriptFailure -Type Switch (Get-Command $global:function).Parameters['commitPolicyTimeout'].Attributes.Where{ $_ -is [ValidateRange] }.MaxRange | Should -Be 86400
} }
} }
Context "$global:function::Parameter Functional Tests" {
}
} }
Remove-Variable -Name here -Scope Global
Remove-Variable -Name function -Scope Global