ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Disable-AlkamiServiceFabricNode.Tests.ps1
2023-05-30 22:51:22 -07:00

119 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 = ""
Describe "Disable-AlkamiServiceFabricNode" {
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Disable-AlkamiServiceFabricNode.tests' }
Mock -CommandName Connect-AlkamiServiceFabricCluster -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Disable-ServiceFabricNode -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Wait-AlkamiServiceFabricNodeStatus -ModuleName $moduleForMock -MockWith {}
Mock -CommandName Write-Error -ModuleName $moduleForMock -MockWith {}
Context "Parameter Validation" {
It "Throws if Hostname is Null" {
{ Disable-AlkamiServiceFabricNode -Hostname $null } | Should -Throw
}
It "Throws if Hostname is Empty" {
{ Disable-AlkamiServiceFabricNode -Hostname '' } | Should -Throw
}
It "Throws if Intent is Not Allowable Value" {
{ Disable-AlkamiServiceFabricNode -Intent 'Test' } | Should -Throw
}
It "Throws if Sleep Interval is Zero" {
{ Disable-AlkamiServiceFabricNode -SleepIntervalSeconds 0 } | Should -Throw
}
It "Throws if Sleep Interval is Too Large" {
{ Disable-AlkamiServiceFabricNode -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' }
}
Disable-AlkamiServiceFabricNode -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" }
}
}
Context "Logic" {
It "Uses Localhost Hostname By Default" {
$testHostname = "$env:COMPUTERNAME"
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = $testHostname }
}
Disable-AlkamiServiceFabricNode
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 Node Operations" {
$testHostname = "$env:COMPUTERNAME"
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = $testHostname }
}
Disable-AlkamiServiceFabricNode
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 1 -Exactly -Scope It
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 Disable-ServiceFabricNode -Times 1 -Exactly -Scope It `
-ParameterFilter { $NodeName -eq $testHostname }
Assert-MockCalled -CommandName Wait-AlkamiServiceFabricNodeStatus -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq $testHostname }
}
It "Uses Hostname Parameter if Provided" {
$testHostname = "Test"
Mock -CommandName Get-ServiceFabricNode -ModuleName $moduleForMock -MockWith {
return @{ 'NodeName' = $testHostname }
}
Disable-AlkamiServiceFabricNode -Hostname $testHostname
Assert-MockCalled -CommandName Get-ServiceFabricNode -Times 1 -Exactly -Scope It
Assert-MockCalled -CommandName Write-Error -Times 0 -Exactly -Scope It
Assert-MockCalled -CommandName Connect-AlkamiServiceFabricCluster -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq $testHostname }
Assert-MockCalled -CommandName Disable-ServiceFabricNode -Times 1 -Exactly -Scope It `
-ParameterFilter { $NodeName -eq $testHostname }
Assert-MockCalled -CommandName Wait-AlkamiServiceFabricNodeStatus -Times 1 -Exactly -Scope It `
-ParameterFilter { $Hostname -eq $testHostname }
}
}
}