Skip to content

Commit e051b4d

Browse files
authored
Add integration test for Set-SqlDscTraceFlag command (#2263)
1 parent 10519d9 commit e051b4d

File tree

4 files changed

+188
-1
lines changed

4 files changed

+188
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1111
correctly in real environments
1212
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
13+
- Added integration tests for `Set-SqlDscTraceFlag` command to ensure it functions
14+
correctly in real environments
15+
[issue #2232](https://github.com/dsccommunity/SqlServerDsc/issues/2232).
1316
- Added integration tests for `ConvertFrom-SqlDscServerPermission` command to
1417
ensure it functions correctly in real environments
1518
[issue #2210](https://github.com/dsccommunity/SqlServerDsc/issues/2210).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ stages:
296296
'tests/Integration/Commands/Get-SqlDscConfigurationOption.Integration.Tests.ps1'
297297
'tests/Integration/Commands/Set-SqlDscConfigurationOption.Integration.Tests.ps1'
298298
'tests/Integration/Commands/Test-SqlDscConfigurationOption.Integration.Tests.ps1'
299+
'tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1'
299300
'tests/Integration/Commands/Get-SqlDscManagedComputerInstance.Integration.Tests.ps1'
300301
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
301302
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'

tests/Integration/Commands/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisit
5151
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5252
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5353
Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
54+
Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5455
Disable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
55-
Enable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5656
Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5757
New-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestRole_Persistent role
5858
Get-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies have not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:moduleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
}
31+
32+
Describe 'Set-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
# Starting the named instance SQL Server service prior to running tests.
35+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36+
37+
$script:mockInstanceName = 'DSCSQLTEST'
38+
$script:mockServerName = Get-ComputerName
39+
40+
# Store the original trace flags to restore them later
41+
$script:originalTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
42+
}
43+
44+
AfterAll {
45+
# Restore the original trace flags
46+
if ($script:originalTraceFlags)
47+
{
48+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $script:originalTraceFlags -Force -ErrorAction 'SilentlyContinue'
49+
}
50+
else
51+
{
52+
# Clear all trace flags if there were none originally
53+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @() -Force -ErrorAction 'SilentlyContinue'
54+
}
55+
56+
# Stop the named instance SQL Server service to save memory on the build worker.
57+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
58+
}
59+
60+
Context 'When setting trace flags' {
61+
It 'Should set a single trace flag and verify the change' {
62+
# Set trace flag 3226 (suppress successful backup messages)
63+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'
64+
65+
# Verify it's set
66+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
67+
$result | Should -Contain 3226
68+
}
69+
70+
It 'Should set multiple trace flags and verify the change' {
71+
# Set multiple trace flags
72+
$testTraceFlags = @(3226, 1222)
73+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $testTraceFlags -Force -ErrorAction 'Stop'
74+
75+
# Verify they're set
76+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
77+
foreach ($flag in $testTraceFlags)
78+
{
79+
$result | Should -Contain $flag
80+
}
81+
}
82+
83+
It 'Should replace existing trace flags with new ones' {
84+
# First, set trace flag 3226
85+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'
86+
87+
# Verify it's set
88+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
89+
$result | Should -Contain 3226
90+
91+
# Now replace with different trace flag
92+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 1222 -Force -ErrorAction 'Stop'
93+
94+
# Verify the old flag is gone and new one is present
95+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
96+
$result | Should -Contain 1222
97+
$result | Should -Not -Contain 3226
98+
}
99+
100+
It 'Should clear all trace flags when passing empty array' {
101+
# First, set some trace flags
102+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @(3226, 1222) -Force -ErrorAction 'Stop'
103+
104+
# Verify they're set
105+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
106+
$result | Should -Contain 3226
107+
$result | Should -Contain 1222
108+
109+
# Now clear all trace flags
110+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @() -Force -ErrorAction 'Stop'
111+
112+
# Verify they're cleared
113+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
114+
$result | Should -BeNullOrEmpty
115+
}
116+
}
117+
118+
Context 'When using ShouldProcess with WhatIf' {
119+
It 'Should not actually change the trace flags when using WhatIf' {
120+
# Get current trace flags
121+
$originalFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
122+
123+
# Use WhatIf to simulate setting different trace flags
124+
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 4199 -WhatIf
125+
126+
# Verify the trace flags haven't changed
127+
$currentFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
128+
if ($originalFlags)
129+
{
130+
$currentFlags | Should -HaveCount $originalFlags.Count
131+
foreach ($flag in $originalFlags)
132+
{
133+
$currentFlags | Should -Contain $flag
134+
}
135+
}
136+
else
137+
{
138+
$currentFlags | Should -BeNullOrEmpty
139+
}
140+
}
141+
}
142+
143+
Context 'When using ServiceObject parameter' {
144+
It 'Should accept ServiceObject from pipeline and set trace flags' {
145+
# Get the service object
146+
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ServerName $script:mockServerName -ErrorAction 'Stop'
147+
$serviceObject | Should -Not -BeNullOrEmpty
148+
149+
# Set trace flag using pipeline
150+
$serviceObject | Set-SqlDscTraceFlag -TraceFlag 3226 -Force -ErrorAction 'Stop'
151+
152+
# Verify it's set
153+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
154+
$result | Should -Contain 3226
155+
}
156+
157+
It 'Should accept ServiceObject parameter directly' {
158+
# Get the service object
159+
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ServerName $script:mockServerName -ErrorAction 'Stop'
160+
$serviceObject | Should -Not -BeNullOrEmpty
161+
162+
# Set trace flag using direct parameter
163+
Set-SqlDscTraceFlag -ServiceObject $serviceObject -TraceFlag 1222 -Force -ErrorAction 'Stop'
164+
165+
# Verify it's set
166+
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
167+
$result | Should -Contain 1222
168+
}
169+
}
170+
171+
Context 'When using default parameters' {
172+
It 'Should work with default server name and instance name' {
173+
# This test assumes the command is run on the same server as the SQL instance
174+
# and uses MSSQLSERVER as default instance, but our test instance is DSCSQLTEST
175+
# so we need to specify the instance name
176+
Set-SqlDscTraceFlag -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'
177+
178+
# Verify it's set
179+
$result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName
180+
$result | Should -Contain 3226
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)