Skip to content

Commit

Permalink
add suspectpage test #884
Browse files Browse the repository at this point in the history
  • Loading branch information
jpomfret committed May 4, 2022
1 parent 3379941 commit 4b46ed2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
52 changes: 14 additions & 38 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 , $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,25 +35,28 @@ 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


<#
- copy in test
- add skip after describe
$skip = Get-DbcConfigValue skip.database.databasecollation
add to IT -Skip:$skip
#>


Describe "Suspect Page" -Tags SuspectPage, High , Database -ForEach $InstancesToTest {
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"
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 <_.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 "Database Collation" -Tag DatabaseCollation, High, Database -ForEach $InstancesToTest {
# TODO: just add reporting servers into config? rather than here?
#$exclude = "ReportingServer", "ReportingServerTempDB"

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 } } {
Expand All @@ -100,7 +76,7 @@ Describe "Valid Database Owner" -Tag ValidDatabaseOwner, Medium, Database -ForEa
$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>" -Skip:$skip -ForEach $psitem.Databases.Where{ if ($Database) { $_.Name -in $Database }else { $psitem.ConfigValues.validdbownerexclude -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"
}
}
Expand Down
4 changes: 3 additions & 1 deletion internal/configurations/configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Set-PSFConfig -Module dbachecks -Name skip.database.filegrowthdisabled -Validati
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"
Expand Down Expand Up @@ -340,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
8 changes: 8 additions & 0 deletions internal/functions/Get-AllDatabaseInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ function Get-AllDatabaseInfo {
$collation = $true
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'wrongcollation' -Value (Get-DbcConfigValue policy.database.wrongcollation)
}

'SuspectPage' {
$suspectPage = $true
$ConfigValues | Add-Member -MemberType NoteProperty -Name 'suspectpageexclude' -Value (Get-DbcConfigValue policy.suspectpage.excludedb)
}

Default { }
}

Expand All @@ -71,9 +77,11 @@ function Get-AllDatabaseInfo {
Databases = $Instance.Databases.Foreach{
[PSCustomObject]@{
Name = $psitem.Name
SqlInstance = $Instance.Name
Owner = if ($owner) { $psitem.owner }
ServerCollation = if ($collation) { $Instance.collation }
Collation = if ($collation) { $psitem.collation }
SuspectPage = if ($suspectPage) { (Get-DbaSuspectPage -SqlInstance $Instance -Database $psitem.Name | Measure-Object).Count }
ConfigValues = $ConfigValues # can we move this out?
}
}
Expand Down

0 comments on commit 4b46ed2

Please sign in to comment.