Public/Get-OperationValidation.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
function Get-OperationValidation { <# .SYNOPSIS Retrieve the operational tests from modules .DESCRIPTION Modules which include a Diagnostics directory are inspected for Pester tests in either the "Simple" or "Comprehensive" subdirectories. If files are found in those directories, they will be inspected to determine whether they are Pester tests. If Pester tests are found, the test names in those files will be returned. The module structure required is as follows: ModuleBase\ Diagnostics\ Simple # simple tests are held in this location (e.g., ping, serviceendpoint checks) Comprehensive # comprehensive scenario tests should be placed here .PARAMETER Name One or more module names to inspect and return if they adhere to the OVF Pester test structure. By default this is [*] which will inspect all modules in $env:PSModulePath. .PARAMETER Path One or more paths to search for OVF modules in. This bypasses searching the directories contained in $env:PSModulePath. .PARAMETER LiteralPath One or more literal paths to search for OVF modules in. This bypasses searching the directories contained in $env:PSModulePath. Unlike the Path parameter, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences. .PARAMETER TestType The type of tests to retrieve, this may be either "Simple", "Comprehensive", or Both ("Simple,Comprehensive"). "Simple, Comprehensive" is the default. .PARAMETER Version The version of the module to retrieve. If not specified, the latest version of the module will be retured. .PARAMETER Tag Executes tests with specified tag parameter values. Wildcard characters and tag values that include spaces or whitespace characters are not supported. When you specify multiple tag values, Get-OperationValidation executes tests that have any of the listed tags. If you use both Tag and ExcludeTag, ExcludeTag takes precedence. .PARAMETER ExcludeTag Omits tests with the specified tag parameter values. Wildcard characters and tag values that include spaces or whitespace characters are not supported. When you specify multiple ExcludeTag values, Get-OperationValidation omits tests that have any of the listed tags. If you use both Tag and ExcludeTag, ExcludeTag takes precedence. .EXAMPLE PS> Get-OperationValidation -Name OVF.Windows.Server Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2 Version: 1.0.2 Type: Simple Tags: {} File: LogicalDisk.tests.ps1 FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\LogicalDisk.tests.ps1 Name: Logical Disks Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2 Version: 1.0.2 Type: Simple Tags: {} File: Memory.tests.ps1 FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Memory.tests.ps1 Name: Memory Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2 Version: 1.0.2 Type: Simple Tags: {} File: Network.tests.ps1 FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Network.tests.ps1 Name: Network Adapters Module: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2 Version: 1.0.2 Type: Simple Tags: {} File: Services.tests.ps1 FilePath: C:\Program Files\WindowsPowerShell\Modules\OVF.Windows.Server\1.0.2\Diagnostics\Simple\Services.tests.ps1 Name: Operating System .EXAMPLE PS> $tests = Get-OperationValidation Search in all modules found in $env:PSModulePath for OVF tests. .EXAMPLE PS> $tests = Get-OperationValidation -Path C:\MyTests Search for OVF modules under c:\MyTests .EXAMPLE PS> $simpleTests = Get-OperationValidation -ModuleName OVF.Windows.Server -TypeType Simple Get just the simple tests in the OVF.Windows.Server module. .EXAMPLE $tests = Get-OperationValidation -ModuleName OVF.Windows.Server -Version 1.0.2 Get all the tests from version 1.0.2 of the OVF.Windows.Server module. .EXAMPLE $storageTests = Get-OperationValidation -Tag Storage Search in all modules for OVF tests that include the tag Storage. .EXAMPLE $tests = Get-OperationValidation -ExcludeTag memory Search for OVF tests that don't include the tag Memory .LINK Invoke-OperationValidation #> [CmdletBinding(DefaultParameterSetName = 'ModuleName')] param ( [Parameter( ParameterSetName = 'ModuleName', Position = 0 )] [Alias('ModuleName')] [string[]]$Name = '*', [parameter( Mandatory, ParameterSetName = 'Path', Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName )] [ValidateNotNullOrEmpty()] [SupportsWildcards()] [string[]]$Path, [parameter( Mandatory, ParameterSetName = 'LiteralPath', Position = 0, ValueFromPipelineByPropertyName )] [ValidateNotNullOrEmpty()] [Alias('PSPath')] [string[]]$LiteralPath, # [Parameter(ParameterSetName = 'ModuleName')] # [Parameter(ParameterSetName = 'Path')] # [Parameter(ParameterSetName = 'LiteralPath')] [ValidateSet('Simple', 'Comprehensive')] [string[]]$TestType = @('Simple', 'Comprehensive'), [Version]$Version, [string[]]$Tag, [string[]]$ExcludeTag ) PROCESS { Write-Progress -Activity 'Inspecting Modules' -Status ' ' # Resolve module list either by module name, path, or literalpath $modListParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ModuleName' { $modListParams.Name = $Name break } 'Path' { $paths = Resolve-Path -Path $Path | Select-Object -ExpandProperty Path $modListParams.Path = $paths } 'LiteralPath' { $paths = Resolve-Path -LiteralPath $LiteralPath | Select-Object -ExpandProperty Path $modListParams.Path = $paths } } if ($PSBoundParameters.ContainsKey('Version')) { $modListParams.Version = $Version $moduleCollection = @(Get-ModuleList -Name $Name -Version $Version) } $moduleCollection = @(Get-ModuleList @modListParams) $count = 1 $moduleCount = $moduleCollection.Count Write-Debug -Message "Found [$moduleCount] OVF modules" foreach($modulePath in $moduleCollection) { Write-Progress -Activity ("Searching for Diagnostics in $modulePath") -PercentComplete ($count++/$moduleCount*100) -status ' ' $diagnosticsDir = Join-Path -Path $modulePath -ChildPath 'Diagnostics' # Get the module manifest so we can pull out the version $modName = Split-Path -Path $modulePath -Leaf $manifestFile = Get-ChildItem -Path $modulePath -Filter "$($modName).psd1" if (-not $manifestFile) { if ("$modName" -as [version]) { # We are in a "version" directory so get the actual module name from the parent directory $parent = Split-Path -Path (Split-Path -Path $modulePath -Parent) -Leaf $manifestFile = Get-ChildItem -Path $modulePath -Filter "$($parent).psd1" } } # Some OVF modules might not have a manifest (.psd1) file. if ($manifestFile) { if ($PSVersionTable.PSVersion.Major -ge 5) { $manifest = Import-PowerShellDataFile -Path $manifestFile.FullName } else { $manifest = Parse-Psd1 $manifestFile.FullName } } else { $manifest = $null } if (Test-Path -Path $diagnosticsDir) { foreach($dir in $testType) { $testDir = Join-Path -Path $diagnosticsDir -ChildPath $dir if (-not (Test-Path -Path $testDir)) { continue } foreach($file in (Get-ChildItem -Path $testDir | Where-Object {$_.Name -like '*.tests.ps1'})) { # Pull out parameters to Pester script if they exist $script = Get-Command -Name $file.fullname $parameters = $script.Parameters if ($parameters.Keys.Count -gt 0) { Write-Debug -Message 'Test script has overrideable parameters' Write-Debug -Message "`n$($parameters.Keys | Out-String)" } $tests = @(Get-TestFromScript -ScriptPath $file.FullName) foreach ($test in $tests) { # Only return tests that match the tag filter(s) if ($Tag -and @(Compare-Object -ReferenceObject $Tag -DifferenceObject $test.Tags -IncludeEqual -ExcludeDifferent).count -eq 0) { continue } if ($ExcludeTag -and @(Compare-Object -ReferenceObject $ExcludeTag -DifferenceObject $test.Tags -IncludeEqual -ExcludeDifferent).count -gt 0) { continue } $modInfoParams = @{ FilePath = $file.Fullname File = $file.Name Type = $dir Name = $test.Name ModuleName = $modulePath Tags = $test.Tags Version = if ($manifest.ModuleVersion) { [version]$manifest.ModuleVersion } else { $null } Parameters = $parameters } New-OperationValidationInfo @modInfoParams } } } } } } } |