ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Wait-AlkamiServiceFabricNodeStatus.Tests.ps1

165 lines
7.5 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
. $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 "Wait-AlkamiServiceFabricNodeStatus" {
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Wait-AlkamiServiceFabricNodeStatus.tests' }
Mock -CommandName Connect-AlkamiServiceFabricCluster -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Start-Sleep -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Write-Error -ModuleName $moduleForMock -MockWith { Write-Host $Message }
Context "Parameter Validation" {
It "Throws if Desired Status is Not Allowable Value" {
{ Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Test' } | Should -Throw
}
It "Throws if Hostname is Null" {
{ Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -Hostname $null } | Should -Throw
}
It "Throws if Hostname is Empty" {
{ Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -Hostname '' } | Should -Throw
}
It "Throws if Sleep Interval is Zero" {
{ Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -SleepIntervalSeconds 0 } | Should -Throw
}
It "Throws if Sleep Interval is Too Large" {
{ Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -SleepIntervalSeconds 180 } | Should -Throw
}
}
Context "Error Handling" {
It "Writes Error if Node is Not Present in Cluster" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'NotTest'
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 1 -Exactly -Scope It `
-ParameterFilter { $Message -match "Could not retrieve the Service Fabric node" }
}
It "Writes Error if Operation Times Out Due to Node Status" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test'; 'NodeStatus' = 'Test' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'Test' -SkipHealthCheck
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 2 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 1 -Exactly -Scope It `
-ParameterFilter { $Message -match "Timed out waiting for Service Fabric node" }
}
It "Writes Error if Operation Times Out Due to Node Health" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test'; 'NodeStatus' = 'Up'; 'HealthState' = 'Test' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'Test'
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 2 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 1 -Exactly -Scope It `
-ParameterFilter { $Message -match "Timed out waiting for Service Fabric node" }
}
}
Context "Logic" {
It "Uses Localhost Hostname By Default" {
$testHostname = "$env:COMPUTERNAME"
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = $testHostname; 'NodeStatus' = 'Up'; 'HealthState' = 'Ok' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq 'localhost' }
}
It "Resolves Localhost Hostname to Computername for Status" {
$testHostname = "$env:COMPUTERNAME"
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = $testHostname; 'NodeStatus' = 'Up'; 'HealthState' = 'Ok' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq 'localhost' }
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 1 -Exactly -Scope It `
-ParameterFilter { $NodeName -eq $testHostname }
}
It "Uses Hostname Parameter if Provided" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test'; 'NodeStatus' = 'Up'; 'HealthState' = 'Ok' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'Test'
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq 'Test' }
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 1 -Exactly -Scope It `
-ParameterFilter { $NodeName -eq 'Test' }
}
It "Respects Health State by Default" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test'; 'NodeStatus' = 'Up'; 'HealthState' = 'Ok' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'Test'
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 2 -Exactly -Scope It
}
It "Ignores Health State if Parameter Provided" {
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = 'Test'; 'NodeStatus' = 'Up'; 'HealthState' = 'Test' }
}
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -TimeoutMinutes 0 -Hostname 'Test' -SkipHealthCheck
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 2 -Exactly -Scope It
}
}
}