Public/Invoke-WatchmenTest.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
function Invoke-WatchmenTest {
    [cmdletbinding(DefaultParameterSetName = 'File')]
    param(
        [parameter(ParameterSetName = 'File', Mandatory, ValueFromPipeline)]
        [ValidateScript({
            Test-Path -Path $_
        })]
        [string[]]$Path,

        [parameter(ParameterSetName = 'File')]
        [switch]$Recurse,

        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'Watchmen.Test' })]
        [parameter(ParameterSetName = 'InputObject', Mandatory, ValueFromPipeline)]
        [pscustomobject[]]$InputObject,

        [switch]$IncludePesterOutput,

        [switch]$PassThru,

        [switch]$DisableNotifiers
    )

    begin {
        Write-Debug -Message "Entering: $($PSCmdlet.MyInvocation.MyCommand.Name)"
        $tests = @()

        $finalResults = @()

        if ($PSBoundParameters.ContainsKey('DisableNotifiers')) {
            Write-Verbose -Message '[DisableNotifiers] set. No notifiers will be executed.' 
        }
    }

    process {

        # Get the watchmen tests from file if a path to a test script or folder was passed in
        if ($PSCmdlet.ParameterSetName -eq 'File') {
            foreach ($script in $Path) {
                $tests += Get-WatchmenTest -Path $script -Recurse:$Recurse
            }
        } else {
            $tests = $InputObject
        }

        foreach ($test in $tests) {

            # Resolve the OVF test info and install module if needed
            $ovfTestInfo = Get-OvfTestInfo -Test $test

            if ($ovfTestInfo) {

                # Optionally filter the tests by name
                if ($test.Test) {
                    Write-Debug "Filtering tests for [$($Test.Test)]"
                    $filtered = $ovfTestInfo | Where-Object Name -like $test.Test
                }

                # Execute the OVF test
                $testResults = $filtered | Invoke-OvfTest -Test $test
                $finalResults += $testResults

                foreach ($result in $testResults) {
                    if ($result.Result -eq 'Failed') {
                        Write-Warning -Message "Failed: $($result.Name)"
                    } else {
                        Write-Verbose -Message "Passed: $($result.Name)"
                    }
                }

                # Call notifiers on unless told not to
                if (-not $PSBoundParameters.ContainsKey('DisableNotifiers')) {                    
                    Invoke-WatchmenNotifier -TestResults $testResults -WatchmenTest $test                
                } else {
                    Write-Verbose ''
                }

                # TODO
                # If we have a Rorschach endpoint defined, send the results to it

                if ($PSBoundParameters.ContainsKey('PassThru')) {
                    $testResults
                }
            }
        }
    }

    end {
        $pass = @($finalResults | Where-Object Result -eq 'Passed').Count
        $fail = @($finalResults | Where-Object Result -eq 'Failed').Count
        Write-Verbose -Message "Test results: Passed [$Pass] -- Failed [$fail]"
        Write-Debug -Message "Exiting: $($PSCmdlet.MyInvocation.MyCommand.Name)"
    }
}