Skip to content

Commit 3a97cb1

Browse files
committed
Add integration tests for Initialize-SqlDscRebuildDatabase command
1 parent 01f5034 commit 3a97cb1

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

CHANGELOG.md

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

88
### Added
99

10+
- Added integration tests for `Initialize-SqlDscRebuildDatabase` command to ensure
11+
command reliability. The test runs in group 8, alongside `Repair-SqlDscServer`,
12+
to verify the rebuild database functionality on the DSCSQLTEST instance
13+
[issue #2242](https://github.com/dsccommunity/SqlServerDsc/issues/2242).
1014
- Added integration tests for `Repair-SqlDscServer` command to ensure command
1115
reliability. The test runs in group 8, before `Uninstall-SqlDscServer` in
1216
group 9, to verify the repair functionality on the DSCSQLTEST instance

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ stages:
367367
'tests/Integration/Commands/Remove-SqlDscTraceFlag.Integration.Tests.ps1'
368368
# Group 8
369369
'tests/Integration/Commands/Repair-SqlDscServer.Integration.Tests.ps1'
370+
'tests/Integration/Commands/Initialize-SqlDscRebuildDatabase.Integration.Tests.ps1'
370371
# Group 9
371372
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
372373
)
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 'Initialize-SqlDscRebuildDatabase' -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+
# Stop the SQL Server instance so we can rebuild the databases
38+
$serviceName = 'MSSQL$DSCSQLTEST'
39+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
40+
41+
if ($sqlService.Status -eq 'Running')
42+
{
43+
Write-Verbose -Message "Stopping SQL Server service '$serviceName'..." -Verbose
44+
Stop-Service -Name $serviceName -Force -ErrorAction 'Stop'
45+
Start-Sleep -Seconds 5
46+
}
47+
48+
# Verify service is stopped
49+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
50+
if ($sqlService.Status -ne 'Stopped')
51+
{
52+
Write-Error -Message "Failed to stop SQL Server service '$serviceName'"
53+
}
54+
}
55+
56+
AfterAll {
57+
# Ensure SQL Server service is running after tests
58+
$serviceName = 'MSSQL$DSCSQLTEST'
59+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
60+
61+
if ($sqlService.Status -ne 'Running')
62+
{
63+
Write-Verbose -Message "Starting SQL Server service '$serviceName'..." -Verbose
64+
Start-Service -Name $serviceName -ErrorAction 'Stop'
65+
Start-Sleep -Seconds 10
66+
}
67+
68+
# Verify service is running
69+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
70+
if ($sqlService.Status -ne 'Running')
71+
{
72+
Write-Error -Message "Failed to start SQL Server service '$serviceName'"
73+
}
74+
}
75+
76+
Context 'When rebuilding database on a named instance' {
77+
Context 'When specifying only mandatory parameters' {
78+
It 'Should run the rebuild command without throwing (using Force parameter)' {
79+
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
80+
$rebuildSqlDscDatabaseParameters = @{
81+
InstanceName = 'DSCSQLTEST'
82+
SqlSysAdminAccounts = @(
83+
('{0}\SqlAdmin' -f (Get-ComputerName))
84+
)
85+
MediaPath = $env:IsoDrivePath
86+
Verbose = $true
87+
ErrorAction = 'Stop'
88+
Force = $true
89+
}
90+
91+
$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
92+
}
93+
94+
It 'Should have the SQL Server service running after rebuild' {
95+
$serviceName = 'MSSQL$DSCSQLTEST'
96+
Start-Service -Name $serviceName -ErrorAction 'Stop'
97+
Start-Sleep -Seconds 10
98+
99+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
100+
$sqlService.Status | Should -Be 'Running'
101+
}
102+
103+
It 'Should be able to connect to the instance after rebuild' {
104+
$computerName = Get-ComputerName
105+
$mockSqlAdministratorUserName = 'SqlAdmin'
106+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
107+
108+
$mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new(
109+
$mockSqlAdministratorUserName,
110+
$mockSqlAdministratorPassword
111+
)
112+
113+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'DSCSQLTEST' -Credential $mockSqlAdminCredential -ErrorAction 'Stop'
114+
115+
$serverObject | Should -Not -BeNullOrEmpty
116+
$serverObject.Name | Should -Match 'DSCSQLTEST'
117+
118+
Disconnect-SqlDscDatabaseEngine -ServerObject $serverObject
119+
}
120+
}
121+
122+
Context 'When specifying optional TempDB parameters' {
123+
It 'Should run the rebuild command with custom TempDB file count' {
124+
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
125+
$rebuildSqlDscDatabaseParameters = @{
126+
InstanceName = 'DSCSQLTEST'
127+
SqlSysAdminAccounts = @(
128+
('{0}\SqlAdmin' -f (Get-ComputerName))
129+
)
130+
MediaPath = $env:IsoDrivePath
131+
SqlTempDbFileCount = 8
132+
Verbose = $true
133+
ErrorAction = 'Stop'
134+
Force = $true
135+
}
136+
137+
$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
138+
}
139+
140+
It 'Should have the SQL Server service running after rebuild with TempDB parameters' {
141+
$serviceName = 'MSSQL$DSCSQLTEST'
142+
Start-Service -Name $serviceName -ErrorAction 'Stop'
143+
Start-Sleep -Seconds 10
144+
145+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
146+
$sqlService.Status | Should -Be 'Running'
147+
}
148+
}
149+
150+
Context 'When specifying optional SqlCollation parameter' {
151+
It 'Should run the rebuild command with custom collation' {
152+
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
153+
$rebuildSqlDscDatabaseParameters = @{
154+
InstanceName = 'DSCSQLTEST'
155+
SqlSysAdminAccounts = @(
156+
('{0}\SqlAdmin' -f (Get-ComputerName))
157+
)
158+
MediaPath = $env:IsoDrivePath
159+
SqlCollation = 'SQL_Latin1_General_CP1_CI_AS'
160+
Verbose = $true
161+
ErrorAction = 'Stop'
162+
Force = $true
163+
}
164+
165+
$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
166+
}
167+
168+
It 'Should have the SQL Server service running after rebuild with collation' {
169+
$serviceName = 'MSSQL$DSCSQLTEST'
170+
Start-Service -Name $serviceName -ErrorAction 'Stop'
171+
Start-Sleep -Seconds 10
172+
173+
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
174+
$sqlService.Status | Should -Be 'Running'
175+
}
176+
}
177+
}
178+
}

tests/Integration/Commands/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ Remove-SqlDscRole | 7 | 4 (New-SqlDscRole) | DSCSQLTEST | -
121121
Remove-SqlDscLogin | 7 | 4 (New-SqlDscLogin) | DSCSQLTEST | -
122122
Remove-SqlDscTraceFlag | 7 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
123123
Repair-SqlDscServer | 8 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
124-
Uninstall-SqlDscServer | 9 | 8 (Repair-SqlDscServer) | - | -
124+
Initialize-SqlDscRebuildDatabase | 8 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
125+
Uninstall-SqlDscServer | 9 | 8 (Repair-SqlDscServer), 8 (Initialize-SqlDscRebuildDatabase) | - | -
125126
<!-- markdownlint-enable MD013 -->
126127

127128
### Integration_Test_Commands_ReportingServices

0 commit comments

Comments
 (0)