Skip to content

Commit

Permalink
Add test to assert dependent module versions (#555)
Browse files Browse the repository at this point in the history
* Added helper function
And test to verify module versions

* Added tests to assert dependant module versions.

* Removed commented code

* Removed whitespace
  • Loading branch information
jcwalker authored and bcwilhite committed Dec 19, 2019
1 parent 84932d3 commit 0807f0e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fixed [#517](https://github.com/microsoft/PowerStig/issues/520): Need a test to verify the module version in the module manifest matches the DscResources.
* Update PowerSTIG parsing for IIS 8.5 STIG - Ver 1, Rel 9 [#530](https://github.com/microsoft/PowerStig/issues/530)
* Fixed [#428](https://github.com/microsoft/PowerStig/issues/428): Updated JRE rule V-66941.a to be a Organizational setting
* Update PowerSTIG parsing for IIS 8.5 STIG - Ver 1, Rel 9 [#530] (https://github.com/microsoft/PowerStig/issues/530)
Expand Down
4 changes: 2 additions & 2 deletions DSCResources/SqlServer/SqlServer.schema.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ configuration SqlServer
$stig.LoadRules($OrgSettings, $Exception, $SkipRule, $SkipRuleType)
##### END DO NOT MODIFY #####

Import-DscResource -ModuleName SqlServerDsc -ModuleVersion '12.1.0.0'
Import-DscResource -ModuleName SqlServerDsc -ModuleVersion 12.1.0.0
. "$resourcePath\SqlServer.ScriptQuery.ps1"

Import-DscResource -ModuleName SecurityPolicyDsc -ModuleVersion '2.4.0.0'
Import-DscResource -ModuleName SecurityPolicyDsc -ModuleVersion 2.4.0.0
. "$resourcePath\Windows.SecurityOption.ps1"

Import-DscResource -ModuleName AccessControlDsc -ModuleVersion 1.4.0.0
Expand Down
20 changes: 20 additions & 0 deletions Tests/Integration/PowerStig.Integration.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$script:moduleName = 'PowerStig'
$script:modulePath = "$($script:moduleRoot)\$($script:moduleName).psd1"
$script:dscCompositePath = Join-Path -Path $script:moduleRoot -ChildPath 'DSCResources'

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'Tools\TestHelper\TestHelper.psm1') -Force
Import-Module $modulePath -Force
Expand All @@ -14,6 +15,25 @@ Describe "$moduleName module" {
(Get-Module -Name $script:modulePath -ListAvailable).ModuleType | Should Be 'Script'
}

$compositeModulePaths = (Get-ChildItem -Path $script:dscCompositePath -Include '*schema.psm1' -Recurse).FullName
$manifestRequiredModules = (Import-PowerShellDataFile -Path $script:modulePath).RequiredModules |
ForEach-Object -Process {[pscustomobject]$PSItem}

foreach ($compositeModule in $compositeModulePaths)
{
$dscModuleInfo = Get-DscResourceModuleInfo -Path $compositeModule
$dscCompositeFile = $compositeModule | Split-Path -Leaf

foreach ($moduleInfo in $dscModuleInfo)
{
$moduleData = $manifestRequiredModules | Where-Object -FilterScript {$PSItem.ModuleName -eq $moduleInfo.ModuleName}

It "Should require the same module listed in the manifest for DscResource $dscCompositeFile Module: $($moduleInfo.ModuleName)" {
$moduleInfo.ModuleVersion | Should -Be $moduleData.ModuleVersion
}
}
}

Context 'Exported Commands' {

$commands = (Get-Command -Module $moduleName).Name
Expand Down
57 changes: 46 additions & 11 deletions Tools/TestHelper/TestHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,52 @@ function Test-AutomatableRuleType
}
}

<#
.SYNOPSIS
Retrieves the DscResource module name and version.
.Path
Specifies the path to the DscResource composite file.
#>
function Get-DscResourceModuleInfo
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string]
$Path
)

$moduleInfo = @()
$modulePattern = "(?<ModuleName>(?<=ModuleName\s)\w+(?=\s))"
$versionPatthern = "(?<ModuleVersion>(?<=ModuleVersion\s)[\d\.]+(?=$))"

$importModuleCommands = Select-String -Path $Path -Pattern 'Import-DscResource' -AllMatches

foreach ($importModuleCommand in $importModuleCommands)
{
$moduleInfo += @{
ModuleName = ($importModuleCommand.Line | Select-String -Pattern $modulePattern).Matches[0].Value
ModuleVersion = ($importModuleCommand.Line | Select-String -Pattern $versionPatthern).Matches[0].Value
}
}

return $moduleInfo
}

Export-ModuleMember -Function @(
'Split-TestStrings',
'Get-StigDataRootPath',
'Test-Xml',
'Get-TestStigRule',
'Get-StigBaseMethods',
'Format-RuleText',
'Get-PowerStigVersionFromManifest',
'Get-StigVersionTable',
'Get-ConfigurationName',
'Get-StigVersionParameterValidateSet',
'Get-ValidStigVersionNumbers',
'Split-TestStrings'
'Get-StigDataRootPath'
'Test-Xml'
'Get-TestStigRule'
'Get-StigBaseMethods'
'Format-RuleText'
'Get-PowerStigVersionFromManifest'
'Get-StigVersionTable'
'Get-ConfigurationName'
'Get-StigVersionParameterValidateSet'
'Get-ValidStigVersionNumbers'
'Test-AutomatableRuleType'
'Get-DscResourceModuleInfo'
)

0 comments on commit 0807f0e

Please sign in to comment.