Skip to content

Commit 3c9d7cb

Browse files
authored
Add integration test for Set-SqlDscStartupParameter command (#2264)
1 parent 7c84b77 commit 3c9d7cb

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1414
correctly in real environments
1515
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
16+
- Added integration tests for `Set-SqlDscStartupParameter` command to ensure
17+
reliable startup parameter configuration
18+
[issue #2233](https://github.com/dsccommunity/SqlServerDsc/issues/2233).
1619
- Added integration tests for `Set-SqlDscServerPermission` command to ensure it
1720
functions correctly in real environments
1821
[issue #2234](https://github.com/dsccommunity/SqlServerDsc/issues/2234).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ stages:
298298
'tests/Integration/Commands/Get-SqlDscConfigurationOption.Integration.Tests.ps1'
299299
'tests/Integration/Commands/Set-SqlDscConfigurationOption.Integration.Tests.ps1'
300300
'tests/Integration/Commands/Test-SqlDscConfigurationOption.Integration.Tests.ps1'
301+
'tests/Integration/Commands/Set-SqlDscStartupParameter.Integration.Tests.ps1'
301302
'tests/Integration/Commands/Test-SqlDscIsSupportedFeature.Integration.Tests.ps1'
302303
'tests/Integration/Commands/Get-SqlDscManagedComputer.Integration.Tests.ps1'
303304
'tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1'

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Get-SqlDscManagedComputerService | 2 | 1 (Install-SqlDscServer), 0 (Prerequisite
5858
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5959
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6060
Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
61+
Set-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6162
Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6263
Disable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6364
Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
# cSpell: ignore DSCSQLTEST
33+
Describe 'Set-SqlDscStartupParameter' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
34+
BeforeAll {
35+
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
36+
37+
# Starting the named instance SQL Server service prior to running tests.
38+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
39+
40+
$script:mockInstanceName = 'DSCSQLTEST'
41+
$script:mockServerName = Get-ComputerName
42+
43+
# Get the service object for testing
44+
$script:serviceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
45+
46+
# Store original startup parameters to restore later
47+
$script:originalStartupParameters = Get-SqlDscStartupParameter -ServiceObject $script:serviceObject -ErrorAction 'Stop'
48+
}
49+
50+
AfterAll {
51+
# Restore original startup parameters
52+
if ($script:originalStartupParameters)
53+
{
54+
try
55+
{
56+
Set-SqlDscStartupParameter -ServiceObject $script:serviceObject -TraceFlag $script:originalStartupParameters.TraceFlag -InternalTraceFlag $script:originalStartupParameters.InternalTraceFlag -Force -ErrorAction 'SilentlyContinue'
57+
}
58+
catch
59+
{
60+
Write-Warning -Message "Failed to restore original startup parameters: $($_.Exception.Message)"
61+
}
62+
}
63+
64+
# Stop the named instance SQL Server service to save memory on the build worker.
65+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
66+
}
67+
68+
Context 'When using parameter set ByServerName' {
69+
Context 'When setting trace flags' {
70+
It 'Should set a single trace flag' {
71+
# Set a single trace flag
72+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @(4199) -Force -ErrorAction 'Stop'
73+
74+
# Verify the trace flag was set
75+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
76+
$result.TraceFlag | Should -Contain 4199
77+
}
78+
79+
It 'Should set multiple trace flags' {
80+
# Set multiple trace flags
81+
$testTraceFlags = @(4199, 1222)
82+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $testTraceFlags -Force -ErrorAction 'Stop'
83+
84+
# Verify the trace flags were set
85+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
86+
$result.TraceFlag | Should -Contain 4199
87+
$result.TraceFlag | Should -Contain 1222
88+
}
89+
90+
It 'Should clear all trace flags when given empty array' {
91+
# First set some trace flags
92+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @(4199) -Force -ErrorAction 'Stop'
93+
94+
# Then clear them
95+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @() -Force -ErrorAction 'Stop'
96+
97+
# Verify trace flags were cleared
98+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
99+
$result.TraceFlag | Should -BeNullOrEmpty
100+
}
101+
}
102+
103+
Context 'When setting internal trace flags' {
104+
It 'Should set internal trace flags' {
105+
# Set internal trace flags
106+
$testInternalTraceFlags = @(8011, 8012)
107+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -InternalTraceFlag $testInternalTraceFlags -Force -ErrorAction 'Stop'
108+
109+
# Verify the internal trace flags were set
110+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
111+
$result.InternalTraceFlag | Should -Contain 8011
112+
$result.InternalTraceFlag | Should -Contain 8012
113+
}
114+
115+
It 'Should clear internal trace flags when given empty array' {
116+
# First set some internal trace flags
117+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -InternalTraceFlag @(8011) -Force -ErrorAction 'Stop'
118+
119+
# Then clear them
120+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -InternalTraceFlag @() -Force -ErrorAction 'Stop'
121+
122+
# Verify internal trace flags were cleared
123+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
124+
$result.InternalTraceFlag | Should -BeNullOrEmpty
125+
}
126+
}
127+
128+
Context 'When setting both trace flags and internal trace flags' {
129+
It 'Should set both types of flags simultaneously' {
130+
# Set both types of flags
131+
$testTraceFlags = @(4199)
132+
$testInternalTraceFlags = @(8011)
133+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $testTraceFlags -InternalTraceFlag $testInternalTraceFlags -Force -ErrorAction 'Stop'
134+
135+
# Verify both types were set
136+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
137+
$result.TraceFlag | Should -Contain 4199
138+
$result.InternalTraceFlag | Should -Contain 8011
139+
}
140+
}
141+
142+
Context 'When using default server name' {
143+
It 'Should use local computer name when ServerName is not specified' {
144+
Set-SqlDscStartupParameter -InstanceName $script:mockInstanceName -TraceFlag @(4199) -Force -ErrorAction 'Stop'
145+
146+
# Verify the trace flag was set
147+
$result = Get-SqlDscStartupParameter -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
148+
$result.TraceFlag | Should -Contain 4199
149+
}
150+
}
151+
}
152+
153+
Context 'When using parameter set ByServiceObject' {
154+
Context 'When setting trace flags via service object' {
155+
It 'Should set trace flags using service object from pipeline' {
156+
# Set trace flags using pipeline
157+
$script:serviceObject | Set-SqlDscStartupParameter -TraceFlag @(4199, 1222) -Force -ErrorAction 'Stop'
158+
159+
# Verify the trace flags were set
160+
$result = Get-SqlDscStartupParameter -ServiceObject $script:serviceObject -ErrorAction 'Stop'
161+
$result.TraceFlag | Should -Contain 4199
162+
$result.TraceFlag | Should -Contain 1222
163+
}
164+
165+
It 'Should set internal trace flags using service object parameter' {
166+
# Set internal trace flags using service object parameter
167+
Set-SqlDscStartupParameter -ServiceObject $script:serviceObject -InternalTraceFlag @(8011) -Force -ErrorAction 'Stop'
168+
169+
# Verify the internal trace flags were set
170+
$result = Get-SqlDscStartupParameter -ServiceObject $script:serviceObject -ErrorAction 'Stop'
171+
$result.InternalTraceFlag | Should -Contain 8011
172+
}
173+
}
174+
}
175+
176+
Context 'When using ShouldProcess with WhatIf' {
177+
It 'Should not actually change startup parameters when using WhatIf' {
178+
# Get current startup parameters
179+
$currentParams = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
180+
181+
# Use WhatIf to simulate setting different trace flags
182+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @(9999) -WhatIf
183+
184+
# Verify the parameters haven't changed
185+
$newParams = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
186+
187+
# Compare arrays properly handling nulls/empty arrays
188+
if ($currentParams.TraceFlag -and $newParams.TraceFlag) {
189+
Compare-Object -ReferenceObject $currentParams.TraceFlag -DifferenceObject $newParams.TraceFlag | Should -BeNullOrEmpty
190+
} elseif (-not $currentParams.TraceFlag -and -not $newParams.TraceFlag) {
191+
# Both should be null/empty - this is expected
192+
$true | Should -BeTrue
193+
} else {
194+
# One is null and the other isn't - this means something changed
195+
$false | Should -BeTrue -Because "Startup parameters should not change when using WhatIf"
196+
}
197+
}
198+
}
199+
200+
Context 'When handling error conditions' {
201+
It 'Should throw an error when instance does not exist' {
202+
{
203+
Set-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName 'NonExistentInstance' -TraceFlag @(4199) -Force -ErrorAction 'Stop'
204+
} | Should -Throw
205+
}
206+
207+
It 'Should throw an error when server does not exist' {
208+
{
209+
Set-SqlDscStartupParameter -ServerName 'NonExistentServer' -InstanceName $script:mockInstanceName -TraceFlag @(4199) -Force -ErrorAction 'Stop'
210+
} | Should -Throw
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)