Skip to content

Commit f4ae5db

Browse files
Merge branch 'pesterv5' of https://github.com/dataplat/dbachecks into pesterv5
2 parents 9d21191 + c9ced52 commit f4ae5db

File tree

7 files changed

+240
-88
lines changed

7 files changed

+240
-88
lines changed

Perf Testing pesterv5.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ ipmo ./dbachecks.psd1
1414

1515
#
1616

17-
$Checks = 'TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
18-
17+
$Checks = 'XpCmdShellDisabled','WhoIsActiveInstalled','CLREnabled','TraceFlagsNotExpected','TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
18+
$Checks = 'XpCmdShellDisabled'
1919

2020
<#
2121
When there are default skips (some of the CIS checks) we need to set the configs and check
@@ -64,7 +64,7 @@ function Compare-CheckRuns {
6464
param($Checks)
6565
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
6666
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
67-
$Sqlinstances = 'localhost,14333', 'localhost,14334' #'localhost,7401', 'localhost,7402', 'localhost,7403'
67+
$Sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
6868

6969
$originalCode = {
7070
Invoke-DbcCheck -SqlInstance $Sqlinstances -Check $Checks -SqlCredential $cred -legacy $true -Show None
@@ -101,5 +101,5 @@ Write-PSFMessage -Message $savingMessage -Level Output
101101
}
102102

103103

104-
$Checks = 'DbaOperator'
104+
# $Checks = 'DbaOperator'
105105
Compare-CheckRuns -Checks $checks

Validate v4 adn v5.ps1

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ RUN THIS SECTION MANUALLY IF YOU JUST IMPORT THE FUNCTION BELOW!
1515
1616
ipmo ./dbachecks.psd1
1717
18-
$Checks = 'TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
19-
$Checks = 'DatabaseMailEnabled'
18+
$Checks = 'WhoIsActiveInstalled','CLREnabled','TraceFlagsNotExpected','TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
19+
$Checks = 'WhoIsActiveInstalled'
2020
Compare-v4andv5Results -Checks $Checks
2121
2222
# if you need to see the details to see why the results are different
@@ -54,6 +54,18 @@ Enable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1
5454
Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1117,1118
5555
Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1118
5656
57+
Get-DbcConfigValue policy.traceflags.expected
58+
Get-DbcConfigValue policy.traceflags.notexpected
59+
Get-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred
60+
Set-DbcConfig policy.traceflags.expected -Value 1117,1118
61+
Set-DbcConfig policy.traceflags.notexpected -Value 1117,1118
62+
Set-DbcConfig policy.traceflags.expected -Value $null
63+
Set-DbcConfig policy.traceflags.notexpected -Value $null
64+
65+
Enable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1117,1118
66+
Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1117,1118
67+
Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag 1118
68+
5769
#>
5870

5971
# Load the function below and then you can keep running the checks defined above in v4 and v5 and compare the performance
@@ -117,6 +129,7 @@ and
117129
For v5 we ran
118130
{1} tests
119131
The MOST COMMON REASON IS you have used Tags instead of Tag in your Describe block
132+
but TraceFlagsNotExpected will change that also
120133
" -f $v4code.TotalCount, ($v5code.TotalCount - $v5code.NotRunCount)
121134
Write-PSFMessage -Message $Message -Level Warning
122135
}

