-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.ps1
More file actions
195 lines (157 loc) · 7.36 KB
/
run_tests.ps1
File metadata and controls
195 lines (157 loc) · 7.36 KB
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
param(
[int]$NumberOfTestsToTest = 1000,
[int]$NumberOfTestsToSkip = 0,
[string]$CommandToStartWith,
[switch]$ContinueOnFailure,
[switch]$SkipEnvironmentTest,
[switch]$TestForWarnings,
[string]$StatusUrl = $Env:MyStatusUrl,
[string]$ConfigFilename = 'TestConfig_remote_instances.ps1',
[ValidateSet('SINGLE', 'MULTI', 'COPY', 'HADR', 'RESTART', '2008R2', '2016')]
[string]$Scenario,
[hashtable]$Config
)
$ErrorActionPreference = 'Stop'
$githubBase = 'C:\GitHub'
$dbatoolsBase = "$githubBase\dbatools"
$testingBase = "$githubBase\testing-dbatools"
$configFile = "$testingBase\$ConfigFilename"
$logPath = "$testingBase\logs"
$resultsFileName = "$logPath\results_$($Scenario)_$([datetime]::Now.ToString('yyyMMdd_HHmmss')).txt"
function Send-Status {
Param([string]$Message)
if ($StatusUrl) {
$requestParams = @{
Uri = $StatusUrl
Method = 'Post'
ContentType = 'application/json'
Body = @{
IP = '127.0.0.1'
Host = 'localhost'
Message = $Message
} | ConvertTo-Json -Compress
UseBasicParsing = $true
}
try {
$null = Invoke-WebRequest @requestParams
} catch {
Write-Warning -Message "Failed to send status: $_"
}
}
}
$start = Get-Date
Import-Module "$dbatoolsBase\dbatools.psm1" -Force
$TestConfig = Get-TestConfig -LocalConfigPath $configFile
if ($Config) {
foreach ($cfg in $Config.GetEnumerator()) {
Add-Member -InputObject $TestConfig -MemberType NoteProperty -Name $cfg.Name -Value $cfg.Value -Force
}
}
$tests = Get-ChildItem -Path "$dbatoolsBase\tests\*.Tests.ps1" | Sort-Object -Property Name
# Filter tests based on script parameters
if ($Scenario) {
. "$dbatoolsBase\tests\appveyor.common.ps1"
. "$dbatoolsBase\tests\pester.groups.ps1"
$tests = Get-TestsForScenario -Scenario $Scenario -AllTest $tests
}
if ($CommandToStartWith) {
$commandIndex = $tests.Name.IndexOf("$CommandToStartWith.Tests.ps1")
if ($commandIndex -ge 0) {
$tests = $tests[$commandIndex..($tests.Count - 1)]
} else {
Write-Warning -Message "No test for [$CommandToStartWith] found"
break
}
}
$tests = $tests | Select-Object -First $NumberOfTestsToTest -Skip $NumberOfTestsToSkip
#$ProgressPreference = 'SilentlyContinue'
#Get-Date
#Install-DbaSqlPackage
#Get-Date
#$ProgressPreference = 'Continue'
Import-Module -Name Pester -MinimumVersion 5.0
$progressParameter = @{ Id = Get-Random ; Activity = 'Running tests' }
$progressTotal = $tests.Count
$progressCompleted = 0
$progressStart = Get-Date
foreach ($test in $tests) {
# $test = $tests[0]
$progressParameter.Status = "$progressCompleted of $progressTotal tests completed ($sleepingProcs1 / $sleepingProcs2 / $sleepingProcs3 / $usedMemory MB / $startMemory MB)"
$progressParameter.CurrentOperation = "processing $($test.Name)"
$progressParameter.PercentComplete = $progressCompleted * 100 / $progressTotal
if ($progressParameter.PercentComplete -gt 0) {
$progressParameter.SecondsRemaining = ((Get-Date) - $progressStart).TotalSeconds / $progressParameter.PercentComplete * (100 - $progressParameter.PercentComplete)
}
Write-Progress @progressParameter
$content = Get-Content -Path $test.FullName
$instanceNames = foreach ($line in $content) {
$code = $line -replace '#.*$',''
[regex]::Matches($code, '\$TestConfig\.Instance(\w+)', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) | ForEach-Object { $_.Groups[1].Value }
}
$instanceNames = $instanceNames | Sort-Object -Unique
Write-Warning -Message "Using instances: $instanceNames"
Write-Warning -Message "Running $($test.FullName)"
$failure = $false
$startMemory = [int]([System.GC]::GetTotalMemory($false)/1MB)
$warningsFile = "$logPath\$($test.Name).warnings.txt"
if ($TestForWarnings) {
$resultTest = Invoke-Pester -Path $test.FullName -Output Detailed -PassThru 3> $warningsFile
$warnings = Get-Content -Path $warningsFile
if ($warnings) {
$warnings | ForEach-Object { Write-Warning -Message $_ }
$failure = $true
} else {
Remove-Item -Path $warningsFile
}
} else {
$resultTest = Invoke-Pester -Path $test.FullName -Output Detailed -PassThru
}
if ($resultTest.FailedCount -gt 0) {
$failure = $true
}
$usedMemory = [int]([System.GC]::GetTotalMemory($false)/1MB) - $startMemory
if (-not $SkipEnvironmentTest) {
$resultEnvironment = Invoke-Pester -Path "$testingBase\TestEnvironment.Tests.ps1" -Output None -PassThru
if ($resultEnvironment.Result -ne 'Passed') {
Write-Warning -Message "Environment test failed: $($resultEnvironment.Failed.ExpandedPath)"
$failure = $true
}
}
if (Get-Module -Name dbatools | Where-Object { $_.Version.Major -gt 0 }) {
Write-Warning -Message "dbatools was loaded"
$failure = $true
}
# Clear-DbaConnectionPool
# [int]$sleepingProcs1 = (Get-DbaProcess -SqlInstance $TestConfig.instance1 | Where-Object { $_.Program -match 'dbatools' -and $_.Status -eq 'sleeping' }).Count
# [int]$sleepingProcs2 = (Get-DbaProcess -SqlInstance $TestConfig.instance2 | Where-Object { $_.Program -match 'dbatools' -and $_.Status -eq 'sleeping' }).Count
# [int]$sleepingProcs3 = (Get-DbaProcess -SqlInstance $TestConfig.instance3 | Where-Object { $_.Program -match 'dbatools' -and $_.Status -eq 'sleeping' }).Count
# Remove-DbaDbBackupRestoreHistory -SqlInstance $TestConfig.instance1, $TestConfig.instance2, $TestConfig.instance3 -KeepDays -1 -Confirm:$false
Write-Host "`n$((Get-Date).ToString('HH:mm:ss')) ========= $sleepingProcs1 / $sleepingProcs2 / $sleepingProcs3 / $usedMemory MB / $([int]([System.GC]::GetTotalMemory($false)/1MB)) MB ==========`n"
$resultInfo = [ordered]@{
TestFileName = $test.Name
Result = $( if ($resultTest.Result) { $resultTest.Result } elseif ($resultTest.FailedCount -eq 0) { 'Passed' } else { 'Failed' })
DurationSeconds = $( if ($resultTest.Duration) { $resultTest.Duration.TotalSeconds } else { $resultTest.Time.TotalSeconds })
TotalCount = $resultTest.TotalCount
PassedCount = $resultTest.PassedCount
FailedCount = $resultTest.FailedCount
SkippedCount = $resultTest.SkippedCount
UsedMemoryMB = $usedMemory
SleepingProcs1 = $sleepingProcs1
SleepingProcs2 = $sleepingProcs2
SleepingProcs3 = $sleepingProcs3
TestsFailed = $resultTest.Failed
EnvironmentFailed = $(if ($resultEnvironment.Result -ne 'Passed') { $resultEnvironment.Failed })
}
$resultInfo | ConvertTo-Json -Compress | Add-Content -Path $resultsFileName
# $null = Get-DbaConnectedInstance | Disconnect-DbaInstance
# Clear-DbaConnectionPool
# [System.GC]::Collect()
$progressCompleted++
if ($failure -and -not $ContinueOnFailure) {
Send-Status -Message "TEST FAILED: $($test.Name)"
break
}
Send-Status -Message "Test $progressCompleted of $progressTotal ok: $($test.Name)"
}
Write-Progress @progressParameter -Completed
Write-Host "Finished $progressCompleted tests in $([int]((Get-Date) - $start).TotalMinutes) minutes"