ps/Modules/Alkami.PowerShell.Services/Public/Restart-Nag.Tests.ps1
2023-05-30 22:51:22 -07:00

198 lines
8.7 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 "Restart-Nag" {
Mock -ModuleName $moduleForMock -CommandName Write-Warning {}
Mock -ModuleName $moduleForMock -CommandName Test-AreCriticalNagJobsRunning {}
Mock -ModuleName $moduleForMock -CommandName Stop-AlkamiService {}
Mock -ModuleName $moduleForMock -CommandName Write-Verbose {}
Mock -ModuleName $moduleForMock -CommandName Write-Output {}
Mock -ModuleName $moduleForMock -CommandName Get-LogLeadName {}
Mock -ModuleName $moduleForMock -CommandName Get-Service {}
Context "Can Recycle Checks" {
It "Warns when Nag is not present" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return $null }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
$message -eq " : The Alkami Nag Service could not be located"
}
}
It "Does not throw when Nag is not present" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return $null }
{ Restart-Nag } | Should -Not -Throw
}
It "Warns if Nag is disabled." {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Disabled"
} }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
$message -eq " : The Alkami Nag Service is disabled and cannot be restarted"
}
}
It "Does not throw if Nag is disabled." {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Disabled"
} }
{ Restart-Nag } | Should -Not -Throw
}
It "Warns if user is Anonymous" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Get-ChildItem {
$userInfo = @{}
$userInfo.Name = "USERNAME"
$userInfo.Value = "ANONYMOUS"
return $userInfo
} -ParameterFilter { $Path -and $Path -eq "env:USERNAME" }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
$message -eq " : Anonymous users cannot query the database. This function cannot be executed under the current user context"
}
}
It "Does not throw if user is Anonymous" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Get-ChildItem {
$userInfo = @{}
$userInfo.Name = "USERNAME"
$userInfo.Value = "ANONYMOUS"
return $userInfo
} -ParameterFilter { $Path -and $Path -eq "env:USERNAME" }
{ Restart-Nag } | Should -Not -Throw
}
It "Warns if Host is Chocolatey" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Get-ChildItem { return "IAmAValidUser" } -ParameterFilter { $Path -and $Path -eq "env:USERNAME" }
Mock -ModuleName $moduleForMock -CommandName Get-Host { return @{Name = "Chocolatey_PSHost" } }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
$message -eq " : You can't run this from Chocolatey without the -Force flag because we don't know if this is a local or remote session"
}
}
It "Does not throw if Host is Chocolatey" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Get-ChildItem { return "IAmAValidUser" } -ParameterFilter { $Path -and $Path -eq "env:USERNAME" }
Mock -ModuleName $moduleForMock -CommandName Get-Host { return @{Name = "Chocolatey_PSHost" } }
{ Restart-Nag } | Should -Not -Throw
}
It "Recycles Nag if Force Flag is present." {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Running"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Stop-AlkamiService -MockWith { return $null }
Mock -ModuleName $moduleForMock -CommandName Start-Service -MockWith { return $null }
Restart-Nag -Force
Assert-MockCalled -ModuleName $moduleForMock Stop-AlkamiService
Assert-MockCalled -ModuleName $moduleForMock Start-Service
}
}
Context "Recycle Tests" {
Mock -ModuleName $moduleForMock -CommandName Stop-AlkamiService -MockWith { return $null }
Mock -ModuleName $moduleForMock -CommandName Start-Service -MockWith { return $null }
It "Recycles Nag When Service is Stopped" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Stopped"
StartType = "Manual"
} }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock -CommandName Stop-AlkamiService
Assert-MockCalled -ModuleName $moduleForMock -CommandName Start-Service
}
It "Tests for running Nag Jobs if necessary" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Running"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Test-AreCriticalNagJobsRunning -MockWith { return $true }
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-AreCriticalNagJobsRunning
}
It "Warns and does not recycle Nag if jobs are running" {
Mock -ModuleName $moduleForMock -CommandName Get-Service { return @{
DisplayName = "Alkami Nag Service"
Name = "Alkami Nag Service"
Status = "Running"
StartType = "Manual"
} }
Mock -ModuleName $moduleForMock -CommandName Test-AreCriticalNagJobsRunning -MockWith { return $false } -Verifiable
Restart-Nag
Assert-MockCalled -ModuleName $moduleForMock Write-Warning -Times 1 -Exactly -Scope It -ParameterFilter {
$message -eq " : Cannot recycle Alkami Nag Service due to running or scheduled jobs"
}
Assert-MockCalled -ModuleName $moduleForMock -CommandName Stop-AlkamiService -Exactly 0 -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Start-Service -Exactly 0 -Scope It
}
}
}