MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4 $cmdletName = "Get-Counter"5 6. "$PSScriptRoot/CounterTestHelperFunctions.ps1"7 8$badName = "bad-name-DAD288C0-72F8-47D3-8C54-C69481B528DF"9$counterPaths = @{10 MemoryBytes = TranslateCounterPath "\Memory\Available Bytes"11 TotalDiskRead = TranslateCounterPath "\PhysicalDisk(_Total)\Disk Read Bytes/sec"12 Unknown = TranslateCounterPath "\Memory\$badName"13 Bad = $badName14}15 16$nonEnglishCulture = (-not (Get-Culture).Name.StartsWith("en-", [StringComparison]::InvariantCultureIgnoreCase))17 18function ValidateParameters($testCase)19{20 It "$($testCase.Name)" -Skip:$(SkipCounterTests) {21 22 # build up a command23 $counterParam = ""24 if ($testCase.ContainsKey("Counters"))25 {26 $counterParam = "-Counter `"$($testCase.Counters)`""27 }28 $cmd = "$cmdletName $counterParam $($testCase.Parameters) -ErrorAction Stop"29 # Use $cmd to debug a test failure30 # Write-Host "Command to run: $cmd"31 32 $sb = [scriptblock]::Create($cmd)33 { &$sb } | Should -Throw -ErrorId $testCase.ExpectedErrorId34 }35}36 37Describe "CI Tests for Get-Counter cmdlet" -Tags "CI" {38 39 It "Get-Counter with no parameters returns data for a default set of counters" -Skip:$(SkipCounterTests) {40 $counterData = Get-Counter41 # At the very least we should get processor and memory42 $counterData.CounterSamples.Length | Should -BeGreaterThan 143 }44 45 It "Can retrieve the specified counter" -Skip:$(SkipCounterTests) {46 $counterPath = $counterPaths.MemoryBytes47 $counterData = Get-Counter -Counter $counterPath48 $counterData.Length | Should -Be 149 $retrievedPath = RemoveMachineName $counterData[0].CounterSamples[0].Path50 [string]::Compare($retrievedPath, $counterPath, $true) | Should -Be 051 }52}53 54Describe "Feature tests for Get-Counter cmdlet" -Tags "Feature" {55 56 Context "Validate incorrect parameter usage" {57 $parameterTestCases = @(58 @{59 Name = "Fails when MaxSamples parameter is < 1"60 Counters = $counterPaths.MemoryBytes61 Parameters = "-MaxSamples 0"62 ExpectedErrorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCounterCommand"63 }64 @{65 Name = "Fails when MaxSamples parameter is used but no value given"66 Counters = $counterPaths.MemoryBytes67 Parameters = "-MaxSamples"68 ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.GetCounterCommand"69 }70 @{71 Name = "Fails when SampleInterval is < 1"72 Counters = $counterPaths.MemoryBytes73 Parameters = "-SampleInterval -2"74 ExpectedErrorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCounterCommand"75 }76 @{77 Name = "Fails when SampleInterval parameter is used but no value given"78 Counters = $counterPaths.MemoryBytes79 Parameters = "-SampleInterval"80 ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.GetCounterCommand"81 }82 @{83 Name = "Fails when given invalid counter path"84 Counters = $counterPaths.Bad85 ExpectedErrorId = "CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand"86 }87 @{88 Name = "Fails when given unknown counter path"89 Counters = $counterPaths.Unknown90 ExpectedErrorId = "CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand"91 }92 @{93 Name = "Fails when Counter parameter is null"94 Counters = "`$null"95 ExpectedErrorId = "CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand"96 }97 @{98 Name = "Fails when Counter parameter is specified but no names given"99 Parameters = "-Counter"100 ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.GetCounterCommand"101 }102 @{103 Name = "Fails when given invalid counter path in array"104 Counters = "@($($counterPaths.MemoryBytes), $($counterPaths.Bad), $($counterPaths.TotalDiskRead))"105 ExpectedErrorId = "CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand"106 }107 @{108 Name = "Fails when ComputerName parameter is invalid"109 Counters = $counterPaths.MemoryBytes110 Parameters = "-ComputerName $badName"111 ExpectedErrorId = "CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand"112 }113 @{114 Name = "Fails when ComputerName parameter is null"115 Counters = $counterPaths.MemoryBytes116 Parameters = "-ComputerName `$null"117 ExpectedErrorId = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCounterCommand"118 }119 @{120 Name = "Fails when ComputerName parameter is used but no name given"121 Counters = $counterPaths.MemoryBytes122 Parameters = "-ComputerName"123 ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.GetCounterCommand"124 }125 @{126 Name = "Fails when given unknown counter set name"127 Parameters = "-ListSet $badName"128 ExpectedErrorId = "NoMatchingCounterSetsFound,Microsoft.PowerShell.Commands.GetCounterCommand"129 }130 @{131 Name = "Fails when given unknown counter set name in array"132 Parameters = "-List @(`"Memory`", `"Processor`", `"$badname`")"133 ExpectedErrorId = "NoMatchingCounterSetsFound,Microsoft.PowerShell.Commands.GetCounterCommand"134 }135 @{136 Name = "Fails when ListSet parameter is null"137 Parameters = "-List `$null"138 ExpectedErrorId = "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetCounterCommand"139 }140 @{141 Name = "Fails when ListSet parameter is used but no name given"142 Parameters = "-ListSet"143 ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.GetCounterCommand"144 }145 @{146 Name = "Fails when both -Counter and -ListSet parameters are given"147 Counters = $counterPaths.MemoryBytes148 Parameters = "-ListSet `"Memory`""149 ExpectedErrorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.GetCounterCommand"150 }151 )152 153 foreach ($testCase in $parameterTestCases)154 {155 ValidateParameters($testCase)156 }157 }158 159 Context "Get-Counter CounterSet tests" {160 161 It "Can retrieve the specified number of counter samples" -Skip:$(SkipCounterTests) {162 $counterPath = $counterPaths.MemoryBytes163 $counterCount = 5164 $counterData = Get-Counter -Counter $counterPath -MaxSamples $counterCount165 $counterData.Length | Should -Be $counterCount166 }167 168 It "Can specify the sample interval" -Skip:$(SkipCounterTests) {169 $counterPath = TranslateCounterPath "\PhysicalDisk(*)\Current Disk Queue Length"170 $counterCount = 5171 $sampleInterval = 2172 $startTime = Get-Date173 $counterData = Get-Counter -Counter $counterPath -SampleInterval $sampleInterval -MaxSamples $counterCount174 $endTime = Get-Date175 $counterData.Length | Should -Be $counterCount176 ($endTime - $startTime).TotalSeconds -as [int] | Should -Not -BeLessThan ($counterCount * $sampleInterval)177 }178 179 It "Can process array of counter names" -Skip:$(SkipCounterTests) {180 $counterPaths = @((TranslateCounterPath "\PhysicalDisk(_Total)\Disk Read Bytes/sec"),181 (TranslateCounterPath "\Memory\Available bytes"))182 $counterData = Get-Counter -Counter $counterPaths183 $counterData.CounterSamples.Length | Should -Be $counterPaths.Length184 }185 }186 187 Context "Get-Counter ListSet tests" {188 It "Can retrieve specified counter set" -Skip:$(SkipCounterTests) {189 $counterSetName = "Memory"190 $counterSet = Get-Counter -ListSet $counterSetName191 $counterSet.Length | Should -Be 1192 $counterSet.CounterSetName | Should -BeExactly $counterSetName193 }194 195 It "Can process an array of counter set names" -Skip:$(SkipCounterTests) {196 $counterSetNames = @("Memory", "Processor")197 $counterSets = Get-Counter -ListSet $counterSetNames198 $counterSets.Length | Should -Be 2199 $counterSets[0].CounterSetName | Should -BeExactly $counterSetNames[0]200 $counterSets[1].CounterSetName | Should -BeExactly $counterSetNames[1]201 }202 203 # This test will be skipped for non-English languages, since204 # there is no reasonable way to construct a wild-card pattern205 # that will, for every language, result in a known set of values206 # or evan a set with a known minimum number of items.207 It "Can process counter set name with wildcards" -Skip:$($nonEnglishCulture -or (SkipCounterTests)) {208 $wildcardBase = "roc"209 $counterSetName = "*$wildcardBase*"210 $counterSets = Get-Counter -ListSet $counterSetName211 $counterSets.Length | Should -BeGreaterThan 1 # should get at least "Processor" and "Process"212 foreach ($counterSet in $counterSets)213 {214 $counterSet.CounterSetName.ToLower().Contains($wildcardBase.ToLower()) | Should -BeTrue215 }216 }217 218 # This test will be skipped for non-English languages, since219 # there is no reasonable way to construct a wild-card pattern220 # that will, for every language, result in a known set of values221 # or evan a set with a known minimum number of items.222 It "Can process counter set name with wildcards in array" -Skip:$($nonEnglishCulture -or (SkipCounterTests)) {223 $wildcardBases = @("Memory", "roc")224 $counterSetNames = @($wildcardBases[0], ("*" + $wildcardBases[1] + "*"))225 $counterSets = Get-Counter -ListSet $counterSetNames226 $counterSets.Length | Should -BeGreaterThan 2 # should get at least "Memory", "Processor" and "Process"227 foreach ($counterSet in $counterSets)228 {229 ($counterSet.CounterSetName.ToLower().Contains($wildcardBases[0].ToLower()) -Or230 $counterSet.CounterSetName.ToLower().Contains($wildcardBases[1].ToLower())) | Should -BeTrue231 }232 }233 }234}235 236Describe "Get-Counter cmdlet does not run on IoT" -Tags "CI" {237 238 It "Get-Counter throws PlatformNotSupportedException" -Skip:$(-Not [System.Management.Automation.Platform]::IsIoT) {239 { Get-Counter } | Should -Throw -ErrorId "System.PlatformNotSupportedException,Microsoft.PowerShell.Commands.GetCounterCommand"240 }241}242 