Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into pesterv5
  • Loading branch information
SQLDBAWithABeard committed May 5, 2022
2 parents 2068f1f + 4b46ed2 commit 91102f5
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 83 deletions.
21 changes: 12 additions & 9 deletions Perf Testing pesterv5.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@

ipmo ./dbachecks.psd1

#
#

$Checks = 'TraceFlagsExpected','TwoDigitYearCutoff','MaxDopInstance','ErrorLogCount','ModelDbGrowth','DefaultBackupCompression','SaExist','SaDisabled','SaRenamed','DefaultFilePath','AdHocDistributedQueriesEnabled','AdHocWorkload', 'DefaultTrace', 'OleAutomationProceduresDisabled', 'CrossDBOwnershipChaining', 'ScanForStartupProceduresDisabled', 'RemoteAccessDisabled', 'SQLMailXPsDisabled', 'DAC', 'OLEAutomation'
$Checks = 'TraceFlagsExpected'
Compare-CheckRuns -Checks $checks


<#
When there are default skips (some of the CIS checks) we need to set the configs and check
Expand Down Expand Up @@ -58,14 +57,14 @@ Disable-DbaTraceFlag -SqlInstance $Sqlinstances -SqlCredential $cred -TraceFlag

# If you get odd results - or you dont get any checks run

# run the import module and the Invoke Dbc Check with Verbose and that might show you New-Json messing
# run the import module and the Invoke Dbc Check with Verbose and that might show you New-Json messing
# with your files or that you are looking in PSMOdulePath instead of Git Repo path (run Reset-dbcConfig to fix that)

