From 87d23373c6f254fae40e658c2580892c611e04d4 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Fri, 20 Jan 2017 11:32:44 -0600 Subject: [PATCH 01/12] Re-add F5-LTM.Tests.ps1 following merge from trunk --- F5-LTM/F5-LTM.Tests.ps1 | 783 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 783 insertions(+) create mode 100644 F5-LTM/F5-LTM.Tests.ps1 diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 new file mode 100644 index 0000000..5b35ce1 --- /dev/null +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -0,0 +1,783 @@ +$scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) + +Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force + +$F5LTMTestCases = @{} +[Regex]::Matches((Get-Content $MyInvocation.MyCommand.Path -raw),'(?<=F5LTMTestCases\.)Get_\w*') | + Select-Object -ExpandProperty Value -Unique | + Sort-Object | % { + $F5LTMTestCases.Add($_, @()) +} +$TestCasePath = Invoke-NullCoalescing { $env:F5LTMTestCases } { (Join-Path $HOME F5-LTM.TestCases.ps1) } +if (Test-Path -Path $TestCasePath) { + . $TestCasePath +} else { + Write-Host ('Writing test case template to {0}' -f $TestCasePath) -ForegroundColor Green + $F5LTMTestCasesTemplate = @" +`$credentials = New-Object System.Management.Automation.PSCredential ('[username]', (ConvertTo-SecureString '[password]' -AsPlainText -Force)) + +`$Sessions = @{} +`$Sessions.Add('default', (New-F5Session -LTMName '[ipaddress]' -LTMCredentials `$credentials -Default -PassThrough)) + +"@ + $F5LTMTestCases.Keys | Sort-Object | ForEach-Object { + $params = [Regex]::Match($_, '(?<=_By).*$') -replace 'PoolnameFullpath','fullpath' -split 'And' + $hashvalues = @() + if ($_ -match 'FromPipeline$') { + for($x=0;$x -le 1; $x++) { + $pipelineobjects = @() + $pipelineparams = @() + foreach($p in $params) { + if ($p) { + $pipelineparams += ' {0} = ''[{0}]'' ' -f ($p.ToLower() -replace 'frompipeline','') + } + } + $pipelineobjects += ('([pscustomobject]@{{{0}}})' -f ($pipelineparams -join ';')) + } + $hashvalues += '; object = {0}' -f ($pipelineobjects -join ',') + } else { + foreach($p in $params) { + if ($p) { + if ($p -match 'Array$') { + $hashvalues += '; {0} = ''[{0}1]'',''[{0}2]'',''[{0}3]''' -f ($p.ToLower() -replace 'array','') + } else { + $hashvalues += '; {0} = ''[{0}]''' -f $p.ToLower() + } + } + } + } + $F5LTMTestCasesTemplate += '$F5LTMTestCases.{0} += @{{ session = ''default''{1} }}{2}' -f $_,($hashvalues -join ''),[Environment]::NewLine + } + $F5LTMTestCasesTemplate | Out-File -FilePath $TestCasePath +} +Describe 'TestCases' -Tags 'Validation' { + Context 'Empty' { + $F5LTMTestCases.Keys | Sort-Object | ForEach-Object { + If ($F5LTMTestCases[$_].Count -eq 0) { + Write-Warning ('$F5LTMTestCases.{0} is empty' -f $_) + } + } + Write-Host 'Invoke-Pester -ExcludeTag Validation to suppress empty test case warnings' -ForegroundColor Green + } +} +Describe 'HealthMonitor' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_HealthMonitor) { + It "Gets health monitors * on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByType) { + It "Gets health monitors of type '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByType { + param($session, $type) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] -Type $type | + Select-Object -ExpandProperty type | + Should Be $type + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByPartition) { + It "Gets health monitors in partition '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition | + Select-Object -ExpandProperty partition | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition) { + It "Gets health monitors in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition { + param($session, $partition, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition -Name $name | + Select-Object -ExpandProperty name | + Should Be $Name + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByFullpath) { + It "Gets health monitors by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByFullpath { + param($session, $partition, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] -Name $fullPath | + Select-Object -ExpandProperty fullPath | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByNameArray) { + It "Gets health monitors by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitor -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByNameFromPipeline) { + It "Gets health monitors by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-HealthMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_HealthMonitor_ByNameAndPartitionFromPipeline) { + It "Gets health monitors by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartitionFromPipeline { + param($session, $object) + $Sessions.ContainsKey($session) | Should Be $true + + $object | Get-HealthMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $object.Count + } + } + } + } +} +Describe 'HealthMonitorType' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_HealthMonitorType) { + It "Gets health monitor types * on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitorType -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_HealthMonitorType_ByName) { + It "Gets health monitor types by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByName { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitorType -F5Session $Sessions[$session] -Name $name | + Should Be $name + } + } + If ($F5LTMTestCases.Get_HealthMonitorType_ByNameArray) { + It "Gets health monitor types by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-HealthMonitorType -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_HealthMonitorType_ByNameFromPipeline) { + It "Gets health monitor types by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-HealthMonitorType -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + } + } +} +Describe 'iRule' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_iRule) { + It "Gets irules* on ''" -TestCases $F5LTMTestCases.Get_iRule { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_iRule_ByName) { + It "Gets irules by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByName { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] -Name $name | + Select-Object -ExpandProperty name | + Should Be $name + } + } + If ($F5LTMTestCases.Get_iRule_ByPartition) { + It "Gets irules in partition '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] -Partition $partition | + Select-Object -ExpandProperty partition | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_iRule_ByNameAndPartition) { + It "Gets irules in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartition { + param($session, $partition, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] -Partition $partition -Name $name | + Select-Object -ExpandProperty name | + Should Be $Name + } + } + If ($F5LTMTestCases.Get_iRule_ByFullpath) { + It "Gets irules by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByFullpath { + param($session, $partition, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] -Name $fullPath | + Select-Object -ExpandProperty fullPath | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_iRule_ByNameArray) { + It "Gets irules by Name[] on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-iRule -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_iRule_ByNameFromPipeline) { + It "Gets irules by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-iRule -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_iRule_ByNameAndPartitionFromPipeline) { + It "Gets irules by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartitionFromPipeline { + param($session, $object) + $Sessions.ContainsKey($session) | Should Be $true + + $object | Get-iRule -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $object.Count + } + } + } + } +} +Describe 'Node' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_Node) { + It "Gets nodes * on ''" -TestCases $F5LTMTestCases.Get_Node { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Node -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_Node_ByPartition) { + It "Gets nodes in partition '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Node -F5Session $Sessions[$session] -Partition $partition | + Select-Object -ExpandProperty partition | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_Node_ByNameAndPartition) { + It "Gets nodes in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartition { + param($session, $partition, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Node -F5Session $Sessions[$session] -Partition $partition -Name $name | + Select-Object -ExpandProperty name | + Should Be $Name + } + } + If ($F5LTMTestCases.Get_Node_ByFullpath) { + It "Gets nodes by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByFullpath { + param($session, $partition, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Node -F5Session $Sessions[$session] -Name $fullPath | + Select-Object -ExpandProperty fullPath | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_Node_ByNameArray) { + It "Gets nodes by Name[] on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Node -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_Node_ByNameFromPipeline) { + It "Gets nodes by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-Node -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_Node_ByNameAndPartitionFromPipeline) { + It "Gets nodes by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartitionFromPipeline { + param($session, $object) + $Sessions.ContainsKey($session) | Should Be $true + + $object | Get-Node -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $object.Count + } + } + } + } +} +Describe 'Partition' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_Partition) { + It "Gets partitions * on ''" -TestCases $F5LTMTestCases.Get_Partition { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Partition -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_Partition_ByName) { + It "Gets partitions by Name '' on ''" -TestCases $F5LTMTestCases.Get_Partition_ByName { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Partition -F5Session $Sessions[$session] -Name $name | + Should Be $name + } + } + If ($F5LTMTestCases.Get_Partition_ByNameArray) { + It "Gets partitions by Name[] on ''" -TestCases $F5LTMTestCases.Get_Partition_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Partition -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_Partition_ByNameFromPipeline) { + It "Gets partitions by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Partition_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-Partition -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + } + } +} +Describe 'Pool' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_Pool) { + It "Gets pools * on ''" -TestCases $F5LTMTestCases.Get_Pool { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_Pool_ByPartition) { + It "Gets pools in partition '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -Partition $partition | + Select-Object -ExpandProperty partition | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_Pool_ByNameAndPartition) { + It "Gets pools in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartition { + param($session, $partition, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -Partition $partition -Name $name | + Select-Object -ExpandProperty name | + Should Be $Name + } + } + If ($F5LTMTestCases.Get_Pool_ByFullpath) { + It "Gets pools by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByFullpath { + param($session, $partition, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -Name $fullPath | + Select-Object -ExpandProperty fullPath | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_Pool_ByNameArray) { + It "Gets pools by Name[] on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_Pool_ByNameFromPipeline) { + It "Gets pools by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-Pool -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_Pool_ByNameAndPartitionFromPipeline) { + It "Gets pools by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartitionFromPipeline { + param($session, $object) + $Sessions.ContainsKey($session) | Should Be $true + + $object | Get-Pool -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $object.Count + } + } + } + } +} +Describe "PoolMember" { + Context "Get" { + if ($Sessions) { + if ($F5LTMTestCases.Get_PoolMember) { + It "Gets pool members * on ''" -TestCases $F5LTMTestCases.Get_PoolMember { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMember -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_PoolMember_ByPartition) { + It "Gets pool members in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMember -F5Session $Sessions[$session] -Partition $partition | + ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_PoolMember_ByPoolnameAndPartition) { + It "Gets pool members in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameAndPartition { + param($session, $partition, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + # Pool members can be from a different partition than the pool. Check GetPoolName() partition matches instead. + Get-PoolMember -F5Session $Sessions[$session] -PoolName $poolname -Partition $Partition | + ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_PoolMember_ByPoolnameFullpath) { + It "Gets pool members in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameFullpath { + param($session, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMember -F5Session $Sessions[$session] -PoolName $fullPath | + ForEach-Object { $_.GetPoolName() } | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_PoolMember_ByAddressArray) { + It "Gets pool members in pool '' by Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByAddressArray { + param($session, $address, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | + Get-PoolMember -F5Session $Sessions[$session] -Address $address | + Select-Object -ExpandProperty Address | + Should Match '^\d+\.\d+\.\d+\.\d+' + } + } + If ($F5LTMTestCases.Get_PoolMember_ByNameArray) { + It "Gets pool members in pool '' by Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByNameArray { + param($session, $name, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | + Get-PoolMember -F5Session $Sessions[$session] -Name $name | + Select-Object -ExpandProperty Name | + Should Match '^.+:\d+' + } + } + } + } +} +Describe "PoolMemberStats" { + Context "Get" { + if ($Sessions) { + if ($F5LTMTestCases.Get_PoolMemberStats) { + It "Gets pool member statistics in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats { + param($session, $poolname, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition | + Select-Object -ExpandProperty 'serverside.curConns' | + Should Not Be Null + } + } + if ($F5LTMTestCases.Get_PoolMemberStats_ByPoolnameFullpath) { + It "Gets pool member statistics in pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByPoolnameFullpath { + param($session, $fullPath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $fullPath | + Select-Object -ExpandProperty 'serverside.curConns' | + Should Not Be Null + } + } + if ($F5LTMTestCases.Get_PoolMemberStats_ByAddressArray) { + It "Gets pool member statistics in pool '' and Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByAddressArray { + param($session, $address, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | + Get-PoolMemberStats -F5Session $Sessions[$session] -Address $address + $memberstats | + Select-Object -ExpandProperty 'serverside.curConns' | + Should Not Be Null + $memberstats | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $address.Count + } + } + if ($F5LTMTestCases.Get_PoolMemberStats_ByNameArray) { + It "Gets pool member statistics in pool '' and Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByNameArray { + param($session, $name, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | + Get-PoolMemberStats -F5Session $Sessions[$session] -name $name + $memberstats | + Select-Object -ExpandProperty 'serverside.curConns' | + Should Not Be Null + $memberstats | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + } + } +} +Describe "PoolMonitor" { + Context "Get" { + if ($Sessions) { + if ($F5LTMTestCases.Get_PoolMonitor) { + It "Gets pool monitors * on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-PoolMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_PoolMonitor_ByPartition) { + It "Gets pool monitors in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -Partition $partition + $monitors | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | + Should Not Be 0 + $monitors | + Select-Object -ExpandProperty name | + Should Match '/[^/]*/.*' + } + } + If ($F5LTMTestCases.Get_PoolMonitor_ByPoolnameAndPartition) { + It "Gets pool monitors in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameAndPartition { + param($session, $partition, $poolname) + $Sessions.ContainsKey($session) | Should Be $true + + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition + $monitors | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | + Should Not Be 0 + $monitors | + Select-Object -ExpandProperty name | + Should Match '/[^/]*/.*' + } + } + If ($F5LTMTestCases.Get_PoolMonitor_ByPoolnameFullpath) { + It "Gets pool monitors in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameFullpath { + param($session, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath + $monitors | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | + Should Not Be 0 + $monitors | + Select-Object -ExpandProperty name | + Should Match '/[^/]*/.*' + } + } + If ($F5LTMTestCases.Get_PoolMonitor_ByFullpathArray) { + It "Gets pool monitors in pool by Fullpath[] on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByFullpathArray { + param($session, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath + $monitors | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | + Should Not Be 0 + $monitors | + Select-Object -ExpandProperty name | + Should Match '/[^/]*/.*' + } + } + } + } +} +Describe 'VirtualServer' { + Context 'Get' { + If ($Sessions) { + If ($F5LTMTestCases.Get_VirtualServer) { + It "Gets virtual servers * on ''" -TestCases $F5LTMTestCases.Get_VirtualServer { + param($session) + $Sessions.ContainsKey($session) | Should Be $true + + Get-VirtualServer -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByPartition) { + It "Gets virtual servers in partition '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByPartition { + param($session, $partition) + $Sessions.ContainsKey($session) | Should Be $true + + Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition | + Select-Object -ExpandProperty partition | + Should Be $partition + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByNameAndPartition) { + It "Gets virtual servers in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartition { + param($session, $partition, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition -Name $name | + Select-Object -ExpandProperty name | + Should Be $Name + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByFullpath) { + It "Gets virtual servers by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByFullpath { + param($session, $partition, $fullpath) + $Sessions.ContainsKey($session) | Should Be $true + + Get-VirtualServer -F5Session $Sessions[$session] -Name $fullPath | + Select-Object -ExpandProperty fullPath | + Should Be $fullPath + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByNameArray) { + It "Gets virtual servers by Name[] on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameArray { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + Get-VirtualServer -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByNameFromPipeline) { + It "Gets virtual servers by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameFromPipeline { + param($session, $name) + $Sessions.ContainsKey($session) | Should Be $true + + $name | Get-VirtualServer -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $name.Count + } + } + If ($F5LTMTestCases.Get_VirtualServer_ByNameAndPartitionFromPipeline) { + It "Gets virtual servers by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartitionFromPipeline { + param($session, $object) + $Sessions.ContainsKey($session) | Should Be $true + + $object | Get-VirtualServer -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Be $object.Count + } + } + } + } +} \ No newline at end of file From aa8e02f3394f30af95a50f5dbaecbc3a3919303b Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Fri, 2 Jun 2017 11:09:32 -0500 Subject: [PATCH 02/12] F5-LTM.Tests.ps1: rename: Get-Partition to Get-BIGIPPartition --- F5-LTM/F5-LTM.Tests.ps1 | 320 ++++++++++++++++++++-------------------- 1 file changed, 160 insertions(+), 160 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 5b35ce1..7383d2a 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -3,8 +3,8 @@ $scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Pat Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force $F5LTMTestCases = @{} -[Regex]::Matches((Get-Content $MyInvocation.MyCommand.Path -raw),'(?<=F5LTMTestCases\.)Get_\w*') | - Select-Object -ExpandProperty Value -Unique | +[Regex]::Matches((Get-Content $MyInvocation.MyCommand.Path -raw),'(?<=F5LTMTestCases\.)Get_\w*') | + Select-Object -ExpandProperty Value -Unique | Sort-Object | % { $F5LTMTestCases.Add($_, @()) } @@ -12,7 +12,7 @@ $TestCasePath = Invoke-NullCoalescing { $env:F5LTMTestCases } { (Join-Path $HOME if (Test-Path -Path $TestCasePath) { . $TestCasePath } else { - Write-Host ('Writing test case template to {0}' -f $TestCasePath) -ForegroundColor Green + Write-Host ('Writing test case template to {0}' -f $TestCasePath) -ForegroundColor Green $F5LTMTestCasesTemplate = @" `$credentials = New-Object System.Management.Automation.PSCredential ('[username]', (ConvertTo-SecureString '[password]' -AsPlainText -Force)) @@ -48,7 +48,7 @@ if (Test-Path -Path $TestCasePath) { } $F5LTMTestCasesTemplate += '$F5LTMTestCases.{0} += @{{ session = ''default''{1} }}{2}' -f $_,($hashvalues -join ''),[Environment]::NewLine } - $F5LTMTestCasesTemplate | Out-File -FilePath $TestCasePath + $F5LTMTestCasesTemplate | Out-File -FilePath $TestCasePath } Describe 'TestCases' -Tags 'Validation' { Context 'Empty' { @@ -57,7 +57,7 @@ Describe 'TestCases' -Tags 'Validation' { Write-Warning ('$F5LTMTestCases.{0} is empty' -f $_) } } - Write-Host 'Invoke-Pester -ExcludeTag Validation to suppress empty test case warnings' -ForegroundColor Green + Write-Host 'Invoke-Pester -ExcludeTag Validation to suppress empty test case warnings' -ForegroundColor Green } } Describe 'HealthMonitor' { @@ -69,8 +69,8 @@ Describe 'HealthMonitor' { $Sessions.ContainsKey($session) | Should Be $true Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -80,7 +80,7 @@ Describe 'HealthMonitor' { $Sessions.ContainsKey($session) | Should Be $true Get-HealthMonitor -F5Session $Sessions[$session] -Type $type | - Select-Object -ExpandProperty type | + Select-Object -ExpandProperty type | Should Be $type } } @@ -88,19 +88,19 @@ Describe 'HealthMonitor' { It "Gets health monitors in partition '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | + Select-Object -ExpandProperty partition | Should Be $partition } - } + } If ($F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition) { It "Gets health monitors in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition { param($session, $partition, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $Name } } @@ -108,9 +108,9 @@ Describe 'HealthMonitor' { It "Gets health monitors by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByFullpath { param($session, $partition, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-HealthMonitor -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | + Select-Object -ExpandProperty fullPath | Should Be $fullPath } } @@ -118,10 +118,10 @@ Describe 'HealthMonitor' { It "Gets health monitors by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-HealthMonitor -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -129,10 +129,10 @@ Describe 'HealthMonitor' { It "Gets health monitors by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -140,10 +140,10 @@ Describe 'HealthMonitor' { It "Gets health monitors by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartitionFromPipeline { param($session, $object) $Sessions.ContainsKey($session) | Should Be $true - + $object | Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $object.Count } } @@ -159,8 +159,8 @@ Describe 'HealthMonitorType' { $Sessions.ContainsKey($session) | Should Be $true Get-HealthMonitorType -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -177,10 +177,10 @@ Describe 'HealthMonitorType' { It "Gets health monitor types by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-HealthMonitorType -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -188,10 +188,10 @@ Describe 'HealthMonitorType' { It "Gets health monitor types by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-HealthMonitorType -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -207,8 +207,8 @@ Describe 'iRule' { $Sessions.ContainsKey($session) | Should Be $true Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -218,7 +218,7 @@ Describe 'iRule' { $Sessions.ContainsKey($session) | Should Be $true Get-iRule -F5Session $Sessions[$session] -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $name } } @@ -226,19 +226,19 @@ Describe 'iRule' { It "Gets irules in partition '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-iRule -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | + Select-Object -ExpandProperty partition | Should Be $partition } - } + } If ($F5LTMTestCases.Get_iRule_ByNameAndPartition) { It "Gets irules in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartition { param($session, $partition, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-iRule -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $Name } } @@ -246,9 +246,9 @@ Describe 'iRule' { It "Gets irules by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByFullpath { param($session, $partition, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-iRule -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | + Select-Object -ExpandProperty fullPath | Should Be $fullPath } } @@ -256,10 +256,10 @@ Describe 'iRule' { It "Gets irules by Name[] on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-iRule -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -267,10 +267,10 @@ Describe 'iRule' { It "Gets irules by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -278,10 +278,10 @@ Describe 'iRule' { It "Gets irules by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartitionFromPipeline { param($session, $object) $Sessions.ContainsKey($session) | Should Be $true - + $object | Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $object.Count } } @@ -297,8 +297,8 @@ Describe 'Node' { $Sessions.ContainsKey($session) | Should Be $true Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -306,19 +306,19 @@ Describe 'Node' { It "Gets nodes in partition '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-Node -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | + Select-Object -ExpandProperty partition | Should Be $partition } - } + } If ($F5LTMTestCases.Get_Node_ByNameAndPartition) { It "Gets nodes in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartition { param($session, $partition, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-Node -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $Name } } @@ -326,9 +326,9 @@ Describe 'Node' { It "Gets nodes by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByFullpath { param($session, $partition, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-Node -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | + Select-Object -ExpandProperty fullPath | Should Be $fullPath } } @@ -336,10 +336,10 @@ Describe 'Node' { It "Gets nodes by Name[] on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-Node -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -347,10 +347,10 @@ Describe 'Node' { It "Gets nodes by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -358,10 +358,10 @@ Describe 'Node' { It "Gets nodes by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartitionFromPipeline { param($session, $object) $Sessions.ContainsKey($session) | Should Be $true - + $object | Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $object.Count } } @@ -371,45 +371,45 @@ Describe 'Node' { Describe 'Partition' { Context 'Get' { If ($Sessions) { - If ($F5LTMTestCases.Get_Partition) { - It "Gets partitions * on ''" -TestCases $F5LTMTestCases.Get_Partition { + If ($F5LTMTestCases.Get_BIGIPPartition) { + It "Gets partitions * on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition { param($session) $Sessions.ContainsKey($session) | Should Be $true - Get-Partition -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Get-BIGIPPartition -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } - If ($F5LTMTestCases.Get_Partition_ByName) { - It "Gets partitions by Name '' on ''" -TestCases $F5LTMTestCases.Get_Partition_ByName { + If ($F5LTMTestCases.Get_BIGIPPartition_ByName) { + It "Gets partitions by Name '' on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByName { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - Get-Partition -F5Session $Sessions[$session] -Name $name | + Get-BIGIPPartition -F5Session $Sessions[$session] -Name $name | Should Be $name } } - If ($F5LTMTestCases.Get_Partition_ByNameArray) { - It "Gets partitions by Name[] on ''" -TestCases $F5LTMTestCases.Get_Partition_ByNameArray { + If ($F5LTMTestCases.Get_BIGIPPartition_ByNameArray) { + It "Gets partitions by Name[] on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - - Get-Partition -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + + Get-BIGIPPartition -F5Session $Sessions[$session] -Name $name | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } - If ($F5LTMTestCases.Get_Partition_ByNameFromPipeline) { - It "Gets partitions by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Partition_ByNameFromPipeline { + If ($F5LTMTestCases.Get_BIGIPPartition_ByNameFromPipeline) { + It "Gets partitions by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-Partition -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + + $name | Get-BIGIPPartition -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -425,8 +425,8 @@ Describe 'Pool' { $Sessions.ContainsKey($session) | Should Be $true Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -434,19 +434,19 @@ Describe 'Pool' { It "Gets pools in partition '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | + Select-Object -ExpandProperty partition | Should Be $partition } - } + } If ($F5LTMTestCases.Get_Pool_ByNameAndPartition) { It "Gets pools in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartition { param($session, $partition, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $Name } } @@ -454,9 +454,9 @@ Describe 'Pool' { It "Gets pools by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByFullpath { param($session, $partition, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | + Select-Object -ExpandProperty fullPath | Should Be $fullPath } } @@ -464,10 +464,10 @@ Describe 'Pool' { It "Gets pools by Name[] on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -475,10 +475,10 @@ Describe 'Pool' { It "Gets pools by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -486,10 +486,10 @@ Describe 'Pool' { It "Gets pools by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartitionFromPipeline { param($session, $object) $Sessions.ContainsKey($session) | Should Be $true - + $object | Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $object.Count } } @@ -505,8 +505,8 @@ Describe "PoolMember" { $Sessions.ContainsKey($session) | Should Be $true Get-PoolMember -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -514,7 +514,7 @@ Describe "PoolMember" { It "Gets pool members in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-PoolMember -F5Session $Sessions[$session] -Partition $partition | ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | Should Be $partition @@ -524,7 +524,7 @@ Describe "PoolMember" { It "Gets pool members in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameAndPartition { param($session, $partition, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + # Pool members can be from a different partition than the pool. Check GetPoolName() partition matches instead. Get-PoolMember -F5Session $Sessions[$session] -PoolName $poolname -Partition $Partition | ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | @@ -535,7 +535,7 @@ Describe "PoolMember" { It "Gets pool members in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameFullpath { param($session, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-PoolMember -F5Session $Sessions[$session] -PoolName $fullPath | ForEach-Object { $_.GetPoolName() } | Should Be $fullPath @@ -545,10 +545,10 @@ Describe "PoolMember" { It "Gets pool members in pool '' by Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByAddressArray { param($session, $address, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | Get-PoolMember -F5Session $Sessions[$session] -Address $address | - Select-Object -ExpandProperty Address | + Select-Object -ExpandProperty Address | Should Match '^\d+\.\d+\.\d+\.\d+' } } @@ -556,10 +556,10 @@ Describe "PoolMember" { It "Gets pool members in pool '' by Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByNameArray { param($session, $name, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | Get-PoolMember -F5Session $Sessions[$session] -Name $name | - Select-Object -ExpandProperty Name | + Select-Object -ExpandProperty Name | Should Match '^.+:\d+' } } @@ -573,7 +573,7 @@ Describe "PoolMemberStats" { It "Gets pool member statistics in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats { param($session, $poolname, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition | Select-Object -ExpandProperty 'serverside.curConns' | Should Not Be Null @@ -583,7 +583,7 @@ Describe "PoolMemberStats" { It "Gets pool member statistics in pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByPoolnameFullpath { param($session, $fullPath) $Sessions.ContainsKey($session) | Should Be $true - + Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $fullPath | Select-Object -ExpandProperty 'serverside.curConns' | Should Not Be Null @@ -593,15 +593,15 @@ Describe "PoolMemberStats" { It "Gets pool member statistics in pool '' and Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByAddressArray { param($session, $address, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | Get-PoolMemberStats -F5Session $Sessions[$session] -Address $address $memberstats | Select-Object -ExpandProperty 'serverside.curConns' | Should Not Be Null $memberstats | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $address.Count } } @@ -609,15 +609,15 @@ Describe "PoolMemberStats" { It "Gets pool member statistics in pool '' and Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByNameArray { param($session, $name, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | Get-PoolMemberStats -F5Session $Sessions[$session] -name $name $memberstats | Select-Object -ExpandProperty 'serverside.curConns' | Should Not Be Null $memberstats | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -633,8 +633,8 @@ Describe "PoolMonitor" { $Sessions.ContainsKey($session) | Should Be $true Get-PoolMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -642,14 +642,14 @@ Describe "PoolMonitor" { It "Gets pool monitors in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -Partition $partition $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | Should Not Be 0 $monitors | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Match '/[^/]*/.*' } } @@ -657,14 +657,14 @@ Describe "PoolMonitor" { It "Gets pool monitors in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameAndPartition { param($session, $partition, $poolname) $Sessions.ContainsKey($session) | Should Be $true - + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | Should Not Be 0 $monitors | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Match '/[^/]*/.*' } } @@ -672,14 +672,14 @@ Describe "PoolMonitor" { It "Gets pool monitors in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameFullpath { param($session, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | Should Not Be 0 $monitors | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Match '/[^/]*/.*' } } @@ -687,14 +687,14 @@ Describe "PoolMonitor" { It "Gets pool monitors in pool by Fullpath[] on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByFullpathArray { param($session, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | + Measure-Object -Sum Count | + Select-Object -ExpandProperty Sum | Should Not Be 0 $monitors | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Match '/[^/]*/.*' } } @@ -710,8 +710,8 @@ Describe 'VirtualServer' { $Sessions.ContainsKey($session) | Should Be $true Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Not Be 0 } } @@ -719,19 +719,19 @@ Describe 'VirtualServer' { It "Gets virtual servers in partition '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true - + Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | + Select-Object -ExpandProperty partition | Should Be $partition } - } + } If ($F5LTMTestCases.Get_VirtualServer_ByNameAndPartition) { It "Gets virtual servers in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartition { param($session, $partition, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | + Select-Object -ExpandProperty name | Should Be $Name } } @@ -739,9 +739,9 @@ Describe 'VirtualServer' { It "Gets virtual servers by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByFullpath { param($session, $partition, $fullpath) $Sessions.ContainsKey($session) | Should Be $true - + Get-VirtualServer -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | + Select-Object -ExpandProperty fullPath | Should Be $fullPath } } @@ -749,10 +749,10 @@ Describe 'VirtualServer' { It "Gets virtual servers by Name[] on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameArray { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + Get-VirtualServer -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -760,10 +760,10 @@ Describe 'VirtualServer' { It "Gets virtual servers by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameFromPipeline { param($session, $name) $Sessions.ContainsKey($session) | Should Be $true - + $name | Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $name.Count } } @@ -771,10 +771,10 @@ Describe 'VirtualServer' { It "Gets virtual servers by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartitionFromPipeline { param($session, $object) $Sessions.ContainsKey($session) | Should Be $true - + $object | Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | + Measure-Object | + Select-Object -ExpandProperty Count | Should Be $object.Count } } From 99ff90d8c84b6b41625f3af860521f7c8baa6788 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Fri, 2 Jun 2017 18:01:49 -0500 Subject: [PATCH 03/12] Describe,Context edits; Automatic "* on" TestCase initialization --- F5-LTM/F5-LTM.Tests.ps1 | 209 ++++++++++++++++++++-------------------- 1 file changed, 104 insertions(+), 105 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 7383d2a..66c19a4 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -2,6 +2,8 @@ $scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Pat Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force +$PSVersion = $PSVersionTable.PSVersion.Major + $F5LTMTestCases = @{} [Regex]::Matches((Get-Content $MyInvocation.MyCommand.Path -raw),'(?<=F5LTMTestCases\.)Get_\w*') | Select-Object -ExpandProperty Value -Unique | @@ -50,8 +52,13 @@ if (Test-Path -Path $TestCasePath) { } $F5LTMTestCasesTemplate | Out-File -FilePath $TestCasePath } +$SessionsTestCases = @() +foreach ($key in $Sessions.Keys) { + $SessionsTestCases += @{session=$key} +} Describe 'TestCases' -Tags 'Validation' { - Context 'Empty' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest $F5LTMTestCases.Keys | Sort-Object | ForEach-Object { If ($F5LTMTestCases[$_].Count -eq 0) { Write-Warning ('$F5LTMTestCases.{0} is empty' -f $_) @@ -60,19 +67,18 @@ Describe 'TestCases' -Tags 'Validation' { Write-Host 'Invoke-Pester -ExcludeTag Validation to suppress empty test case warnings' -ForegroundColor Green } } -Describe 'HealthMonitor' { - Context 'Get' { +Describe 'Get-HealthMonitor' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_HealthMonitor) { - It "Gets health monitors * on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets health monitors * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-HealthMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_HealthMonitor_ByType) { It "Gets health monitors of type '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByType { @@ -150,19 +156,18 @@ Describe 'HealthMonitor' { } } } -Describe 'HealthMonitorType' { - Context 'Get' { +Describe 'Get-HealthMonitorType' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_HealthMonitorType) { - It "Gets health monitor types * on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets health monitor types * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-HealthMonitorType -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-HealthMonitorType -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_HealthMonitorType_ByName) { It "Gets health monitor types by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByName { @@ -198,19 +203,18 @@ Describe 'HealthMonitorType' { } } } -Describe 'iRule' { - Context 'Get' { +Describe 'Get-iRule' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_iRule) { - It "Gets irules* on ''" -TestCases $F5LTMTestCases.Get_iRule { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets irules* on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-iRule -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_iRule_ByName) { It "Gets irules by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByName { @@ -288,19 +292,18 @@ Describe 'iRule' { } } } -Describe 'Node' { - Context 'Get' { +Describe 'Get-Node' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_Node) { - It "Gets nodes * on ''" -TestCases $F5LTMTestCases.Get_Node { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets nodes * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-Node -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_Node_ByPartition) { It "Gets nodes in partition '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByPartition { @@ -368,19 +371,18 @@ Describe 'Node' { } } } -Describe 'Partition' { - Context 'Get' { +Describe 'Get-Partition' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_BIGIPPartition) { - It "Gets partitions * on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets partitions * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-BIGIPPartition -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-BIGIPPartition -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_BIGIPPartition_ByName) { It "Gets partitions by Name '' on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByName { @@ -416,21 +418,20 @@ Describe 'Partition' { } } } -Describe 'Pool' { - Context 'Get' { +Describe 'Get-Pool' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_Pool) { - It "Gets pools * on ''" -TestCases $F5LTMTestCases.Get_Pool { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets pools * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-Pool -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } - If ($F5LTMTestCases.Get_Pool_ByPartition) { + If ($F5LTMTestCases.Get_Pool_ByPartition) { It "Gets pools in partition '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true @@ -496,22 +497,21 @@ Describe 'Pool' { } } } -Describe "PoolMember" { - Context "Get" { +Describe 'Get-PoolMember' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest if ($Sessions) { - if ($F5LTMTestCases.Get_PoolMember) { - It "Gets pool members * on ''" -TestCases $F5LTMTestCases.Get_PoolMember { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets pool members * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-PoolMember -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-PoolMember -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_PoolMember_ByPartition) { - It "Gets pool members in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPartition { + It "Gets pool members in partition '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true @@ -566,8 +566,9 @@ Describe "PoolMember" { } } } -Describe "PoolMemberStats" { - Context "Get" { +Describe 'Get-PoolMemberStats' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest if ($Sessions) { if ($F5LTMTestCases.Get_PoolMemberStats) { It "Gets pool member statistics in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats { @@ -624,19 +625,18 @@ Describe "PoolMemberStats" { } } } -Describe "PoolMonitor" { - Context "Get" { +Describe 'Get-PoolMonitor' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest if ($Sessions) { - if ($F5LTMTestCases.Get_PoolMonitor) { - It "Gets pool monitors * on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets pool monitors * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-PoolMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-PoolMonitor -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } If ($F5LTMTestCases.Get_PoolMonitor_ByPartition) { It "Gets pool monitors in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPartition { @@ -701,21 +701,20 @@ Describe "PoolMonitor" { } } } -Describe 'VirtualServer' { - Context 'Get' { +Describe 'Get-Get-VirtualServer' { + Context "Strict mode PS$PSVersion" { + Set-StrictMode -Version latest If ($Sessions) { - If ($F5LTMTestCases.Get_VirtualServer) { - It "Gets virtual servers * on ''" -TestCases $F5LTMTestCases.Get_VirtualServer { - param($session) - $Sessions.ContainsKey($session) | Should Be $true + It "Gets virtual servers * on ''" -TestCases $SessionsTestCases { + param($session) + $Sessions.ContainsKey($session) | Should Be $true - Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } + Get-VirtualServer -F5Session $Sessions[$session] | + Measure-Object | + Select-Object -ExpandProperty Count | + Should Not Be 0 } - If ($F5LTMTestCases.Get_VirtualServer_ByPartition) { + If ($F5LTMTestCases.Get_VirtualServer_ByPartition) { It "Gets virtual servers in partition '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByPartition { param($session, $partition) $Sessions.ContainsKey($session) | Should Be $true From e24bad7b501cc315efabfccc0fc2a809e31027b4 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Wed, 23 Aug 2017 00:08:25 -0500 Subject: [PATCH 04/12] #35 Behavior Verification w/Pester Mocks Demonstrates mocking for behavior verification testing of Get-HealthMonitor. --- F5-LTM/F5-LTM.Tests.ps1 | 862 ++++------------------------------------ 1 file changed, 87 insertions(+), 775 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 66c19a4..0190a96 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -1,782 +1,94 @@ $scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) - Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force -$PSVersion = $PSVersionTable.PSVersion.Major - -$F5LTMTestCases = @{} -[Regex]::Matches((Get-Content $MyInvocation.MyCommand.Path -raw),'(?<=F5LTMTestCases\.)Get_\w*') | - Select-Object -ExpandProperty Value -Unique | - Sort-Object | % { - $F5LTMTestCases.Add($_, @()) -} -$TestCasePath = Invoke-NullCoalescing { $env:F5LTMTestCases } { (Join-Path $HOME F5-LTM.TestCases.ps1) } -if (Test-Path -Path $TestCasePath) { - . $TestCasePath -} else { - Write-Host ('Writing test case template to {0}' -f $TestCasePath) -ForegroundColor Green - $F5LTMTestCasesTemplate = @" -`$credentials = New-Object System.Management.Automation.PSCredential ('[username]', (ConvertTo-SecureString '[password]' -AsPlainText -Force)) - -`$Sessions = @{} -`$Sessions.Add('default', (New-F5Session -LTMName '[ipaddress]' -LTMCredentials `$credentials -Default -PassThrough)) - -"@ - $F5LTMTestCases.Keys | Sort-Object | ForEach-Object { - $params = [Regex]::Match($_, '(?<=_By).*$') -replace 'PoolnameFullpath','fullpath' -split 'And' - $hashvalues = @() - if ($_ -match 'FromPipeline$') { - for($x=0;$x -le 1; $x++) { - $pipelineobjects = @() - $pipelineparams = @() - foreach($p in $params) { - if ($p) { - $pipelineparams += ' {0} = ''[{0}]'' ' -f ($p.ToLower() -replace 'frompipeline','') - } - } - $pipelineobjects += ('([pscustomobject]@{{{0}}})' -f ($pipelineparams -join ';')) - } - $hashvalues += '; object = {0}' -f ($pipelineobjects -join ',') - } else { - foreach($p in $params) { - if ($p) { - if ($p -match 'Array$') { - $hashvalues += '; {0} = ''[{0}1]'',''[{0}2]'',''[{0}3]''' -f ($p.ToLower() -replace 'array','') - } else { - $hashvalues += '; {0} = ''[{0}]''' -f $p.ToLower() - } - } - } - } - $F5LTMTestCasesTemplate += '$F5LTMTestCases.{0} += @{{ session = ''default''{1} }}{2}' -f $_,($hashvalues -join ''),[Environment]::NewLine - } - $F5LTMTestCasesTemplate | Out-File -FilePath $TestCasePath -} -$SessionsTestCases = @() -foreach ($key in $Sessions.Keys) { - $SessionsTestCases += @{session=$key} -} -Describe 'TestCases' -Tags 'Validation' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - $F5LTMTestCases.Keys | Sort-Object | ForEach-Object { - If ($F5LTMTestCases[$_].Count -eq 0) { - Write-Warning ('$F5LTMTestCases.{0} is empty' -f $_) - } - } - Write-Host 'Invoke-Pester -ExcludeTag Validation to suppress empty test case warnings' -ForegroundColor Green - } -} -Describe 'Get-HealthMonitor' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets health monitors * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_HealthMonitor_ByType) { - It "Gets health monitors of type '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByType { - param($session, $type) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] -Type $type | - Select-Object -ExpandProperty type | - Should Be $type - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByPartition) { - It "Gets health monitors in partition '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition) { - It "Gets health monitors in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartition { - param($session, $partition, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | - Should Be $Name - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByFullpath) { - It "Gets health monitors by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByFullpath { - param($session, $partition, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByNameArray) { - It "Gets health monitors by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitor -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByNameFromPipeline) { - It "Gets health monitors by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_HealthMonitor_ByNameAndPartitionFromPipeline) { - It "Gets health monitors by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitor_ByNameAndPartitionFromPipeline { - param($session, $object) - $Sessions.ContainsKey($session) | Should Be $true - - $object | Get-HealthMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $object.Count - } +Describe 'Get-HealthMonitor' -Tags 'Unit' { + InModuleScope F5-LTM { + Context "Strict mode PS$($PSVersionTable.PSVersion.Major)" { + Set-StrictMode -Version latest + +#region Arrange: Initialize Mocks + + # We aren't testing Get-HealthMonitorType here, just Get-HealthMonitor + # Using a mock to return a static set of types for use by subsequent requests + $healthmonitortypes = @('http','https','icmp','smtp','tcp') + Mock Get-HealthMonitorType { $healthmonitortypes } + + # Mocking Invoke-RestMethodOverride for unit testing Module without F5 device connectivity + Mock Invoke-RestMethodOverride { + # Behavior (not state) verification is applied to this mock. + # Therefore, the output need only meet the bare minimum requirements to maximize code coverage of the Subject Under Test. + [pscustomobject]@{ + kind="tm:ltm:monitor:http:httpstate" + items=@('bogus item for testing') + name='name' + partition='partition' + fullPath='/partition/name' + selfLink="https://localhost/mgmt/tm/ltm/monitor/type/~partition~name?ver=12.1.2" + } + } + # Mock session with fictional IP,credentials, and version + $mocksession = [pscustomobject]@{ + Name = '192.168.1.1' + BaseURL = 'https://192.168.1.1/mgmt/tm/ltm/' + Credential = New-Object System.Management.Automation.PSCredential ('georgejetson', (ConvertTo-SecureString 'judyr0ck$!' -AsPlainText -Force)) + LTMVersion = [Version]'11.5.1' + WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + } | Add-Member -Name GetLink -MemberType ScriptMethod { + param($Link) + $Link -replace 'localhost', $this.Name + } -PassThru + +#endregion Arrange: Initialize Mocks + + It "Requests health monitors *" { + Get-HealthMonitor -F5Session $mocksession | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It + Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It + } + It "Requests health monitors of type ''" -TestCases @(@{type='http'},@{type='https'}) { + param($type) + Get-HealthMonitor -F5Session $mocksession -Type $type | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 0 -Scope It + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It + } + It "Requests health monitors in partition ''" -TestCases @(@{partition='Development'},@{partition='Common'}) { + param($partition) + Get-HealthMonitor -F5Session $mocksession -Partition $partition | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It + } + It "Requests health monitors in partition '' by Name ''" -TestCases @(@{partition='Common';name='http'},@{partition='Development';name='Test'}) { + param($partition, $name) + Get-HealthMonitor -F5Session $mocksession -Partition $partition -Name $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It + } + It "Requests health monitors by fullPath ''" -TestCases @(@{fullpath='/Common/https'}) { + param($fullpath) + Get-HealthMonitor -F5Session $mocksession -Name $fullPath | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It + } + It "Requests health monitors by Name[]" -TestCases @(@{name=@('http','https','tcp')}) { + param($name) + Get-HealthMonitor -F5Session $mocksession -Name $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times ($name.Count * $healthmonitortypes.Count) -Exactly -Scope It + } + It "Requests health monitors by Name From Pipeline" -TestCases @(@{name=@('http','https')}) { + param($name) + $name | Get-HealthMonitor -F5Session $mocksession | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times ($name.Count * $healthmonitortypes.Count) -Exactly -Scope It + } + It "Requests health monitors by Name and Partition From Pipeline" -TestCases @(@{ object = ([pscustomobject]@{name = 'http'; partition = 'Common'}),([pscustomobject]@{name = 'host_ashx'; partition = 'Common'}) }) { + param($object) + $object | Get-HealthMonitor -F5Session $mocksession | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Invoke-RestMethodOverride -Times ($object.Count * $healthmonitortypes.Count) -Exactly -Scope It } } } -} -Describe 'Get-HealthMonitorType' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets health monitor types * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitorType -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_HealthMonitorType_ByName) { - It "Gets health monitor types by Name '' on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByName { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitorType -F5Session $Sessions[$session] -Name $name | - Should Be $name - } - } - If ($F5LTMTestCases.Get_HealthMonitorType_ByNameArray) { - It "Gets health monitor types by Name[] on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-HealthMonitorType -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_HealthMonitorType_ByNameFromPipeline) { - It "Gets health monitor types by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_HealthMonitorType_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-HealthMonitorType -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - } - } -} -Describe 'Get-iRule' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets irules* on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_iRule_ByName) { - It "Gets irules by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByName { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] -Name $name | - Select-Object -ExpandProperty name | - Should Be $name - } - } - If ($F5LTMTestCases.Get_iRule_ByPartition) { - It "Gets irules in partition '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_iRule_ByNameAndPartition) { - It "Gets irules in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartition { - param($session, $partition, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | - Should Be $Name - } - } - If ($F5LTMTestCases.Get_iRule_ByFullpath) { - It "Gets irules by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_iRule_ByFullpath { - param($session, $partition, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_iRule_ByNameArray) { - It "Gets irules by Name[] on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-iRule -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_iRule_ByNameFromPipeline) { - It "Gets irules by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_iRule_ByNameAndPartitionFromPipeline) { - It "Gets irules by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_iRule_ByNameAndPartitionFromPipeline { - param($session, $object) - $Sessions.ContainsKey($session) | Should Be $true - - $object | Get-iRule -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $object.Count - } - } - } - } -} -Describe 'Get-Node' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets nodes * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_Node_ByPartition) { - It "Gets nodes in partition '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Node -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_Node_ByNameAndPartition) { - It "Gets nodes in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartition { - param($session, $partition, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Node -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | - Should Be $Name - } - } - If ($F5LTMTestCases.Get_Node_ByFullpath) { - It "Gets nodes by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Node_ByFullpath { - param($session, $partition, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Node -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_Node_ByNameArray) { - It "Gets nodes by Name[] on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Node -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_Node_ByNameFromPipeline) { - It "Gets nodes by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_Node_ByNameAndPartitionFromPipeline) { - It "Gets nodes by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Node_ByNameAndPartitionFromPipeline { - param($session, $object) - $Sessions.ContainsKey($session) | Should Be $true - - $object | Get-Node -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $object.Count - } - } - } - } -} -Describe 'Get-Partition' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets partitions * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-BIGIPPartition -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_BIGIPPartition_ByName) { - It "Gets partitions by Name '' on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByName { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-BIGIPPartition -F5Session $Sessions[$session] -Name $name | - Should Be $name - } - } - If ($F5LTMTestCases.Get_BIGIPPartition_ByNameArray) { - It "Gets partitions by Name[] on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-BIGIPPartition -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_BIGIPPartition_ByNameFromPipeline) { - It "Gets partitions by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_BIGIPPartition_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-BIGIPPartition -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - } - } -} -Describe 'Get-Pool' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets pools * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_Pool_ByPartition) { - It "Gets pools in partition '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_Pool_ByNameAndPartition) { - It "Gets pools in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartition { - param($session, $partition, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | - Should Be $Name - } - } - If ($F5LTMTestCases.Get_Pool_ByFullpath) { - It "Gets pools by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_Pool_ByFullpath { - param($session, $partition, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_Pool_ByNameArray) { - It "Gets pools by Name[] on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_Pool_ByNameFromPipeline) { - It "Gets pools by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_Pool_ByNameAndPartitionFromPipeline) { - It "Gets pools by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_Pool_ByNameAndPartitionFromPipeline { - param($session, $object) - $Sessions.ContainsKey($session) | Should Be $true - - $object | Get-Pool -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $object.Count - } - } - } - } -} -Describe 'Get-PoolMember' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - if ($Sessions) { - It "Gets pool members * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMember -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_PoolMember_ByPartition) { - It "Gets pool members in partition '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMember -F5Session $Sessions[$session] -Partition $partition | - ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_PoolMember_ByPoolnameAndPartition) { - It "Gets pool members in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameAndPartition { - param($session, $partition, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - # Pool members can be from a different partition than the pool. Check GetPoolName() partition matches instead. - Get-PoolMember -F5Session $Sessions[$session] -PoolName $poolname -Partition $Partition | - ForEach-Object { [Regex]::Match($_.GetPoolName(),'(?<=^/)[^/]*').Value } | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_PoolMember_ByPoolnameFullpath) { - It "Gets pool members in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByPoolnameFullpath { - param($session, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMember -F5Session $Sessions[$session] -PoolName $fullPath | - ForEach-Object { $_.GetPoolName() } | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_PoolMember_ByAddressArray) { - It "Gets pool members in pool '' by Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByAddressArray { - param($session, $address, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | - Get-PoolMember -F5Session $Sessions[$session] -Address $address | - Select-Object -ExpandProperty Address | - Should Match '^\d+\.\d+\.\d+\.\d+' - } - } - If ($F5LTMTestCases.Get_PoolMember_ByNameArray) { - It "Gets pool members in pool '' by Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMember_ByNameArray { - param($session, $name, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | - Get-PoolMember -F5Session $Sessions[$session] -Name $name | - Select-Object -ExpandProperty Name | - Should Match '^.+:\d+' - } - } - } - } -} -Describe 'Get-PoolMemberStats' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - if ($Sessions) { - if ($F5LTMTestCases.Get_PoolMemberStats) { - It "Gets pool member statistics in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats { - param($session, $poolname, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition | - Select-Object -ExpandProperty 'serverside.curConns' | - Should Not Be Null - } - } - if ($F5LTMTestCases.Get_PoolMemberStats_ByPoolnameFullpath) { - It "Gets pool member statistics in pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByPoolnameFullpath { - param($session, $fullPath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMemberStats -F5Session $Sessions[$session] -PoolName $fullPath | - Select-Object -ExpandProperty 'serverside.curConns' | - Should Not Be Null - } - } - if ($F5LTMTestCases.Get_PoolMemberStats_ByAddressArray) { - It "Gets pool member statistics in pool '' and Address[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByAddressArray { - param($session, $address, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | - Get-PoolMemberStats -F5Session $Sessions[$session] -Address $address - $memberstats | - Select-Object -ExpandProperty 'serverside.curConns' | - Should Not Be Null - $memberstats | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $address.Count - } - } - if ($F5LTMTestCases.Get_PoolMemberStats_ByNameArray) { - It "Gets pool member statistics in pool '' and Name[] on ''" -TestCases $F5LTMTestCases.Get_PoolMemberStats_ByNameArray { - param($session, $name, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - $memberstats = Get-Pool -F5Session $Sessions[$session] -PoolName $poolname | - Get-PoolMemberStats -F5Session $Sessions[$session] -name $name - $memberstats | - Select-Object -ExpandProperty 'serverside.curConns' | - Should Not Be Null - $memberstats | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - } - } -} -Describe 'Get-PoolMonitor' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - if ($Sessions) { - It "Gets pool monitors * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-PoolMonitor -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_PoolMonitor_ByPartition) { - It "Gets pool monitors in partition ' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -Partition $partition - $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | - Should Not Be 0 - $monitors | - Select-Object -ExpandProperty name | - Should Match '/[^/]*/.*' - } - } - If ($F5LTMTestCases.Get_PoolMonitor_ByPoolnameAndPartition) { - It "Gets pool monitors in partition '' and pool '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameAndPartition { - param($session, $partition, $poolname) - $Sessions.ContainsKey($session) | Should Be $true - - $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $poolname -Partition $partition - $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | - Should Not Be 0 - $monitors | - Select-Object -ExpandProperty name | - Should Match '/[^/]*/.*' - } - } - If ($F5LTMTestCases.Get_PoolMonitor_ByPoolnameFullpath) { - It "Gets pool monitors in pool by Fullpath '' on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByPoolnameFullpath { - param($session, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath - $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | - Should Not Be 0 - $monitors | - Select-Object -ExpandProperty name | - Should Match '/[^/]*/.*' - } - } - If ($F5LTMTestCases.Get_PoolMonitor_ByFullpathArray) { - It "Gets pool monitors in pool by Fullpath[] on ''" -TestCases $F5LTMTestCases.Get_PoolMonitor_ByFullpathArray { - param($session, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - $monitors = Get-PoolMonitor -F5Session $Sessions[$session] -PoolName $fullpath - $monitors | - Measure-Object -Sum Count | - Select-Object -ExpandProperty Sum | - Should Not Be 0 - $monitors | - Select-Object -ExpandProperty name | - Should Match '/[^/]*/.*' - } - } - } - } -} -Describe 'Get-Get-VirtualServer' { - Context "Strict mode PS$PSVersion" { - Set-StrictMode -Version latest - If ($Sessions) { - It "Gets virtual servers * on ''" -TestCases $SessionsTestCases { - param($session) - $Sessions.ContainsKey($session) | Should Be $true - - Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Not Be 0 - } - If ($F5LTMTestCases.Get_VirtualServer_ByPartition) { - It "Gets virtual servers in partition '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByPartition { - param($session, $partition) - $Sessions.ContainsKey($session) | Should Be $true - - Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition | - Select-Object -ExpandProperty partition | - Should Be $partition - } - } - If ($F5LTMTestCases.Get_VirtualServer_ByNameAndPartition) { - It "Gets virtual servers in partition '' by Name '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartition { - param($session, $partition, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-VirtualServer -F5Session $Sessions[$session] -Partition $partition -Name $name | - Select-Object -ExpandProperty name | - Should Be $Name - } - } - If ($F5LTMTestCases.Get_VirtualServer_ByFullpath) { - It "Gets virtual servers by fullPath '' on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByFullpath { - param($session, $partition, $fullpath) - $Sessions.ContainsKey($session) | Should Be $true - - Get-VirtualServer -F5Session $Sessions[$session] -Name $fullPath | - Select-Object -ExpandProperty fullPath | - Should Be $fullPath - } - } - If ($F5LTMTestCases.Get_VirtualServer_ByNameArray) { - It "Gets virtual servers by Name[] on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameArray { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - Get-VirtualServer -F5Session $Sessions[$session] -Name $name | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_VirtualServer_ByNameFromPipeline) { - It "Gets virtual servers by Name From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameFromPipeline { - param($session, $name) - $Sessions.ContainsKey($session) | Should Be $true - - $name | Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $name.Count - } - } - If ($F5LTMTestCases.Get_VirtualServer_ByNameAndPartitionFromPipeline) { - It "Gets virtual servers by Name and Partition From Pipeline on ''" -TestCases $F5LTMTestCases.Get_VirtualServer_ByNameAndPartitionFromPipeline { - param($session, $object) - $Sessions.ContainsKey($session) | Should Be $true - - $object | Get-VirtualServer -F5Session $Sessions[$session] | - Measure-Object | - Select-Object -ExpandProperty Count | - Should Be $object.Count - } - } - } - } } \ No newline at end of file From 874a3d43521e6c6b0411c50ab0b211ccd23709c4 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Wed, 23 Aug 2017 00:31:02 -0500 Subject: [PATCH 05/12] #35 Assert-MockCalled Get-HealthMonitorType Intended for inclusion in previous commit e24bad7. --- F5-LTM/F5-LTM.Tests.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 0190a96..90791e5 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -57,36 +57,42 @@ Describe 'Get-HealthMonitor' -Tags 'Unit' { param($partition) Get-HealthMonitor -F5Session $mocksession -Partition $partition | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It } It "Requests health monitors in partition '' by Name ''" -TestCases @(@{partition='Common';name='http'},@{partition='Development';name='Test'}) { param($partition, $name) Get-HealthMonitor -F5Session $mocksession -Partition $partition -Name $name | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It } It "Requests health monitors by fullPath ''" -TestCases @(@{fullpath='/Common/https'}) { param($fullpath) Get-HealthMonitor -F5Session $mocksession -Name $fullPath | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times $healthmonitortypes.Count -Exactly -Scope It } It "Requests health monitors by Name[]" -TestCases @(@{name=@('http','https','tcp')}) { param($name) Get-HealthMonitor -F5Session $mocksession -Name $name | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times ($name.Count * $healthmonitortypes.Count) -Exactly -Scope It } It "Requests health monitors by Name From Pipeline" -TestCases @(@{name=@('http','https')}) { param($name) $name | Get-HealthMonitor -F5Session $mocksession | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times ($name.Count * $healthmonitortypes.Count) -Exactly -Scope It } It "Requests health monitors by Name and Partition From Pipeline" -TestCases @(@{ object = ([pscustomobject]@{name = 'http'; partition = 'Common'}),([pscustomobject]@{name = 'host_ashx'; partition = 'Common'}) }) { param($object) $object | Get-HealthMonitor -F5Session $mocksession | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } + Assert-MockCalled Get-HealthMonitorType -Times 1 -Exactly -Scope It Assert-MockCalled Invoke-RestMethodOverride -Times ($object.Count * $healthmonitortypes.Count) -Exactly -Scope It } } From c4eb502ea7d0733f1c87e359765c735f8e5d2271 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Fri, 1 Sep 2017 08:27:01 -0500 Subject: [PATCH 06/12] New-F5Session unit tests; -ParameterFilter sample New-F5Session unit tests -ParameterFilter sample to assert Invoke-RestMethodOverride is called with the correct $Uri for a given scenario. --- F5-LTM/F5-LTM.Tests.ps1 | 76 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 90791e5..7aabc22 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -51,7 +51,7 @@ Describe 'Get-HealthMonitor' -Tags 'Unit' { Get-HealthMonitor -F5Session $mocksession -Type $type | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'PoshLTM.HealthMonitor' } Assert-MockCalled Get-HealthMonitorType -Times 0 -Scope It - Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq ('{0}monitor/{1}/' -f $mocksession.BaseURL,$type) } } It "Requests health monitors in partition ''" -TestCases @(@{partition='Development'},@{partition='Common'}) { param($partition) @@ -97,4 +97,78 @@ Describe 'Get-HealthMonitor' -Tags 'Unit' { } } } +} +Describe 'New-F5Session' -Tags 'Unit' { + InModuleScope F5-LTM { + Context "Strict mode PS$($PSVersionTable.PSVersion.Major)" { + Set-StrictMode -Off + +#region Arrange: Initialize Mocks + + # Mocking Invoke-RestMethodOverride for unit testing Module without F5 device connectivity + Mock Invoke-RestMethodOverride { + switch ($LTMName) { + 'version11' { + if ($URI -match 'sys/version/') { + '{"version":"11.5.1"}' + } else { + throw '404 Not found' + } + } + Default { + if ($URI -match 'mgmt/shared/authn/login') { + [pscustomobject]@{token=@{token='dummytoken';starttime=[DateTime]::Now;uuid='9912a8f9-6fa9-474d-b00d-3f16226352b7'}} + #} elseif ($URI -match 'mgmt/shared/authz/tokens') { + # token extension request currently doesn't have to return anything, just not fail + } elseif ($URI -match 'sys/version/') { + '{"version":"12.1.0"}' + } + } + } + } + Mock Invoke-WebRequest { $true } + + $credentials = New-Object System.Management.Automation.PSCredential ('georgejetson', (ConvertTo-SecureString 'judyr0ck$!' -AsPlainText -Force)) + +#endregion Arrange: Initialize Mocks + + It "`$Script:F5Session initialized on 1st call" { + $testsession = New-F5Session -LTMName 'any' -LTMCredentials $credentials -PassThru + Assert-MockCalled Invoke-WebRequest -Times 0 -Exactly -Scope It # Only v11 calls Invoke-WebRequest + Assert-MockCalled Invoke-RestMethodOverride -Times 2 -Exactly -Scope It + $Script:F5Session.BaseURL -eq $testsession.BaseURL | Should Be $true + $Script:F5Session.LTMVersion -eq $testsession.LTMVersion | Should Be $true + } + It "`$Script:F5Session overridden with -Default switch" { + $testsession = New-F5Session -LTMName 'newdefault' -LTMCredentials $credentials -Default -PassThru + Assert-MockCalled Invoke-WebRequest -Times 0 -Exactly -Scope It # Only v11 calls Invoke-WebRequest + Assert-MockCalled Invoke-RestMethodOverride -Times 2 -Exactly -Scope It + $Script:F5Session.BaseURL -eq $testsession.BaseURL | Should Be $true + $Script:F5Session.LTMVersion -eq $testsession.LTMVersion | Should Be $true + } + It "v11: Authentication with Credentials" { + $testsession = New-F5Session -LTMName 'version11' -LTMCredentials $credentials -PassThru + Assert-MockCalled Invoke-WebRequest -Times 1 -Exactly -Scope It # Only v11 calls Invoke-WebRequest + Assert-MockCalled Invoke-RestMethodOverride -Times 2 -Exactly -Scope It + $testsession.LTMVersion -eq [Version]'11.5.1' | Should Be $true + } + It "v12+: Authentication with X-F5-Auth-Token header" { + $testsession = New-F5Session -LTMName 'version12' -LTMCredentials $credentials -PassThru + Assert-MockCalled Invoke-WebRequest -Times 0 -Scope It # Only v11 calls Invoke-WebRequest + Assert-MockCalled Invoke-RestMethodOverride -Times 2 -Exactly -Scope It + $testsession.WebSession.Headers.Keys.Contains('X-F5-Auth-Token') | Should Be $true + $testsession.WebSession.Headers.Keys.Contains('Token-Expiration') | Should Be $true + } + It "v12+: Authentication with X-F5-Auth-Token header + custom TokenLifespan" { + $testsession = New-F5Session -LTMName 'version12' -LTMCredentials $credentials -TokenLifespan 36000 -PassThru + Assert-MockCalled Invoke-WebRequest -Times 0 -Scope It # Only v11 calls Invoke-WebRequest + Assert-MockCalled Invoke-RestMethodOverride -Times 3 -Exactly -Scope It #3rd call for TokenLifespan change + $testsession.WebSession.Headers.Keys.Contains('X-F5-Auth-Token') | Should Be $true + $testsession.WebSession.Headers.Keys.Contains('Token-Expiration') | Should Be $true + } + It "Throws an error if TokenLifespan is out of range" { + { New-F5Session -LTMName 'version12' -LTMCredentials $credentials -TokenLifespan 60000 } | Should Throw + } + } + } } \ No newline at end of file From 0bbde80914c61c8c8fa55dce5c5e68c2550fc6e7 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 18:25:48 -0500 Subject: [PATCH 07/12] Fix PSScriptAnalyzer Errors and Warnings ## Fix -PSPossibleIncorrectComparisonWithNull -PSUseDeclaredVarsMoreThanAssignments -PSAvoidUsingCmdletAliases ## Suppress -PSUseSingularNouns - Get-PoolMemberStats - Resolve-NestedStats -PSAvoidUsingConvertToSecureStringWithPlainText - Only for dummy credentials used by Pester tests --- F5-LTM/F5-LTM.Tests.ps1 | 4 ++++ F5-LTM/F5-LTM.psm1 | 31 +++++++++++-------------- F5-LTM/Private/Resolve-NestedStats.ps1 | 2 +- F5-LTM/Public/Disable-VirtualServer.ps1 | 2 +- F5-LTM/Public/Enable-VirtualServer.ps1 | 2 +- F5-LTM/Public/Get-PoolMemberStats.ps1 | 1 + F5-LTM/Public/Set-Pool.ps1 | 6 ++--- F5-LTM/Public/Set-VirtualServer.ps1 | 8 +++---- 8 files changed, 29 insertions(+), 27 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 7aabc22..0411648 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -1,3 +1,7 @@ +# PSScriptAnalyzer - ignore creation of a SecureString using plain text for the contents of this script file +[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] +param() + $scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force diff --git a/F5-LTM/F5-LTM.psm1 b/F5-LTM/F5-LTM.psm1 index 74bb3d3..dd06513 100644 --- a/F5-LTM/F5-LTM.psm1 +++ b/F5-LTM/F5-LTM.psm1 @@ -18,31 +18,28 @@ Add-Type -Path "${PSScriptRoot}\Validation.cs" Add-Type -Path "${PSScriptRoot}\TypeData\PoshLTM.Types.cs" Update-FormatData "${PSScriptRoot}\TypeData\PoshLTM.Format.ps1xml" $ScriptPath = Split-Path $MyInvocation.MyCommand.Path - #region Load Public Functions -try { Get-ChildItem "$ScriptPath\Public" -Filter *.ps1 -Recurse| Select-Object -Expand FullName | ForEach-Object { $Function = Split-Path $_ -Leaf - . $_ - } -} catch { - Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message) - Continue -} + try { + . $_ + } catch { + Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message) + } + } #endregion #region Load Private Functions -try { - Get-ChildItem "$ScriptPath\Private" -Filter *.ps1 -Recurse | Select-Object -Expand FullName | ForEach-Object { - $Function = Split-Path $_ -Leaf - . $_ - } -} catch { - Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message) - Continue -} + Get-ChildItem "$ScriptPath\Private" -Filter *.ps1 -Recurse | Select-Object -Expand FullName | ForEach-Object { + $Function = Split-Path $_ -Leaf + try { + . $_ + } catch { + Write-Warning ("{0}: {1}" -f $Function,$_.Exception.Message) + } + } #endregion \ No newline at end of file diff --git a/F5-LTM/Private/Resolve-NestedStats.ps1 b/F5-LTM/Private/Resolve-NestedStats.ps1 index 50b3542..8a891ad 100644 --- a/F5-LTM/Private/Resolve-NestedStats.ps1 +++ b/F5-LTM/Private/Resolve-NestedStats.ps1 @@ -6,7 +6,7 @@ To resolve this discrepancy, this function performs version-specific transformations to the JSON data and returns it in a standardized format with the "entries" property at the top. #> - + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")] param ( $F5Session=$Script:F5Session, diff --git a/F5-LTM/Public/Disable-VirtualServer.ps1 b/F5-LTM/Public/Disable-VirtualServer.ps1 index 8148cd8..cf33a98 100644 --- a/F5-LTM/Public/Disable-VirtualServer.ps1 +++ b/F5-LTM/Public/Disable-VirtualServer.ps1 @@ -32,7 +32,7 @@ foreach($vs in $InputObject) { $URI = $F5Session.GetLink($vs.selfLink) $FullPath = $vs.fullPath - $JSON = Invoke-F5RestMethod -Method PATCH -Uri $URI -F5Session $F5Session -Body $JSONBody + $null = Invoke-F5RestMethod -Method PATCH -Uri $URI -F5Session $F5Session -Body $JSONBody Get-VirtualServer -F5Session $F5Session -Name $FullPath } } diff --git a/F5-LTM/Public/Enable-VirtualServer.ps1 b/F5-LTM/Public/Enable-VirtualServer.ps1 index bca95f6..4b05c36 100644 --- a/F5-LTM/Public/Enable-VirtualServer.ps1 +++ b/F5-LTM/Public/Enable-VirtualServer.ps1 @@ -32,7 +32,7 @@ foreach($vs in $InputObject) { $URI = $F5Session.GetLink($vs.selfLink) $FullPath = $vs.fullPath - $JSON = Invoke-F5RestMethod -Method PATCH -Uri $URI -F5Session $F5Session -Body $JSONBody + $null = Invoke-F5RestMethod -Method PATCH -Uri $URI -F5Session $F5Session -Body $JSONBody Get-VirtualServer -F5Session $F5Session -Name $FullPath } } diff --git a/F5-LTM/Public/Get-PoolMemberStats.ps1 b/F5-LTM/Public/Get-PoolMemberStats.ps1 index 6d600e9..5cdf419 100644 --- a/F5-LTM/Public/Get-PoolMemberStats.ps1 +++ b/F5-LTM/Public/Get-PoolMemberStats.ps1 @@ -6,6 +6,7 @@ Pool and member names are case-specific. #> [cmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")] param ( $F5Session=$Script:F5Session, diff --git a/F5-LTM/Public/Set-Pool.ps1 b/F5-LTM/Public/Set-Pool.ps1 index 6aab1aa..fd10bd8 100644 --- a/F5-LTM/Public/Set-Pool.ps1 +++ b/F5-LTM/Public/Set-Pool.ps1 @@ -124,10 +124,10 @@ # This performs the magic necessary for ChgProperties to override $InputObject properties $NewObject = Join-Object -Left $InputObject -Right ([pscustomobject]$ChgProperties) -Join FULL -WarningAction SilentlyContinue - if ($NewObject -ne $null -and $pscmdlet.ShouldProcess($F5Session.Name, "Setting Pool $Name")) { + if ($null -ne $NewObject -and $pscmdlet.ShouldProcess($F5Session.Name, "Setting Pool $Name")) { # We only update the pool if properties other than 'Name' are passed in - If ($NewObject | Get-Member -MemberType NoteProperty | Where Name -ne 'Name'){ + If ($NewObject | Get-Member -MemberType NoteProperty | Where-Object Name -ne 'Name'){ Write-Verbose -Message 'Setting Pool details...' @@ -144,7 +144,7 @@ #endregion - $result = Invoke-F5RestMethod -Method PATCH -URI "$URI" -F5Session $F5Session -Body $JSONBody -ContentType 'application/json' + $null = Invoke-F5RestMethod -Method PATCH -URI "$URI" -F5Session $F5Session -Body $JSONBody -ContentType 'application/json' } diff --git a/F5-LTM/Public/Set-VirtualServer.ps1 b/F5-LTM/Public/Set-VirtualServer.ps1 index 45be117..7ae5ca9 100644 --- a/F5-LTM/Public/Set-VirtualServer.ps1 +++ b/F5-LTM/Public/Set-VirtualServer.ps1 @@ -219,7 +219,7 @@ Function Set-VirtualServer { if (-not $NewProperties.ContainsKey('DestinationIP')) { $destination = if ($InputObject -and $InputObject.destination) { $InputObject.destination - } elseif ($ExistingVirtualServer -ne $null) { + } elseif ($null -ne $ExistingVirtualServer) { $ExistingVirtualServer.destination } if ($destination) { $NewProperties['DestinationIP'] = ($destination -split ':')[0] } @@ -227,7 +227,7 @@ Function Set-VirtualServer { if (-not $NewProperties.ContainsKey('DestinationPort')) { $destination = if ($InputObject -and $InputObject.destination) { $InputObject.destination - } elseif ($ExistingVirtualServer -ne $null) { + } elseif ($null -eq $ExistingVirtualServer) { $ExistingVirtualServer.destination } if ($destination) { $NewProperties['DestinationPort'] = ($destination -split ':')[1] } @@ -243,7 +243,7 @@ Function Set-VirtualServer { } # This performs the magic necessary for ChgProperties to override $InputObject properties $NewObject = Join-Object -Left $InputObject -Right ([pscustomobject]$ChgProperties) -Join FULL -WarningAction SilentlyContinue - if ($NewObject -ne $null -and $pscmdlet.ShouldProcess($F5Session.Name, "Setting VirtualServer $Name")) { + if ($null -ne $NewObject -and $pscmdlet.ShouldProcess($F5Session.Name, "Setting VirtualServer $Name")) { Write-Verbose -Message 'Setting VirtualServer details...' $URI = $F5Session.BaseURL + 'virtual/{0}' -f (Get-ItemPath -Name $Name -Application $Application -Partition $Partition) @@ -259,7 +259,7 @@ Function Set-VirtualServer { #endregion - $result = Invoke-F5RestMethod -Method PATCH -URI "$URI" -F5Session $F5Session -Body $JSONBody -ContentType 'application/json' + $null = Invoke-F5RestMethod -Method PATCH -URI "$URI" -F5Session $F5Session -Body $JSONBody -ContentType 'application/json' } if ($PassThru) { Get-VirtualServer -F5Session $F5Session -Name $Name -Application $Application -Partition $Partition } } From 67293cf90cd3b86c3ed03fba6fc66c5a8e22dcd1 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 18:55:23 -0500 Subject: [PATCH 08/12] appveyor: enable Pester tests --- appveyor.yml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 98c771a..4cf2da1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,7 +18,7 @@ install: # Force bootstrap of the Nuget PackageManagement Provider; Reference: http://www.powershellgallery.com/GettingStarted?section=Get%20Started - ps: Get-PackageProvider -Name NuGet -Force # Install pester PowerShell Unit Testing module - #- cinst -y pester + - cinst -y pester before_test: # Set FunctionsToExport and ModuleVersion in the module manifest (F5-LTM.psd1); Fixes #37 Do not export Private\*.ps1 functions @@ -35,19 +35,19 @@ test_script: # Invoke PSScriptAnalyzer against the module to make sure it's not failing any tests - ps: Invoke-ScriptAnalyzer -Path (Join-Path $env:APPVEYOR_BUILD_FOLDER 'F5-LTM') -Recurse # Invoke-Pester unit tests - #- ps: | - # $testResultsFile = '.\TestsResults.xml' - # - # # run tests - # $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru - # - # # upload results to AppVeyor - # (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $testResultsFile)) - # - # # if failures, quit to prevent publish - # if ($res.FailedCount -gt 0) { - # throw "$($res.FailedCount) tests failed." - # } + - ps: | + $testResultsFile = '.\TestsResults.xml' + + # run tests + $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru + + # upload results to AppVeyor + (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $testResultsFile)) + + # if failures, quit to prevent publish + if ($res.FailedCount -gt 0) { + throw "$($res.FailedCount) tests failed." + } deploy_script: # Publish module to the PowerShellGallery From e90632a136ca01a31174d74d1d44690d66669638 Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 19:02:25 -0500 Subject: [PATCH 09/12] Correct appveyor Pester commands syntax --- appveyor.yml | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 4cf2da1..8df1ae6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -35,20 +35,14 @@ test_script: # Invoke PSScriptAnalyzer against the module to make sure it's not failing any tests - ps: Invoke-ScriptAnalyzer -Path (Join-Path $env:APPVEYOR_BUILD_FOLDER 'F5-LTM') -Recurse # Invoke-Pester unit tests - - ps: | - $testResultsFile = '.\TestsResults.xml' - + - ps: $testResultsFile = '.\TestsResults.xml' # run tests - $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru - + - ps: $res = Invoke-Pester -OutputFormat NUnitXml -OutputFile $testResultsFile -PassThru # upload results to AppVeyor - (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $testResultsFile)) - + - ps: (New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path $testResultsFile)) # if failures, quit to prevent publish - if ($res.FailedCount -gt 0) { - throw "$($res.FailedCount) tests failed." - } - + - ps: if ($res.FailedCount -gt 0) { "$($res.FailedCount) tests failed." } + deploy_script: # Publish module to the PowerShellGallery - ps: Publish-Module -NugetApiKey $env:POWERSHELLGALLERY_APIKEY -Path (Join-Path $env:APPVEYOR_BUILD_FOLDER 'F5-LTM') From 03fdb1fece3e0b5a13e2b1f202867f81668f209f Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 20:43:56 -0500 Subject: [PATCH 10/12] Get-BigIPPartition Pester Unit Tests --- F5-LTM/F5-LTM.Tests.ps1 | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 0411648..40e67f4 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -5,6 +5,78 @@ param() $scriptroot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) Import-Module (Join-Path $scriptroot 'F5-LTM\F5-LTM.psm1') -Force +Describe 'Get-BigIPPartition' -Tags 'Unit' { + InModuleScope F5-LTM { + Context "Strict mode PS$($PSVersionTable.PSVersion.Major)" { + Set-StrictMode -Version latest + +#region Arrange: Initialize Mocks + + $partitions = @('Common','Development','Production') + + # Mocking Invoke-RestMethodOverride for unit testing Module without F5 device connectivity + Mock Invoke-RestMethodOverride { + # Behavior (not state) verification is applied to this mock. + # Therefore, the output need only meet the bare minimum requirements to maximize code coverage of the Subject Under Test. + [pscustomobject]@{ + items=@(@{name='bogus item for testing';subPath='subPath'}) + name='name' + selfLink="https://localhost/mgmt/tm/sys/folder/~name?ver=12.1.2" + } + } + # Mock session with fictional IP,credentials, and version + $mocksession = [pscustomobject]@{ + Name = '192.168.1.1' + BaseURL = 'https://192.168.1.1/mgmt/tm/ltm/' + Credential = New-Object System.Management.Automation.PSCredential ('georgejetson', (ConvertTo-SecureString 'judyr0ck$!' -AsPlainText -Force)) + LTMVersion = [Version]'11.5.1' + WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession + } | Add-Member -Name GetLink -MemberType ScriptMethod { + param($Link) + $Link -replace 'localhost', $this.Name + } -PassThru + +#endregion Arrange: Initialize Mocks + + It "Requests BigIP partitions *" { + Get-BigIPPartition -F5Session $mocksession | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq ($mocksession.BaseURL -replace 'ltm/','sys/folder/?$select=name,subPath') } + } + It "Requests BigIP partitions by Name ''" -TestCases @(@{name='Common'},@{name='Development'},@{name='Production'}) { + param($name) + Get-BigIPPartition -F5Session $mocksession -Name $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq (($mocksession.BaseURL -replace 'ltm/','sys/folder') + ('/~{0}?$select=name,subPath' -f $name)) } + } + It "Requests BigIP partitions with Name [-Folder alias] ''" -TestCases @(@{name='Common'}) { + param($name) + Get-BigIPPartition -F5Session $mocksession -Folder $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq (($mocksession.BaseURL -replace 'ltm/','sys/folder') + ('/~{0}?$select=name,subPath' -f $name)) } + } + It "Requests BigIP partitions with Name [-Partition alias] ''" -TestCases @(@{name='Common'}) { + param($name) + Get-BigIPPartition -F5Session $mocksession -Partition $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq (($mocksession.BaseURL -replace 'ltm/','sys/folder') + ('/~{0}?$select=name,subPath' -f $name)) } + } + It "Requests BigIP partitions by Name[]" -TestCases @(@{name=@('Common','Development','Production')}) { + param($name) + Get-BigIPPartition -F5Session $mocksession -Name $name | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times 3 -Exactly -Scope It + } + It "Requests BigIP partitions by Name From Pipeline" -TestCases @(@{ object = ([pscustomobject]@{name = 'Common'}),([pscustomobject]@{name = 'Development'}) }) { + param($object) + $object | Get-BigIPPartition -F5Session $mocksession | + ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } + Assert-MockCalled Invoke-RestMethodOverride -Times ($object.Count) -Exactly -Scope It + } + } + } +} + Describe 'Get-HealthMonitor' -Tags 'Unit' { InModuleScope F5-LTM { Context "Strict mode PS$($PSVersionTable.PSVersion.Major)" { From 9c2017225a4349825b07900cf0dc9fd485c4a61f Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 20:48:57 -0500 Subject: [PATCH 11/12] Remove unused $partitions variable in tests Correct PSUseDeclaredVarsMoreThanAssignments for Get-BigIPPartition tests --- F5-LTM/F5-LTM.Tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 40e67f4..20d7bc6 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -12,8 +12,6 @@ Describe 'Get-BigIPPartition' -Tags 'Unit' { #region Arrange: Initialize Mocks - $partitions = @('Common','Development','Production') - # Mocking Invoke-RestMethodOverride for unit testing Module without F5 device connectivity Mock Invoke-RestMethodOverride { # Behavior (not state) verification is applied to this mock. From 14492a0ca75bf130a187bff8899bd4d366a7d47b Mon Sep 17 00:00:00 2001 From: Jason Vercellone Date: Sun, 25 Mar 2018 21:08:56 -0500 Subject: [PATCH 12/12] Get-BigIPPartition 100% code coverage Tweaks to achieve 100% code coverage for F5-LTM\Public\Get-BigIPPartition.ps1 --- F5-LTM/F5-LTM.Tests.ps1 | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/F5-LTM/F5-LTM.Tests.ps1 b/F5-LTM/F5-LTM.Tests.ps1 index 20d7bc6..1011e82 100644 --- a/F5-LTM/F5-LTM.Tests.ps1 +++ b/F5-LTM/F5-LTM.Tests.ps1 @@ -16,10 +16,20 @@ Describe 'Get-BigIPPartition' -Tags 'Unit' { Mock Invoke-RestMethodOverride { # Behavior (not state) verification is applied to this mock. # Therefore, the output need only meet the bare minimum requirements to maximize code coverage of the Subject Under Test. - [pscustomobject]@{ - items=@(@{name='bogus item for testing';subPath='subPath'}) - name='name' - selfLink="https://localhost/mgmt/tm/sys/folder/~name?ver=12.1.2" + if ($URI -match 'JSON') { + # This case included to support maximum code coverage + [pscustomobject]@{ + items=$null + name='name' + subPath='subPath' + selfLink="https://localhost/mgmt/tm/sys/folder/~name?ver=12.1.2" + } + } else { + [pscustomobject]@{ + items=@(@{name='bogus item for testing';subPath='subPath'}) + name='name' + selfLink="https://localhost/mgmt/tm/sys/folder/~name?ver=12.1.2" + } } } # Mock session with fictional IP,credentials, and version @@ -41,7 +51,8 @@ Describe 'Get-BigIPPartition' -Tags 'Unit' { ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' } Assert-MockCalled Invoke-RestMethodOverride -Times 1 -Exactly -Scope It -ParameterFilter { $Uri.AbsoluteUri -eq ($mocksession.BaseURL -replace 'ltm/','sys/folder/?$select=name,subPath') } } - It "Requests BigIP partitions by Name ''" -TestCases @(@{name='Common'},@{name='Development'},@{name='Production'}) { + # JSON test also forces a codecoverage scenario for a single item without an items property returned from the F5 + It "Requests BigIP partitions by Name ''" -TestCases @(@{name='Common'},@{name='Development'},@{name='Production'},@{name='JSON'}) { param($name) Get-BigIPPartition -F5Session $mocksession -Name $name | ForEach-Object { $_.PSObject.TypeNames[0] | Should Be 'string' }