Skip to content

Commit 04c3a55

Browse files
committed
Start Fixing Unit Tests
1 parent 792cd12 commit 04c3a55

File tree

2 files changed

+109
-50
lines changed

2 files changed

+109
-50
lines changed

source/DSCResources/DSC_xServiceResource/DSC_xServiceResource.psm1

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,57 +2145,58 @@ function Get-ServiceFailureActions {
21452145
$Service
21462146
)
21472147
process {
2148-
$registryData = Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\$service
2149-
2150-
$failureActions = [PSCustomObject]@{
2151-
resetPeriodSeconds = $null
2152-
hasRebootMessage = $null
2153-
hasFailureCommand = $null
2154-
failureActionCount = $null
2155-
failureCommand = $null
2156-
rebootMessage = $null
2157-
actionsCollection = $null
2158-
$FailureActionsOnNonCrashFailures = $false
2159-
}
2148+
if($registryData = Get-Item -Path HKLM:\SYSTEM\CurrentControlSet\Services\$service -ErrorAction SilentlyContinue)
2149+
{
2150+
$failureActions = [PSCustomObject]@{
2151+
resetPeriodSeconds = $null
2152+
hasRebootMessage = $null
2153+
hasFailureCommand = $null
2154+
failureActionCount = $null
2155+
failureCommand = $null
2156+
rebootMessage = $null
2157+
actionsCollection = $null
2158+
FailureActionsOnNonCrashFailures = $false
2159+
}
21602160

2161-
if($registryData.GetvalueNames() -match 'FailureCommand') {
2162-
$failureActions.failureCommand = $registryData.GetValue('FailureCommand')
2163-
}
2161+
if($registryData.GetvalueNames() -match 'FailureCommand') {
2162+
$failureActions.failureCommand = $registryData.GetValue('FailureCommand')
2163+
}
21642164

2165-
if($registryData.GetValueNames() -match 'RebootMessage') {
2166-
$failureActions.rebootMessage = $registryData.GetValue('RebootMessage')
2167-
}
2165+
if($registryData.GetValueNames() -match 'RebootMessage') {
2166+
$failureActions.rebootMessage = $registryData.GetValue('RebootMessage')
2167+
}
21682168

2169-
if($registryData.GetvalueNames() -match 'FailureActionsOnNonCrashFailures') {
2170-
$failureActions.FailureActionsOnNonCrashFailures = [System.Boolean]$registryData.GetValue('FailureActionsOnNonCrashFailures')
2171-
}
2169+
if($registryData.GetvalueNames() -match 'FailureActionsOnNonCrashFailures') {
2170+
$failureActions.FailureActionsOnNonCrashFailures = [System.Boolean]$registryData.GetValue('FailureActionsOnNonCrashFailures')
2171+
}
21722172

2173-
if($registryData.GetValueNames() -match 'FailureActions')
2174-
{
2175-
$failureActionsBinaryData = $registryData.GetValue('FailureActions')
2173+
if($registryData.GetValueNames() -match 'FailureActions')
2174+
{
2175+
$failureActionsBinaryData = $registryData.GetValue('FailureActions')
21762176

2177-
# The first four bytes represent the Reset Period.
2178-
$failureActions.resetPeriodSeconds = Get-FailureActionsProperty -PropertyName ResetPeriodSeconds -Bytes $failureActionsBinaryData
2177+
# The first four bytes represent the Reset Period.
2178+
$failureActions.resetPeriodSeconds = Get-FailureActionsProperty -PropertyName ResetPeriodSeconds -Bytes $failureActionsBinaryData
21792179

2180-
# Next four bytes indicate the presence of a reboot message in case one of the chosen failure actions is
2181-
# SC_ACTION_REBOOT. The actual value of the message is stored in the 'RebootMessage' property
2182-
$failureActions.hasRebootMessage = Get-FailureActionsProperty -PropertyName HasRebootMsg -Bytes $failureActionsBinaryData
2180+
# Next four bytes indicate the presence of a reboot message in case one of the chosen failure actions is
2181+
# SC_ACTION_REBOOT. The actual value of the message is stored in the 'RebootMessage' property
2182+
$failureActions.hasRebootMessage = Get-FailureActionsProperty -PropertyName HasRebootMsg -Bytes $failureActionsBinaryData
21832183

2184-
# The next four bytes indicate whether a failure action run command exists. This command
2185-
# would be run in the case one of the failure actions chosen is SC_ACTION_RUN_COMMAND
2186-
# If this value is true then the actual command string is stored in the 'FailureCommand' property.
2187-
$failureActions.hasFailureCommand = Get-FailureActionsProperty -PropertyName HasFailureCommand -Bytes $failureActionsBinaryData
2184+
# The next four bytes indicate whether a failure action run command exists. This command
2185+
# would be run in the case one of the failure actions chosen is SC_ACTION_RUN_COMMAND
2186+
# If this value is true then the actual command string is stored in the 'FailureCommand' property.
2187+
$failureActions.hasFailureCommand = Get-FailureActionsProperty -PropertyName HasFailureCommand -Bytes $failureActionsBinaryData
21882188

2189-
# These four bytes give the count of how many reboot failure actions have been defined.
2190-
$failureActions.failureActionCount = Get-FailureActionsProperty -PropertyName FailureActionCount -Bytes $failureActionsBinaryData
2189+
# These four bytes give the count of how many reboot failure actions have been defined.
2190+
$failureActions.failureActionCount = Get-FailureActionsProperty -PropertyName FailureActionCount -Bytes $failureActionsBinaryData
21912191

2192-
if($failureActions.failureActionCount -gt 0)
2193-
{
2194-
$failureActions.ActionsCollection = Get-FailureActionCollection -Bytes $failureActionsBinaryData -ActionsCount $failureActions.failureActionCount
2192+
if($failureActions.failureActionCount -gt 0)
2193+
{
2194+
$failureActions.ActionsCollection = Get-FailureActionCollection -Bytes $failureActionsBinaryData -ActionsCount $failureActions.failureActionCount
2195+
}
21952196
}
2196-
}
21972197

2198-
$failureActions
2198+
$failureActions
2199+
}
21992200
}
22002201
}
22012202