function Compare-CheckRuns {
param($Checks)
$password = ConvertTo-SecureString "dbatools.IO" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sqladmin", $password
$Sqlinstances = 'localhost,7401', 'localhost,7402', 'localhost,7403'
$Sqlinstances = 'localhost,14333', 'localhost,14334' #'localhost,7401', 'localhost,7402', 'localhost,7403'

$originalCode = {
Invoke-DbcCheck -SqlInstance $Sqlinstances -Check $Checks -SqlCredential $cred -legacy $true -Show None
Expand All @@ -82,16 +81,16 @@ $originalCodeMessage = "With original Code it takes {0} MilliSeconds" -f $origin


$savingMessage = "
Running with
Running with
{3}
{3}
Checks against 3 SQL Containers
Checks against $($Sqlinstances.Count) SQL Containers
With original Code it takes {1} Seconds
With New Code it takes {4} Seconds
New Code for these {5} checks
New Code for these {5} checks
is saving {0} seconds
from a run of {1} seconds
New Code runs in {2} % of the time
Expand All @@ -100,3 +99,7 @@ cls

Write-PSFMessage -Message $savingMessage -Level Output
}


$Checks = 'DbaOperator'
Compare-CheckRuns -Checks $checks
40 changes: 34 additions & 6 deletions checks/Agentv5.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ BeforeDiscovery {
# Gather the instances we know are not contactable
[string[]]$NotContactable = (Get-PSFConfig -Module dbachecks -Name global.notcontactable).Value
# Get all the tags in use in this run
$Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks -ExcludeCheck $ChecksToExclude
$Tags = Get-CheckInformation -Check $Check -Group Agent -AllChecks $AllChecks -ExcludeCheck $ChecksToExclude

$InstancesToTest = @(Get-Instance).ForEach{
# just add it to the Not Contactable list
if ($NotContactable -notcontains $psitem) {
$Instance = $psitem
try {
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
}
catch {
$NotContactable += $Instance
Expand All @@ -37,15 +37,40 @@ BeforeDiscovery {


Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $InstancesToTest {
$DatabaseMailEnabled = Get-DbcConfigValue policy.security.DatabaseMailEnabled
$skip = Get-DbcConfigValue skip.agent.databasemailenabled
Context "Testing Database Mail XPs on <_.Name>" {
It "Testing Database Mail XPs is set to $DatabaseMailEnabled on <_.Name>" -Skip:$skip {
$PSItem.Configuration.DatabaseMailEnabled | Should -Be $PSItem.ConfigValues.DatabaseMailEnabled -Because 'The Database Mail XPs setting should be set correctly'
It "Testing Database Mail XPs is set to <_.DatabaseMailEnabled> on <_.Name>" -Skip:$skip {
$PSItem.DatabaseMailEnabled | Should -Be $PSItem.ConfigValues.DatabaseMailEnabled -Because 'The Database Mail XPs setting should be set correctly'
}
}
}

Describe "SQL Agent Account" -Tag AgentServiceAccount, ServiceAccount -ForEach $InstancesToTest {
#can't check agent on container - hmm does this actually work with instance need to check
#if (-not $IsLinux -and ($PSItem.HostPlatform -ne 'Linux')) {
$skipServiceState = Get-DbcConfigValue skip.agent.servicestate
$skipServiceStartMode = Get-DbcConfigValue skip.agent.servicestartmode

Write-PSFMessage -Message "Agent = $($PSItem | Out-String)" -Level Verbose

Context "Testing SQL Agent is running on <_.Name>" {
It "SQL Agent should be running for <_.InstanceName> on <_.Name>" -Skip:$skipServiceState {
$PSItem.Agent.State | Should -Be "Running" -Because 'The agent service is required to run SQL Agent jobs'
}
}
if ($PSItem.IsClustered) {
It "SQL Agent service should have a start mode of Manual for FailOver Clustered Instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
$PSItem.Agent.StartMode | Should -Be "Manual" -Because 'Clustered Instances required that the Agent service is set to manual'
}
}
else {
It "SQL Agent service should have a start mode of Automatic for standalone instance <_.InstanceName> on <_.Name>" -Skip:$skipServiceStartMode {
$PSItem.Agent.StartMode | Should -Be "Automatic" -Because 'Otherwise the Agent Jobs wont run if the server is restarted'
}
}
#}
}

# Describe "SQL Agent Account" -Tags AgentServiceAccount, ServiceAccount, $filename {
# if ($NotContactable -contains $psitem) {
# Context "Testing SQL Agent is running on $psitem" {
Expand Down Expand Up @@ -466,4 +491,7 @@ Describe "Database Mail XPs" -Tag DatabaseMailEnabled, CIS, security -ForEach $I
# }
# }
# }
# }
# }



92 changes: 34 additions & 58 deletions checks/Databasev5.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
# So the v5 files need to be handled differently.
# Ww will start with a BeforeDiscovery which for the Database Checks will need to gather the Instances up front
# Ww will start with a BeforeDiscovery , $Filename which for the Database Checks will need to gather the Instances up front
BeforeDiscovery {
<#
. $PSScriptRoot/../internal/assertions/Database.Assertions.ps1
[array]$ExcludedDatabases = Get-DbcConfigValue command.invokedbccheck.excludedatabases
$ExcludedDatabases += $ExcludeDatabase
[string[]]$NotContactable = (Get-PSFConfig -Module dbachecks -Name global.notcontactable).Value
$InstancesToTest = @(Get-Instance).ForEach{
# just add it to the Not Contactable list
if ($NotContactable -notcontains $psitem) {
$Instance = $psitem
try {
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
} catch {
$NotContactable += $Instance
}
if ($NotContactable -notcontains $psitem) {
if ($null -eq $InstanceSMO.version) {
$NotContactable += $Instance
} else {
$InstanceSMO
}
}
}
}
Write-PSFMessage -Message "Instances = $InstancesToTest" -Level Significant
Set-PSFConfig -Module dbachecks -Name global.notcontactable -Value $NotContactable
#>

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

# Each Test will have a -ForEach for the Instances and the InstancesToTest object will have a
# lot of information gathered up front to reduce trips and connections to the database


<#
Describe "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
BeforeAll {
$Wrongcollation = Get-DbcConfigValue policy.database.wrongcollation
$exclude = "ReportingServer", "ReportingServerTempDB"
$exclude += $Wrongcollation
$exclude += $ExcludedDatabases
- copy in test
- add skip after describe
$skip = Get-DbcConfigValue skip.database.databasecollation
add to IT -Skip:$skip
#>

}

Context "Testing database collation on <_.Name>" {
It "Database <_.Database> collation <_.DatabaseCollation> should match server collation <_.ServerCollation> on <_.SqlInstance>" -ForEach @(Test-DbaDbCollation -SqlInstance $psitem -Database $Database -ExcludeDatabase $exclude) {
$psitem.ServerCollation | Should -Be $psitem.DatabaseCollation -Because "You will get collation conflict errors in tempdb"
}
if ($Wrongcollation) {
@(Test-DbaDbCollation -SqlInstance $psitem -Database $Wrongcollation ).ForEach{
It "Database $($psitem.Database) collation ($($psitem.DatabaseCollation)) should not match server collation ($($psitem.ServerCollation)) on $($psitem.SqlInstance)" {
$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"
}
}
}
}
}

Describe "Suspect Page" -Tag SuspectPage, High , Database -ForEach $InstancesToTest {
$skip = Get-DbcConfigValue skip.database.suspectpage
Context "Testing suspect pages on <_.Name>" {
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 } } {
$results = Get-DbaSuspectPage -SqlInstance $psitem.Parent -Database $psitem.Name
@($results).Count | Should -Be 0 -Because "You do not want suspect pages - $results"
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 } } {
$psitem.SuspectPage | Should -Be 0 -Because "You do not want any suspect pages"
}
}
}
#>

Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
BeforeAll {
$ExcludedDatabases += Get-DbcConfigValue policy.validdbowner.excludedb
Describe "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
Context "Testing database collation on <_.Name>" {
$skip = Get-DbcConfigValue skip.database.databasecollation
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 } } {
$psitem.ServerCollation | Should -Be $psitem.Collation -Because "You will get collation conflict errors in tempdb"
}

# wrong collation set
It "Database <_.Name> collation <_.Collation> should not match server collation <_.ServerCollation> on <_.SqlInstance>" -ForEach $psitem.Databases.Where{ $_.Name -in $psitem.ConfigValues.wrongcollation } {
$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"
}

}
#add skip but where
#$skip = Get-DbcConfigValue skip.instance.scanforstartupproceduresdisabled
}


Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEach $InstancesToTest {
$skip = Get-DbcConfigValue skip.database.validdatabaseowner
Context "Testing Database Owners on <_.Name>" {
#TODO fix the it text - needs commas --> should be in this list ( sqladmin sa ) )
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 } } {
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 } } {
$psitem.Owner | Should -BeIn $psitem.ConfigValues.validdbownername -Because "The account that is the database owner is not what was expected"
}
}
}


