Pester tests are like any other script. They grow and evolve
over time. Here are a few more tests that I have testing my DSC resources and
configurations that I recently added to my collection.
Does every resource
have a pester test?
This is probably one of the most important tests I have.
Every resource should have a test, so why not test for that?
describe "DSCResources
located in $PSScriptRoot\DSCResources" {
foreach($Resource in
$ResourceList)
{
context $Resource.name
{
it "Has a pester test" {
($Resource.fullname + "\*.test.ps1") |
should exist
}
If it is a standard
resource, does it have the files it needs?
Each DSC resource needs to have two files in it. A *.psm1
file and a *.schema.mof file. I use the *.psm1 file as a quick way to identify
standard resources differently than a composite resource. I know I will not
ever reach a test condition that would cause once of these to fail, but I left
it in place so I could change the logic later.
if(Test-Path
($Resource.fullname
+ "\$Resource.psm1"))
{
it "Has a $Resource.schema.mof" {
($Resource.fullname
+ "\$Resource.schema.mof")
| should exist
}
it "Has a $Resource.psm1" {
($Resource.fullname
+ "\$Resource.psm1")
| should exist
}
Does it pass
Test-xDscSchema and Test-xDscResource tests?
I may as well test for these as part of my pester tests.
They already validate a lot of things that are easy to overlook.
it "Passes
Test-xDscSchema *.schema.mof" {
Test-xDscSchema
($Resource.fullname
+ "\$Resource.schema.mof")
| should be true
}
it "Passes
Test-xDscResource" {
Test-xDscResource
$Resource.fullname
| should be true
}
If it is a composite resource,
does it have the required files?
A composite resource uses different files than a standard
resource. It has a *.psd1 and a *.shema.psm1 that should exists. I don’t have
any Test-xDSC functions for the composite resources so I add a few extra
checks. I verify that the *.psd1 file references the *.psm1 and that the module
does not throw any errors when dot sourcing it.
else
{
it "Has a $Resource.schema.psm1" {
($Resource.fullname + "\$Resource.schema.psm1") |
should exist
}
it "Has a $Resource.psd1" {
($Resource.fullname + "\$Resource.psd1") |
should exist
}
it "Has a psd1 that loads the schema.psm1"
{
($Resource.fullname + "\$Resource.psd1") |
should contain
"$Resource.schema.psm1"
}
it "dot-sourcing should not throw an error"
{
$path = ($Resource.fullname + "\$Resource.schema.psm1")
{ Invoke-expression
(Get-Content $path
-raw) } | should not throw
}
I hope you find this examples useful. If you want to see more, take a look at part 1.