Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Export-Counter.Tests.ps1230 linesDownload Raw Back to Microsoft.PowerShell.Diagnostics
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3<############################################################################################4 # File: Export-Counter.Tests.ps15 # Provides Pester tests for the Export-Counter cmdlet.6 ############################################################################################>7 8 # Counter CmdLets are removed see issue #42729 # Tests are disabled10 return11 12$cmdletName = "Export-Counter"13 14. "$PSScriptRoot/CounterTestHelperFunctions.ps1"15 16$rootFilename = "exportedCounters"17$filePath = $null18$counterNames = @(19    (TranslateCounterPath "\Memory\Available Bytes")20    (TranslateCounterPath "\Processor(*)\% Processor Time")21    (TranslateCounterPath "\Processor(_Total)\% Processor Time")22    (TranslateCounterPath "\PhysicalDisk(_Total)\Current Disk Queue Length")23    (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Bytes/sec")24    (TranslateCounterPath "\PhysicalDisk(_Total)\Disk Read Bytes/sec")25)26$counterValues = $null27 28# Test the results of Export-Counter by importing the exported29# counters and comparing the two sets30function CheckExportResults31{32    Test-Path $filePath | Should -BeTrue33    $importedCounterValues = Import-Counter $filePath34 35    CompareCounterSets $counterValues $importedCounterValues36}37 38# Run a test case39function RunTest($testCase)40{41    It "$($testCase.Name)" -Skip:$(SkipCounterTests) {42        $getCounterParams = ""43        if ($testCase.ContainsKey("GetCounterParams"))44        {45            $getCounterParams = $testCase.GetCounterParams46        }47        $counterValues = &([ScriptBlock]::Create("Get-Counter -Counter `$counterNames $getCounterParams"))48 49        # build up a command50        $filePath = ""51        $pathParam = ""52        $formatParam = ""53        if ($testCase.ContainsKey("Path"))54        {55            $filePath = $testCase.Path56        }57        else58        {59            if ($testCase.ContainsKey("FileFormat"))60            {61                $formatParam = "-FileFormat $($testCase.FileFormat)"62                $filePath = Join-Path $script:outputDirectory "$rootFilename.$($testCase.FileFormat)"63            }64            else65            {66                $filePath = Join-Path $script:outputDirectory "$rootFilename.blg"67            }68        }69        if ($testCase.NoDashPath)70        {71            $pathParam = $filePath72        }73        else74        {75            $pathParam = "-Path $filePath"76        }77        $cmd = "$cmdletName $pathParam $formatParam -InputObject `$counterValues $($testCase.Parameters) -ErrorAction Stop"78        # Use $cmd to debug a test failure79        # Write-Host "Command to run: $cmd"80 81        if ($testCase.CreateFileFirst)82        {83            if (-not (Test-Path $filePath))84            {85                New-Item $filePath -ItemType file86            }87        }88 89        try90        {91            if ($testCase.ContainsKey("Script"))92            {93                # Here we want to run the command then do our own post-run checks94                $sb = [ScriptBlock]::Create($cmd)95                &$sb96                &$testCase.Script97            }98            else99            {100                # Here we expect and want the command to fail101                $sb = [ScriptBlock]::Create($cmd)102                { &$sb } | Should -Throw -ErrorId $testCase.ExpectedErrorId103            }104        }105        finally106        {107            if ($filePath)108            {109                Remove-Item $filePath -ErrorAction SilentlyContinue110            }111        }112    }113}114 115Describe "CI tests for Export-Counter cmdlet" -Tags "CI" {116 117    BeforeAll {118        $script:outputDirectory = $testDrive119    }120 121    $testCases = @(122        @{123            Name = "Can export BLG format"124            FileFormat = "blg"125            GetCounterParams = "-MaxSamples 5"126            Script = { CheckExportResults }127        }128        @{129            Name = "Exports BLG format by default"130            GetCounterParams = "-MaxSamples 5"131            Script = { CheckExportResults }132        }133    )134 135    foreach ($testCase in $testCases)136    {137        RunTest $testCase138    }139}140 141Describe "Feature tests for Export-Counter cmdlet" -Tags "Feature" {142 143    BeforeAll {144        $script:outputDirectory = $testDrive145    }146 147    Context "Validate incorrect parameter usage" {148        $testCases = @(149            @{150                Name = "Fails when given invalid path"151                Path = "c:\DAD288C0-72F8-47D3-8C54-C69481B528DF\counterExport.blg"152                ExpectedErrorId = "FileCreateFailed,Microsoft.PowerShell.Commands.ExportCounterCommand"153            }154            @{155                Name = "Fails when given null path"156                Path = "`$null"157                ExpectedErrorId = "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCounterCommand"158            }159            @{160                Name = "Fails when -Path specified but no path given"161                Path = ""162                ExpectedErrorId = "MissingArgument,Microsoft.PowerShell.Commands.ExportCounterCommand"163            }164            @{165                Name = "Fails when given -Circular without -MaxSize"166                Parameters = "-Circular"167                ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"168            }169            @{170                Name = "Fails when given -Circular with zero -MaxSize"171                Parameters = "-Circular -MaxSize 0"172                ExpectedErrorId = "CounterCircularNoMaxSize,Microsoft.PowerShell.Commands.ExportCounterCommand"173            }174            @{175                Name = "Fails when -MaxSize < zero"176                Parameters = "-MaxSize -2"177                ExpectedErrorId = "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCounterCommand"178            }179        )180 181        foreach ($testCase in $testCases)182        {183            RunTest $testCase184        }185    }186 187    Context "Export tests" {188        $testCases = @(189            @{190                Name = "Fails when output file exists"191                CreateFileFirst = $true     # the output file will be created before the test command runs192                ExpectedErrorId = "CounterFileExists,Microsoft.PowerShell.Commands.ExportCounterCommand"193            }194            @{195                Name = "Can force overwriting existing file"196                Parameters = "-Force"197                Script = { Test-Path $filePath | Should -BeTrue }198            }199            @{200                Name = "Can export BLG format"201                FileFormat = "blg"202                GetCounterParams = "-MaxSamples 5"203                Script = { CheckExportResults }204            }205            @{206                Name = "Exports BLG format by default"207                GetCounterParams = "-MaxSamples 5"208                Script = { CheckExportResults }209            }210            @{211                Name = "Can export CSV format"212                FileFormat = "csv"213                GetCounterParams = "-MaxSamples 2"214                Script = { CheckExportResults }215            }216            @{217                Name = "Can export TSV format"218                FileFormat = "tsv"219                GetCounterParams = "-MaxSamples 5"220                Script = { CheckExportResults }221            }222        )223 224        foreach ($testCase in $testCases)225        {226            RunTest $testCase227        }228    }229}230