#and can evey check have a skip policy.GROUP.UNIQUETAG - if it doesnt have one already and that will live on the line below the describe



16 changes: 13 additions & 3 deletions internal/configurations/configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Set-PSFConfig -Module dbachecks -Name policy.database.logfilesizepercentage -Val
Set-PSFConfig -Module dbachecks -Name policy.database.logfilesizecomparison -Validation validation.logfilecomparisonvalidations -Value 'average' -Initialize -Description "How to compare data and log file size, options are maximum or average"
Set-PSFConfig -Module dbachecks -Name policy.database.filebalancetolerance -Value 5 -Initialize -Description "Percentage for Tolerance for checking for balanced files in a filegroups"
Set-PSFConfig -Module dbachecks -Name policy.database.filegrowthfreespacethreshold -Value 20 -Initialize -Description "Integer representing percentage of free space within a database file before warning"
Set-PSFConfig -Module dbachecks -Name policy.database.wrongcollation -Value @() -Initialize -Description "Databases that doesnt match server collation check"
Set-PSFConfig -Module dbachecks -Name policy.database.wrongcollation -Value @('ReportingServer', 'ReportingServerTempDB') -Initialize -Description "Databases that doesnt match server collation check"
Set-PSFConfig -Module dbachecks -Name policy.database.maxdopexcludedb -Value @() -Initialize -Description "Database Names that we don't want to check for maxdop"
Set-PSFConfig -Module dbachecks -Name policy.database.maxdop -Value 0 -Initialize -Description "The value for the database maxdop that we expect"
Set-PSFConfig -Module dbachecks -Name policy.database.status.excludereadonly -Value @() -Initialize -Description "Database names that we expect to be readonly"
Expand Down Expand Up @@ -246,6 +246,11 @@ Set-PSFConfig -Module dbachecks -Name skip.logfilecounttest -Validation bool -Va
Set-PSFConfig -Module dbachecks -Name skip.diffbackuptest -Validation bool -Value $false -Initialize -Description "Skip the Differential backup test"
Set-PSFConfig -Module dbachecks -Name skip.database.filegrowthdisabled -Validation bool -Value $true -Initialize -Description "Skip validation of datafiles which have growth value equal to zero."
Set-PSFConfig -Module dbachecks -Name skip.database.logfilecounttest -Validation bool -Value $false -Initialize -Description "Skip the logfilecount test"
Set-PSFConfig -Module dbachecks -Name skip.database.validdatabaseowner -Validation bool -Value $false -Initialize -Description "Skip the valid database owner test"
Set-PSFConfig -Module dbachecks -Name skip.database.databasecollation -Validation bool -Value $false -Initialize -Description "Skip the database collation test"
Set-PSFConfig -Module dbachecks -Name skip.database.suspectpage -Validation bool -Value $false -Initialize -Description "Skip the suspect pages test"


