108 lines
4.2 KiB
PowerShell
108 lines
4.2 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 = ""
|
|
|
|
#region Disable-Service-Integration
|
|
Describe "Disable-Service-Integration" -Tag 'Integration' {
|
|
|
|
Context "Validation" {
|
|
|
|
# The preceeding space makes it show up at the top of the service list
|
|
# Should make it visible in the event of test cleanup failure
|
|
$randomFileName = [System.IO.Path]::GetRandomFileName()
|
|
$testServiceName = ("_PESTER{0}" -f ($randomFileName.Split(".") | Select-Object -First 1))
|
|
<#
|
|
To delete any/all _PESTER services that don't get cleaned up.
|
|
get-service -Name _PESTER* |% {(Get-CIMInstance Win32_Service -filter ("name='{0}'" -f $_.Name)) | Invoke-CIMMethod -MethodName Delete}
|
|
#>
|
|
Write-Warning ("Using Test Service Name '{0}'" -f $testServiceName)
|
|
Write-Host "Creating Test Service $testServiceName"
|
|
|
|
It "It Disables the Windows Service Provided" {
|
|
try
|
|
{
|
|
Write-Host "Creating Test Service $testServiceName"
|
|
New-Service -Name $testServiceName -BinaryPathName "FakePath"
|
|
|
|
$svc = Get-Service $testServiceName -ErrorAction SilentlyContinue
|
|
|
|
if ($null -eq $svc)
|
|
{
|
|
throw "The Test Service Could Not Be Created. Check for permissions issues"
|
|
}
|
|
|
|
Disable-Service $svc
|
|
Get-Service $testServiceName | Select-Object StartType | Should Match "Disabled"
|
|
}
|
|
finally
|
|
{
|
|
if ($null -ne (Get-Service $testServiceName -ErrorAction SilentlyContinue))
|
|
{
|
|
(Get-CIMInstance Win32_Service -filter ("name='{0}'" -f $testServiceName)) | Invoke-CIMMethod -MethodName Delete
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Disable-Service-Integration
|
|
#region Disable-Service-Unit
|
|
Describe "Disable-Service-Unit" -Tag 'Unit' {
|
|
Context "Logic" {
|
|
#region Arrange
|
|
|
|
$randomFileName = [System.IO.Path]::GetRandomFileName()
|
|
$testServiceName = ("_PESTER{0}" -f ($randomFileName.Split(".") | Select-Object -First 1))
|
|
<#
|
|
To delete any/all _PESTER services that don't get cleaned up.
|
|
get-service -Name _PESTER* |% {(Get-CIMInstance Win32_Service -filter ("name='{0}'" -f $_.Name)) | Invoke-CIMMethod -MethodName Delete}
|
|
#>
|
|
Write-Warning ("Using Test Service Name '{0}'" -f $testServiceName)
|
|
Write-Host "Creating Test Service $testServiceName"
|
|
|
|
$svc = New-Service -Name $testServiceName -BinaryPathName "FakePath" -StartupType "Manual"
|
|
$svc = Get-Service $testServiceName -ErrorAction SilentlyContinue
|
|
|
|
Mock -CommandName Stop-AlkamiService -MockWith { return $true } -ModuleName $moduleForMock
|
|
Mock -CommandName Set-Service -MockWith { } -ModuleName $moduleForMock
|
|
<#
|
|
Pester `Mock -MockWith` is ... weird.
|
|
It lets you use parameter variable names from *inside* the scope of the namespace you're
|
|
mocking the command call *from*.
|
|
So $service.Name below is not evaluated until the call to Get-Service from *inside* of
|
|
Alkami.PowerShell.Services happens. At which point, $service has a value, as does
|
|
$service.Name
|
|
https://github.com/pester/Pester/wiki/Mock - where they describe the `Mock-With` parameter.
|
|
#>
|
|
Mock -CommandName Get-Service -MockWith {
|
|
[PSCustomObject]@{
|
|
Name = $service.Name;
|
|
StartType = "Disabled";
|
|
Status = "Stopped"
|
|
}
|
|
} -ModuleName $moduleForMock
|
|
|
|
if ($null -eq $svc){
|
|
Set-ItResult -Inconclusive -Because 'Could not create service fake'
|
|
}
|
|
#endregion Arrange
|
|
|
|
It "Calls Set-Service with correct parameters" {
|
|
#region Act
|
|
Disable-Service $svc
|
|
#endregion Act
|
|
#region Assert
|
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Set-Service -ParameterFilter { $Name -eq $testServiceName -and $StartupType -eq "Disabled" } -Scope It -Exactly 1
|
|
#endregion Assert
|
|
}
|
|
#region Cleanup
|
|
(Get-CIMInstance Win32_Service -filter ("name='{0}'" -f $testServiceName)) | Invoke-CIMMethod -MethodName Delete
|
|
#endregion Cleanup
|
|
|
|
}
|
|
}
|
|
#endregion Disable-Service-Unit |