Skip to content

Commit 55926f1

Browse files
authored
Uninstall-SqlDscServer: Add integration test (#2033)
- `Uninstall-SqlDscServer` - Added integration test for the command.
1 parent c5e7fe7 commit 55926f1

File tree

5 files changed

+177
-45
lines changed

5 files changed

+177
-45
lines changed

CHANGELOG.md

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

1010
- `Connect-SqlDscDatabaseEngine`
1111
- Added integration test for the command.
12+
- `Uninstall-SqlDscServer`
13+
- Added integration test for the command.
1214

1315
### Changed
1416

azure-pipelines.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ stages:
256256
# Group 1
257257
'tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1'
258258
'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1'
259-
#'tests/Integration/Commands/Install-SqlDscReportingServices.Integration.Tests.ps1'
259+
# Group 9
260+
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
260261
)
261262
name: test
262263
displayName: 'Run Integration Test'

tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1

Lines changed: 77 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ BeforeDiscovery {
2323
}
2424
}
2525

26+
# cSpell: ignore DSCSQLTEST
2627
Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
2728
BeforeAll {
2829
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
@@ -40,65 +41,98 @@ Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2016', 'Integrati
4041
# }
4142

4243
Context 'When connecting to the default instance impersonating a Windows user' {
43-
It 'Should return the correct result' {
44-
{
45-
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
46-
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
44+
BeforeAll {
45+
# Starting the default instance SQL Server service prior to running tests.
46+
Start-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
47+
}
48+
49+
AfterAll {
50+
# Stop the default instance SQL Server service to save memory on the build worker.
51+
Stop-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
52+
}
53+
54+
It 'Should have the default instance SQL Server service started' {
55+
$getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop'
4756

48-
$connectSqlDscDatabaseEngineParameters = @{
49-
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
50-
Verbose = $true
51-
ErrorAction = 'Stop'
52-
}
57+
$getServiceResult.Status | Should -Be 'Running'
58+
}
59+
60+
Context 'When impersonating a Windows user' {
61+
It 'Should return the correct result' {
62+
{
63+
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
64+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
5365

66+
$connectSqlDscDatabaseEngineParameters = @{
67+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
68+
Verbose = $true
69+
ErrorAction = 'Stop'
70+
}
5471

55-
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
72+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
5673

57-
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
58-
} | Should -Not -Throw
74+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
75+
} | Should -Not -Throw
76+
}
5977
}
6078
}
6179

62-
Context 'When connecting to the named instance impersonating a Windows user' {
63-
It 'Should return the correct result' {
64-
{
65-
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
66-
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
67-
68-
$connectSqlDscDatabaseEngineParameters = @{
69-
InstanceName = 'DSCSQLTEST'
70-
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
71-
Verbose = $true
72-
ErrorAction = 'Stop'
73-
}
80+
Context 'When connecting to a named instance' {
81+
BeforeAll {
82+
# Starting the named instance SQL Server service prior to running tests.
83+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
84+
}
7485

86+
AfterAll {
87+
# Stop the named instance SQL Server service to save memory on the build worker.
88+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
89+
}
7590

76-
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
91+
It 'Should have the named instance SQL Server service started' {
92+
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'
7793

78-
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
79-
} | Should -Not -Throw
94+
$getServiceResult.Status | Should -Be 'Running'
8095
}
81-
}
8296

83-
Context 'When connecting to the named instance using a SQL login' {
84-
It 'Should return the correct result' {
85-
{
86-
$sqlAdministratorUserName = 'sa'
87-
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
97+
Context 'When impersonating a Windows user' {
98+
It 'Should return the correct result' {
99+
{
100+
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
101+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
102+
103+
$connectSqlDscDatabaseEngineParameters = @{
104+
InstanceName = 'DSCSQLTEST'
105+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
106+
Verbose = $true
107+
ErrorAction = 'Stop'
108+
}
88109

89-
$connectSqlDscDatabaseEngineParameters = @{
90-
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
91-
LoginType = 'SqlLogin'
92-
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
93-
Verbose = $true
94-
ErrorAction = 'Stop'
95-
}
110+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
96111

112+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
113+
} | Should -Not -Throw
114+
}
115+
}
116+
117+
Context 'When using a SQL login' {
118+
It 'Should return the correct result' {
119+
{
120+
$sqlAdministratorUserName = 'sa'
121+
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
97122

98-
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
123+
$connectSqlDscDatabaseEngineParameters = @{
124+
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
125+
LoginType = 'SqlLogin'
126+
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
127+
Verbose = $true
128+
ErrorAction = 'Stop'
129+
}
99130

100-
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
101-
} | Should -Not -Throw
131+
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
132+
133+
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
134+
} | Should -Not -Throw
135+
}
102136
}
103137
}
104138
}

tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ BeforeDiscovery {
2323
}
2424
}
2525

26+
# cSpell: ignore SQLSERVERAGENT, DSCSQLTEST
2627
Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
2728
BeforeAll {
2829
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
@@ -150,6 +151,22 @@ Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL20
150151
$sqlServerService | Should -Not -BeNullOrEmpty
151152
$sqlServerService.Status | Should -Be 'Running'
152153
}
154+
155+
It 'Should stop the default instance SQL Server service' {
156+
# Stop the default instance SQL Server service to save memory on the build worker.
157+
$stopServiceResult = Stop-Service -Name 'MSSQLSERVER' -Force -PassThru -Verbose -ErrorAction 'Stop'
158+
159+
write-verbose -Message ($stopServiceResult | Out-String) -Verbose
160+
161+
(
162+
<#
163+
Filter services. This will also have stopped the dependent
164+
service 'SQLSERVERAGENT'
165+
#>
166+
$stopServiceResult |
167+
Where-Object -FilterScript { $_.Name -eq 'MSSQLSERVER'}
168+
).Status | Should -Be 'Stopped'
169+
}
153170
}
154171

155172
Context 'When installing database engine named instance' {
@@ -273,11 +290,25 @@ Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL20
273290

274291
It 'Should have installed the SQL Server database engine' {
275292
# Validate the SQL Server installation
276-
$sqlServerService = Get-Service -Name 'SQL Server (DSCSQLTEST)' # cSpell: disable-line
293+
$sqlServerService = Get-Service -Name 'MSSQL$DSCSQLTEST'
277294

278295
$sqlServerService | Should -Not -BeNullOrEmpty
279296
$sqlServerService.Status | Should -Be 'Running'
280297
}
298+
299+
It 'Should stop the named instance SQL Server service' {
300+
# Stop the named instance SQL Server service to save memory on the build worker.
301+
$stopServiceResult = Stop-Service -Name 'MSSQL$DSCSQLTEST' -Force -PassThru -Verbose -ErrorAction 'Stop'
302+
303+
(
304+
<#
305+
Filter services. This will also have stopped the dependent
306+
service 'SQL Server Agent (DSCSQLTEST)'.
307+
#>
308+
$stopServiceResult |
309+
Where-Object -FilterScript { $_.Name -eq 'MSSQL$DSCSQLTEST' }
310+
).Status | Should -Be 'Stopped'
311+
}
281312
}
282313

283314
# # Enable this to debugging the last installation by output the Summary.txt.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 has 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' 2>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies has 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 build" first.'
23+
}
24+
}
25+
26+
# cSpell: ignore DSCSQLTEST
27+
Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
28+
BeforeAll {
29+
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
30+
31+
# Starting the named instance SQL Server service prior to running tests.
32+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
33+
34+
$computerName = Get-ComputerName
35+
}
36+
37+
It 'Should have the named instance SQL Server service started' {
38+
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'
39+
40+
$getServiceResult.Status | Should -Be 'Running'
41+
}
42+
43+
Context 'When uninstalling a named instance' {
44+
It 'Should run the command without throwing' {
45+
{
46+
# Set splatting parameters for Uninstall-SqlDscServer
47+
$uninstallSqlDscServerParameters = @{
48+
InstanceName = 'DSCSQLTEST'
49+
Features = 'SQLENGINE'
50+
MediaPath = $env:IsoDrivePath
51+
Verbose = $true
52+
ErrorAction = 'Stop'
53+
Force = $true
54+
}
55+
56+
Uninstall-SqlDscServer @uninstallSqlDscServerParameters
57+
} | Should -Not -Throw
58+
}
59+
60+
It 'Should not have a named instance SQL Server service' {
61+
Get-Service -Name 'SQL Server (DSCSQLTEST)' -ErrorAction 'Ignore' | Should -BeNullOrEmpty
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)