-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Set-DscMachineRebootRequired and New-InvalidDataException (#44)
- Loading branch information
Showing
7 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<# | ||
.SYNOPSIS | ||
Creates and throws an invalid data exception. | ||
.DESCRIPTION | ||
Creates and throws an invalid data exception. | ||
.PARAMETER ErrorId | ||
The error Id to assign to the exception. | ||
.PARAMETER ErrorMessage | ||
The error message to assign to the exception. | ||
.EXAMPLE | ||
if ( -not $resultOfEvaluation ) | ||
{ | ||
$errorMessage = $script:localizedData.InvalidData -f $Action | ||
New-InvalidDataException -ErrorId 'InvalidDataError' -ErrorMessage $errorMessage | ||
} | ||
#> | ||
function New-InvalidDataException | ||
{ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$ErrorId, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$ErrorMessage | ||
) | ||
|
||
$errorCategory = [System.Management.Automation.ErrorCategory]::InvalidData | ||
$exception = New-Object ` | ||
-TypeName System.InvalidOperationException ` | ||
-ArgumentList $ErrorMessage | ||
$errorRecord = New-Object ` | ||
-TypeName System.Management.Automation.ErrorRecord ` | ||
-ArgumentList $exception, $ErrorId, $errorCategory, $null | ||
|
||
throw $errorRecord | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<# | ||
.SYNOPSIS | ||
Set the DSC reboot required status variable. | ||
.DESCRIPTION | ||
Sets the global DSCMachineStatus variable to a value of 1. | ||
This function is used to set the global variable that indicates | ||
to the LCM that a reboot of the node is required. | ||
.EXAMPLE | ||
PS C:\> Set-DscMachineRebootRequired | ||
Sets the $global:DSCMachineStatus variable to 1. | ||
.NOTES | ||
This function is implemented so that individual resource modules | ||
do not need to use and therefore suppress Global variables | ||
directly. It also enables mocking to increase testability of | ||
consumers. | ||
#> | ||
function Set-DscMachineRebootRequired | ||
{ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] | ||
# Suppressing this rule because $global:DSCMachineStatus is used to trigger a reboot. | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')] | ||
<# | ||
Suppressing this rule because $global:DSCMachineStatus is only set, | ||
never used (by design of Desired State Configuration). | ||
#> | ||
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')] | ||
[CmdletBinding()] | ||
param | ||
( | ||
) | ||
|
||
$global:DSCMachineStatus = 1 | ||
} |
39 changes: 39 additions & 0 deletions
39
tests/Integration/Public/Set-DscMachineRebootRequired.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try | ||
{ | ||
Test-ModuleManifest $_.FullName -ErrorAction Stop | ||
} | ||
catch | ||
{ | ||
$false | ||
} ) | ||
}).BaseName | ||
|
||
Import-Module $ProjectName -Force | ||
|
||
Describe 'Set-DscMachineRebootRequired' -Tag 'Set-DscMachineRebootRequired' { | ||
BeforeAll { | ||
$script:currentDSCMachineStatus = $global:DSCMachineStatus | ||
} | ||
|
||
Context 'When setting the DSC reboot status' { | ||
BeforeAll { | ||
$global:DSCMachineStatus = 0 | ||
} | ||
|
||
AfterAll { | ||
if ($script:currentDSCMachineStatus -ne $global:DSCMachineStatus) | ||
{ | ||
$global:DSCMachineStatus = $script:currentDSCMachineStatus | ||
} | ||
} | ||
|
||
It 'Should not throw an error and have set the correct value' { | ||
{ Set-DscMachineRebootRequired } | Should -Not -Throw | ||
|
||
$global:DSCMachineStatus | Should -Be 1 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } ) | ||
}).BaseName | ||
|
||
Import-Module $ProjectName -Force | ||
|
||
Describe 'New-InvalidDataException' { | ||
Context 'When calling with both the ErrorId and ErrorMessage parameter' { | ||
It 'Should throw the correct error' { | ||
$mockErrorId = 'MockedErrorId' | ||
$mockErrorMessage = 'Mocked error' | ||
|
||
$exception = { New-InvalidDataException -ErrorId $mockErrorId -ErrorMessage $mockErrorMessage } | | ||
Should -Throw -PassThru | ||
|
||
$exception.CategoryInfo.Category | Should -Be 'InvalidData' | ||
$exception.FullyQualifiedErrorId | Should -Be $mockErrorId | ||
$exception.Exception.Message | Should -Be $mockErrorMessage | ||
} | ||
} | ||
|
||
Assert-VerifiableMock | ||
} |