Skip to content

Commit 02de599

Browse files
authored
Add integration test for Get-SqlDscManagedComputerService command (#2250)
1 parent b0dbd88 commit 02de599

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ 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 test for `Get-SqlDscManagedComputerService` command to ensure
17+
command reliability [issue #2219](https://github.com/dsccommunity/SqlServerDsc/issues/2219).
1618
- Added integration tests for `Set-SqlDscTraceFlag` command to ensure it functions
1719
correctly in real environments
1820
[issue #2232](https://github.com/dsccommunity/SqlServerDsc/issues/2232).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ stages:
299299
'tests/Integration/Commands/Get-SqlDscManagedComputer.Integration.Tests.ps1'
300300
'tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1'
301301
'tests/Integration/Commands/Get-SqlDscManagedComputerInstance.Integration.Tests.ps1'
302+
'tests/Integration/Commands/Get-SqlDscManagedComputerService.Integration.Tests.ps1'
302303
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
303304
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'
304305
'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1'
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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-SqlDscManagedComputerService' -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+
44+
AfterAll {
45+
# Stop the named instance SQL Server service to save memory on the build worker.
46+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
47+
}
48+
49+
Context 'When using parameter set ByServerName' {
50+
Context 'When getting all services on the current managed computer' {
51+
It 'Should return all available services' {
52+
$result = Get-SqlDscManagedComputerService -ErrorAction 'Stop'
53+
54+
$result | Should -Not -BeNullOrEmpty
55+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
56+
57+
# Should contain SQL Server related services
58+
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
59+
$sqlServices | Should -Not -BeNullOrEmpty
60+
}
61+
}
62+
63+
Context 'When getting all services on the specified managed computer' {
64+
It 'Should return all available services' {
65+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop'
66+
67+
$result | Should -Not -BeNullOrEmpty
68+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
69+
70+
# Should contain SQL Server related services
71+
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
72+
$sqlServices | Should -Not -BeNullOrEmpty
73+
}
74+
}
75+
76+
Context 'When filtering by ServiceType' {
77+
It 'Should return only Database Engine services' {
78+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
79+
80+
$result | Should -Not -BeNullOrEmpty
81+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
82+
83+
# All returned services should be of type SqlServer
84+
foreach ($service in $result)
85+
{
86+
$service.Type | Should -Be 'SqlServer'
87+
}
88+
}
89+
90+
It 'Should return SQL Server Browser service when filtering by SQLServerBrowser' {
91+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'SQLServerBrowser' -ErrorAction 'Stop'
92+
93+
if ($result)
94+
{
95+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
96+
$result.Type | Should -Be 'SqlBrowser'
97+
}
98+
}
99+
}
100+
101+
Context 'When filtering by InstanceName' {
102+
It 'Should return services for the specified instance' {
103+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
104+
105+
if ($result)
106+
{
107+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
108+
109+
# All returned services should contain the instance name
110+
foreach ($service in $result)
111+
{
112+
$service.Name | Should -Match ('\$' + $script:mockInstanceName + '$')
113+
}
114+
}
115+
}
116+
117+
It 'Should return services for the default instance when filtering by MSSQLSERVER' {
118+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName 'MSSQLSERVER' -ErrorAction 'Stop'
119+
120+
if ($result)
121+
{
122+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
123+
124+
# Should contain the default instance service
125+
$defaultInstanceService = $result | Where-Object -FilterScript { $_.Name -eq 'MSSQLSERVER' }
126+
$defaultInstanceService | Should -Not -BeNullOrEmpty
127+
}
128+
}
129+
}
130+
}
131+
132+
Context 'When using parameter set ByManagedComputerObject' {
133+
BeforeAll {
134+
$script:managedComputerObject = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
135+
}
136+
137+
Context 'When getting all services from managed computer object' {
138+
It 'Should return all available services' {
139+
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ErrorAction 'Stop'
140+
141+
$result | Should -Not -BeNullOrEmpty
142+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
143+
144+
# Should contain SQL Server related services
145+
$sqlServices = $result | Where-Object -FilterScript { $_.Name -like '*SQL*' }
146+
$sqlServices | Should -Not -BeNullOrEmpty
147+
}
148+
}
149+
150+
Context 'When filtering by ServiceType from managed computer object' {
151+
It 'Should return only Database Engine services' {
152+
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -ErrorAction 'Stop'
153+
154+
$result | Should -Not -BeNullOrEmpty
155+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
156+
157+
# All returned services should be of type SqlServer
158+
foreach ($service in $result)
159+
{
160+
$service.Type | Should -Be 'SqlServer'
161+
}
162+
}
163+
}
164+
165+
Context 'When filtering by InstanceName from managed computer object' {
166+
It 'Should return services for the specified instance' {
167+
$result = $script:managedComputerObject | Get-SqlDscManagedComputerService -InstanceName $script:mockInstanceName -ErrorAction 'Stop'
168+
169+
if ($result)
170+
{
171+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
172+
173+
# All returned services should contain the instance name
174+
foreach ($service in $result)
175+
{
176+
$service.Name | Should -Match ('\$' + $script:mockInstanceName + '$')
177+
}
178+
}
179+
}
180+
}
181+
}
182+
183+
Context 'When validating SMO object properties' {
184+
It 'Should return objects with correct SMO properties' {
185+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop'
186+
187+
$result | Should -Not -BeNullOrEmpty
188+
189+
# Verify it's a proper SMO Service object
190+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
191+
192+
# Verify key properties exist for at least one service
193+
$firstService = $result | Select-Object -First 1
194+
$firstService.Name | Should -Not -BeNullOrEmpty
195+
$firstService.Type | Should -Not -BeNullOrEmpty
196+
197+
# Verify the service has access to its parent ManagedComputer
198+
$firstService.Parent | Should -Not -BeNullOrEmpty
199+
$firstService.Parent.Name | Should -Be $script:mockServerName
200+
}
201+
}
202+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Get-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST |
4949
Get-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5050
Get-SqlDscManagedComputer | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5151
Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
52+
Get-SqlDscManagedComputerService | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5253
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5354
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5455
Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)