Skip to content

Commit

Permalink
SqlTraceFlag: Fix for #1688 (#1689)
Browse files Browse the repository at this point in the history
- SqlTraceFlag
  - Fixed $null reference error when no actual trace flags are present.
    Added two arrays to prevent a $nul reference at compare-object
    (issue #1688).
  • Loading branch information
Fiander authored Feb 12, 2021
1 parent d7cddae commit dc4301c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- SqlTraceFlag
- Fixed $nul reference error when no actual traceflags are present.
Added two arrays to prevent a $nul reference at compare-object.
([issue #1688](https://github.com/dsccommunity/SqlServerDsc/issues/1688)).
- SqlServerDsc
- Removed a left-over comment in the file `analyzersettings.psd1`.

Expand Down
26 changes: 23 additions & 3 deletions source/DSCResources/DSC_SqlTraceFlag/DSC_SqlTraceFlag.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,19 @@ function Set-TargetResource

if ($PSBoundParameters.ContainsKey('TraceFlags'))
{
$wishTraceFlags.AddRange($TraceFlags)
if ($null -ne $TraceFlags)
{
$wishTraceFlags.AddRange($TraceFlags)
}
}
else
{
$getTargetResourceResult = Get-TargetResource @getTargetResourceParameters

$wishTraceFlags.AddRange($getTargetResourceResult.TraceFlags)
if ($null -ne $getTargetResourceResult.TraceFlags)
{
$wishTraceFlags.AddRange($getTargetResourceResult.TraceFlags)
}

if ($PSBoundParameters.ContainsKey('TraceFlagsToInclude'))
{
Expand Down Expand Up @@ -371,8 +377,22 @@ function Test-TargetResource
}
else
{
$reference = [System.Collections.ArrayList]::new()

if ($null -ne $getTargetResourceResult.TraceFlags)
{
$reference.AddRange($getTargetResourceResult.TraceFlags)
}

$difference = [System.Collections.ArrayList]::new()

if ($null -ne $TraceFlags)
{
$difference.AddRange($TraceFlags)
}

# Compare $TraceFlags to the Actual TraceFlags ($getTargetResourceResult.TraceFlags) to see if they contain the same values.
$nullIfTheSame = Compare-Object -ReferenceObject $getTargetResourceResult.TraceFlags -DifferenceObject $TraceFlags
$nullIfTheSame = Compare-Object -ReferenceObject $reference -DifferenceObject $difference
if ($null -ne $nullIfTheSame)
{
Write-Verbose -Message (
Expand Down
131 changes: 123 additions & 8 deletions tests/Unit/DSC_SqlTraceFlag.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
Mock -CommandName Import-SQLPSModule
}

Context 'When the system is not in the desired state and TraceFlags is empty' {
Context 'When the system is not in the desired state and TraceFlags is empty with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -375,7 +375,7 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is in the desired state and TraceFlags is empty' {
Context 'When the system is in the desired state and TraceFlags is empty without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
Expand All @@ -393,7 +393,7 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags' {
Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -411,7 +411,25 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude are not in the actual TraceFlags' {
Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlags = '3228'
}
}

It 'Should return false when Traceflags do not match the actual TraceFlags' {
$result = Test-TargetResource @testParameters
$result | Should -BeFalse
}

It 'Should be executed once' {
Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope Context
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude are not in the actual TraceFlags with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -429,6 +447,24 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude are not in the actual TraceFlags without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlagsToInclude = '3228'
}
}

It 'Should return false when TraceflagsToInclude are not in the actual TraceFlags' {
$result = Test-TargetResource @testParameters
$result | Should -BeFalse
}

It 'Should be executed once' {
Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope Context
}
}

Context 'When the system is in the desired state and ensure is set to Present and `$TraceFlagsToInclude are in the actual TraceFlags' {
BeforeAll {
$testParameters = $mockDefaultParameters1
Expand Down Expand Up @@ -465,7 +501,7 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is in the desired state and ensure is set to Present and `$TraceFlagsToExclude are not in the actual TraceFlags' {
Context 'When the system is in the desired state and ensure is set to Present and `$TraceFlagsToExclude are not in the actual TraceFlags with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -483,6 +519,23 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
}
}

Context 'When the system is in the desired state and ensure is set to Present and `$TraceFlagsToExclude are not in the actual TraceFlags without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlagsToExclude = '3228'
}
}

It 'Should return true when TraceflagsToExclude are not in the actual TraceFlags' {
$result = Test-TargetResource @testParameters
$result | Should -BeTrue
}

It 'Should be executed once' {
Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope Context
}
}

Context 'When both the parameters TraceFlags and TraceFlagsToInclude are assigned a value.' {
BeforeAll {
Expand Down Expand Up @@ -651,7 +704,7 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf
Mock -CommandName Import-SQLPSModule
}

Context 'When the system is not in the desired state and ensure is set to Absent' {
Context 'When the system is not in the desired state and ensure is set to Absent with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -670,10 +723,30 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf

Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It
}
}

Context 'When the system is not in the desired state and ensure is set to Absent without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlags = '3228'
}
}

It 'Should not throw when calling the alter method' {
{ Set-TargetResource @testParameters } | Should -Not -Throw
$script:mockMethodAlterRan | Should -BeTrue -Because 'Alter should run'
$script:mockMethodAlterValue | Should -Be @"
-dC:\Program Files\Microsoft SQL Server\MSSQL15.INST00\MSSQL\DATA\master.mdf;-eC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\Log\ERRORLOG;-lC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf;-T3228
"@ -Because 'Alter must change the value correct'

Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags' {
Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -694,7 +767,28 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf;-T3228
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude is not in TraceFlags' {
Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlags does not match the actual TraceFlags without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlags = '3228'
}
}

It 'Should not throw when calling the alter method' {
{ Set-TargetResource @testParameters } | Should -Not -Throw
$script:mockMethodAlterRan | Should -BeTrue -Because 'Alter should run'
$script:mockMethodAlterValue | Should -Be @"
-dC:\Program Files\Microsoft SQL Server\MSSQL15.INST00\MSSQL\DATA\master.mdf;-eC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\Log\ERRORLOG;-lC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf;-T3228
"@ -Because 'Alter must change the value correct'

Assert-MockCalled -CommandName New-Object -Exactly -Times 1 -Scope It
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude is not in TraceFlags with existing traceflag' {
BeforeAll {
$testParameters = $mockDefaultParameters1
$testParameters += @{
Expand All @@ -715,6 +809,27 @@ Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf;-T3226;-T1802;-T3228
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToInclude is not in TraceFlags without existing traceflag' {
BeforeAll {
$testParameters = $mockInst00Parameters
$testParameters += @{
TraceFlagsToInclude = '3228'
}
}

It 'Should not throw when calling the alter method' {
{ Set-TargetResource @testParameters } | Should -Not -Throw
$script:mockMethodAlterRan | Should -BeTrue
$script:mockMethodAlterValue | Should -Be @"
-dC:\Program Files\Microsoft SQL Server\MSSQL15.INST00\MSSQL\DATA\master.mdf;-eC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\Log\ERRORLOG;-lC:\Program Files\Microsoft SQL
Server\MSSQL15.INST00\MSSQL\DATA\mastlog.ldf;-T3228
"@

Assert-MockCalled -CommandName New-Object -Exactly -Times 2 -Scope It
}
}

Context 'When the system is not in the desired state and ensure is set to Present and `$TraceFlagsToExclude is in TraceFlags' {
BeforeAll {
$testParameters = $mockDefaultParameters1
Expand Down

0 comments on commit dc4301c

Please sign in to comment.