-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathInvoke-SqlFailOverDetection.Tests.ps1
577 lines (550 loc) · 24.2 KB
/
Invoke-SqlFailOverDetection.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
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
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
. "$here\$sut"
"$here\$sut"
Import-Module PSScriptAnalyzer
$Rules = Get-ScriptAnalyzerRule
$Name = $sut.Split('.')[0]
Describe 'Script Analyzer Tests' -Tag Invoke-SqlFailOverDetection {
Context 'Testing $sut for Standard Processing' {
foreach ($rule in $rules.Where{$_.RuleName -ne 'PSAvoidUsingWMICmdlet'}) {
$i = $rules.IndexOf($rule)
It "passes the PSScriptAnalyzer Rule number $i - $rule " {
(Invoke-ScriptAnalyzer -Path "$here\$sut" -IncludeRule $rule.RuleName ).Count | Should Be 0
}
}
}
}
Describe 'Tests For Help' -Tag Invoke-SqlFailOverDetection {
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
$Help = Get-Help $Name -ErrorAction SilentlyContinue
# Should be a description for every function
It "gets description for $Name" {
$Help.Description | Should Not BeNullOrEmpty
}
# Should be at least one example
It "gets example code from $Name" {
($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
}
# Should be at least one example description
It "gets example help from $Name" {
($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
}
Context "Test parameter help for $Name" {
$Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable',
'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' , 'Confirm', 'WhatIf'
$command = Get-Command $name
$parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object { $_.Name -notin $common }
$parameterNames = $parameters.Name
$HelpParameterNames = $Help.Parameters.Parameter.Name| Where-Object { $_ -notin $common } | Sort-Object -Unique
foreach ($parameter in $parameters) {
$parameterName = $parameter.Name
$parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName
# Should be a description for every parameter
It "gets help for parameter: $parameterName : in $Name" {
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
}
# Required value in Help should match IsMandatory property of parameter
It "help for $parameterName parameter in $Name has correct Mandatory value" {
$codeMandatory = $parameter.IsMandatory.toString()
$parameterHelp.Required | Should Be $codeMandatory
}
# Parameter type in Help should match code
It "help for $Name has correct parameter type for $parameterName" {
$codeType = $parameter.ParameterType.Name
# To avoid calling Trim method on a null object.
$helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
$helpType | Should be $codeType
}
}
foreach ($helpParm in $HelpParameterNames) {
# Shouldn't find extra parameters in help.
It "finds help parameter in code: $helpParm" {
$helpParm -in $parameterNames | Should Be $true
}
}
}
}
Describe "$Name Tests" -Tag Invoke-SqlFailOverDetection {
#region Mocks
Function Get-ClusterLog {}
Mock Get-ClusterLog {}
Function Get-TheModule {}
Mock Get-TheModule {'Summat'}
Mock Move-Item {}
Mock Remove-Item {}
Mock Get-WinEvent {'Summat'} # because otherwise nothing passes down the pipeline to export-csv
Mock Copy-Item {}
Mock Write-Verbose {}
Mock Write-Warning {}
Mock New-Item {}
function DownloadFile {}
Mock DownloadFile {}
Mock Expand-Archive {}
Mock Get-DbaAvailabilityGroup {
@{
Name = 'DummyAgName'
AvailabilityReplicas = @(
@{
Name = 'Dummy1'
},
@{
Name = 'Dummy2'
},
@{
Name = 'Dummy3'
},
@{
Name = 'Dummy4'
}
)
}
}
function Get-DbaErrorLogConfig {}
Mock Get-DbaErrorLogConfig {
@{
LogPath = 'C:\Summat\Summat'
}
}
Mock Get-ChildItem {
@{
Name = 'Summat'
LastWriteTime = (Get-Date)
}
} # because otherwise nothing passes down the pipeline to copy-item
Mock Get-ClusterLog {}
Mock Out-File {}
Mock Set-Location {}
Mock Export-Csv {}
Mock Remove-Item {}
function RunFailOverDetection {}
Mock RunFailOverDetection {}
Mock Test-Path {$false}
Mock Test-Path {$true} -ParameterFilter {$Path -and $path -like '*Download\FailoverDetector.zip'}
$InstallationFolder = 'C:\temp\'
$DownloadFolder = 'C:\temp\Download'
$DataFolder = 'C:\temp\'
$SQLInstance = 'Dummy'
#endregion
Context 'Function' {
$MadatoryParams = 'InstallationFolder', 'DownloadFolder', 'DataFolder', 'SQLInstance'
It 'Has Cmdlet Binding set to true' {
(Get-Command $Name).CmdletBinding | Should -BeTrue
}
$MadatoryParams.ForEach{
It "$Name Should have a mandatory parameter $PsItem" {
(Get-Command $Name).Parameters[$PsItem].Attributes.Mandatory | Should -BeTrue -Because "The Parameter $Psitem should be mandatory for $Name"
}
}
$Params = 'AvailabilityGroup', 'AlreadyDownloaded', 'Analyze', 'Show'
$Params.ForEach{
It "$Name Should have a parameter $PSItem" {
(Get-Command $Name).Parameters[$PSItem].Count | Should -Be 1 -Because "We need to have the Parameter $PSItem for $Name"
}
}
}
Context 'Execution without folders existing and no switches' {
$invokeSqlFailOverDetectionSplat = @{
DownloadFolder = $DownloadFolder
SQLInstance = $SQLInstance
DataFolder = $DataFolder
InstallationFolder = $InstallationFolder
}
Invoke-SqlFailOverDetection @invokeSqlFailOverDetectionSplat
It 'Should Create the Required Folders' {
$assertMockParams = @{
'CommandName' = 'New-Item'
'Times' = 8
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The New-item function is called whenver we create folders"
}
It 'Should Downlaod the file' {
$assertMockParams = @{
'CommandName' = 'DownloadFile'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We want to download the files otherwise how can we extract them"
}
It 'Should Extract the file' {
$assertMockParams = @{
'CommandName' = 'Expand-Archive'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to extract the zipped files otherwise how can we run them?"
}
It 'Should get the Availability Group Information' {
$assertMockParams = @{
'CommandName' = 'Get-DbaAvailabilityGroup'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the replica names from the Availablity Group"
}
It 'Should get the location of the Error Log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-DbaErrorLogConfig'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the error log locatio for each replica"
}
It 'Should get the cluster log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-ClusterLog'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the cluster log from each replica to perform the analysis"
}
It 'Should get the system event log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-WinEvent'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
$assertMockParams = @{
'CommandName' = 'Export-Csv'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
}
It 'Should copy the files to the Data Folder' {
$assertMockParams = @{
'CommandName' = 'Copy-Item'
'Times' = 21
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "How else will we ge thte files ready fo rth eapplication?"
}
It 'Should create the JSON file' {
$assertMockParams = @{
'CommandName' = 'Out-File'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
It 'Should run the Executable' {
$assertMockParams = @{
'CommandName' = 'RunFailOverDetection'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
}
Context 'Execution with folders existing and no switches' {
Mock Test-Path {$true}
$invokeSqlFailOverDetectionSplat = @{
DownloadFolder = $DownloadFolder
SQLInstance = $SQLInstance
DataFolder = $DataFolder
InstallationFolder = $InstallationFolder
}
Invoke-SqlFailOverDetection @invokeSqlFailOverDetectionSplat
It 'Should Not Create the Required Folders' {
$assertMockParams = @{
'CommandName' = 'New-Item'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Dont create folders if they already exist"
}
It 'Should Downlaod the file' {
$assertMockParams = @{
'CommandName' = 'DownloadFile'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We want to download the files otherwise how can we extract them"
}
It 'Should Extract the file' {
$assertMockParams = @{
'CommandName' = 'Expand-Archive'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to extract the zipped files otherwise how can we run them?"
}
It 'Should get the Availability Group Information' {
$assertMockParams = @{
'CommandName' = 'Get-DbaAvailabilityGroup'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the replica names from the Availablity Group"
}
It 'Should get the location of the Error Log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-DbaErrorLogConfig'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the error log locatio for each replica"
}
It 'Should get the cluster log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-ClusterLog'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the cluster log from each replica to perform the analysis"
}
It 'Should get the system event log once per replica' {
$assertMockParams = @{
'CommandName' = 'WinEvent'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
$assertMockParams = @{
'CommandName' = 'Export-Csv'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
}
It 'Should copy the files to the Data Folder' {
$assertMockParams = @{
'CommandName' = 'Copy-Item'
'Times' = 21
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "How else will we ge thte files ready fo rth eapplication?"
}
It 'Should create the JSON file' {
$assertMockParams = @{
'CommandName' = 'Out-File'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
It 'Should create the Executable' {
$assertMockParams = @{
'CommandName' = 'RunFailOverDetection'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
}
Context 'Execution with folders existing and AlreadyDownloaded switch' {
Mock Test-Path {$true}
$invokeSqlFailOverDetectionSplat = @{
DownloadFolder = $DownloadFolder
SQLInstance = $SQLInstance
DataFolder = $DataFolder
InstallationFolder = $InstallationFolder
AlreadyDownloaded = $true
}
Invoke-SqlFailOverDetection @invokeSqlFailOverDetectionSplat
It 'Should Create the Required Folders' {
$assertMockParams = @{
'CommandName' = 'New-Item'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Dont create folders if they already exist"
}
It 'Should Not Downlaod the file' {
$assertMockParams = @{
'CommandName' = 'DownloadFile'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Thats the point of the AlreadyDownloaded switch!"
}
It 'Should Not Extract the file' {
$assertMockParams = @{
'CommandName' = 'Expand-Archive'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We dont need to extraxct it if it has already been downloaded"
}
It 'Should get the Availability Group Information' {
$assertMockParams = @{
'CommandName' = 'Get-DbaAvailabilityGroup'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the replica names from the Availablity Group"
}
It 'Should get the location of the Error Log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-DbaErrorLogConfig'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the error log locatio for each replica"
}
It 'Should get the cluster log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-ClusterLog'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the cluster log from each replica to perform the analysis"
}
It 'Should get the system event log once per replica' {
$assertMockParams = @{
'CommandName' = 'Get-WinEvent'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
$assertMockParams = @{
'CommandName' = 'Export-Csv'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the system event log from each replica to perform the analysis"
}
It 'Should copy the files to the Data Folder' {
$assertMockParams = @{
'CommandName' = 'Copy-Item'
'Times' = 21
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "How else will we ge thte files ready fo rth eapplication?"
}
It 'Should create the JSON file' {
$assertMockParams = @{
'CommandName' = 'Out-File'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
It 'Should create the Executable' {
$assertMockParams = @{
'CommandName' = 'RunFailOverDetection'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
}
Context 'Execution with folders existing and Analyze switch' {
Mock Test-Path {$true}
$invokeSqlFailOverDetectionSplat = @{
DownloadFolder = $DownloadFolder
SQLInstance = $SQLInstance
DataFolder = $DataFolder
InstallationFolder = $InstallationFolder
Analyze = $true
}
Invoke-SqlFailOverDetection @invokeSqlFailOverDetectionSplat
It 'Should Create the Required Folders' {
$assertMockParams = @{
'CommandName' = 'New-Item'
'Times' = 4
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Dont create folders if they already exist"
}
It 'Should Not Downlaod the file' {
$assertMockParams = @{
'CommandName' = 'DownloadFile'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Thats the point of the AlreadyDownloaded switch!"
}
It 'Should Not Extract the file' {
$assertMockParams = @{
'CommandName' = 'Expand-Archive'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We dont need to extraxct it if it has already been downloaded"
}
It 'Should get the Availability Group Information' {
$assertMockParams = @{
'CommandName' = 'Get-DbaAvailabilityGroup'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "We need to get the replica names from the Availablity Group"
}
It 'Should Not get the location of the Error Log on any replica' {
$assertMockParams = @{
'CommandName' = 'Get-DbaErrorLogConfig'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Analyze means the data is there"
}
It 'Should get Not the cluster log on any replica' {
$assertMockParams = @{
'CommandName' = 'Get-ClusterLog'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Analyze means the data is there"
}
It 'Should Not get the system event log on any replica' {
$assertMockParams = @{
'CommandName' = 'WinEvent'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Analyze means the data is there"
$assertMockParams = @{
'CommandName' = 'Export-Csv'
'Times' = 0
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Analyze means the data is there"
}
It 'Should copy the files to the Data Folder' {
$assertMockParams = @{
'CommandName' = 'Copy-Item'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Analyze means the data is there"
}
It 'Should create the JSON file' {
$assertMockParams = @{
'CommandName' = 'Out-File'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
It 'Should create the Executable' {
$assertMockParams = @{
'CommandName' = 'RunFailOverDetection'
'Times' = 1
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "The configuration JSON is needed"
}
}
Context "No FailOvercluster module" {
Mock Get-TheModule {}
$invokeSqlFailOverDetectionSplat = @{
DownloadFolder = $DownloadFolder
SQLInstance = $SQLInstance
DataFolder = $DataFolder
InstallationFolder = $InstallationFolder
Analyze = $true
}
Invoke-SqlFailOverDetection @invokeSqlFailOverDetectionSplat
It "Should give a warning and quit" {
$assertMockParams = @{
'CommandName' = 'Write-Warning'
'Times' = 2
'Exactly' = $true
}
{Assert-MockCalled @assertMockParams} | Should -Not -Throw -Because "Because we need to stop and tell if there is no failvoer cluster module"
}
}
}