PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Export-Counter.Tests.ps1

https://gitlab.com/unofficial-mirrors/PowerShell
Powershell | 229 lines | 193 code | 20 blank | 16 comment | 11 complexity | 198139a6aa7ba2fedcf42f19c00d1339 MD5 | raw file
  1. # Copyright (c) Microsoft Corporation. All rights reserved.
  2. # Licensed under the MIT License.
  3. <############################################################################################
  4. # File: Export-Counter.Tests.ps1
  5. # Provides Pester tests for the Export-Counter cmdlet.
  6. ############################################################################################>
  7. # Counter CmdLets are removed see issue #4272
  8. # Tests are disabled
  9. return
  10. $cmdletName = "Export-Counter"
  11. . "$PSScriptRoot/CounterTestHelperFunctions.ps1"
  12. $rootFilename = "exportedCounters"
  13. $filePath = $null
  14. $counterNames = @(
  15. (TranslateCounterPath "\Memory\Available Bytes")
  16. (TranslateCounterPath "\Processor(*)\% Processor Time")
  17. (TranslateCounterPath "\Processor(_Total)\% Processor Time")
  18. (TranslateCounterPath "\PhysicalDisk(_Total)\Current Disk Queue Length")
  19. (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Bytes/sec")
  20. (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Read Bytes/sec")
  21. )
  22. $counterValues = $null
  23. # Test the results of Export-Counter by importing the exported
  24. # counters and comparing the two sets
  25. function CheckExportResults
  26. {
  27. Test-Path $filePath | Should -BeTrue
  28. $importedCounterValues = Import-Counter $filePath
  29. CompareCounterSets $counterValues $importedCounterValues
  30. }
  31. # Run a test case
  32. function RunTest($testCase)
  33. {
  34. It "$($testCase.Name)" -Skip:$(SkipCounterTests) {
  35. $getCounterParams = ""
  36. if ($testCase.ContainsKey("GetCounterParams"))
  37. {
  38. $getCounterParams = $testCase.GetCounterParams
  39. }
  40. $counterValues = &([ScriptBlock]::Create("Get-Counter -Counter `$counterNames $getCounterParams"))
  41. # build up a command
  42. $filePath = ""
  43. $pathParam = ""
  44. $formatParam = ""
  45. if ($testCase.ContainsKey("Path"))
  46. {
  47. $filePath = $testCase.Path
  48. }
  49. else
  50. {
  51. if ($testCase.ContainsKey("FileFormat"))
  52. {
  53. $formatParam = "-FileFormat $($testCase.FileFormat)"
  54. $filePath = Join-Path $script:outputDirectory "$rootFilename.$($testCase.FileFormat)"
  55. }
  56. else
  57. {
  58. $filePath = Join-Path $script:outputDirectory "$rootFilename.blg"
  59. }
  60. }
  61. if ($testCase.NoDashPath)
  62. {
  63. $pathParam = $filePath
  64. }
  65. else
  66. {
  67. $pathParam = "-Path $filePath"
  68. }
  69. $cmd = "$cmdletName $pathParam $formatParam -InputObject `$counterValues $($testCase.Parameters) -ErrorAction Stop"
  70. # Use $cmd to debug a test failure
  71. # Write-Host "Command to run: $cmd"
  72. if ($testCase.CreateFileFirst)
  73. {
  74. if (-not (Test-Path $filePath))
  75. {
  76. New-Item $filePath -ItemType file
  77. }
  78. }
  79. try
  80. {
  81. if ($testCase.ContainsKey("Script"))
  82. {
  83. # Here we want to run the command then do our own post-run checks
  84. $sb = [ScriptBlock]::Create($cmd)
  85. &$sb
  86. &$testCase.Script
  87. }
  88. else
  89. {
  90. # Here we expect and want the command to fail
  91. $sb = [ScriptBlock]::Create($cmd)
  92. { &$sb } | Should -Throw -ErrorId $testCase.ExpectedErrorId
  93. }
  94. }
  95. finally
  96. {
  97. if ($filePath)
  98. {
  99. Remove-Item $filePath -ErrorAction SilentlyContinue
  100. }
  101. }
  102. }
  103. }
  104. Describe "CI tests for Export-Counter cmdlet" -Tags "CI" {
  105. BeforeAll {
  106. $script:outputDirectory = $testDrive
  107. }
  108. $testCases = @(
  109. @{
  110. Name = "Can export BLG format"
  111. FileFormat = "blg"
  112. GetCounterParams = "-MaxSamples 5"
  113. Script = { CheckExportResults }
  114. }
  115. @{
  116. Name = "Exports BLG format by default"
  117. GetCounterParams = "-MaxSamples 5"
  118. Script = { CheckExportResults }
  119. }
  120. )
  121. foreach ($testCase in $testCases)
  122. {
  123. RunTest $testCase
  124. }
  125. }
  126. Describe "Feature tests for Export-Counter cmdlet" -Tags "Feature" {
  127. BeforeAll {
  128. $script:outputDirectory = $testDrive
  129. }
  130. Context "Validate incorrect parameter usage" {
  131. $testCases = @(
  132. @{
  133. Name = "Fails when given invalid path"
  134. Path = "c:\DAD288C0-72F8-47D3-8C54-C69481B528DF\counterExport.blg"
  135. ExpectedErrorId = "FileCreateFailed,Microsoft.PowerShell.Commands.ExportCounterCommand"
  136. }
  137. @{
  138. Name = "Fails when given null path"
  139. Path = "`$null"
  140. ExpectedErrorId = "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCounterCommand"
  141. }
  142. @{
  143. Name = "Fails when -Path specified but no path given"
  144. Path = ""
  145. ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.ExportCounterCommand"
  146. }
  147. @{
  148. Name = "Fails when given -Circular without -MaxSize"
  149. Parameters = "-Circular"
  150. ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"
  151. }
  152. @{
  153. Name = "Fails when given -Circular with zero -MaxSize"
  154. Parameters = "-Circular -MaxSize 0"
  155. ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"
  156. }
  157. @{
  158. Name = "Fails when -MaxSize < zero"
  159. Parameters = "-MaxSize -2"
  160. ExpectedErrorId = "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCounterCommand"
  161. }
  162. )
  163. foreach ($testCase in $testCases)
  164. {
  165. RunTest $testCase
  166. }
  167. }
  168. Context "Export tests" {
  169. $testCases = @(
  170. @{
  171. Name = "Fails when output file exists"
  172. CreateFileFirst = $true # the output file will be created before the test command runs
  173. ExpectedErrorId = "CounterFileExists,Microsoft.PowerShell.Commands.ExportCounterCommand"
  174. }
  175. @{
  176. Name = "Can force overwriting existing file"
  177. Parameters = "-Force"
  178. Script = { Test-Path $filePath | Should -BeTrue }
  179. }
  180. @{
  181. Name = "Can export BLG format"
  182. FileFormat = "blg"
  183. GetCounterParams = "-MaxSamples 5"
  184. Script = { CheckExportResults }
  185. }
  186. @{
  187. Name = "Exports BLG format by default"
  188. GetCounterParams = "-MaxSamples 5"
  189. Script = { CheckExportResults }
  190. }
  191. @{
  192. Name = "Can export CSV format"
  193. FileFormat = "csv"
  194. GetCounterParams = "-MaxSamples 2"
  195. Script = { CheckExportResults }
  196. }
  197. @{
  198. Name = "Can export TSV format"
  199. FileFormat = "tsv"
  200. GetCounterParams = "-MaxSamples 5"
  201. Script = { CheckExportResults }
  202. }
  203. )
  204. foreach ($testCase in $testCases)
  205. {
  206. RunTest $testCase
  207. }
  208. }
  209. }