Skip to content

Commit b0dbd88

Browse files
authored
Add integration test for Get-SqlDscManagedComputer command (#2248)
1 parent e051b4d commit b0dbd88

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- Added integration tests for `Get-SqlDscManagedComputer` command to ensure it
11+
functions correctly in real environments
12+
[issue #2220](https://github.com/dsccommunity/SqlServerDsc/issues/2220).
1013
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1114
correctly in real environments
1215
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).

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/Get-SqlDscManagedComputer.Integration.Tests.ps1'
299300
'tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1'
300301
'tests/Integration/Commands/Get-SqlDscManagedComputerInstance.Integration.Tests.ps1'
301302
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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-SqlDscManagedComputer' -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+
# Note: On Windows CI environment, this would start the SQL Server service
39+
if (Get-Command -Name 'Start-Service' -ErrorAction 'SilentlyContinue')
40+
{
41+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
42+
}
43+
44+
$script:mockServerName = Get-ComputerName
45+
}
46+
47+
AfterAll {
48+
# Stop the named instance SQL Server service to save memory on the build worker.
49+
# Note: On Windows CI environment, this would stop the SQL Server service
50+
if (Get-Command -Name 'Stop-Service' -ErrorAction 'SilentlyContinue')
51+
{
52+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
53+
}
54+
}
55+
56+
Context 'When using default parameters' {
57+
It 'Should return the managed computer object for the local computer' {
58+
$result = Get-SqlDscManagedComputer -ErrorAction 'Stop'
59+
60+
$result | Should -Not -BeNullOrEmpty
61+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer])
62+
$result.Name | Should -Be $script:mockServerName
63+
}
64+
}
65+
66+
Context 'When specifying a server name' {
67+
It 'Should return the managed computer object for the specified server' {
68+
$result = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
69+
70+
$result | Should -Not -BeNullOrEmpty
71+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer])
72+
$result.Name | Should -Be $script:mockServerName
73+
}
74+
}
75+
76+
Context 'When validating SMO object properties' {
77+
It 'Should return objects with correct SMO properties' {
78+
$result = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
79+
80+
# Verify it's a proper SMO ManagedComputer object
81+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer])
82+
83+
# Verify key properties exist
84+
$result.Name | Should -Not -BeNullOrEmpty
85+
$result.Name | Should -Be $script:mockServerName
86+
87+
# Verify ServerInstances collection is accessible
88+
$result.ServerInstances | Should -Not -BeNullOrEmpty
89+
$result.ServerInstances.Count | Should -BeGreaterThan 0
90+
91+
# Verify Services collection is accessible
92+
$result.Services | Should -Not -BeNullOrEmpty
93+
$result.Services.Count | Should -BeGreaterThan 0
94+
95+
# Verify the DSCSQLTEST instance is present in the ServerInstances collection
96+
$testInstance = $result.ServerInstances | Where-Object -FilterScript { $_.Name -eq 'DSCSQLTEST' }
97+
$testInstance | Should -Not -BeNullOrEmpty
98+
$testInstance.Name | Should -Be 'DSCSQLTEST'
99+
}
100+
}
101+
102+
Context 'When using the managed computer object with other commands' {
103+
It 'Should be compatible with Get-SqlDscManagedComputerInstance' {
104+
$managedComputer = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
105+
106+
# Test pipeline compatibility
107+
$result = $managedComputer | Get-SqlDscManagedComputerInstance -InstanceName 'DSCSQLTEST' -ErrorAction 'Stop'
108+
109+
$result | Should -Not -BeNullOrEmpty
110+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance])
111+
$result.Name | Should -Be 'DSCSQLTEST'
112+
$result.Parent.Name | Should -Be $script:mockServerName
113+
}
114+
}
115+
116+
Context 'When testing error handling' {
117+
It 'Should handle non-existent server names gracefully' {
118+
# This test verifies that the command creates a ManagedComputer object
119+
# even for non-existent servers (SMO behavior)
120+
$result = Get-SqlDscManagedComputer -ServerName 'NonExistentServer123' -ErrorAction 'Stop'
121+
122+
$result | Should -Not -BeNullOrEmpty
123+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer])
124+
$result.Name | Should -Be 'NonExistentServer123'
125+
}
126+
}
127+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Assert-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTES
4747
New-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | IntegrationTestSqlLogin, SqlIntegrationTestGroup login
4848
Get-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
4949
Get-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
50+
Get-SqlDscManagedComputer | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5051
Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5152
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5253
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)