ps/Modules/Alkami.PowerShell.Configuration/Public/Update-SystemPortReservations.tests.ps1
2023-05-30 22:51:22 -07:00

89 lines
4.9 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
#region Update-SystemPortReservations
Describe "Update-SystemPortReservations" -Tags @("Integration") {
Mock -ModuleName $moduleForMock Remove-NetshExcludedPortRange { Write-Host "removed 1" } -Verifiable
Mock -ModuleName $moduleForMock Add-NetshExcludedPortRange { Write-Host "created 1" } -Verifiable
Mock -ModuleName $moduleForMock Get-NetshExcludedPortRanges { return @(@{ Start = 1; End = 10; }); } -Verifiable
Mock -ModuleName $moduleForMock Set-DefaultNetshIPListens {} -Verifiable
Mock -ModuleName $moduleForMock Set-DefaultNetshURLACLS {} -Verifiable
Mock -ModuleName $moduleForMock Start-AlkamiService {} -Verifiable
Mock -ModuleName $moduleForMock Set-EnvironmentVariable { } -Verifiable
Mock -ModuleName $moduleForMock Get-EnvironmentVariable { return $null } -Verifiable
Mock -ModuleName $moduleForMock Remove-EnvironmentVariable { } -Verifiable
Mock -ModuleName $moduleForMock Get-LogLeadName { "SUT: Update-SystemPortReservations" }
Mock -ModuleName $moduleForMock Get-NetTCPConnection { Write-Host "asked for a connection"; return $null } -Verifiable
Mock -ModuleName $moduleForMock Get-Process { return $null }
Mock -ModuleName $moduleForMock Stop-Process { }
Mock -ModuleName $moduleForMock Write-Error { }
Mock -ModuleName $moduleForMock Write-Verbose { }
Mock -ModuleName $moduleForMock Write-Warning { }
It "handles a basic create pair" {
Update-SystemPortReservations -Create @(50000,60)
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 1 -CommandName Add-NetshExcludedPortRange
}
It "handles a basic remove pair with no creates" {
Update-SystemPortReservations -Remove @(1,11)
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 1 -CommandName Remove-NetshExcludedPortRange
}
It "throws when there is no port to remove as asked" {
{ Update-SystemPortReservations -Remove @(1,10) } | Should -Throw
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
It "throws with a basic remove pair where one port doesn't exist" {
{ Update-SystemPortReservations -Remove @(1,10),@(20,5) } | Should -Throw
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
It "creates two ports with the default configuration" {
Update-SystemPortReservations
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 2 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
It "creates one port because one already existed" {
Update-SystemPortReservations -Create @(50000,60),@(1,10)
# This is actually _wrong_ but because we aren't managing a super complex system state above, we assume two got created
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 2 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
# Seriously, don't do this in the real world
It "doesn't handle removing and adding the same port" {
{ Update-SystemPortReservations -Create @(1,10) -Remove @(1,10) } | Should -Not -Throw
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
It "handles the same addition twice" {
Update-SystemPortReservations -Create @(50000,60),@(50000,60)
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 1 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
It "can't handle overlapping create inputs" {
{ Update-SystemPortReservations -Create @(50000,60),@(50010,60) } | Should -Throw
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Add-NetshExcludedPortRange
Assert-MockCalled -ModuleName $moduleForMock -Scope It -Times 0 -CommandName Remove-NetshExcludedPortRange
}
}
#endregion Update-SystemPortReservations