checks/Databasev5.Tests.ps1

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,6 @@
11
# So the v5 files need to be handled differently.
2-
# Ww will start with a BeforeDiscovery which for the Database Checks will need to gather the Instances up front
2+
# Ww will start with a BeforeDiscovery , $Filename which for the Database Checks will need to gather the Instances up front
33
BeforeDiscovery {
4-
<#
5-
. $PSScriptRoot/../internal/assertions/Database.Assertions.ps1
6-
[array]$ExcludedDatabases = Get-DbcConfigValue command.invokedbccheck.excludedatabases
7-
$ExcludedDatabases += $ExcludeDatabase
8-
[string[]]$NotContactable = (Get-PSFConfig -Module dbachecks -Name global.notcontactable).Value
9-
10-
$InstancesToTest = @(Get-Instance).ForEach{
11-
# just add it to the Not Contactable list
12-
if ($NotContactable -notcontains $psitem) {
13-
$Instance = $psitem
14-
try {
15-
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
16-
} catch {
17-
$NotContactable += $Instance
18-
}
19-
if ($NotContactable -notcontains $psitem) {
20-
if ($null -eq $InstanceSMO.version) {
21-
$NotContactable += $Instance
22-
} else {
23-
$InstanceSMO
24-
}
25-
}
26-
}
27-
}
28-
Write-PSFMessage -Message "Instances = $InstancesToTest" -Level Significant
29-
Set-PSFConfig -Module dbachecks -Name global.notcontactable -Value $NotContactable
30-
#>
314

325
# Gather the instances we know are not contactable
336
[string[]]$NotContactable = (Get-PSFConfig -Module dbachecks -Name global.notcontactable).Value
@@ -62,52 +35,88 @@ BeforeDiscovery {
6235

6336
# Each Test will have a -ForEach for the Instances and the InstancesToTest object will have a
6437
# lot of information gathered up front to reduce trips and connections to the database
38+
39+
6540
<#
6641
67-
Describe "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
68-
BeforeAll {
69-
$Wrongcollation = Get-DbcConfigValue policy.database.wrongcollation
70-
$exclude = "ReportingServer", "ReportingServerTempDB"
71-
$exclude += $Wrongcollation
72-
$exclude += $ExcludedDatabases
42+
- copy in test
43+
- add skip after describe
44+
$skip = Get-DbcConfigValue skip.database.databasecollation
45+
add to IT -Skip:$skip
46+
#>
47+
7348

49+
50+
Describe "Suspect Page" -Tag SuspectPage, High , Database -ForEach $InstancesToTest {
51+
$skip = Get-DbcConfigValue skip.database.suspectpage
52+
Context "Testing suspect pages on <_.Name>" {
53+
It "Database <_.Name> should return 0 suspect pages on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.suspectpageexclude -notcontains $PsItem.Name } } {
54+
$psitem.SuspectPage | Should -Be 0 -Because "You do not want any suspect pages"
55+
}
7456
}
57+
}
7558

59+
Describe "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
7660
Context "Testing database collation on <_.Name>" {
77-
It "Database <_.Database> collation <_.DatabaseCollation> should match server collation <_.ServerCollation> on <_.SqlInstance>" -ForEach @(Test-DbaDbCollation -SqlInstance $psitem -Database $Database -ExcludeDatabase $exclude) {
78-
$psitem.ServerCollation | Should -Be $psitem.DatabaseCollation -Because "You will get collation conflict errors in tempdb"
61+
$skip = Get-DbcConfigValue skip.database.databasecollation
62+
It "Database <_.Name> collation <_.Collation> should match server collation <_.ServerCollation> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.wrongcollation -notcontains $PsItem.Name } } {
63+
$psitem.ServerCollation | Should -Be $psitem.Collation -Because "You will get collation conflict errors in tempdb"
7964
}
80-
if ($Wrongcollation) {
81-
@(Test-DbaDbCollation -SqlInstance $psitem -Database $Wrongcollation ).ForEach{
82-
It "Database $($psitem.Database) collation ($($psitem.DatabaseCollation)) should not match server collation ($($psitem.ServerCollation)) on $($psitem.SqlInstance)" {
83-
$psitem.ServerCollation | Should -Not -Be $psitem.DatabaseCollation -Because "You have defined the database to have another collation then the server. You will get collation conflict errors in tempdb"
84-
}
85-
}
65+
66+
# wrong collation set
67+
It "Database <_.Name> collation <_.Collation> should not match server collation <_.ServerCollation> on <_.SqlInstance>" -ForEach $psitem.Databases.Where{ $_.Name -in $psitem.ConfigValues.wrongcollation } {
68+
$psitem.ServerCollation | Should -Not -Be $psitem.Collation -Because "You have defined the database to have another collation then the server. You will get collation conflict errors in tempdb"
8669
}
70+
8771
}
8872
}
8973

90-
Describe "Suspect Page" -Tag SuspectPage, High , Database -ForEach $InstancesToTest {
91-
Context "Testing suspect pages on <_.Name>" {
92-
It "Database <_.Name> should return 0 suspect pages on <_.Parent.Name>" -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database }else { $ExcludedDatabases -notcontains $PsItem.Name } } {
93-
$results = Get-DbaSuspectPage -SqlInstance $psitem.Parent -Database $psitem.Name
94-
@($results).Count | Should -Be 0 -Because "You do not want suspect pages - $results"
74+
75+
Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
76+
$skip = Get-DbcConfigValue skip.database.validdatabaseowner
77+
Context "Testing Database Owners on <_.Name>" {
78+
#TODO fix the it text - needs commas --> should be in this list ( sqladmin sa ) )
79+
It "Database <_.Name> - owner '<_.Owner>' should be in this list ( <_.ConfigValues.validdbownername> ) ) on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.validdbownerexclude -notcontains $PsItem.Name } } {
80+
$psitem.Owner | Should -BeIn $psitem.ConfigValues.validdbownername -Because "The account that is the database owner is not what was expected"
9581
}
9682
}
9783
}
98-
#>
9984

100-
Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
101-
BeforeAll {
102-
$ExcludedDatabases += Get-DbcConfigValue policy.validdbowner.excludedb
103-
}
104-
#add skip but where
105-
#$skip = Get-DbcConfigValue skip.instance.scanforstartupproceduresdisabled
10685

86+
Describe "Invalid Database Owner" -Tag InvalidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
87+
$skip = Get-DbcConfigValue skip.database.invaliddatabaseowner
10788
Context "Testing Database Owners on <_.Name>" {
108-
#TODO fix the it text - needs commas --> should be in this list ( sqladmin sa ) )
109-
It "Database <_.Name> - owner '<_.Owner>' should be in this list ( <_.ConfigValues.validdbownername> ) ) on <_.Parent.Name>" -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database }else { $ExcludedDatabases -notcontains $PsItem.Name } } {
110-
$psitem.Owner | Should -BeIn $psitem.ConfigValues.validdbownername -Because "The account that is the database owner is not what was expected"
89+
90+
It "Database <_.Name> - owner '<_.Owner>' should not be in this list ( <_.ConfigValues.invaliddbownername> ) ) on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.invaliddbownerexclude -notcontains $PsItem.Name } } {
91+
$psitem.Owner | Should -Not -BeIn $psitem.ConfigValues.invaliddbownername -Because "The database owner was one specified as incorrect"
11192
}
11293
}
11394
}
95+
96+
Describe "AsymmetricKeySize" -Tag AsymmetricKeySize, CIS, Database -ForEach $InstancesToTest {
97+
$skip = Get-DbcConfigValue skip.security.asymmetrickeysize
98+
Context "Testing Asymmetric Key Size is 2048 or higher on <_.Name>" {
99+
It "Database <_.Name> asymmetric key size should be at least 2048 on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.asymmetrickeysizeexclude -notcontains $PsItem.Name } } {
100+
$psitem.AsymmetricKeySize | Should -Be 0 -Because "Asymmetric keys should have a key length greater than or equal to 2048"
101+
#$psitem.AsymmetricKeySize | Should -BeGreaterOrEqual 2048 -Because "Asymmetric keys should have a key length greater than or equal to 2048"
102+
}
103+
}
104+
}
105+
106+
Describe "Auto Close" -Tag AutoClose, High, Database -ForEach $InstancesToTest {
107+
$skip = Get-DbcConfigValue skip.database.autoclose
108+
Context "Testing Auto Close on <_.Name>" {
109+
It "Database <_.Name> should have Auto Close set to <_.ConfigValues.autoclose> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.autocloseexclude -notcontains $PsItem.Name } } {
110+
$psitem.AutoClose | Should -Be $psitem.ConfigValues.autoclose -Because "Because!"
111+
}
112+
}
113+
}
114+
115+
Describe "Auto Shrink" -Tag AutoShrink, High, Database -ForEach $InstancesToTest {
116+
$skip = Get-DbcConfigValue skip.database.autoshrink
117+
Context "Testing Auto Shrink on <_.Name>" {
118+
It "Database <_.Name> should have Auto Shrink set to <_.ConfigValues.autoshrink> on <_.SqlInstance>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database } else { $psitem.ConfigValues.autoshrinkexclude -notcontains $PsItem.Name } } {
119+
$psitem.AutoShrink | Should -Be $psitem.ConfigValues.autoshrink -Because "Shrinking databases causes fragmentation and performance issues"
120+
}
121+
}
122+
}