Set-PSFConfig -Module dbachecks -Name skip.logshiptesting -Validation bool -Value $false -Initialize -Description "Skip the logshipping test"
Set-PSFConfig -Module dbachecks -Name skip.instance.modeldbgrowth -Validation bool -Value $false -Initialize -Description "Skip the model database growth settings test"
Set-PSFConfig -Module dbachecks -Name skip.cluster.netclusterinterface -Validation bool -Value $false -Initialize -Description "Skip cluster private network interface checks"
Expand All @@ -254,7 +259,11 @@ Set-PSFConfig -Module dbachecks -Name skip.hadr.listener.tcpport -Validation boo
Set-PSFConfig -Module dbachecks -Name skip.hadr.replica.tcpport -Validation bool -Value $false -Initialize -Description "Skip the HADR Replica TCP port number (If port number is not standard across the entire AG architecture)"
Set-PSFConfig -Module dbachecks -Name skip.hadr.listener.pingcheck -Validation bool -Value $false -Initialize -Description "Skip the HADR listener ping test (especially useful for Azure and AWS)"
Set-PSFConfig -Module dbachecks -Name skip.instance.defaulttrace -Validation bool -Value $false -Initialize -Description "Skip the default trace check"
Set-PSFConfig -Module dbachecks -Name skip.agent.databasemailenabled -Validation bool -Value $false -Initialize -Description "Skip the long Database Mail Enabled agent check"
Set-PSFConfig -Module dbachecks -Name skip.agent.databasemailenabled -Validation bool -Value $false -Initialize -Description "Skip the Database Mail Enabled agent check"
Set-PSFConfig -Module dbachecks -Name skip.agent.servicestartmode -Validation bool -Value $false -Initialize -Description "Skip the Agent Service State check"
Set-PSFConfig -Module dbachecks -Name skip.agent.servicestate -Validation bool -Value $false -Initialize -Description "Skip the Agent Service Start Mode check"
Set-PSFConfig -Module dbachecks -Name skip.agent.operatorname -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Name check"
Set-PSFConfig -Module dbachecks -Name skip.agent.operatoremail -Validation bool -Value $false -Initialize -Description "Skip the Agent Operator Email check"
Set-PSFConfig -Module dbachecks -Name skip.agent.longrunningjobs -Validation bool -Value $false -Initialize -Description "Skip the long running agent jobs check"
Set-PSFConfig -Module dbachecks -Name skip.agent.lastjobruntime -Validation bool -Value $false -Initialize -Description "Skip the last agent job time check"
Set-PSFConfig -Module dbachecks -Name skip.instance.dac -Validation bool -Value $false -Initialize -Description "Skip Dedicated Administrator Connection (DAC) check"
Expand Down Expand Up @@ -332,7 +341,8 @@ Set-PSFConfig -Module dbachecks -Name command.invokedbccheck.excludedatabases -V
Set-PSFConfig -Module dbachecks -Name testing.integration.instance -Value @("localhost") -Initialize -Description "Default SQL Server instances to be used by integration tests"

# Suspect pages
Set-PSFConfig -Module dbachecks -Name policy.suspectpages.threshold -Value 90 -Initialize -Description "Default threshold (%) to check whether suspect_pages is nearing row limit of 1000"
Set-PSFConfig -Module dbachecks -Name policy.suspectpage.excludedb -Value 90 -Initialize -Description "Default threshold (%) to check whether suspect_pages is nearing row limit of 1000"
Set-PSFConfig -Module dbachecks -Name policy.suspectpage.threshold -Value 90 -Initialize -Description "Default threshold (%) to check whether suspect_pages is nearing row limit of 1000"

# Server
Set-PSFConfig -Module dbachecks -Name policy.server.cpuprioritisation -Value $true -Initialize -Description "Shall we skip the CPU Prioritisation check"
Expand Down
Loading

0 comments on commit 91102f5

Please sign in to comment.