MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Trace-Command" -tags "Feature" {4 5 Context "Listener options" {6 BeforeAll {7 $logFile = Setup -f traceCommandLog.txt -pass8 $actualLogFile = Setup -f actualTraceCommandLog.txt -pass9 }10 11 AfterEach {12 if ( Test-Path $logfile ) { Remove-Item $logFile }13 if ( Test-Path $actualLogFile ) { Remove-Item $actualLogFile }14 }15 16 It "LogicalOperationStack works" -Pending:($IsCoreCLR) {17 $keyword = "Trace_Command_ListenerOption_LogicalOperationStack_Foo"18 $stack = [System.Diagnostics.Trace]::CorrelationManager.LogicalOperationStack19 $stack.Push($keyword)20 21 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption LogicalOperationStack -FilePath $logfile22 23 $log = Get-Content $logfile | Where-Object {$_ -like "*LogicalOperationStack=$keyword*"}24 $log.Count | Should -BeGreaterThan 025 }26 27 It "Callstack works" -Pending:($IsCoreCLR) {28 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption Callstack -FilePath $logfile29 $log = Get-Content $logfile | Where-Object {$_ -like "*Callstack= * System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)*"}30 $log.Count | Should -BeGreaterThan 031 }32 33 It "Datetime works" {34 $expectedDate = Trace-Command -Name * -Expression {Get-Date} -ListenerOption DateTime -FilePath $logfile35 $log = Get-Content $logfile | Where-Object {$_ -like "*DateTime=*"}36 $results = $log | ForEach-Object {[DateTime]::Parse($_.Split("=")[1])}37 38 ## allow a gap of 6 seconds. All traces should be finished within 6 seconds.39 $allowedGap = [timespan](60 * 1000 * 1000)40 $results | ForEach-Object {41 $actualGap = $_ - $expectedDate;42 if ($expectedDate -gt $_)43 {44 $actualGap = $expectedDate - $_;45 }46 47 $allowedGap | Should -BeGreaterThan $actualGap48 }49 }50 51 It "None options has no effect" {52 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption None -FilePath $actualLogfile53 Trace-Command -Name * -Expression {Write-Output Foo} -FilePath $logfile54 55 Compare-Object (Get-Content $actualLogfile) (Get-Content $logfile) | Should -BeNullOrEmpty56 }57 58 It "ThreadID works" {59 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption ThreadId -FilePath $logfile60 $log = Get-Content $logfile | Where-Object {$_ -like "*ThreadID=*"}61 $results = $log | ForEach-Object {$_.Split("=")[1]}62 63 $results | ForEach-Object { $_ | Should -Be ([threading.thread]::CurrentThread.ManagedThreadId) }64 }65 66 It "Timestamp creates logs in ascending order" {67 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption Timestamp -FilePath $logfile68 $log = Get-Content $logfile | Where-Object {$_ -like "*Timestamp=*"}69 $results = $log | ForEach-Object {$_.Split("=")[1]}70 $sortedResults = $results | Sort-Object71 $sortedResults | Should -Be $results72 }73 74 It "ProcessId logs current process Id" {75 Trace-Command -Name * -Expression {Write-Output Foo} -ListenerOption ProcessId -FilePath $logfile76 $log = Get-Content $logfile | Where-Object {$_ -like "*ProcessID=*"}77 $results = $log | ForEach-Object {$_.Split("=")[1]}78 79 $results | ForEach-Object { $_ | Should -Be $PID }80 }81 }82}83 