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

612 lines
23 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 "Get-ProcessFromService" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith {}
Mock -ModuleName $moduleForMock Get-Process -MockWith {}
Mock -ModuleName $moduleForMock Get-Process -ParameterFilter { $Name } -MockWith {}
Mock -ModuleName $moduleForMock Write-Host -MockWith {}
Mock -ModuleName $moduleForMock Write-Warning -MockWith {}
Context "no CIM instance" {
$fakeService = @{ Name = "fakeService" }
$emptyArray = Get-ProcessFromService $fakeService
It "Should return an empty value" {
(Test-IsCollectionNullOrEmpty $emptyArray) | Should -BeTrue
}
It "Did actually warn us" {
Assert-MockCalled -ModuleName $moduleForMock Write-Warning
}
}
Context "no processes but CIM instance" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith { return @{
Name = "fakeService"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
} }
$fakeService = @{ Name = "fakeService" }
$emptyArray = Get-ProcessFromService $fakeService
It "Should return an empty value" {
(Test-IsCollectionNullOrEmpty $emptyArray) | Should -BeTrue
}
}
Context "happy path 1 process and 1 CIM instance" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith { return @{
Name = "fakeService"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
} }
Mock -ModuleName $moduleForMock Get-Process -ParameterFilter { $Name -eq "fakeService" } -MockWith {
param ($Name)
return @{
Handlecount = 1
ProcessName = $Name
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\Fake\Path\fakeService.exe"
}
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234" {
$processExists.Id | Should -Be 1234
}
}
Context "2 processes but only 1 CIM instance" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith { return @{
Name = "fakeService"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
} }
Mock -ModuleName $moduleForMock Get-Process -MockWith {
param ($Name)
return @(
@{
Handlecount = 1
ProcessName = $Name
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\Fake\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "NOT IT"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9999
Path = "C:\Whoa\Partner\Wrong\NotYourService.exe"
}
)
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234" {
$processExists.Id | Should -Be 1234
}
}
Context "many processes and 2 CIM instances but only one running" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith {
return @(
@{
Name = "fakeService (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
},@{
Name = "fakeService_os (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path_os\fakeService.exe"
ExePath = "C:\Fake\Path_os\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
}
)
}
Mock -ModuleName $moduleForMock Get-Process -MockWith {
return @(
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\Fake\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "NotYourService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9999
Path = "C:\Whoa\Partner\Wrong\NotYourService.exe"
},
@{
Handlecount = 1
ProcessName = "anotherNotService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9998
Path = "C:\Whoa\Partner\Yikes\anotherNotService.exe"
},
@{
Handlecount = 1
ProcessName = "moreWrong"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9997
Path = "C:\Whoa\Partner\Again\moreWrong.exe"
},
@{
Handlecount = 1
ProcessName = "blue"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9996
Path = "C:\Whoa\Partner\ISay\blue.exe"
}
)
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234" {
$processExists.Id | Should -Be 1234
}
}
Context "many processes and 2 CIM instances and both running" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith {
return @(
@{
Name = "fakeService (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
},@{
Name = "fakeService_os (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path_os\fakeService.exe"
ExePath = "C:\Fake\Path_os\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
}
)
}
Mock -ModuleName $moduleForMock Get-Process -MockWith {
return @(
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\Fake\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 5555
Path = "C:\Fake\Path_os\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "NotYourService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9999
Path = "C:\Whoa\Partner\Wrong\NotYourService.exe"
},
@{
Handlecount = 1
ProcessName = "anotherNotService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9998
Path = "C:\Whoa\Partner\Yikes\anotherNotService.exe"
},
@{
Handlecount = 1
ProcessName = "moreWrong"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9997
Path = "C:\Whoa\Partner\Again\moreWrong.exe"
},
@{
Handlecount = 1
ProcessName = "blue"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9996
Path = "C:\Whoa\Partner\ISay\blue.exe"
}
)
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234 and 5555" {
$processExists.Id | Should -Be (1234,5555)
}
}
Context "many processes and 2 CIM instances and both running with returned pids" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith {
return @(
@{
Name = "fakeService (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = 1234
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
},@{
Name = "fakeService_os (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path_os\fakeService.exe"
ExePath = "C:\Fake\Path_os\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = 5555
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
}
)
}
Mock -ModuleName $moduleForMock Get-Process -ParameterFilter { $Id } -MockWith {
return @(
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\Fake\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 5555
Path = "C:\Fake\Path_os\fakeService.exe"
}
)
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234 and 5555" {
$processExists.Id | Should -Be (1234,5555)
}
}
Context "many processes and 2 CIM instances and both running but legacy lookup by name" {
Mock -ModuleName $moduleForMock Get-ServiceInfoByCIMFragment -MockWith {
return @(
@{
Name = "fakeService (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path\fakeService.exe"
ExePath = "C:\Fake\Path\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
},@{
Name = "fakeService_os (Haystack)"
DisplayName = "Definitely a fake service"
Path = "C:\Fake\Path_os\fakeService.exe"
ExePath = "C:\Fake\Path_os\fakeService.exe"
Started = $true
State = "Running"
Status = "OK"
StartMode = "Manual"
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
StartType = "Manual"
ProcessId = $null
InstallDate = $null
UserName = "NT AUTHORITY\FakeService"
}
)
}
Mock -ModuleName $moduleForMock Get-Process -MockWith {
return @(
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\other\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 5555
Path = "C:\other\Path_os\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "NotYourService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9999
Path = "C:\Whoa\Partner\Wrong\NotYourService.exe"
},
@{
Handlecount = 1
ProcessName = "anotherNotService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9998
Path = "C:\Whoa\Partner\Yikes\anotherNotService.exe"
},
@{
Handlecount = 1
ProcessName = "moreWrong"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9997
Path = "C:\Whoa\Partner\Again\moreWrong.exe"
},
@{
Handlecount = 1
ProcessName = "blue"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 9996
Path = "C:\Whoa\Partner\ISay\blue.exe"
}
)
}
Mock -ModuleName $moduleForMock Get-Process -ParameterFilter { $Name } -MockWith {
return @(
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 1234
Path = "C:\other\Path\fakeService.exe"
},
@{
Handlecount = 1
ProcessName = "fakeService"
NonpagedSystemMemorySize64 = 1
PagedMemorySize64 = 1
SessionId = 1
VirtualMemorySize64 = 1
WorkingSet64 = 1
Id = 5555
Path = "C:\other\Path_os\fakeService.exe"
}
)
}
$fakeService = @{ Name = "fakeService" }
$processExists = Get-ProcessFromService $fakeService
It "Should return a process item" {
$processExists | Should -Not -BeNull
}
It "Should have process id 1234 and 5555" {
$processExists.Id | Should -Be (1234,5555)
}
}
}