ps/Modules/Alkami.PowerShell.Services/Public/Set-ServiceRecoveryOneRestart.tests.ps1
2023-05-30 22:51:22 -07:00

114 lines
5.0 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 = ""
Describe "Set-ServiceRecoveryOneRestart" {
Context "Ensure that it returns with a warning for no service returned" {
Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { return $null }
Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith { $global:LASTEXITCODE = 1 }
Mock -ModuleName $moduleForMock -CommandName Write-Error
Mock -ModuleName $moduleForMock -CommandName Write-Warning
Set-ServiceRecoveryOneRestart "anything"
It "Ensures Write-Warning got called once for `$null service" {
Assert-MockCalled -CommandName Write-Warning -Times 1 -Scope Context -ModuleName $moduleForMock
}
It "Ensures Write-Error did not get called for `$null service" {
Assert-MockCalled -CommandName Write-Error -Times 0 -Scope Context -ModuleName $moduleForMock
}
}
Context "Ensure that it returns an error for a non-zero exit code" {
Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { return @{}; } #return a non-null value
Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith { $global:LASTEXITCODE = 1 }
Mock -ModuleName $moduleForMock -CommandName Write-Error
Mock -ModuleName $moduleForMock -CommandName Write-Warning
Set-ServiceRecoveryOneRestart "anything"
It "Ensures Write-Warning did not get called for `$null service" {
Assert-MockCalled -CommandName Write-Warning -Times 0 -Scope Context -ModuleName $moduleForMock
}
It "Ensures Write-Error got called twice for `$null service" {
Assert-MockCalled -CommandName Write-Error -Times 1 -Scope Context -ModuleName $moduleForMock -ParameterFilter {$Message -match "Attempt to call sc.exe"}
Assert-MockCalled -CommandName Write-Error -Times 1 -Scope Context -ModuleName $moduleForMock -ParameterFilter {$Message -match "An error occurred setting" }
}
}
# The only really useful thing we can check is to make sure we didn't shoot ourselves in the foot with a bad parameter.
Context "Ensure that the parameters being passed to sc.exe match what we need them to be" {
Mock -ModuleName $moduleForMock -CommandName Get-Service -MockWith { return @{}; } #return a non-null value
$global:MagicValue = @()
Mock -ModuleName $moduleForMock -CommandName Invoke-SCExe -MockWith {
if ($global:MagicValue.Length -eq 0) {
$global:MagicValue = $args
$global:LASTEXITCODE = 0
}
}
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
Set-ServiceRecoveryOneRestart "anything"
$testArray = $global:MagicValue -split ' '
# failure anything actions= restart/10000//// reset= 86400
$testSpecial = $testArray[4] # -> restart/10000////
# When you are debugging this and want to see it fail, ues this test case
# Leaving this here in case I need to understand what broke later
# $testSpecial = "restart/10000///anything-but-run-restart-reboot/NaN"
$testComponents = $testSpecial -split '/'
$evenCount = 0;
$oddCount = 0;
for ($i = 0; $i -lt $testComponents.Length; $i++) {
$component = $testComponents[$i]
if (($i % 2) -eq 0) {
# evens
It "Verifies that the even numbered parameters are correctly parseable as run/restart/reboot/empty" {
$component | Should -BeIn @('', 'run', 'restart', 'reboot') -Because "Position [$i] was [$component]"
}
$evenCount++
} else {
# odds
It "Verifies that the odd numbered parameters are correctly parseable as digits" {
$component | Should -Match '^[0-9]*$' -Because "Position [$i] was [$component]"
}
$oddCount++
}
}
$evenCount | Should -BeGreaterThan 0
$evenCount | Should -MatchExactly $oddCount
Remove-Variable -Scope Global -Name MagicValue
It "Ensures Write-Warning did not get called for best path" {
Assert-MockCalled -CommandName Write-Warning -Times 0 -Scope Context -ModuleName $moduleForMock
}
It "Ensures Write-Error did not get called for best path" {
Assert-MockCalled -CommandName Write-Error -Times 0 -Scope Context -ModuleName $moduleForMock
}
It "Calls Invoke-SCExe Twice" {
Assert-MockCalled -CommandName Invoke-SCExe -Times 2 -Scope Context -ModuleName $moduleForMock
}
}
}