Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlTraceFlag: Fix for #1688 #1689

Merged
merged 31 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
69ebf57
Initial version of new resource SqlTraceFlag
Fiander Nov 26, 2020
e1cc2da
small change to trigger pipline
Fiander Nov 26, 2020
f7cab74
Change Schema friendliname
Fiander Nov 26, 2020
105dd1b
schema change
Fiander Nov 26, 2020
c5346a2
some HQRM fixes
Fiander Nov 26, 2020
8125732
first batch of fixes
Fiander Nov 27, 2020
5ff9a87
Removed to much
Fiander Nov 27, 2020
7e92280
Style guideline fixes and move change log entries
johlju Nov 27, 2020
a7fbcf3
Fix examples
johlju Nov 27, 2020
e8f27db
Merge pull request #1 from johlju/traceflag-styleguideline
Fiander Nov 27, 2020
06d505d
Removed "Envorce" also in all tests
Fiander Nov 28, 2020
8cd6970
Remove Ensure from examples
Fiander Nov 28, 2020
ba872f2
first batch of fixes.
Fiander Nov 29, 2020
ca65655
removed readonly parameters, changed to Restart-SqlService, adjusted …
Fiander Dec 3, 2020
36eb33c
last fixes ( i hope )
Fiander Dec 4, 2020
8b603c3
last fixes
Fiander Dec 5, 2020
ba5ff81
Last changes. ( i hope ;-) )
Fiander Dec 5, 2020
bd76c22
mised a RestartService
Fiander Dec 5, 2020
281d19b
Adden to SqlServerDsc and SqlTraceFlag get missed a return value
Fiander Dec 5, 2020
5ac0c80
Fixed Assembly not loaded error #1680
Fiander Jan 29, 2021
0893b13
import external main
Fiander Jan 29, 2021
c3ec2e2
fix 1680
Fiander Jan 29, 2021
df5f0f4
Some small style fixes and a mock in test
Fiander Jan 29, 2021
bb48262
Fix in mock
Fiander Jan 30, 2021
8c290fc
Fix for #1688 and aditional tests to find the problem.
Fiander Feb 10, 2021
8f940d7
added changelog.md
Fiander Feb 10, 2021
16aff4a
added entry for changelog.md
Fiander Feb 10, 2021
50801ad
beter changelog entry
Fiander Feb 10, 2021
c6fc093
some HQRM fixes
Fiander Feb 11, 2021
d1c5769
fix changelog.md
Fiander Feb 11, 2021
c250c97
take two
Fiander Feb 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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