Private/InvokeOperationValidation.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
function InvokeOperationValidation {
    <#
    .SYNOPSIS
    Invoke the operational tests from modules
 
    .DESCRIPTION
    Modules which include Diagnostics tests are executed via this cmdlet
 
    .PARAMETER testFilePath
    The path to a diagnostic test to execute. By default all discoverable diagnostics will be invoked
 
    .PARAMETER TestInfo
    The type of tests to invoke, this may be either "Simple", "Comprehensive"
    or Both ("Simple,Comprehensive"). "Simple,Comprehensive" is the default.
 
    .EXAMPLE
    PS> Get-OperationValidation -ModuleName OperationValidation | Invoke-OperationValidation -IncludePesterOutput
    Describing Simple Test Suite
    [+] first Operational test 20ms
    [+] second Operational test 19ms
    [+] third Operational test 9ms
    Tests completed in 48ms
    Passed: 3 Failed: 0 Skipped: 0 Pending: 0
    Describing Scenario targeted tests
    Context The RemoteAccess service
        [+] The service is running 37ms
    Context The Firewall Rules
        [+] A rule for TCP port 3389 is enabled 1.19s
        [+] A rule for UDP port 3389 is enabled 11ms
    Tests completed in 1.24s
    Passed: 3 Failed: 0 Skipped: 0 Pending: 0
 
 
    Module: OperationValidation
 
    Result Name
    ------- --------
    Passed Simple Test Suite::first Operational test
    Passed Simple Test Suite::second Operational test
    Passed Simple Test Suite::third Operational test
    Passed Scenario targeted tests:The RemoteAccess service:The service is running
    Passed Scenario targeted tests:The Firewall Rules:A rule for TCP port 3389 is enabled
    Passed Scenario targeted tests:The Firewall Rules:A rule for UDP port 3389 is enabled
 
    .LINK
    Get-OperationValidation
    #>

    #[CmdletBinding(SupportsShouldProcess=$true,DefaultParameterSetName="FileAndTest")]
    [cmdletbinding()]
    param (
        [Parameter(ParameterSetName="Path",ValueFromPipelineByPropertyName=$true)]
        [string[]]$testFilePath,

        [Parameter(ParameterSetName="FileAndTest",ValueFromPipeline=$true)]
        [pscustomobject[]]$TestInfo,

        [Parameter(ParameterSetName="UseGetOperationTest")]
        [string[]]$ModuleName = "*",

        [Parameter(ParameterSetName="UseGetOperationTest")]
        [ValidateSet("Simple","Comprehensive")]
        [string[]]$TestType = @("Simple","Comprehensive"),

        [switch]$IncludePesterOutput,

        [Version]$Version,

        [hashtable]$Overrides
    )

    BEGIN {
        $quiet = ! $IncludePesterOutput
        if ( ! (get-module -Name Pester)) {
            if ( get-module -list Pester ) {
                import-module -Name Pester
            } else {
                Throw "Cannot load Pester module"
            }
        }
    }
    PROCESS {
        if ( $PSCmdlet.ParameterSetName -eq "UseGetOperationTest" ) {
            if ($PSBoundParameters.ContainsKey('Version')) {
                $tests = GetOperationValidation -ModuleName $ModuleName -TestType $TestType -Version $Version
            } else {
                $tests = GetOperationValidation -ModuleName $ModuleName -TestType $TestType
            }
            #$tests | InvokeOperationValidation -IncludePesterOutput:$IncludePesterOutput -Overrides $Overrides
            return
        }

        if ( ($testFilePath -eq $null) -and ($TestInfo -eq $null) ) {
            if ($PSBoundParameters.ContainsKey('Version')) {
                #GetOperationValidation -Version $Version | InvokeOperationValidation -IncludePesterOutput:$IncludePesterOutput -Overrides $Overrides
            } else {
                #GetOperationValidation | InvokeOperationValidation -IncludePesterOutput:$IncludePesterOutput -Overrides $Overrides
            }
            return
        }

        if ( $testInfo -ne $null ) {
            # first check to be sure all of the TestInfos are sane
            foreach($ti in $testinfo) {
                if ( ! ($ti.FilePath -and $ti.Name)) {
                    throw "TestInfo must contain the path and the list of tests"
                }
            }

            #Write-Verbose -Message ("EXECUTING: {0} {1}" -f $ti.FilePath,($ti.Name -join ","))
            foreach($ti in $testinfo) {

                #Write-Verbose "Test name: $tname"

                $pesterParams = @{
                    TestName = $ti.Name
                    Quiet = $quiet
                    PassThru = $true
                    Verbose = $false
                }

                if ($ti.ScriptParameters) {
                    Write-Verbose -Message "Test has script parameters"  
                    if ($PSBoundParameters.ContainsKey('Overrides')) {
                        Write-Verbose -Message "Overriding with parameters:`n$($Overrides | Format-List -Property * | Out-String)"
                        $pesterParams.Script = @{
                            Path = $ti.FilePath
                            Parameters = $Overrides
                        }
                    } else {
                        Write-Verbose -Message 'Using default parameters for test'
                        $pesterParams.Path = $ti.FilePath
                    }
                } else {
                    $pesterParams.Path = $ti.FilePath
                }

                $testResult = Invoke-Pester @pesterParams
                if ($testResult) {
                    Add-member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $ti.FilePath
                    Convert-TestResult $testResult
                }
            }
            return
        }

        # foreach($test in $testFilePath) {
        # write-progress -Activity "Invoking tests in $test"
        # if ( $PSCmdlet.ShouldProcess($test)) {
        # $testResult = Invoke-Pester $test -passthru -quiet:$quiet
        # Add-Member -InputObject $testResult -MemberType NoteProperty -Name Path -Value $test
        # Convert-TestResult $testResult
        # }
        # }
    }
}