From 89bb3de7771b733b69b1fcb320d950f300433042 Mon Sep 17 00:00:00 2001 From: SQLDbaWithABeard Date: Thu, 28 Apr 2022 14:04:07 +0100 Subject: [PATCH] so that the old checks will match the new ones!! #882 --- checks/Instance.Tests.ps1 | 53 +++++++++++++-------- internal/assertions/Instance.Assertions.ps1 | 30 ++++++------ 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/checks/Instance.Tests.ps1 b/checks/Instance.Tests.ps1 index ca895dee..175b9963 100644 --- a/checks/Instance.Tests.ps1 +++ b/checks/Instance.Tests.ps1 @@ -541,29 +541,29 @@ $Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks Describe "XE Sessions That Are Allowed to Be Running" -Tags XESessionRunningAllowed, ExtendedEvent, Medium, $filename { $xesession = Get-DbcConfigValue policy.xevent.validrunningsession if ((Get-Version -SQLInstance $psitem) -gt 10) { - # no point running if we dont have something to check - if ($xesession) { - if ($NotContactable -contains $psitem) { - Context "Checking running sessions allowed on $psitem" { - It "Can't Connect to $Psitem" { - $true | Should -BeFalse -Because "The instance should be available to be connected to!" + # no point running if we dont have something to check + if ($xesession) { + if ($NotContactable -contains $psitem) { + Context "Checking running sessions allowed on $psitem" { + It "Can't Connect to $Psitem" { + $true | Should -BeFalse -Because "The instance should be available to be connected to!" + } } } - } - else { - Context "Checking running sessions allowed on $psitem" { - @(Get-DbaXESession -SqlInstance $psitem).Where{ $_.Status -eq 'Running' }.ForEach{ - It "Session $($Psitem.Name) is allowed to be running on $Instance" { - $psitem.name | Should -BeIn $xesession -Because "Only these sessions are allowed to be running" + else { + Context "Checking running sessions allowed on $psitem" { + @(Get-DbaXESession -SqlInstance $psitem).Where{ $_.Status -eq 'Running' }.ForEach{ + It "Session $($Psitem.Name) is allowed to be running on $Instance" { + $psitem.name | Should -BeIn $xesession -Because "Only these sessions are allowed to be running" + } } } } } + else { + Write-Warning "You need to use Set-DbcConfig -Name policy.xevent.validrunningsession -Value to add some Extended Events session names to run this check" + } } - else { - Write-Warning "You need to use Set-DbcConfig -Name policy.xevent.validrunningsession -Value to add some Extended Events session names to run this check" - } - } else { Context "Checking running sessions allowed on $psitem" { It "Version does not support XE sessions on $Instance" -skip { @@ -788,9 +788,20 @@ $Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks } } else { + if ($null -eq $ExpectedTraceFlags) { $ExpectedTraceFlags = 0 } + $ActualTraceflags = (Get-DbaTraceFlag -SqlInstance $psitem).TraceFlag Context "Testing Expected Trace Flags on $psitem" { - It "Expected Trace Flags $ExpectedTraceFlags exist on $psitem" { - Assert-TraceFlag -SQLInstance $psitem -ExpectedTraceFlag $ExpectedTraceFlags + foreach ($ExpectedTraceFlag in $ExpectedTraceFlags) { + if ($ExpectedTraceFlag -ne 0) { + It "Expected Trace Flag $ExpectedTraceFlag to exist on $psitem" { + Assert-TraceFlag -ActualTraceflags $ActualTraceflags -ExpectedTraceFlag $ExpectedTraceFlag + } + } + else { + It "Expected No Trace Flag to exist on $psitem" { + Assert-TraceFlag -ActualTraceflags $ActualTraceflags -ExpectedTraceFlag $ExpectedTraceFlag + } + } } } } @@ -1204,7 +1215,7 @@ $Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks else { Context "Testing if the suspect_pages table is nearing the limit of 1000 rows on $psitem" { It "The suspect_pages table in msdb shouldn't be nearing the limit of 1000 rows on $psitem" -Skip:$skip { - (((Get-DbaSuspectPage -SqlInstance $psitem | Measure-Object).Count)/1000)*100 | Should -BeLessThan $thresholdPercent + (((Get-DbaSuspectPage -SqlInstance $psitem | Measure-Object).Count) / 1000) * 100 | Should -BeLessThan $thresholdPercent } } } @@ -1214,14 +1225,14 @@ $Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks $skip = Get-DbcConfigValue skip.security.SQLMailXPsDisabled if ($NotContactable -contains $psitem) { Context "Testing SQL Mail XPs on $psitem" { - It "Can't Connect to $Psitem" -Skip:($skip -or $InstanceSMO.VersionMajor -gt 10){ + It "Can't Connect to $Psitem" -Skip:($skip -or $InstanceSMO.VersionMajor -gt 10) { $false | Should -BeTrue -Because "The instance should be available to be connected to!" } } } else { Context "Testing SQL Mail XPs on $psitem" { - It "The SQL Mail XPs should be disabled on $psitem" -Skip:($skip -or $InstanceSMO.VersionMajor -gt 10){ + It "The SQL Mail XPs should be disabled on $psitem" -Skip:($skip -or $InstanceSMO.VersionMajor -gt 10) { Assert-SQLMailXPs -AllInstanceInfo $AllInstanceInfo } } diff --git a/internal/assertions/Instance.Assertions.ps1 b/internal/assertions/Instance.Assertions.ps1 index 78eb7118..35deaf4d 100644 --- a/internal/assertions/Instance.Assertions.ps1 +++ b/internal/assertions/Instance.Assertions.ps1 @@ -214,9 +214,10 @@ function Get-AllInstanceInfo { AND name <> 'sp_MSrepl_startup'" $results = Invoke-DbaQuery -SqlInstance $Instance -Query $query - if ($null -eq $results) { + if ($null -eq $results) { $Value = 0 - } else { + } + else { $Value = $SpConfig.ConfiguredValue } @@ -251,7 +252,7 @@ function Get-AllInstanceInfo { $MaxDump = [pscustomobject] @{ # Warning Action removes dbatools output for version too low from test results # Skip on the it will show in the results - Count = (@(Get-DbaDump -SqlInstance $Instance -WarningAction SilentlyContinue).Where{ $_.CreationTime -gt $datetocheckfrom}).Count + Count = (@(Get-DbaDump -SqlInstance $Instance -WarningAction SilentlyContinue).Where{ $_.CreationTime -gt $datetocheckfrom }).Count } } catch { @@ -446,10 +447,10 @@ function Get-AllInstanceInfo { if ($There) { try { $ComputerName, $InstanceName = $Instance.Name.Split('\') - if ($null -eq $InstanceName){ + if ($null -eq $InstanceName) { $InstanceName = 'MSSQLSERVER' } - $logins = Get-DbaLogin -SqlInstance $Instance | Where-Object {$_.LoginType -eq 'WindowsGroup' -and $_.Name.Split('\') -eq $ComputerName} + $logins = Get-DbaLogin -SqlInstance $Instance | Where-Object { $_.LoginType -eq 'WindowsGroup' -and $_.Name.Split('\') -eq $ComputerName } if ($null -ne $logins) { $LocalWindowsGroup = [pscustomobject] @{ Exist = $true @@ -727,7 +728,7 @@ function Get-AllInstanceInfo { if ($There) { try { $LoginCheckPolicy = [pscustomobject] @{ - Count = @(Get-DbaLogin -SQLInstance $instance -Type SQL | Where-Object { $_.PasswordPolicyEnforced -eq $false -and $_.IsDisabled -eq $false}).Count + Count = @(Get-DbaLogin -SQLInstance $instance -Type SQL | Where-Object { $_.PasswordPolicyEnforced -eq $false -and $_.IsDisabled -eq $false }).Count } } catch { @@ -751,7 +752,7 @@ function Get-AllInstanceInfo { $role = Get-DbaServerRole -SQLInstance $instance -ServerRole "sysadmin" $LoginPasswordExpiration = [pscustomobject] @{ - Count = @(Get-DbaLogin -SQLInstance $instance -Login @($role.Login) -Type SQL | Where-Object { $_.PasswordExpirationEnabled -eq $false -and $_.IsDisabled -eq $false}).Count + Count = @(Get-DbaLogin -SQLInstance $instance -Login @($role.Login) -Type SQL | Where-Object { $_.PasswordExpirationEnabled -eq $false -and $_.IsDisabled -eq $false }).Count } } catch { @@ -842,7 +843,7 @@ function Get-AllInstanceInfo { LoginCheckPolicy = $LoginCheckPolicy LoginPasswordExpiration = $LoginPasswordExpiration LoginMustChange = $LoginMustChange - SQLMailXPsDisabled = $SQLMailXPsDisabled + SQLMailXPsDisabled = $SQLMailXPsDisabled } } @@ -954,16 +955,15 @@ function Assert-TwoDigitYearCutoff { function Assert-TraceFlag { Param( - [string]$SQLInstance, + $ActualTraceflags, [int[]]$ExpectedTraceFlag ) - if ($null -eq $ExpectedTraceFlag) { - (Get-DbaTraceFlag -SqlInstance $SQLInstance).TraceFlag | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set on $SQLInstance" + + if ($ExpectedTraceFlag -ne 0) { + $ExpectedTraceFlag | Should -BeIn $ActualTraceflags -Because "We expect that Trace Flag $ExpectedTraceFlag will be set" } else { - @($ExpectedTraceFlag).ForEach{ - (Get-DbaTraceFlag -SqlInstance $SQLInstance).TraceFlag | Should -Contain $PSItem -Because "We expect that Trace Flag $PsItem will be set on $SQLInstance" - } + $ActualTraceflags | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set" } } function Assert-NotTraceFlag { @@ -976,7 +976,7 @@ function Assert-NotTraceFlag { ) if ($null -eq $NotExpectedTraceFlag) { - (@(Get-DbaTraceFlag -SqlInstance $SQLInstance).Where{ $_.TraceFlag -notin $ExpectedTraceFlag} | Select-Object).TraceFlag | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set on $SQLInstance" + (@(Get-DbaTraceFlag -SqlInstance $SQLInstance).Where{ $_.TraceFlag -notin $ExpectedTraceFlag } | Select-Object).TraceFlag | Should -BeNullOrEmpty -Because "We expect that there will be no Trace Flags set on $SQLInstance" } else { @($NotExpectedTraceFlag).ForEach{