tests/Unit/DSC_xServiceResource.Tests.ps1

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,44 @@ try
217217
$getTargetResourceResult.Dependencies | Should -Be $ExpectedValues.Dependencies
218218
}
219219
}
220+
221+
if ($ExpectedValues.ContainsKey('ResetPeriodSeconds'))
222+
{
223+
it 'Should return the reset period in seconds' {
224+
$getTargetResourceResult.resetPeriodSeconds | Should -Be $ExpectedValues.resetPeriodSeconds
225+
}
226+
}
227+
228+
if ($ExpectedValues.ContainsKey('FailureCommand'))
229+
{
230+
it 'Should return the failure command' {
231+
$getTargetResourceResult.failureCommand | Should -Be $ExpectedValues.FailureCommand
232+
}
233+
}
234+
235+
if ($ExpectedValues.ContainsKey('RebootMessage'))
236+
{
237+
it 'Should return the reboot message' {
238+
$getTargetResourceResult.rebootMessage | Should -Be $ExpectedValues.RebootMessage
239+
}
240+
}
241+
242+
if ($ExpectedValues.ContainsKey('FailureActionsCollection'))
243+
{
244+
foreach ($index in 0..($ExpectedValues.failureActionsCollection.count - 1)) {
245+
it "Should return the correct failure action for index $index" {
246+
$getTargetResourceResult.failureActionsCollection[$index].type | Should -Be $ExpectedValues.FailureActionsCollection[$index].type
247+
$getTargetResourceResult.failureActionsCollection[$index].delaySeconds | Should -Be $ExpectedValues.FailureActionsCollection[$index].delaySeconds
248+
}
249+
}
250+
}
251+
252+
if ($ExpectedValues.ContainsKey('FailureActionsOnNonCrashFailures'))
253+
{
254+
it 'Should return the failure actions on non crash failures flag' {
255+
$getTargetResourceResult.failureActionsOnNonCrashFailures | Should -Be $ExpectedValues.failureActionsOnNonCrashFailures
256+
}
257+
}
220258
}
221259

222260
Mock -CommandName 'Get-Service' -MockWith { }
@@ -265,23 +303,43 @@ try
265303
DesktopInteract = $true
266304
}
267305

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+
}
319+
268320
Mock -CommandName 'Get-Service' -MockWith { return $testService }
269321
Mock -CommandName 'Get-ServiceCimInstance' -MockWith { return $testServiceCimInstance }
270322
Mock -CommandName 'ConvertTo-StartupTypeString' -MockWith { return $convertToStartupTypeStringResult }
323+
Mock -CommandName 'Get-ServiceFailureActions' -MockWith { return $testServiceFailureActions }
271324

272325
Test-GetTargetResourceDoesntThrow -GetTargetResourceParameters $getTargetResourceParameters -TestServiceCimInstance $testServiceCimInstance
273326

274327
$expectedValues = @{
275-
Name = $getTargetResourceParameters.Name
276-
Ensure = 'Present'
277-
Path = $testServiceCimInstance.PathName
278-
StartupType = $convertToStartupTypeStringResult
279-
BuiltInAccount = $testServiceCimInstance.StartName
280-
State = $testService.Status
281-
DisplayName = $testService.DisplayName
282-
Description = $testServiceCimInstance.Description
283-
DesktopInteract = $testServiceCimInstance.DesktopInteract
284-
Dependencies = [System.Object[]] $testService.ServicesDependedOn.Name
328+
Name = $getTargetResourceParameters.Name
329+
Ensure = 'Present'
330+
Path = $testServiceCimInstance.PathName
331+
StartupType = $convertToStartupTypeStringResult
332+
BuiltInAccount = $testServiceCimInstance.StartName
333+
State = $testService.Status
334+
DisplayName = $testService.DisplayName
335+
Description = $testServiceCimInstance.Description
336+
DesktopInteract = $testServiceCimInstance.DesktopInteract
337+
Dependencies = [System.Object[]] $testService.ServicesDependedOn.Name
338+
ResetPeriodSeconds = $testServiceFailureActions.resetPeriodSeconds
339+
FailureCommand = $testServiceFailureActions.failureCommand
340+
RebootMessage = $testServiceFailureActions.rebootMessage
341+
FailureActionsCollection = $testServiceFailureActions.actionsCollection
342+
failureActionsOnNonCrashFailures = $testServiceFailureActions.failureActionsOnNonCrashFailures
285343
}
286344

287345
Test-GetTargetResourceResult -GetTargetResourceParameters $getTargetResourceParameters -ExpectedValues $expectedValues

0 commit comments

Comments
 (0)