forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-gtest.ps1
930 lines (806 loc) · 30.8 KB
/
run-gtest.ps1
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
<#
.SYNOPSIS
This script runs a google test executable and collects logs or dumps
as necessary.
.PARAMETER Path
The path to the test executable.
.PARAMETER Kernel
Runs for Windows kernel mode, given the path for binaries.
.PARAMETER Filter
A filter to include test cases from the list to execute. Multiple filters
are separated by :. Negative filters are prefixed with -.
.PARAMETER ListTestCases
Lists all the test cases.
.PARAMETER IsolationMode
Controls the isolation mode when running each test case.
.PARAMETER KeepOutputOnSuccess
Don't discard console output or logs on success.
.PARAMETER GenerateXmlResults
Generates an xml Test report for the run.
.PARAMETER Debugger
Attaches the debugger to the process.
.PARAMETER InitialBreak
Debugger starts broken into the process to allow setting breakpoints, etc.
.PARAMETER BreakOnFailure
Triggers a break point on a test failure.
.PARAMETER LogProfile
The name of the profile to use for log collection.
.PARAMETER CompressOutput
Compresses the output files generated for failed test cases.
.PARAMETER NoProgress
Disables the progress bar.
.Parameter EnableAppVerifier
Enables all basic Application Verifier checks on the test binary.
.Parameter EnableSystemVerifier
Enables TCPIP verifier in user mode tests.
.Parameter CodeCoverage
Collects code coverage for this test run. Incompatible with -Debugger.
.Parameter AZP
Runs in Azure Pipelines mode.
.Parameter ErrorsAsWarnings
Treats all errors as warnings.
.PARAMETER DuoNic
Uses DuoNic instead of loopback.
#>
param (
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory = $false)]
[string]$Kernel = "",
[Parameter(Mandatory = $false)]
[string]$Filter = "",
[Parameter(Mandatory = $false)]
[switch]$ListTestCases = $false,
[Parameter(Mandatory = $false)]
[ValidateSet("Batch", "Isolated")]
[string]$IsolationMode = "Isolated",
[Parameter(Mandatory = $false)]
[switch]$KeepOutputOnSuccess = $false,
[Parameter(Mandatory = $false)]
[switch]$GenerateXmlResults = $false,
[Parameter(Mandatory = $false)]
[switch]$Debugger = $false,
[Parameter(Mandatory = $false)]
[switch]$InitialBreak = $false,
[Parameter(Mandatory = $false)]
[switch]$BreakOnFailure = $false,
[Parameter(Mandatory = $false)]
[ValidateSet("None", "Basic.Light", "Basic.Verbose", "Full.Light", "Full.Verbose")]
[string]$LogProfile = "None",
[Parameter(Mandatory = $false)]
[switch]$CompressOutput = $false,
[Parameter(Mandatory = $false)]
[switch]$NoProgress = $false,
[Parameter(Mandatory = $false)]
[switch]$EnableAppVerifier = $false,
[Parameter(Mandatory = $false)]
[switch]$EnableSystemVerifier = $false,
[Parameter(Mandatory = $false)]
[switch]$CodeCoverage = $false,
[Parameter(Mandatory = $false)]
[String]$PfxPath = "",
[Parameter(Mandatory = $false)]
[switch]$AZP = $false,
[Parameter(Mandatory = $false)]
[switch]$GHA = $false,
[Parameter(Mandatory = $false)]
[switch]$ErrorsAsWarnings = $false,
[Parameter(Mandatory = $false)]
[string]$ExtraArtifactDir = "",
[Parameter(Mandatory = $false)]
[switch]$DuoNic = $false,
[Parameter(Mandatory = $false)]
[string]$OsRunner = "",
[Parameter(Mandatory = $false)]
[switch]$UseQtip = $false
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
function Test-Administrator
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function Log($msg) {
Write-Host "[$(Get-Date)] $msg"
}
function LogWrn($msg) {
if ($AZP -and !$ErrorsAsWarnings) {
Write-Host "##vso[task.LogIssue type=warning;][$(Get-Date)] $msg"
} elseif ($GHA -and !$ErrorsAsWarnings) {
Write-Host "::warning::[$(Get-Date)] $msg"
} else {
Write-Warning "[$(Get-Date)] $msg"
}
}
function LogErr($msg) {
if ($AZP -and !$ErrorsAsWarnings) {
Write-Host "##vso[task.LogIssue type=error;][$(Get-Date)] $msg"
} elseif ($GHA -and !$ErrorsAsWarnings) {
Write-Host "::error::[$(Get-Date)] $msg"
} else {
Write-Warning "[$(Get-Date)] $msg"
}
}
function LogFatal($msg) {
if ($AZP -and !$ErrorsAsWarnings) {
Write-Error "##vso[task.LogIssue type=error;][$(Get-Date)] $msg"
} elseif ($GHA -and !$ErrorsAsWarnings) {
Write-Error "::error::[$(Get-Date)] $msg"
} else {
Write-Error "[$(Get-Date)] $msg"
}
}
# Make sure the test executable is present.
if (!(Test-Path $Path)) {
Write-Error "$($Path) does not exist!"
}
# Validate the the kernel switch.
if ($Kernel -ne "" -and !$IsWindows) {
Write-Error "-Kernel switch only supported on Windows";
}
# Validate the code coverage switch
if ($CodeCoverage) {
if (!$IsWindows) {
Write-Error "-CodeCoverage switch only supported on Windows";
}
if ($Debugger) {
Write-Error "-CodeCoverage switch is not supported with debugging";
}
if ($Kernel -ne "") {
Write-Error "-CodeCoverage is not supported for kernel mode tests";
}
if (!(Test-Path "C:\Program Files\OpenCppCoverage\OpenCppCoverage.exe")) {
Write-Error "Code coverage tools are not installed";
}
}
# Root directory of the project.
$RootDir = Split-Path $PSScriptRoot -Parent
# Script for controlling loggings.
$LogScript = Join-Path $RootDir "scripts" "log.ps1"
# Executable name.
$TestExeName = Split-Path $Path -Leaf
$CoverageName = "$(Split-Path $Path -LeafBase).cov"
$ExeLogFolder = $TestExeName
if (![string]::IsNullOrWhiteSpace($ExtraArtifactDir)) {
$ExeLogFolder += "_$ExtraArtifactDir"
}
# Folder for log files.
$LogDir = Join-Path $RootDir "artifacts" "logs" $ExeLogFolder (Get-Date -UFormat "%m.%d.%Y.%T").Replace(':','.')
New-Item -Path $LogDir -ItemType Directory -Force | Out-Null
# Folder for coverage files
$CoverageDir = $null
if ($CodeCoverage) {
$CoverageDir = Join-Path $RootDir "artifacts" "coverage"
New-Item -Path $CoverageDir -ItemType Directory -Force | Out-Null
}
# The file path of the final XML results.
$FinalResultsPath = "$($LogDir)-results.xml"
# Base XML results data.
$XmlResults = [xml]@"
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" failures="0" disabled="0" errors="0" time="0" timestamp="date" name="AllTests">
</testsuites>
"@
$XmlResults.testsuites.timestamp = Get-Date -UFormat "%Y-%m-%dT%T"
# XML for creating new (failure) result data.
$FailXmlText = @"
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="1" failures="1" disabled="0" errors="0" time="0" name="AllTests">
<testsuite name="TestSuiteName" tests="1" failures="1" disabled="0" errors="0" timestamp="date" time="0" >
<testcase name="TestCaseName" status="run" result="completed" time="0" timestamp="date" classname="TestSuiteName">
<failure message="Application Crashed" type=""><![CDATA[Application Crashed]]></failure>
</testcase>
</testsuite>
</testsuites>
"@
# Global state for tracking if any crashes occurred.
$global:CrashedProcessCount = 0
# Path to the WER registry key used for collecting dumps.
$WerDumpRegPath = "HKLM:\Software\Microsoft\Windows\Windows Error Reporting\LocalDumps\$TestExeName"
# Helper script to build up a combined XML file for all test cases. This
# function just appends the given test case's xml output to the existing xml
# file. If not present (because of a crash) it generates one instead and appends
# it.
function Add-XmlResults($TestCase) {
$TestHasResults = Test-Path $TestCase.ResultsPath
$TestSuiteName = $TestCase.Name.Split(".")[0]
$TestCaseName = $TestCase.Name.Split(".")[1]
$NewXmlResults = $null
if ($TestHasResults) {
# Get the results from the test output.
$NewXmlResults = [xml](Get-Content $TestCase.ResultsPath)
Remove-Item $TestCase.ResultsPath -Force | Out-Null
} else {
# Generate our own results xml.
$NewXmlText = $FailXmlText.Replace("TestSuiteName", $TestSuiteName)
$NewXmlText = $NewXmlText.Replace("TestCaseName", $TestCaseName)
$NewXmlText = $NewXmlText.Replace("date", $TestCase.Timestamp)
$NewXmlResults = [xml]($NewXmlText)
}
$IsFailure = $NewXmlResults.testsuites.failures -eq 1
$Time = $NewXmlResults.testsuites.testsuite.testcase.time -as [Decimal]
$Node = $null
if ($XmlResults.testsuites.tests -ne 0) {
# Look for a matching test suite that might already exist.
$Node = $XmlResults.testsuites.testsuite | Where-Object { $_.Name -eq $TestSuiteName }
}
if ($null -ne $Node) {
# Already has a matching test suite. Add the test case to it.
$Node.tests = ($Node.tests -as [Int]) + 1
if ($IsFailure) {
$Node.failures = ($Node.failures -as [Int]) + 1
}
$Node.time = ($Node.time -as [Decimal]) + $Time
$NewNode = $XmlResults.ImportNode($NewXmlResults.testsuites.testsuite.testcase, $true)
$Node.AppendChild($NewNode) | Out-Null
} else {
# First instance of this test suite. Add the test suite.
$NewNode = $XmlResults.ImportNode($NewXmlResults.testsuites.testsuite, $true)
$XmlResults.testsuites.AppendChild($NewNode) | Out-Null
}
# Update the top level test and failure counts.
$XmlResults.testsuites.tests = ($XmlResults.testsuites.tests -as [Int]) + 1
if ($IsFailure) {
$XmlResults.testsuites.failures = ($XmlResults.testsuites.failures -as [Int]) + 1
}
$XmlResults.testsuites.time = ($XmlResults.testsuites.time -as [Decimal]) + $Time
}
# Asynchronously starts the test executable with the given arguments.
function Start-TestExecutable([String]$Arguments, [String]$OutputDir) {
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
if ($IsWindows) {
if ($Debugger) {
if (Get-Command "windbgx.exe" -ErrorAction SilentlyContinue) {
$pinfo.FileName = "windbgx.exe"
} else {
$pinfo.FileName = "windbg.exe"
}
if ($InitialBreak) {
$pinfo.Arguments = "-G $($Path) $($Arguments)"
} else {
$pinfo.Arguments = "-g -G $($Path) $($Arguments)"
}
} elseif ($CodeCoverage) {
$CoverageOutput = Join-Path $OutputDir $CoverageName
$pinfo.FileName = "C:\Program Files\OpenCppCoverage\OpenCppCoverage.exe"
$pinfo.Arguments = "--modules=$(Split-Path $Path -Parent) --cover_children --sources src\core --excluded_sources unittest --working_dir $($OutputDir) --export_type binary:$($CoverageOutput) -- $($Path) $($Arguments)"
$pinfo.WorkingDirectory = $OutputDir
} else {
$pinfo.FileName = $Path
$pinfo.Arguments = $Arguments
if (Test-Administrator) {
# Enable WER dump collection.
New-ItemProperty -Path $WerDumpRegPath -Name DumpType -PropertyType DWord -Value 2 -Force | Out-Null
New-ItemProperty -Path $WerDumpRegPath -Name DumpFolder -PropertyType ExpandString -Value $OutputDir -Force | Out-Null
}
}
} else {
if ($Debugger) {
$pinfo.FileName = "gdb"
if ($InitialBreak) {
$pinfo.Arguments = "--args $($Path) $($Arguments)"
} else {
$pinfo.Arguments = "-ex=r --args $($Path) $($Arguments)"
}
} else {
$pinfo.FileName = "bash"
$pinfo.Arguments = "-c `"ulimit -c unlimited && LSAN_OPTIONS=report_objects=1 ASAN_OPTIONS=disable_coredump=0:abort_on_error=1 UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 $($Path) $($Arguments) && echo Done`""
$pinfo.WorkingDirectory = $OutputDir
}
}
if (!$Debugger) {
$pinfo.RedirectStandardOutput = $true
$pinfo.RedirectStandardError = $true
}
$pinfo.UseShellExecute = $false
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p
}
# Asynchronously starts a single msquictest test case running.
function Start-TestCase([String]$Name) {
$InstanceName = $Name.Replace("/", "_")
$LocalLogDir = Join-Path $LogDir $InstanceName
mkdir $LocalLogDir | Out-Null
if ($LogProfile -ne "None") {
# Start the logs
& $LogScript -Start -Profile $LogProfile | Out-Null
}
# Build up the argument list.
$ResultsPath = Join-Path $LocalLogDir "results.xml"
$Arguments = "--gtest_catch_exceptions=0 --gtest_filter=$($Name) --gtest_output=xml:$($ResultsPath) --timeout 60000"
if ($BreakOnFailure) {
$Arguments += " --gtest_break_on_failure"
}
if ($Kernel -ne "") {
$Arguments += " --kernelPriv"
}
if ($DuoNic) {
$Arguments += " --duoNic"
}
if ($UseQtip) {
$Arguments += " --useQTIP"
}
if ("" -ne $OsRunner) {
$Arguments += " --osRunner=$OsRunner"
}
if ($PfxPath -ne "") {
$Arguments += " -PfxPath:$PfxPath"
}
# Start the test process and return some information about the test case.
[pscustomobject]@{
Name = $Name
InstanceName = $InstanceName
LogDir = $LocalLogDir
StartTime = Get-Date
Timestamp = (Get-Date -UFormat "%Y-%m-%dT%T")
ResultsPath = $ResultsPath
Process = (Start-TestExecutable $Arguments $LocalLogDir)
}
}
# Asynchronously start all the msquictest test cases running.
function Start-AllTestCases {
$Name = "all"
$InstanceName = $Name
if ($LogProfile -ne "None") {
# Start the logs
& $LogScript -Start -Profile $LogProfile | Out-Null
}
# Build up the argument list.
$Arguments = "--gtest_catch_exceptions=0 --gtest_output=xml:$($FinalResultsPath)"
if ($Filter -ne "") {
$Arguments += " --gtest_filter=$($Filter)"
}
if ($BreakOnFailure) {
$Arguments += " --gtest_break_on_failure"
}
if ($Kernel -ne "") {
$Arguments += " --kernelPriv"
}
if ($DuoNic) {
$Arguments += " --duoNic"
}
if ($UseQtip) {
$Arguments += " --useQTIP"
}
if ("" -ne $OsRunner) {
$Arguments += " --osRunner=$OsRunner"
}
if ($PfxPath -ne "") {
$Arguments += " -PfxPath:$PfxPath"
}
# Start the test process and return some information about the test case.
[pscustomobject]@{
Name = $Name
InstanceName = $InstanceName
LogDir = $LogDir
StartTime = Get-Date
Timestamp = (Get-Date -UFormat "%Y-%m-%dT%T")
ResultsPath = $FinalResultsPath
Process = (Start-TestExecutable $Arguments $LogDir)
}
}
# Uses CDB.exe to print the crashing callstack in the dump file.
function PrintDumpCallStack($DumpFile) {
$env:_NT_SYMBOL_PATH = Split-Path $Path
try {
if ($env:BUILD_BUILDNUMBER -ne $null) {
$env:PATH += ";c:\Program Files (x86)\Windows Kits\10\Debuggers\x64"
}
$Output = cdb.exe -z $File -c "kn;q" | Join-String -Separator "`n"
$Output = ($Output | Select-String -Pattern " # Child-SP(?s).*quit:").Matches[0].Groups[0].Value
Write-Host "=================================================================================="
Write-Host " $(Split-Path $DumpFile -Leaf)"
Write-Host "=================================================================================="
$Output -replace "quit:", "=================================================================================="
$Output | Out-File "$DumpFile.txt"
} catch {
# Silently fail
}
}
function PrintLldbCoreCallStack($CoreFile) {
try {
$Output = lldb $Path -c $CoreFile -b -o "`"bt all`""
Write-Host "=================================================================================="
Write-Host " $(Split-Path $CoreFile -Leaf)"
Write-Host "=================================================================================="
# Find line containing Current thread
$Found = $false
$LastThreadStart = 0
for ($i = 0; $i -lt $Output.Length; $i++) {
if ($Output[$i] -like "*stop reason =*") {
if ($Found) {
break
}
$LastThreadStart = $i
}
if ($Output[$i] -like "*quic_bugcheck*") {
$Found = $true
for ($j = $LastThreadStart; $j -lt $i; $j++) {
$Output[$j]
}
}
if ($Found) {
$Output[$i]
}
}
if (!$Found) {
$Output | Join-String -Separator "`n"
}
$Output | Join-String -Separator "`n" | Out-File "$CoreFile.txt"
} catch {
# Silently Fail
}
}
function PrintGdbCoreCallStack($CoreFile) {
try {
$Output = gdb $Path $CoreFile -batch -ex "`"bt`"" -ex "`"quit`""
Write-Host "=================================================================================="
Write-Host " $(Split-Path $CoreFile -Leaf)"
Write-Host "=================================================================================="
# Find line containing Current thread
$Found = $false
for ($i = 0; $i -lt $Output.Length; $i++) {
if ($Output[$i] -like "*Current thread*") {
$Found = $true
}
if ($Found) {
$Output[$i]
}
}
if (!$Found) {
$Output | Join-String -Separator "`n"
}
$Output | Join-String -Separator "`n" | Out-File "$CoreFile.txt"
} catch {
# Silently Fail
}
}
# Waits for the executable to finish and processes the results.
function Wait-TestCase($TestCase) {
$ProcessCrashed = $false
$AnyTestFailed = $false
$StdOut = $null
$StdOutTxt = $null
$StdError = $null
$StdErrorTxt = $null
$IsReadingStreams = $false
try {
if (!$Debugger) {
$IsReadingStreams = $true
$StdOut = $TestCase.Process.StandardOutput.ReadToEndAsync()
$StdError = $TestCase.Process.StandardError.ReadToEndAsync()
}
$TestCase.Process.WaitForExit()
if ($TestCase.Process.ExitCode -ne 0) {
Log "Process had nonzero exit code: $($TestCase.Process.ExitCode)"
$ProcessCrashed = $true
}
if ($IsReadingStreams) {
[System.Threading.Tasks.Task]::WaitAll(@($StdOut, $StdError))
$StdOutTxt = $StdOut.Result
$StdErrorTxt = $StdError.Result
if (!$IsWindows -and !$ProcessCrashed) {
$ProcessCrashed = $StdErrorTxt.Contains("Aborted")
}
$AnyTestFailed = $StdOutTxt.Contains("[ FAILED ]")
if (!(Test-Path $TestCase.ResultsPath) -and !$ProcessCrashed) {
LogWrn "No test results generated! Treating as crash!"
$ProcessCrashed = $true
}
}
$DumpFiles = (Get-ChildItem $TestCase.LogDir) | Where-Object { $_.Extension -eq ".dmp" }
if ($DumpFiles) {
LogWrn "Dump file(s) generated"
foreach ($File in $DumpFiles) {
PrintDumpCallStack($File)
}
$ProcessCrashed = $true
}
$CoreFiles = (Get-ChildItem $TestCase.LogDir) | Where-Object { $_.Extension -eq ".core" }
if ($CoreFiles) {
LogWrn "Core file(s) generated"
foreach ($File in $CoreFiles) {
if ($IsMacOS) {
PrintLldbCoreCallStack $File
} else {
PrintGdbCoreCallStack $File
}
}
$ProcessCrashed = $true
}
} catch {
LogWrn "Treating exception as crash!"
$ProcessCrashed = $true
throw
} finally {
# Add the current test case results.
if ($IsolationMode -ne "Batch") {
try { Add-XmlResults $TestCase } catch { }
}
if ($CodeCoverage) {
$NewCoverage = Join-Path $TestCase.LogDir $Coveragename
if ($IsolationMode -eq "Isolated") {
# Merge coverage with previous runs
$PreviousCoverage = Join-Path $CoverageDir $CoverageName
if (!(Test-Path $PreviousCoverage)) {
# No previous coverage data, just copy
Copy-Item $NewCoverage $CoverageDir
} else {
# Merge new coverage data with existing coverage data
# On a developer machine, this will always merge coverage until the dev deletes old coverage.
$TempMergedCoverage = Join-Path $CoverageDir "mergetemp.cov"
$CoverageExe = 'C:\"Program Files"\OpenCppCoverage\OpenCppCoverage.exe'
$CoverageMergeParams = " --input_coverage $($PreviousCoverage) --input_coverage $($NewCoverage) --export_type binary:$($TempMergedCoverage)"
Invoke-Expression ($CoverageExe + $CoverageMergeParams) | Out-Null
Move-Item $TempMergedCoverage $PreviousCoverage -Force
}
} else {
# Copy the coverage to destination
Copy-Item $NewCoverage $CoverageDir -Force
# Copy coverage log
$LogName = "LastCoverageResults-$(Split-Path $Path -LeafBase).log"
Copy-Item (Join-Path $TestCase.LogDir "LastCoverageResults.log") (Join-Path $CoverageDir $LogName) -Force
}
}
if ($ProcessCrashed) {
$global:CrashedProcessCount++
}
if ($IsolationMode -eq "Batch") {
if ($StdOutTxt) { Write-Host $StdOutTxt }
if ($StdErrorTxt) { Write-Host $StdErrorTxt }
} else {
$Delta = (Get-Date) - $TestCase.StartTime
if ($AnyTestFailed -or $ProcessCrashed) {
LogErr "$($TestCase.Name) failed (in $($Delta.TotalSeconds) sec):"
if ($StdOutTxt) { Write-Host $StdOutTxt }
if ($StdErrorTxt) { Write-Host $StdErrorTxt }
} else {
Log "$($TestCase.Name) succeeded (in $($Delta.TotalSeconds) sec)"
}
}
if ($KeepOutputOnSuccess -or $ProcessCrashed -or $AnyTestFailed) {
if ($LogProfile -ne "None") {
& $LogScript -Stop -OutputPath (Join-Path $TestCase.LogDir "quic")
}
if ($StdOutTxt) {
$StdOutTxt > (Join-Path $TestCase.LogDir "stdout.txt")
}
if ($StdErrorTxt) {
$StdErrorTxt > (Join-Path $TestCase.LogDir "stderr.txt")
}
if ($CompressOutput) {
# Zip the output.
CompressOutput-Archive -Path "$($TestCase.LogDir)\*" -DestinationPath "$($TestCase.LogDir).zip" | Out-Null
Remove-Item $TestCase.LogDir -Recurse -Force | Out-Null
}
} else {
if ($LogProfile -ne "None") {
& $LogScript -Cancel | Out-Null
}
Remove-Item $TestCase.LogDir -Recurse -Force | Out-Null
}
}
}
# Runs the test executable to query all available test cases, parses the console
# output and returns a list of test case names.
function GetTestCases {
$Arguments = " --gtest_list_tests"
if ($Filter -ne "") {
$Arguments = " --gtest_filter=$Filter --gtest_list_tests"
}
$stdout = Invoke-Expression ($Path + $Arguments)
$Tests = New-Object System.Collections.ArrayList
if ($null -ne $stdout) {
$Lines = ($stdout.Split([Environment]::NewLine)) | Where-Object { $_.Length -ne 0 }
$CurTestGroup = $null
for ($i = 0; $i -lt $Lines.Length; $i++) {
if (!($Lines[$i].StartsWith(" "))) {
$CurTestGroup = $Lines[$i]
} else {
$Tests.Add($CurTestGroup + $Lines[$i].Split("#")[0].Trim()) | Out-Null
}
}
}
$Tests.ToArray()
}
function Get-WindowsKitTool {
param (
[string]$Arch = "x86",
[Parameter(Mandatory = $true)]
[string]$Tool
)
$KitBinRoot = "C:\Program Files (x86)\Windows Kits\10\bin"
if (!(Test-Path $KitBinRoot)) {
Write-Error "Windows Kit Binary Folder not Found"
return ""
}
$FoundToolPath = $null
$FoundToolVersion = "0"
$Subfolders = Get-ChildItem -Path $KitBinRoot -Directory
foreach ($Subfolder in $Subfolders) {
$ToolPath = Join-Path $Subfolder "$Arch\$Tool"
if (Test-Path $ToolPath) {
$KitVersion = $Subfolder.Name
if ($KitVersion -gt $FoundToolVersion) {
$FoundToolVersion = $KitVersion
$FoundToolPath = $ToolPath
}
}
}
if ($null -ne $FoundToolPath) {
return $FoundToolPath
}
Write-Error "Failed to find tool"
return $null
}
##############################################################
# Main Execution #
##############################################################
# Query all the test cases.
$TestCases = GetTestCases
if ($null -eq $TestCases) {
Log "$Path (Skipped)"
exit
}
$TestCount = ($TestCases -as [String[]]).Length
Log "$Path ($TestCount test case(s))"
if ($ListTestCases) {
# List the tst cases.
$TestCases
exit
}
# Cancel any outstanding logs that might be leftover.
& $LogScript -Cancel | Out-Null
# Initialize WER dump registry key if necessary.
if ($IsWindows -and !(Test-Path $WerDumpRegPath) -and (Test-Administrator)) {
New-Item -Path $WerDumpRegPath -Force | Out-Null
}
# Initialize application verifier (Windows only).
if ($IsWindows -and $EnableAppVerifier) {
where.exe appverif.exe
if ($LastExitCode -eq 0) {
appverif.exe /verify $Path
} else {
Write-Warning "Application Verifier not installed!"
$EnableAppVerifier = $false;
}
}
$DriverPath = (Split-Path $Path -Parent)
# Install the kernel mode drivers.
if ($Kernel -ne "") {
if ($null -ne (Get-Service -Name "msquicpriv" -ErrorAction Ignore)) {
try { net.exe stop msquicpriv /y | Out-Null } catch {}
sc.exe delete msquicpriv /y | Out-Null
}
if ($null -ne (Get-Service -Name "msquictestpriv" -ErrorAction Ignore)) {
try { net.exe stop msquictestpriv /y | Out-Null } catch {}
sc.exe delete msquictestpriv /y | Out-Null
}
Copy-Item (Join-Path $Kernel "msquictestpriv.sys") $DriverPath -Force
Copy-Item (Join-Path $Kernel "msquicpriv.sys") $DriverPath -Force
Log "Creating msquicpriv service"
sc.exe create "msquicpriv" type= kernel binpath= (Join-Path $DriverPath msquicpriv.sys) start= demand | Out-Null
if ($LastExitCode) {
Log ("sc.exe " + $LastExitCode)
}
if ($EnableSystemVerifier) {
verifier.exe /volatile /adddriver msquicpriv.sys msquictestpriv.sys /flags 0x9BB
if ($LastExitCode) {
Log ("verifier.exe " + $LastExitCode)
}
}
Log "Starting msquicpriv service"
sc.exe start msquicpriv
if ($LastExitCode) {
Log ("net.exe " + $LastExitCode)
}
try {
if ("Running" -ne (Get-Service -Name msquicpriv).Status) {
LogFatal "msquicpriv isn't running"
}
} catch {
LogFatal "msquicpriv query failed"
}
}
if ($IsWindows -and $EnableSystemVerifier) {
verifier.exe /volatile /adddriver afd.sys netio.sys tcpip.sys /flags 0x9BB
if ($LastExitCode) {
Log ("verifier.exe " + $LastExitCode)
}
}
try {
if ($IsolationMode -eq "Batch") {
# Run the the test process once for all tests.
Wait-TestCase (Start-AllTestCases)
} else {
# Run the test cases individually.
for ($i = 0; $i -lt $TestCount; $i++) {
Wait-TestCase (Start-TestCase ($TestCases -as [String[]])[$i])
if (!$NoProgress) {
Write-Progress -Activity "Running tests" -Status "Progress:" -PercentComplete ($i/$TestCount*100)
}
}
}
} catch {
Log "Exception Thrown"
Log $_
Get-Error
$_ | Format-List *
} finally {
if ($LogProfile -ne "None") {
& $LogScript -Cancel | Out-Null
}
if ($IsWindows) {
# Cleanup the WER registry.
if (Test-Administrator) {
Remove-Item -Path $WerDumpRegPath -Force | Out-Null
}
# Turn off App Verifier
if ($EnableAppVerifier) {
appverif.exe -disable * -for $Path
}
}
if ($IsolationMode -eq "Batch") {
if (Test-Path $FinalResultsPath) {
$XmlResults = [xml](Get-Content $FinalResultsPath)
if (!$GenerateXmlResults) {
# Delete the XML results file since it's not needed.
Remove-Item $FinalResultsPath -Force | Out-Null
}
} else {
# No results file means the tests crashed most likely.
$NewXmlText = $FailXmlText.Replace("TestSuiteName", "all")
$NewXmlText = $NewXmlText.Replace("TestCaseName", "all")
$NewXmlText = $NewXmlText.Replace("date", $XmlResults.testsuites.timestamp)
$XmlResults = [xml]($NewXmlText)
if ($GenerateXmlResults) {
# Save the xml results.
$XmlResults.Save($FinalResultsPath) | Out-Null
}
}
} else {
if ($GenerateXmlResults) {
# Save the xml results.
$XmlResults.Save($FinalResultsPath) | Out-Null
}
}
$TestCount = $XmlResults.testsuites.tests -as [Int]
$TestsFailed = $XmlResults.testsuites.failures -as [Int]
# Uninstall the kernel mode test driver and revert the msquic driver.
if ($Kernel -ne "") {
net.exe stop msquicpriv /y | Out-Null
sc.exe delete msquictestpriv | Out-Null
sc.exe delete msquicpriv | Out-Null
Remove-Item (Join-Path $DriverPath msquicpriv.sys) -Force
Remove-Item (Join-Path $DriverPath msquictestpriv.sys) -Force
}
if ($IsWindows -and $EnableSystemVerifier) {
if ($Kernel -ne "") {
verifier.exe /volatile /removedriver msquicpriv.sys msquictestpriv.sys
verifier.exe /volatile /flags 0x0
}
verifier.exe /volatile /removedriver afd.sys netio.sys tcpip.sys
verifier.exe /volatile /flags 0x0
}
# Print out the results.
Log "$($TestCount) test(s) run."
if ($KeepOutputOnSuccess -or ($TestsFailed -ne 0) -or ($global:CrashedProcessCount -ne 0)) {
Log "Output can be found in $($LogDir)"
if ($ErrorsAsWarnings) {
Write-Warning "$($TestsFailed) test(s) failed."
Write-Warning "$($TestsFailed) test(s) failed, $($global:CrashedProcessCount) test(s) crashed."
} else {
Write-Error "$($TestsFailed) test(s) failed, $($global:CrashedProcessCount) test(s) crashed."
$LastExitCode = 1
}
} elseif ($AZP -and $TestCount -eq 0) {
Write-Error "Failed to run any tests."
} else {
if (Test-Path $LogDir) {
Remove-Item $LogDir -Recurse -Force | Out-Null
}
}
}