Testing custom DSC Resources

There are a few methods available to you to test the custom DSC Resources you have created, whether they are PowerShell v4- or v5-based.

Using xDscResourceDesigner

We covered using xDscResourceDesigner in the previous section on creating module-based DSC Resources, but automating DSC Resource creation is not the only trick up this module's sleeve. The two Cmdlets, Test-xDscResource and Test-xDscSchema, provided by this module aid in testing your custom DSC Resources.

Test-xDscResource and Test-xDscSchema will check your custom DSC Resource to make sure that the data types are the same between your DSC MOF schema file and your DSC PowerShell script Get, Set, and Test-TargetResource functions, that the mandatory parameters are the same in all three functions, and that any parameters that are marked as Mandatory in the scripts are set to Required in the schema file.

You use Test-xDscSchema by providing it with a schema file to test:

 [PS]> Import-Module $env:ProgramFilesWindowsPowerShellModulesxDscResourceDesigner
[PS]> test-xdscschema "xWebAdministrationDSCResourcesMSFT_xWebsiteMSFT_xWebsite.schema.mof"
True

You use Test-xDscResource by passing it the path of the DSC Resource you want to test:

[PS]> import-module $env:ProgramFilesWindowsPowerShellModulesxDscResourceDesigner
[PS]> Test-xDscResource -Name 'xWebAdministrationDSCR
esourcesMSFT_xWebsite'
True

In both Test-xDscSchema and Test-xDscResource, we had to provide exact paths to look for the DSC Resource we were testing. It was not enough to just provide a path to the PowerShell module holding the DSC Resources.

Pester

Pester is the name of an open source project that has become the de facto standard in testing PowerShell code, so much so that it is a Microsoft-sponsored project that is being included in Windows 10. We can use Pester to write repeatable tests that run against our DSC Resources as we develop them.

Pester and testing in general are topics larger than we can go into great detail within this book, so if you need a starting point or a good refresher, the Pester wiki page (https://github.com/pester/Pester/wiki) is the perfect place to start.

Here is an example Pester script that tests ExampleResource we have been using in our examples using the Cmdlets from the xDscResourceDesigner module. This script will test our DSC Resources and output any errors as we develop them:

Import-Module $PSScriptRootDSCResourcesxExampleResourcexExampleResource.psm1

Describe 'ExampleDscResource Passes Standard DSC Resource Tests' {
    It 'should be syntactically correct' {
        $res = Test-xDscSchema -Path $PSScriptRootDSCResourcesxExampleResource

xExampleResource.schema.mof
        $res | Should Be $true
    }

    It 'should be a well formed resource' {
        $res = Test-xDscResource -Name xExampleResource
        $res | Should Be $true
    }
}

Invoking a Pester run is done by importing the Pester module and executing the Invoke-Pester Cmdlet like so:

[PS]> Invoke-Pester '$env:ProgramFilesWindowsPowerShellModulesxExampleResourcexExampleResource.tests.ps1'
Describing ExampleDscResource Passes Standard DSC Resource Tests
 [+] should be syntactically correct 147ms
 [+] should be a well formed resource 751ms
Tests completed in 898ms
Passed: 2 Failed: 0 Skipped: 0 Pending: 0

You can also skip using the Invoke-Pester cmdlet and just invoke the PowerShell script file directly if you are using the latest Pester release.

Microsoft recommendations

On the PowerShell blog: (http://blogs.msdn.com/b/powershell/archive/2014/11/18/powershell-dsc-resource-design-and-testing-checklist.aspx), Microsoft published a list of recommended steps to test your custom DSC Resources. This is a very long list of suggestions that you should peruse and adapt to your testing process.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset