Skip to content

Commit 4f8d9e8

Browse files
authored
Add integration test for Get-SqlDscStartupParameter command (#2249)
1 parent 2eee794 commit 4f8d9e8

File tree

6 files changed

+270
-43
lines changed

6 files changed

+270
-43
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1717
correctly in real environments
1818
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
19+
- Added integration tests for `Get-SqlDscStartupParameter` command to ensure it
20+
functions correctly in real environments
21+
[issue #2217](https://github.com/dsccommunity/SqlServerDsc/issues/2217).
1922
- Added integration tests for `Get-SqlDscTraceFlag` command to ensure it functions
2023
correctly in real environments
2124
[issue #2216](https://github.com/dsccommunity/SqlServerDsc/issues/2216).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ stages:
316316
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'
317317
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
318318
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
319+
'tests/Integration/Commands/Get-SqlDscStartupParameter.Integration.Tests.ps1'
319320
'tests/Integration/Commands/Test-SqlDscIsRole.Integration.Tests.ps1'
320321
'tests/Integration/Commands/Test-SqlDscIsDatabasePrincipal.Integration.Tests.ps1'
321322
'tests/Integration/Commands/Grant-SqlDscServerPermission.Integration.Tests.ps1'

source/Public/Get-SqlDscStartupParameter.ps1

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,24 @@
66
Get current startup parameters on a Database Engine instance.
77
88
.PARAMETER ServiceObject
9-
Specifies the Service object to return the trace flags from.
9+
Specifies the Service object to return the startup parameters from.
1010
1111
.PARAMETER ServerName
12-
Specifies the server name to return the trace flags from.
12+
Specifies the server name to return the startup parameters from.
1313
1414
.PARAMETER InstanceName
15-
Specifies the instance name to return the trace flags for.
15+
Specifies the instance name to return the startup parameters for.
16+
17+
.INPUTS
18+
Microsoft.SqlServer.Management.Smo.Wmi.Service
19+
20+
A service object representing the Database Engine service.
21+
22+
.OUTPUTS
23+
StartupParameters
24+
25+
Returns a StartupParameters object containing the startup parameters
26+
for the specified Database Engine instance.
1627
1728
.EXAMPLE
1829
Get-SqlDscStartupParameter
@@ -39,9 +50,6 @@
3950
4051
Get the startup parameters from the Database Engine instance 'SQL2022' on
4152
the server where the command in run.
42-
43-
.OUTPUTS
44-
`[StartupParameters]`
4553
#>
4654
function Get-SqlDscStartupParameter
4755
{
@@ -50,7 +58,7 @@ function Get-SqlDscStartupParameter
5058
[CmdletBinding(DefaultParameterSetName = 'ByServerName')]
5159
param
5260
(
53-
[Parameter(ParameterSetName = 'ByServiceObject', Mandatory = $true)]
61+
[Parameter(ParameterSetName = 'ByServiceObject', Mandatory = $true, ValueFromPipeline = $true)]
5462
[Microsoft.SqlServer.Management.Smo.Wmi.Service]
5563
$ServiceObject,
5664

@@ -65,56 +73,62 @@ function Get-SqlDscStartupParameter
6573
$InstanceName = 'MSSQLSERVER'
6674
)
6775

68-
$originalErrorActionPreference = $ErrorActionPreference
69-
70-
$ErrorActionPreference = 'Stop'
76+
begin
77+
{
78+
$originalErrorActionPreference = $ErrorActionPreference
7179

72-
Assert-ElevatedUser -ErrorAction 'Stop'
80+
$ErrorActionPreference = 'Stop'
7381

74-
$ErrorActionPreference = $originalErrorActionPreference
82+
Assert-ElevatedUser -ErrorAction 'Stop'
7583

76-
if ($PSCmdlet.ParameterSetName -eq 'ByServiceObject')
77-
{
78-
$ServiceObject | Assert-ManagedServiceType -ServiceType 'DatabaseEngine'
84+
$ErrorActionPreference = $originalErrorActionPreference
7985
}
8086

81-
if ($PSCmdlet.ParameterSetName -eq 'ByServerName')
87+
process
8288
{
83-
$getSqlDscManagedComputerServiceParameters = @{
84-
ServerName = $ServerName
85-
InstanceName = $InstanceName
86-
ServiceType = 'DatabaseEngine'
89+
if ($PSCmdlet.ParameterSetName -eq 'ByServiceObject')
90+
{
91+
$ServiceObject | Assert-ManagedServiceType -ServiceType 'DatabaseEngine'
8792
}
8893

89-
$ServiceObject = Get-SqlDscManagedComputerService @getSqlDscManagedComputerServiceParameters
90-
91-
if (-not $ServiceObject)
94+
if ($PSCmdlet.ParameterSetName -eq 'ByServerName')
9295
{
93-
$writeErrorParameters = @{
94-
Message = $script:localizedData.StartupParameter_Get_FailedToFindServiceObject
95-
Category = 'InvalidOperation'
96-
ErrorId = 'GSDSP0001' # CSpell: disable-line
97-
TargetObject = $ServiceObject
96+
$getSqlDscManagedComputerServiceParameters = @{
97+
ServerName = $ServerName
98+
InstanceName = $InstanceName
99+
ServiceType = 'DatabaseEngine'
98100
}
99101

100-
Write-Error @writeErrorParameters
102+
$ServiceObject = Get-SqlDscManagedComputerService @getSqlDscManagedComputerServiceParameters
103+
104+
if (-not $ServiceObject)
105+
{
106+
$writeErrorParameters = @{
107+
Message = $script:localizedData.StartupParameter_Get_FailedToFindServiceObject
108+
Category = 'InvalidOperation'
109+
ErrorId = 'GSDSP0001' # CSpell: disable-line
110+
TargetObject = $ServiceObject
111+
}
112+
113+
Write-Error @writeErrorParameters
114+
}
101115
}
102-
}
103116

104-
Write-Verbose -Message (
105-
$script:localizedData.StartupParameter_Get_ReturnStartupParameters -f $InstanceName, $ServerName
106-
)
117+
Write-Verbose -Message (
118+
$script:localizedData.StartupParameter_Get_ReturnStartupParameters -f $InstanceName, $ServerName
119+
)
107120

108-
$startupParameters = $null
121+
$startupParameters = $null
109122

110-
if ($ServiceObject.StartupParameters)
111-
{
112-
$startupParameters = [StartupParameters]::Parse($ServiceObject.StartupParameters)
113-
}
114-
else
115-
{
116-
Write-Debug -Message ($script:localizedData.StartupParameter_Get_FailedToFindStartupParameters -f $MyInvocation.MyCommand)
117-
}
123+
if ($ServiceObject.StartupParameters)
124+
{
125+
$startupParameters = [StartupParameters]::Parse($ServiceObject.StartupParameters)
126+
}
127+
else
128+
{
129+
Write-Debug -Message ($script:localizedData.StartupParameter_Get_FailedToFindStartupParameters -f $MyInvocation.MyCommand)
130+
}
118131

119-
return $startupParameters
132+
return $startupParameters
133+
}
120134
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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 'Get-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+
$script:mockInstanceName = 'DSCSQLTEST'
38+
$script:mockServerName = Get-ComputerName
39+
}
40+
41+
Context 'When using parameter set ByServerName' {
42+
Context 'When getting startup parameters with default parameters' {
43+
It 'Should return a StartupParameters object for the test instance' {
44+
$result = Get-SqlDscStartupParameter -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
45+
46+
$result | Should -Not -BeNullOrEmpty
47+
$result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] })
48+
$result.DataFilePath | Should -Not -BeNullOrEmpty
49+
$result.LogFilePath | Should -Not -BeNullOrEmpty
50+
$result.ErrorLogPath | Should -Not -BeNullOrEmpty
51+
}
52+
}
53+
54+
Context 'When getting startup parameters for a specific instance' {
55+
It 'Should return a StartupParameters object for the specified instance' {
56+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
57+
58+
$result | Should -Not -BeNullOrEmpty
59+
$result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] })
60+
$result.DataFilePath | Should -Not -BeNullOrEmpty
61+
$result.LogFilePath | Should -Not -BeNullOrEmpty
62+
$result.ErrorLogPath | Should -Not -BeNullOrEmpty
63+
}
64+
}
65+
66+
Context 'When getting startup parameters for a specific server name' {
67+
It 'Should return a StartupParameters object for the specified server' {
68+
$result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
69+
70+
$result | Should -Not -BeNullOrEmpty
71+
$result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] })
72+
$result.DataFilePath | Should -Not -BeNullOrEmpty
73+
$result.LogFilePath | Should -Not -BeNullOrEmpty
74+
$result.ErrorLogPath | Should -Not -BeNullOrEmpty
75+
}
76+
}
77+
78+
Context 'When getting startup parameters for a non-existent instance' {
79+
It 'Should throw an error when the instance does not exist' {
80+
{
81+
Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName 'NonExistentInstance' -ErrorAction 'Stop'
82+
} | Should -Throw -ErrorId 'GSDSP0001,Get-SqlDscStartupParameter'
83+
}
84+
}
85+
}
86+
87+
Context 'When using parameter set ByServiceObject' {
88+
BeforeAll {
89+
$script:serviceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
90+
}
91+
92+
Context 'When getting startup parameters using a service object' {
93+
It 'Should return a StartupParameters object for the service object' {
94+
$result = Get-SqlDscStartupParameter -ServiceObject $script:serviceObject -ErrorAction 'Stop'
95+
96+
$result | Should -Not -BeNullOrEmpty
97+
$result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] })
98+
$result.DataFilePath | Should -Not -BeNullOrEmpty
99+
$result.LogFilePath | Should -Not -BeNullOrEmpty
100+
$result.ErrorLogPath | Should -Not -BeNullOrEmpty
101+
}
102+
}
103+
104+
Context 'When getting startup parameters using pipeline input' {
105+
It 'Should accept ServiceObject from pipeline and return StartupParameters' {
106+
$result = $script:serviceObject | Get-SqlDscStartupParameter -ErrorAction 'Stop'
107+
108+
$result | Should -Not -BeNullOrEmpty
109+
$result | Should -BeOfType (InModuleScope -ModuleName $script:moduleName -ScriptBlock { [StartupParameters] })
110+
$result.DataFilePath | Should -Not -BeNullOrEmpty
111+
$result.LogFilePath | Should -Not -BeNullOrEmpty
112+
$result.ErrorLogPath | Should -Not -BeNullOrEmpty
113+
}
114+
}
115+
116+
Context 'When passing wrong service type' {
117+
BeforeAll {
118+
# Get a non-DatabaseEngine service for testing
119+
$script:wrongServiceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'SqlServerAgent' -ErrorAction 'Stop'
120+
}
121+
122+
It 'Should throw an error when the service type is not DatabaseEngine' {
123+
{
124+
Get-SqlDscStartupParameter -ServiceObject $script:wrongServiceObject -ErrorAction 'Stop'
125+
} | Should -Throw
126+
}
127+
}
128+
}
129+
130+
Context 'When validating output properties' {
131+
BeforeAll {
132+
$script:result = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
133+
}
134+
135+
It 'Should return an object with expected DataFilePath property' {
136+
$script:result.DataFilePath | Should -Not -BeNullOrEmpty
137+
$script:result.DataFilePath | Should -BeOfType ([System.String])
138+
$script:result.DataFilePath | Should -Match '\.mdf$'
139+
}
140+
141+
It 'Should return an object with expected LogFilePath property' {
142+
$script:result.LogFilePath | Should -Not -BeNullOrEmpty
143+
$script:result.LogFilePath | Should -BeOfType ([System.String])
144+
$script:result.LogFilePath | Should -Match '\.ldf$'
145+
}
146+
147+
It 'Should return an object with expected ErrorLogPath property' {
148+
$script:result.ErrorLogPath | Should -Not -BeNullOrEmpty
149+
$script:result.ErrorLogPath | Should -BeOfType ([System.String])
150+
$script:result.ErrorLogPath | Should -Match 'ERRORLOG$'
151+
}
152+
153+
It 'Should return TraceFlag property as expected type' {
154+
# TraceFlag can be null, a single UInt32, or an array
155+
if ($null -ne $script:result.TraceFlag) {
156+
# Check if it's either a single UInt32 or UInt32 array
157+
($script:result.TraceFlag -is [System.UInt32]) -or ($script:result.TraceFlag -is [System.UInt32[]]) | Should -BeTrue -Because 'TraceFlag can be a single value or array depending on how many flags are set'
158+
} else {
159+
$script:result.TraceFlag | Should -BeNullOrEmpty -Because 'TraceFlag can be empty/null if no trace flags are set'
160+
}
161+
}
162+
163+
It 'Should return InternalTraceFlag property as expected type' {
164+
# InternalTraceFlag can be null, a single UInt32, or an array
165+
if ($null -ne $script:result.InternalTraceFlag) {
166+
# Check if it's either a single UInt32 or UInt32 array
167+
($script:result.InternalTraceFlag -is [System.UInt32]) -or ($script:result.InternalTraceFlag -is [System.UInt32[]]) | Should -BeTrue -Because 'InternalTraceFlag can be a single value or array depending on how many flags are set'
168+
} else {
169+
$script:result.InternalTraceFlag | Should -BeNullOrEmpty -Because 'InternalTraceFlag can be empty/null if no internal trace flags are set'
170+
}
171+
}
172+
}
173+
174+
Context 'When comparing results from different parameter sets' {
175+
It 'Should return the same results for ByServerName and ByServiceObject parameter sets' {
176+
$resultByServerName = Get-SqlDscStartupParameter -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
177+
178+
$serviceObject = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
179+
$resultByServiceObject = Get-SqlDscStartupParameter -ServiceObject $serviceObject -ErrorAction 'Stop'
180+
181+
$resultByServerName.DataFilePath | Should -Be $resultByServiceObject.DataFilePath
182+
$resultByServerName.LogFilePath | Should -Be $resultByServiceObject.LogFilePath
183+
$resultByServerName.ErrorLogPath | Should -Be $resultByServiceObject.ErrorLogPath
184+
$resultByServerName.TraceFlag | Should -Be $resultByServiceObject.TraceFlag
185+
$resultByServerName.InternalTraceFlag | Should -Be $resultByServiceObject.InternalTraceFlag
186+
}
187+
}
188+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Test-SqlDscIsLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTES
7070
Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7171
New-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestRole_Persistent role
7272
Get-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
73+
Get-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7374
Test-SqlDscIsRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
7475
Test-SqlDscIsDatabasePrincipal | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Test database and database principals
7576
Grant-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Grants CreateEndpoint permission to role

tests/Unit/Public/Get-SqlDscStartupParameter.Tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@ Describe 'Get-SqlDscStartupParameter' -Tag 'Public' {
251251
Should -Invoke -CommandName Get-SqlDscManagedComputerService -Exactly -Times 0 -Scope It
252252
}
253253
}
254+
255+
Context 'When passing a service object via pipeline' {
256+
It 'Should accept ServiceObject from pipeline and return correct results' {
257+
$result = $mockServiceObject | Get-SqlDscStartupParameter
258+
259+
Should -ActualValue $result -BeOfType (InModuleScope -ScriptBlock { [StartupParameters] })
260+
261+
Should -ActualValue $result.TraceFlag -BeOfType 'System.UInt32[]'
262+
Should -ActualValue $result.DataFilePath -BeOfType 'System.String[]'
263+
Should -ActualValue $result.LogFilePath -BeOfType 'System.String[]'
264+
Should -ActualValue $result.ErrorLogPath -BeOfType 'System.String[]'
265+
266+
$result.DataFilePath | Should -Be 'C:\Program Files\Microsoft SQL Server\MSSQL16.SQL2022\MSSQL\DATA\master.mdf'
267+
$result.LogFilePath | Should -Be 'C:\Program Files\Microsoft SQL Server\MSSQL16.SQL2022\MSSQL\DATA\log.ldf'
268+
$result.ErrorLogPath | Should -Be 'C:\Program Files\Microsoft SQL Server\MSSQL16.SQL2022\MSSQL\Log\ERRORLOG'
269+
$result.TraceFlag | Should -BeNullOrEmpty
270+
271+
Should -Invoke -CommandName Get-SqlDscManagedComputerService -Exactly -Times 0 -Scope It
272+
}
273+
}
254274
}
255275

256276
Context 'When one trace flag exist' {

0 commit comments

Comments
 (0)