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

85 lines
3.4 KiB
PowerShell
Raw 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-AlkamiServiceFabricClusterHealthy" {
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Wait-AlkamiServiceFabricClusterHealthy.tests' }
Mock -CommandName Connect-AlkamiServiceFabricCluster -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Start-Sleep -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Write-Error -ModuleName $moduleForMock -MockWith {}
Context "Parameter Validation" {
It "Throws if Hostname is Null" {
{ Wait-AlkamiServiceFabricClusterHealthy -Hostname $null } | Should -Throw
}
It "Throws if Hostname is Empty" {
{ Wait-AlkamiServiceFabricClusterHealthy -Hostname '' } | Should -Throw
}
It "Throws if Sleep Interval is Zero" {
{ Wait-AlkamiServiceFabricClusterHealthy -SleepIntervalSeconds 0 } | Should -Throw
}
It "Throws if Sleep Interval is Too Large" {
{ Wait-AlkamiServiceFabricClusterHealthy -SleepIntervalSeconds 180 } | Should -Throw
}
}
Context "Error Handling" {
Mock -CommandName Get-ServiceFabricClusterHealth -ModuleName $moduleForMock -MockWith {
return @{ 'AggregatedHealthState' = 'Test' }
}
It "Writes Error if Operation Times Out" {
Wait-AlkamiServiceFabricClusterHealthy -TimeoutMinutes 0
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricClusterHealth -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 1 -Exactly -Scope It `
-ParameterFilter { $Message -match "Timed out waiting for Service Fabric cluster to be healthy" }
}
}
Context "Logic" {
Mock -CommandName Get-ServiceFabricClusterHealth -ModuleName $moduleForMock -MockWith {
return @{ 'AggregatedHealthState' = 'Ok' }
}
It "Uses Localhost Hostname By Default" {
Wait-AlkamiServiceFabricClusterHealthy -TimeoutMinutes 0
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -match 'localhost' }
}
It "Uses Hostname Parameter if Provided" {
Wait-AlkamiServiceFabricClusterHealthy -TimeoutMinutes 0 -Hostname 'Test'
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -match 'Test' }
}
It "Returns Without Error if Cluster is Healthy" {
Wait-AlkamiServiceFabricClusterHealthy -TimeoutMinutes 0
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Get-ServiceFabricClusterHealth -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
}
}
}