File size: 7,418 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
<############################################################################################
 # File: Export-Counter.Tests.ps1
 # Provides Pester tests for the Export-Counter cmdlet.
 ############################################################################################>

 # Counter CmdLets are removed see issue #4272
 # Tests are disabled
 return

$cmdletName = "Export-Counter"

. "$PSScriptRoot/CounterTestHelperFunctions.ps1"

$rootFilename = "exportedCounters"
$filePath = $null
$counterNames = @(
    (TranslateCounterPath "\Memory\Available Bytes")
    (TranslateCounterPath "\Processor(*)\% Processor Time")
    (TranslateCounterPath "\Processor(_Total)\% Processor Time")
    (TranslateCounterPath "\PhysicalDisk(_Total)\Current Disk Queue Length")
    (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Bytes/sec")
    (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Read Bytes/sec")
)
$counterValues = $null

# Test the results of Export-Counter by importing the exported
# counters and comparing the two sets
function CheckExportResults
{
    Test-Path $filePath | Should -BeTrue
    $importedCounterValues = Import-Counter $filePath

    CompareCounterSets $counterValues $importedCounterValues
}

# Run a test case
function RunTest($testCase)
{
    It "$($testCase.Name)" -Skip:$(SkipCounterTests) {
        $getCounterParams = ""
        if ($testCase.ContainsKey("GetCounterParams"))
        {
            $getCounterParams = $testCase.GetCounterParams
        }
        $counterValues = &([ScriptBlock]::Create("Get-Counter -Counter `$counterNames $getCounterParams"))

        # build up a command
        $filePath = ""
        $pathParam = ""
        $formatParam = ""
        if ($testCase.ContainsKey("Path"))
        {
            $filePath = $testCase.Path
        }
        else
        {
            if ($testCase.ContainsKey("FileFormat"))
            {
                $formatParam = "-FileFormat $($testCase.FileFormat)"
                $filePath = Join-Path $script:outputDirectory "$rootFilename.$($testCase.FileFormat)"
            }
            else
            {
                $filePath = Join-Path $script:outputDirectory "$rootFilename.blg"
            }
        }
        if ($testCase.NoDashPath)
        {
            $pathParam = $filePath
        }
        else
        {
            $pathParam = "-Path $filePath"
        }
        $cmd = "$cmdletName $pathParam $formatParam -InputObject `$counterValues $($testCase.Parameters) -ErrorAction Stop"
        # Use $cmd to debug a test failure
        # Write-Host "Command to run: $cmd"

        if ($testCase.CreateFileFirst)
        {
            if (-not (Test-Path $filePath))
            {
                New-Item $filePath -ItemType file
            }
        }

        try
        {
            if ($testCase.ContainsKey("Script"))
            {
                # Here we want to run the command then do our own post-run checks
                $sb = [ScriptBlock]::Create($cmd)
                &$sb
                &$testCase.Script
            }
            else
            {
                # Here we expect and want the command to fail
                $sb = [ScriptBlock]::Create($cmd)
                { &$sb } | Should -Throw -ErrorId $testCase.ExpectedErrorId
            }
        }
        finally
        {
            if ($filePath)
            {
                Remove-Item $filePath -ErrorAction SilentlyContinue
            }
        }
    }
}

Describe "CI tests for Export-Counter cmdlet" -Tags "CI" {

    BeforeAll {
        $script:outputDirectory = $testDrive
    }

    $testCases = @(
        @{
            Name = "Can export BLG format"
            FileFormat = "blg"
            GetCounterParams = "-MaxSamples 5"
            Script = { CheckExportResults }
        }
        @{
            Name = "Exports BLG format by default"
            GetCounterParams = "-MaxSamples 5"
            Script = { CheckExportResults }
        }
    )

    foreach ($testCase in $testCases)
    {
        RunTest $testCase
    }
}

Describe "Feature tests for Export-Counter cmdlet" -Tags "Feature" {

    BeforeAll {
        $script:outputDirectory = $testDrive
    }

    Context "Validate incorrect parameter usage" {
        $testCases = @(
            @{
                Name = "Fails when given invalid path"
                Path = "c:\DAD288C0-72F8-47D3-8C54-C69481B528DF\counterExport.blg"
                ExpectedErrorId = "FileCreateFailed,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Fails when given null path"
                Path = "`$null"
                ExpectedErrorId = "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Fails when -Path specified but no path given"
                Path = ""
                ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Fails when given -Circular without -MaxSize"
                Parameters = "-Circular"
                ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Fails when given -Circular with zero -MaxSize"
                Parameters = "-Circular -MaxSize 0"
                ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Fails when -MaxSize < zero"
                Parameters = "-MaxSize -2"
                ExpectedErrorId = "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
        )

        foreach ($testCase in $testCases)
        {
            RunTest $testCase
        }
    }

    Context "Export tests" {
        $testCases = @(
            @{
                Name = "Fails when output file exists"
                CreateFileFirst = $true     # the output file will be created before the test command runs
                ExpectedErrorId = "CounterFileExists,Microsoft.PowerShell.Commands.ExportCounterCommand"
            }
            @{
                Name = "Can force overwriting existing file"
                Parameters = "-Force"
                Script = { Test-Path $filePath | Should -BeTrue }
            }
            @{
                Name = "Can export BLG format"
                FileFormat = "blg"
                GetCounterParams = "-MaxSamples 5"
                Script = { CheckExportResults }
            }
            @{
                Name = "Exports BLG format by default"
                GetCounterParams = "-MaxSamples 5"
                Script = { CheckExportResults }
            }
            @{
                Name = "Can export CSV format"
                FileFormat = "csv"
                GetCounterParams = "-MaxSamples 2"
                Script = { CheckExportResults }
            }
            @{
                Name = "Can export TSV format"
                FileFormat = "tsv"
                GetCounterParams = "-MaxSamples 5"
                Script = { CheckExportResults }
            }
        )

        foreach ($testCase in $testCases)
        {
            RunTest $testCase
        }
    }
}