Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to assert dependent module versions #555

Merged
merged 5 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
)