checks/Instancev5.Tests.ps1

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,37 @@ Describe "Trace Flags Expected" -Tag TraceFlagsExpected, TraceFlag, High, Instan
225225
$PsItem.ExpectedTraceFlags.ActualTraceFlags.TraceFlag | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set on $($Psitem.Name) "
226226
}
227227
It "Expected Trace Flags <_.ExpectedTraceFlag> to exist on <_.InstanceName>" -Skip:$skip -ForEach ($PsItem.ExpectedTraceFlags | Where-Object { $psitem.ExpectedTraceFlag -ne 'null' }) {
228-
$PsItem.ActualTraceFlags.TraceFlag | Should -Contain $PsItem.ExpectedTraceFlag -Because "We expect that Trace Flag $($PsItem.ExpectedTraceFlag) will be set on $($Psitem.InstanceName) "
228+
$PsItem.ActualTraceFlags.TraceFlag | Should -Contain $PsItem.ExpectedTraceFlag -Because "We expect that Trace Flag $($PsItem.ExpectedTraceFlag) will be set on $($Psitem.InstanceName) "
229229
}
230230
}
231231
}
232232

233+
Describe "Trace Flags Not Expected" -Tag TraceFlagsNotExpected, TraceFlag, Medium, Instance -ForEach $InstancesToTest {
234+
$skip = Get-DbcConfigValue skip.instance.TraceFlagsNotExpected
235+
Context "Testing Not Expected Trace Flags on <_.Name>" {
236+
It "Expected No Trace Flags except for <_.ConfigValues.TraceFlagsExpected> to exist on <_.Name>" -Skip:$skip -ForEach ($Psitem | Where-Object { $null -eq $psitem.ConfigValues.TraceFlagsNotExpected }) {
237+
$PsItem.NotExpectedTraceFlags.ActualTraceFlags.TraceFlag | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set on $($Psitem.Name) except for $($psitem.ConfigValues.ExpectedTraceFlag)"
238+
}
239+
It "Expected <_.NotExpectedTraceFlag> Trace Flag to not exist on <_.InstanceName>" -Skip:$skip -ForEach ($PsItem.NotExpectedTraceFlags | Where-Object { $psitem.NotExpectedTraceFlag -ne 'null' }) {
240+
$PsItem.ActualTraceFlags.TraceFlag | Should -Not -Contain $PsItem.NotExpectedTraceFlag -Because "We expect that Trace Flag $($PsItem.NotExpectedTraceFlag) will not be set on $($Psitem.InstanceName) except for $($psitem.ConfigValues.ExpectedTraceFlag)"
241+
}
242+
}
243+
}
244+
245+
Describe "CLR Enabled" -Tag CLREnabled, security, CIS, High, Instance -ForEach $InstancesToTest {
246+
$skip = Get-DbcConfigValue skip.instance.CLREnabled
247+
Context "Testing CLR Enabled on <_.Name>" {
248+
It "CLR Enabled is set to <_.ConfigValues.CLREnabled> on <_.Name>" -Skip:$skip {
249+
$PSItem.Configuration.IsSqlClrEnabled.ConfigValue -eq 1 | Should -Be $psitem.ConfigValues.CLREnabled -Because 'This is the setting you have chosen for CLR Enabled'
250+
}
251+
}
252+
}
253+
254+
Describe "sp_whoisactive is Installed" -Tag WhoIsActiveInstalled, Low, Instance -ForEach $InstancesToTest {
255+
$skip = Get-DbcConfigValue skip.instance.WhoIsActiveInstalled
256+
Context "Testing WhoIsActive exists on <_.Name>" {
257+
It "WhoIsActive should exist on <_.ConfigValues.whoisactivedatabase> on <_.Name>" -Skip:$skip {
258+
$Psitem.ConfigValues.WhoIsActiveInstalled | Should -Be 1 -Because "The sp_WhoIsActive stored procedure should be installed in $($psitem.ConfigValues.whoisactivedatabase)"
259+
}
260+
}
261+
}

0 commit comments

Comments
 (0)