Skip to content

Commit f780b8b

Browse files
committed
Do a better job of mocking the registry
1 parent d5f7cf6 commit f780b8b

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

tests/Unit/DSC_xServiceResource.Tests.ps1

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,76 @@ try
257257
}
258258
}
259259

260+
function Get-MockFailureActionsData
261+
{
262+
[CmdletBinding()]
263+
param (
264+
[Parameter()]
265+
[System.String]
266+
$DataSetName
267+
)
268+
269+
switch ($DataSetName) {
270+
'1ActionNoCommandOrMessage' {
271+
$data = @{
272+
resetPeriodSeconds = 86400
273+
hasRebootMessage = 0
274+
hasFailureCommand = 0
275+
failureActionCount = 1
276+
failureCommand = $null
277+
rebootMessage = $null
278+
actionsCollection = @([PSCustomObject]@{
279+
type = 'RESTART'
280+
delaySeconds = 30000
281+
})
282+
failureActionsOnNonCrashFailures = $true
283+
}
284+
}
285+
Default {}
286+
}
287+
288+
$keyData = New-Object 'System.Collections.Generic.List[int]'
289+
290+
$keyData.add($data.resetPeriodSeconds) | Out-Null
291+
$keyData.add($data.hasRebootMessage) | Out-Null
292+
$keyData.add($data.hasFailureCommand) | Out-Null
293+
$keyData.add($data.failureActionCount) | Out-Null
294+
$keyData.add(20) | Out-Null
295+
296+
foreach ($action in $data.ActionsCollection) {
297+
$keyData.add([ACTION_TYPE]$action.type) | Out-Null
298+
$keyData.add($action.delaySeconds) | Out-Null
299+
}
300+
301+
$key = @{
302+
FailureActions = $keyData | Format-Hex -raw | Select-Object -ExpandProperty bytes
303+
}
304+
305+
if ($data.failureActionsOnNonCrashFailures)
306+
{
307+
$key.failureActionsOnNonCrashFailures = 1
308+
}
309+
310+
if ($data.hasRebootMessage)
311+
{
312+
$key.RebootMessage = 'Mocked Reboot Message'
313+
}
314+
315+
if ($data.hasFailureCommand)
316+
{
317+
$key.FailureCommand = 'C:\new\command.exe'
318+
}
319+
320+
$GetValueNames = {$this.keys}
321+
$GetValue = {param($key) $this[$key]}
322+
323+
Add-Member -InputObject $key -MemberType ScriptMethod -Name GetValueNames -Value $GetValueNames
324+
Add-Member -InputObject $key -MemberType ScriptMethod -Name GetValue -Value $GetValue
325+
326+
@([PSCustomObject]$data, $key)
327+
}
328+
329+
260330
Mock -CommandName 'Get-Service' -MockWith { }
261331
Mock -CommandName 'Get-ServiceCimInstance' -MockWith { }
262332
Mock -CommandName 'ConvertTo-StartupTypeString' -MockWith { }
@@ -303,24 +373,12 @@ try
303373
DesktopInteract = $true
304374
}
305375

306-
$testServiceFailureActions = @{
307-
resetPeriodSeconds = 86400
308-
hasRebootMessage = 0
309-
hasFailureCommand = 0
310-
failureActionCount = 1
311-
failureCommand = $null
312-
rebootMessage = $null
313-
actionsCollection = @([PSCustomObject]@{
314-
type = 'RESTART'
315-
delaySeconds = 30000
316-
})
317-
failureActionsOnNonCrashFailures = $true
318-
}
376+
$testServiceFailureActions, $failureRegistryKey = Get-MockFailureActionsData -DataSetName '1ActionNoCommandOrMessage'
319377

320378
Mock -CommandName 'Get-Service' -MockWith { return $testService }
321379
Mock -CommandName 'Get-ServiceCimInstance' -MockWith { return $testServiceCimInstance }
322380
Mock -CommandName 'ConvertTo-StartupTypeString' -MockWith { return $convertToStartupTypeStringResult }
323-
Mock -CommandName 'Get-ServiceFailureActions' -MockWith { return $testServiceFailureActions }
381+
Mock -CommandName 'Get-Item' -ParameterFilter { $Path -eq "HKLM:\SYSTEM\CurrentControlSet\Services\$($testService.name)" } -MockWith { return $failureRegistryKey}
324382

325383
Test-GetTargetResourceDoesntThrow -GetTargetResourceParameters $getTargetResourceParameters -TestServiceCimInstance $testServiceCimInstance
326384

0 commit comments

Comments
 (0)