-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL Execute query file.Tests.ps1
377 lines (330 loc) · 14 KB
/
SQL Execute query file.Tests.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
#Requires -Version 7
#Requires -Modules Pester, ImportExcel
BeforeAll {
$testOutParams = @{
FilePath = (New-Item "TestDrive:/params.json" -ItemType File).FullName
Encoding = 'utf8'
}
$testData = @{
sqlFile = @(
@{
Path = (New-Item 'TestDrive:/s1.sql' -ItemType File).FullName
Content = '-- SQL instructions file 1'
}
@{
Path = (New-Item 'TestDrive:/s2.sql' -ItemType File).FullName
Content = '-- SQL instructions file 2'
}
)
}
$testData.sqlFile.foreach(
{ $_.Content | Out-File -FilePath $_.Path -NoNewline }
)
$testInputFile = @{
MailTo = '[email protected]'
MaxConcurrentTasks = 1
Tasks = @(
@{
ComputerNames = @('PC1')
DatabaseNames = @('db1')
SqlFiles = $testData.sqlFile.Path
}
)
}
$testScript = $PSCommandPath.Replace('.Tests.ps1', '.ps1')
$testParams = @{
ScriptName = 'Test (Brecht)'
ImportFile = $testOutParams.FilePath
SqlScript = (New-Item 'TestDrive:/s.ps1' -ItemType File).FullName
LogFolder = New-Item 'TestDrive:/log' -ItemType Directory
ScriptAdmin = '[email protected]'
}
Mock Invoke-Command
Mock Send-MailHC
Mock Write-EventLog
}
Describe 'the mandatory parameters are' {
It '<_>' -ForEach @('ImportFile', 'ScriptName') {
(Get-Command $testScript).Parameters[$_].Attributes.Mandatory |
Should -BeTrue
}
}
Describe 'send an e-mail to the admin when' {
BeforeAll {
$MailAdminParams = {
($To -eq $ScriptAdmin) -and ($Priority -eq 'High') -and
($Subject -eq 'FAILURE')
}
}
It 'the log folder cannot be created' {
$testNewParams = Copy-ObjectHC $testParams
$testNewParams.LogFolder = 'xxx:://notExistingLocation'
.$testScript @testNewParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and
($Message -like '*Failed creating the log folder*')
}
}
It 'the file SqlScript cannot be found' {
$testNewParams = Copy-ObjectHC $testParams
$testNewParams.SqlScript = 'c:\upDoesNotExist.ps1'
$testInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testNewParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and ($Message -like "*SQL script with path '$($testNewParams.SqlScript)' not found*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
Context 'the ImportFile' {
It 'is not found' {
$testNewParams = Copy-ObjectHC $testParams
$testNewParams.ImportFile = 'nonExisting.json'
.$testScript @testNewParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and ($Message -like "Cannot find path*nonExisting.json*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
Context 'property' {
It '<_> not found' -ForEach @(
'MaxConcurrentTasks', 'Tasks', 'MailTo'
) {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.$_ = $null
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and
($Message -like "*$ImportFile*Property '$_' not found*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
It 'Tasks.<_> not found' -ForEach @(
'ComputerNames', 'DatabaseNames', 'SqlFiles'
) {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].$_ = $null
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and
($Message -like "*$ImportFile*Property 'Tasks.$_' not found*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
Context 'SqlFiles' {
It 'file not found' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].SqlFiles = @('notExisting.sql')
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and ($Message -like "*$ImportFile*SQL file 'notExisting.sql' not found*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
It 'file extension not .sql' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].SqlFiles = @((
New-Item 'TestDrive:/a.txt' -ItemType File).FullName
)
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and ($Message -like "*$ImportFile*SQL file '$($testNewInputFile.Tasks[0].SqlFiles)' needs to have extension '.sql'*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
It 'file empty' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].SqlFiles = @((
New-Item 'TestDrive:/b.sql' -ItemType File).FullName
)
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and
($Message -like "*No file content in SQL file '$($testNewInputFile.Tasks[0].SqlFiles[0])'*")
}
}
}
Context 'MaxConcurrentTasks' {
It 'is not a number' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.MaxConcurrentTasks = 'a'
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Send-MailHC -Exactly 1 -ParameterFilter {
(&$MailAdminParams) -and
($Message -like "*$ImportFile*Property 'MaxConcurrentTasks' needs to be a number, the value 'a' is not supported*")
}
Should -Invoke Write-EventLog -Exactly 1 -ParameterFilter {
$EntryType -eq 'Error'
}
}
}
}
}
}
Describe 'execute the SQL script with Invoke-Command' {
It 'once for 1 computer name and 1 database name' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].ComputerNames = 'PC1'
$testNewInputFile.Tasks[0].DatabaseNames = 'db1'
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
($ComputerName -eq $env:COMPUTERNAME) -and
($FilePath -eq $testParams.SqlScript) -and
($EnableNetworkAccess) -and
($ErrorAction -eq 'Stop') -and
($ArgumentList[0] -eq $testNewInputFile.Tasks[0].ComputerNames) -and
($ArgumentList[1] -eq $testNewInputFile.Tasks[0].DatabaseNames) -and
($ArgumentList[2][0] -eq $testData.sqlFile[0].Content) -and
($ArgumentList[2][1] -eq $testData.sqlFile[1].Content) -and
($ArgumentList[3][0] -eq $testData.sqlFile[0].Path) -and
($ArgumentList[3][1] -eq $testData.sqlFile[1].Path)
}
Should -Invoke Invoke-Command -Times 1 -Exactly
}
It 'once for each computer name and each database name' {
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile.Tasks[0].ComputerNames = @('PC1', 'PC2')
$testNewInputFile.Tasks[0].DatabaseNames = @('db1', 'db2')
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
foreach (
$testComputer in
$testNewInputFile.Tasks[0].ComputerNames
) {
foreach (
$testDatabase in
$testNewInputFile.Tasks[0].DatabaseNames
) {
Should -Invoke Invoke-Command -Times 1 -Exactly -ParameterFilter {
($ComputerName -eq $env:COMPUTERNAME) -and
($FilePath -eq $testParams.SqlScript) -and
($EnableNetworkAccess) -and
($ErrorAction -eq 'Stop') -and
($ArgumentList[0] -eq $testComputer) -and
($ArgumentList[1] -eq $testDatabase) -and
($ArgumentList[2][0] -eq $testData.sqlFile[0].Content) -and
($ArgumentList[2][1] -eq $testData.sqlFile[1].Content) -and
($ArgumentList[3][0] -eq $testData.sqlFile[0].Path) -and
($ArgumentList[3][1] -eq $testData.sqlFile[1].Path)
}
}
}
Should -Invoke Invoke-Command -Times 4 -Exactly
}
}
Describe 'on a successful run' {
BeforeAll {
$Error.Clear()
$testData.ScriptOutput = @(
[PSCustomObject]@{
ComputerName = 'PC1'
DatabaseName = 'dn1'
SqlFile = $testData.sqlFile[0].Path
StartTime = Get-Date
EndTime = (Get-Date).AddMinutes(5)
Executed = $true
Error = $null
Output = @('a', 'b')
}
[PSCustomObject]@{
ComputerName = 'PC2'
DatabaseName = 'db1'
SqlFile = $testData.sqlFile[1].Path
StartTime = Get-Date
EndTime = (Get-Date).AddMinutes(10)
Executed = $false
Error = 'prob'
Output = $null
}
)
Mock Invoke-Command {
$testData.ScriptOutput
}
$testExportedExcelRows = $testData.ScriptOutput |
Select-Object -Property 'ComputerName',
'DatabaseName', 'StartTime', 'EndTime', 'Executed',
@{
Name = 'Duration'
Expression = {
New-TimeSpan -Start $_.StartTime -End $_.EndTime
}
},
'SqlFile', @{
Name = 'Output'
Expression = {
$_.Output -join ', '
}
}, 'Error'
$testNewInputFile = Copy-ObjectHC $testInputFile
$testNewInputFile | ConvertTo-Json -Depth 7 |
Out-File @testOutParams
.$testScript @testParams
$testExcelLogFile = Get-ChildItem $testParams.LogFolder -File -Recurse -Filter '*.xlsx'
$actual = Import-Excel -Path $testExcelLogFile.FullName -WorksheetName 'Overview'
}
Context 'export an Excel file' {
It 'to the log folder' {
$testExcelLogFile | Should -Not -BeNullOrEmpty
}
It 'with the correct total rows' {
$actual | Should -HaveCount $testExportedExcelRows.Count
}
It 'with the correct data in the rows' {
foreach ($testRow in $testExportedExcelRows) {
$actualRow = $actual | Where-Object {
($_.ComputerName -eq $testRow.ComputerName)
}
$actualRow.DatabaseName | Should -Be $testRow.DatabaseName
$actualRow.StartTime.ToString('yyyyMMddhhmm') |
Should -Be $testRow.StartTime.ToString('yyyyMMddhhmm')
$actualRow.EndTime.ToString('yyyyMMddhhmm') |
Should -Be $testRow.EndTime.ToString('yyyyMMddhhmm')
$actualRow.Duration | Should -Not -BeNullOrEmpty
$actualRow.Executed | Should -Be $testRow.Executed
$actualRow.SqlFile | Should -Be $testRow.SqlFile
$actualRow.Output | Should -Be $testRow.Output
$actualRow.Error | Should -Be $testRow.Error
}
}
}
Context 'send an e-mail ' {
It 'to the user' {
Should -Invoke Send-MailHC -Exactly 1 -Scope Describe -ParameterFilter {
($To -eq $testInputFile.MailTo) -and
($Bcc -eq $testParams.ScriptAdmin) -and
($Priority -eq 'High') -and
($Subject -eq '2 queries, 1 error') -and
($Attachments.Count -eq 1) -and
($Attachments -like '* - Log.xlsx') -and
($Message -like "*Total queries*2*Executed queries*1*Not executed queries*1*Failed queries*1*Check the attachment for details*")
}
}
}
}