Skip to content

Commit aa84ee2

Browse files
authored
Add integration test for Disconnect-SqlDscDatabaseEngine command (#2255)
1 parent 90a7c9d commit aa84ee2

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-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 `Disconnect-SqlDscDatabaseEngine` command to ensure
17+
command reliability in real environments
18+
[issue #2224](https://github.com/dsccommunity/SqlServerDsc/issues/2224).
1619
- Added integration tests for `Invoke-SqlDscQuery` command to ensure it functions
1720
correctly in real environments
1821
[issue #2227](https://github.com/dsccommunity/SqlServerDsc/issues/2227).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ stages:
291291
# Group 1
292292
'tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1'
293293
'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1'
294+
'tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1'
294295
# Group 2
295296
'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1'
296297
'tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1'
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 'Disconnect-SqlDscDatabaseEngine' -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+
38+
Context 'When disconnecting from the default instance' {
39+
It 'Should have the default instance SQL Server service started' {
40+
$getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop'
41+
42+
$getServiceResult.Status | Should -Be 'Running'
43+
}
44+
45+
Context 'When disconnecting using Force parameter' {
46+
It 'Should disconnect successfully without confirmation' {
47+
{
48+
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
49+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
50+
51+
$connectSqlDscDatabaseEngineParameters = @{
52+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
53+
Verbose = $true
54+
ErrorAction = 'Stop'
55+
}
56+
57+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
58+
59+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
60+
61+
# Test the disconnect functionality
62+
Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop'
63+
64+
# After disconnect, the connection should be closed
65+
$sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse
66+
} | Should -Not -Throw
67+
}
68+
}
69+
70+
Context 'When disconnecting using pipeline input' {
71+
It 'Should disconnect successfully via pipeline' {
72+
{
73+
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
74+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
75+
76+
$connectSqlDscDatabaseEngineParameters = @{
77+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
78+
Verbose = $true
79+
ErrorAction = 'Stop'
80+
}
81+
82+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
83+
84+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
85+
86+
# Test the disconnect functionality via pipeline
87+
$sqlServerObject | Disconnect-SqlDscDatabaseEngine -Force -ErrorAction 'Stop'
88+
89+
# After disconnect, the connection should be closed
90+
$sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse
91+
} | Should -Not -Throw
92+
}
93+
}
94+
}
95+
96+
Context 'When disconnecting from a named instance' {
97+
It 'Should have the named instance SQL Server service started' {
98+
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'
99+
100+
$getServiceResult.Status | Should -Be 'Running'
101+
}
102+
103+
Context 'When disconnecting using Windows authentication' {
104+
It 'Should disconnect successfully from named instance' {
105+
{
106+
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
107+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
108+
109+
$connectSqlDscDatabaseEngineParameters = @{
110+
InstanceName = 'DSCSQLTEST'
111+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
112+
Verbose = $true
113+
ErrorAction = 'Stop'
114+
}
115+
116+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
117+
118+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
119+
120+
# Test the disconnect functionality
121+
Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop'
122+
123+
# After disconnect, the connection should be closed
124+
$sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse
125+
} | Should -Not -Throw
126+
}
127+
}
128+
129+
Context 'When disconnecting using SQL authentication' {
130+
It 'Should disconnect successfully from named instance with SQL login' {
131+
{
132+
$sqlAdministratorUserName = 'sa'
133+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
134+
135+
$connectSqlDscDatabaseEngineParameters = @{
136+
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
137+
LoginType = 'SqlLogin'
138+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
139+
Verbose = $true
140+
ErrorAction = 'Stop'
141+
}
142+
143+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
144+
145+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
146+
147+
# Test the disconnect functionality
148+
Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop'
149+
150+
# After disconnect, the connection should be closed
151+
$sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse
152+
} | Should -Not -Throw
153+
}
154+
}
155+
}
156+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ConvertTo-SqlDscEditionName | 0 | - | - | -
4646
Import-SqlDscPreferredModule | 0 | - | - | -
4747
Install-SqlDscServer | 1 | 0 (Prerequisites) | - | DSCSQLTEST instance
4848
Connect-SqlDscDatabaseEngine | 1 | 0 (Prerequisites) | DSCSQLTEST | -
49+
Disconnect-SqlDscDatabaseEngine | 1 | 0 (Prerequisites) | DSCSQLTEST | -
4950
Assert-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
5051
New-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | IntegrationTestSqlLogin, SqlIntegrationTestGroup login
5152
Get-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)