Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 68ed14d

Browse files
committed
Add Version and Override parameters
Add the ability to specify a specific version of an OVF module to execute as well as the ability to override script parameters defined in Pester tests.
1 parent 7f2acc1 commit 68ed14d

File tree

1 file changed

+147
-50
lines changed

1 file changed

+147
-50
lines changed

Modules/OperationValidation/OperationValidation.psm1

Lines changed: 147 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ function New-OperationValidationResult
3939
}
4040
function new-OperationValidationInfo
4141
{
42-
param (
42+
param (
4343
[Parameter(Mandatory=$true)][string]$File,
4444
[Parameter(Mandatory=$true)][string]$FilePath,
4545
[Parameter(Mandatory=$true)][string[]]$Name,
4646
[Parameter()][string[]]$TestCases,
4747
[Parameter(Mandatory=$true)][ValidateSet("None","Simple","Comprehensive")][string]$Type,
48-
[Parameter()][string]$modulename
48+
[Parameter()][string]$modulename,
49+
[Parameter()][hashtable]$Parameters
4950
)
5051
$o = [pscustomobject]@{
5152
File = $File
@@ -54,6 +55,7 @@ function new-OperationValidationInfo
5455
TestCases = $testCases
5556
Type = $type
5657
ModuleName = $modulename
58+
ScriptParameters = $Parameters
5759
}
5860
$o.psobject.Typenames.Insert(0,"OperationValidationInfo")
5961
$ToString = { return ("{0} ({1}): {2}" -f $this.testFile, $this.Type, ($this.TestCases -join ",")) }
@@ -70,13 +72,13 @@ function Get-TestFromScript
7072
write-verbose -Message $scriptPath
7173

7274
for($i = 0; $i -lt $tok.count; $i++) {
73-
if ( $tok[$i].type -eq "Command" -and $tok[$i].content -eq "Describe" )
75+
if ( $tok[$i].type -eq "Command" -and $tok[$i].content -eq "Describe" )
7476
{
7577
$i++
7678
if ( $tok[$i].Type -eq "String" ) { $tok[$i].Content }
7779
else
7880
{
79-
# ok - we didn't get the describe text first,
81+
# ok - we didn't get the describe text first,
8082
# we likely saw a "-Tags" statement, so that means that
8183
# the describe text will immediately preceed the scriptblock
8284
while($tok[$i].Type -ne "GroupStart")
@@ -95,20 +97,20 @@ function Get-TestFromScript
9597
Retrieve the operational tests from modules
9698
9799
.DESCRIPTION
98-
Modules which include a Diagnostics directory are inspected for
100+
Modules which include a Diagnostics directory are inspected for
99101
Pester tests in either the "Simple" or "Comprehensive" directories.
100102
If files are found in those directories, they will be inspected to determine
101-
whether they are Pester tests. If Pester tests are found, the
103+
whether they are Pester tests. If Pester tests are found, the
102104
test names in those files will be returned.
103105
104106
The module structure required is as follows:
105107
106108
ModuleBase\
107109
Diagnostics\
108-
Simple # simple tests are held in this location
110+
Simple # simple tests are held in this location
109111
(e.g., ping, serviceendpoint checks)
110112
Comprehensive # comprehensive scenario tests should be placed here
111-
113+
112114
.PARAMETER ModuleName
113115
By default this is * which will retrieve all modules in $env:psmodulepath
114116
Additional module directories may be added. If you wish to check both
@@ -146,7 +148,8 @@ function Get-OperationValidation
146148
[CmdletBinding()]
147149
param (
148150
[Parameter(Position=0)][string[]]$ModuleName = "*",
149-
[Parameter()][ValidateSet("Simple","Comprehensive")][string[]]$TestType = @("Simple","Comprehensive")
151+
[Parameter()][ValidateSet("Simple","Comprehensive")][string[]]$TestType = @("Simple","Comprehensive"),
152+
[Parameter()][Version]$Version
150153
)
151154

152155
BEGIN
@@ -197,9 +200,12 @@ param (
197200
}
198201
}
199202
}
200-
function Get-ModuleList
203+
function Get-ModuleList
201204
{
202-
param ( [string[]]$Name )
205+
param (
206+
[string[]]$Name,
207+
[version]$Version
208+
)
203209
foreach($p in $env:psmodulepath.split(";"))
204210
{
205211
if ( test-path -path $p )
@@ -213,17 +219,42 @@ param (
213219
# now determine if there's a diagnostics directory, or a version
214220
if ( test-path -path ($modDir.FullName + "\Diagnostics"))
215221
{
216-
$modDir.FullName
217-
break
222+
# Did we specify a specific version to find?
223+
if ($PSBoundParameters.ContainsKey('Version'))
224+
{
225+
$manifestFile = Get-ChildItem -Path $modDir.FullName -Filter "$modDir.psd1" | Select-Object -First 1
226+
$manifest = Test-ModuleManifest -Path $manifestFile.FullName
227+
if ($manifest.Version -eq $Version)
228+
{
229+
$modDir.FullName
230+
break
231+
}
232+
}
233+
else
234+
{
235+
$modDir.FullName
236+
break
237+
}
238+
}
239+
240+
# Get latest version if no specific version specified
241+
if ($PSBoundParameters.ContainsKey('Version'))
242+
{
243+
$versionDirectories = Get-Childitem -path $modDir.FullName -dir |
244+
where-object { $_.name -as [version] -and $_.Name -eq $Version }
218245
}
219-
$versionDirectories = Get-Childitem -path $modDir.FullName -dir |
220-
where-object { $_.name -as [version] }
246+
else
247+
{
248+
$versionDirectories = Get-Childitem -path $modDir.FullName -dir |
249+
where-object { $_.name -as [version] }
250+
}
251+
221252
$potentialDiagnostics = $versionDirectories | where-object {
222253
test-path ($_.fullname + "\Diagnostics")
223254
}
224255
# now select the most recent module path which has diagnostics
225-
$DiagnosticDir = $potentialDiagnostics |
226-
sort-object {$_.name -as [version]} |
256+
$DiagnosticDir = $potentialDiagnostics |
257+
sort-object {$_.name -as [version]} |
227258
Select-Object -Last 1
228259
if ( $DiagnosticDir )
229260
{
@@ -240,28 +271,48 @@ param (
240271
PROCESS
241272
{
242273
Write-Progress -Activity "Inspecting Modules" -Status " "
243-
$moduleCollection = Get-ModuleList -Name $ModuleName
244-
$count = 1;
274+
if ($PSBoundParameters.ContainsKey('Version'))
275+
{
276+
$moduleCollection = Get-ModuleList -Name $ModuleName -Version $Version
277+
}
278+
else
279+
{
280+
$moduleCollection = Get-ModuleList -Name $ModuleName
281+
}
282+
283+
$count = 1;
245284
$moduleCount = @($moduleCollection).Count
246285
foreach($module in $moduleCollection)
247286
{
248287
Write-Progress -Activity ("Searching for Diagnostics in " + $module) -PercentComplete ($count++/$moduleCount*100) -status " "
249-
$diagnosticsDir=$module + "\Diagnostics"
288+
$diagnosticsDir=$module + "\Diagnostics"
250289
if ( test-path -path $diagnosticsDir )
251290
{
252291
foreach($dir in $testType)
253292
{
254293
$testDir = "$diagnosticsDir\$dir"
255294
write-verbose -Message "SPECIFIC TEST: $testDir"
256-
if ( ! (test-path -path $testDir) )
295+
if ( ! (test-path -path $testDir) )
257296
{
258297
continue
259298
}
260299
foreach($file in get-childitem -path $testDir -filter *.tests.ps1)
261300
{
262301
Write-Verbose -Message $file.fullname
263-
$testName = Get-TestFromScript -scriptPath $file.FullName
264-
new-OperationValidationInfo -FilePath $file.Fullname -File $file.Name -Type $dir -Name $testName -ModuleName $Module
302+
303+
# Pull out parameters to Pester script if they exist
304+
$script = Get-Command -Name $file.fullname
305+
$parameters = $script.Parameters
306+
if ($parameters.Keys.Count -gt 0)
307+
{
308+
Write-Debug -Message 'Test script has overrideable parameters'
309+
Write-Debug -Message "`n$($parameters.Keys | Out-String)"
310+
}
311+
312+
$testNames = @(Get-TestFromScript -scriptPath $file.FullName)
313+
foreach ($testName in $testNames) {
314+
New-OperationValidationInfo -FilePath $file.Fullname -File $file.Name -Type $dir -Name $testName -ModuleName $Module -Parameters $parameters
315+
}
265316
}
266317
}
267318
}
@@ -324,7 +375,12 @@ function Invoke-OperationValidation
324375
[Parameter(ParameterSetName="UseGetOperationTest")][string[]]$ModuleName = "*",
325376
[Parameter(ParameterSetName="UseGetOperationTest")]
326377
[ValidateSet("Simple","Comprehensive")][string[]]$TestType = @("Simple","Comprehensive"),
327-
[Parameter()][switch]$IncludePesterOutput
378+
[Parameter()][switch]$IncludePesterOutput,
379+
[Parameter(ParameterSetName="UseGetOperationTest")]
380+
[Parameter()][Version]$Version,
381+
[Parameter(ParameterSetName="FileAndTest")]
382+
[Parameter(ParameterSetName="UseGetOperationTest")]
383+
[Parameter()][hashtable]$Overrides
328384
)
329385
BEGIN
330386
{
@@ -340,25 +396,22 @@ function Invoke-OperationValidation
340396
Throw "Cannot load Pester module"
341397
}
342398
}
343-
# $resultCollection = @()
344399
}
345400
PROCESS
346401
{
347402
if ( $PSCmdlet.ParameterSetName -eq "UseGetOperationTest" )
348403
{
349-
$tests = Get-OperationValidation -ModuleName $ModuleName -TestType $TestType
350-
$tests | Invoke-OperationValidation -IncludePesterOutput:$IncludePesterOutput
351-
return
352-
}
353-
354-
if ( ($testFilePath -eq $null) -and ($TestInfo -eq $null) )
355-
{
356-
Get-OperationValidation | Invoke-OperationValidation -IncludePesterOutput:$IncludePesterOutput
357-
return
404+
if ($PSBoundParameters.ContainsKey('Version'))
405+
{
406+
$TestInfo = Get-OperationValidation -ModuleName $ModuleName -TestType $TestType -Version $Version
407+
}
408+
else
409+
{
410+
$TestInfo = Get-OperationValidation -ModuleName $ModuleName -TestType $TestType
411+
}
358412
}
359413

360-
361-
if ( $testInfo -ne $null )
414+
if ( $null -ne $testInfo )
362415
{
363416
# first check to be sure all of the TestInfos are sane
364417
foreach($ti in $testinfo)
@@ -368,29 +421,73 @@ function Invoke-OperationValidation
368421
throw "TestInfo must contain the path and the list of tests"
369422
}
370423
}
371-
372-
write-verbose -Message ("EXECUTING: {0} {1}" -f $ti.FilePath,($ti.Name -join ","))
373-
foreach($tname in $ti.Name)
424+
425+
# first check to be sure all of the TestInfos are sane
426+
foreach($ti in $testinfo)
374427
{
375-
$testResult = Invoke-pester -Path $ti.FilePath -TestName $tName -quiet:$quiet -PassThru
376-
Add-member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $ti.FilePath
377-
Convert-TestResult $testResult
428+
if ( ! ($ti.FilePath -and $ti.Name))
429+
{
430+
throw "TestInfo must contain the path and the list of tests"
431+
}
432+
}
433+
434+
Write-Verbose -Message ("EXECUTING: {0} {1}" -f $ti.FilePath,($ti.Name -join ","))
435+
foreach($ti in $testinfo)
436+
{
437+
$pesterParams = @{
438+
TestName = $ti.Name
439+
Quiet = $quiet
440+
PassThru = $true
441+
Verbose = $false
442+
}
443+
444+
if ($ti.ScriptParameters)
445+
{
446+
Write-Verbose -Message 'Test has script parameters'
447+
if ($PSBoundParameters.ContainsKey('Overrides'))
448+
{
449+
Write-Verbose -Message "Overriding with parameters:`n$($Overrides | Format-List -Property * | Out-String)"
450+
$pesterParams.Script = @{
451+
Path = $ti.FilePath
452+
Parameters = $Overrides
453+
}
454+
}
455+
else
456+
{
457+
Write-Verbose -Message 'Using default parameters for test'
458+
$pesterParams.Path = $ti.FilePath
459+
}
460+
}
461+
else
462+
{
463+
$pesterParams.Path = $ti.FilePath
464+
}
465+
466+
if ( $PSCmdlet.ShouldProcess("$($ti.Name) [$($ti.FilePath)]"))
467+
{
468+
$testResult = Invoke-Pester @pesterParams
469+
if ($testResult)
470+
{
471+
Add-member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $ti.FilePath
472+
Convert-TestResult $testResult
473+
}
474+
}
378475
}
379476
return
380477
}
381478

382-
foreach($test in $testFilePath)
479+
if ($testFilePath)
383480
{
384-
write-progress -Activity "Invoking tests in $test"
385-
if ( $PSCmdlet.ShouldProcess($test))
386-
{
387-
$testResult = Invoke-Pester $test -passthru -quiet:$quiet
388-
Add-Member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $test
389-
Convert-TestResult $testResult
481+
foreach($filePath in $testFilePath) {
482+
write-progress -Activity "Invoking tests in $filePath"
483+
if ( $PSCmdlet.ShouldProcess($filePath)) {
484+
$testResult = Invoke-Pester $filePath -passthru -quiet:$quiet
485+
Add-Member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $filePath
486+
Convert-TestResult $testResult
487+
}
390488
}
391489
}
392490
}
393-
394491
}
395492

396493
# emit an object which can be used in reporting

0 commit comments

Comments
 (0)