Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added integration tests for `Initialize-SqlDscRebuildDatabase` command to ensure
command reliability. The test runs in group 8, alongside `Repair-SqlDscServer`,
to verify the rebuild database functionality on the DSCSQLTEST instance
[issue #2242](https://github.com/dsccommunity/SqlServerDsc/issues/2242).
- Added integration tests for `Repair-SqlDscServer` command to ensure command
reliability. The test runs in group 8, before `Uninstall-SqlDscServer` in
group 9, to verify the repair functionality on the DSCSQLTEST instance
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ stages:
'tests/Integration/Commands/Remove-SqlDscTraceFlag.Integration.Tests.ps1'
# Group 8
'tests/Integration/Commands/Repair-SqlDscServer.Integration.Tests.ps1'
'tests/Integration/Commands/Initialize-SqlDscRebuildDatabase.Integration.Tests.ps1'
# Group 9
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
param ()

BeforeDiscovery {
try
{
if (-not (Get-Module -Name 'DscResource.Test'))
{
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
{
# Redirect all streams to $null, except the error stream (stream 2)
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies have not been resolved, this will throw an error.
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
}
}
catch [System.IO.FileNotFoundException]
{
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
}
}

BeforeAll {
$script:moduleName = 'SqlServerDsc'

Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}

# cSpell: ignore DSCSQLTEST
Describe 'Initialize-SqlDscRebuildDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose

# Stop the SQL Server instance so we can rebuild the databases
$serviceName = 'MSSQL$DSCSQLTEST'
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'

if ($sqlService.Status -eq 'Running')
{
Write-Verbose -Message "Stopping SQL Server service '$serviceName'..." -Verbose
Stop-Service -Name $serviceName -Force -ErrorAction 'Stop'
Start-Sleep -Seconds 5
}

# Verify service is stopped
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
if ($sqlService.Status -ne 'Stopped')
{
Write-Error -Message "Failed to stop SQL Server service '$serviceName'"
}
}

AfterAll {
# Ensure SQL Server service is running after tests
$serviceName = 'MSSQL$DSCSQLTEST'
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'

if ($sqlService.Status -ne 'Running')
{
Write-Verbose -Message "Starting SQL Server service '$serviceName'..." -Verbose
Start-Service -Name $serviceName -ErrorAction 'Stop'
Start-Sleep -Seconds 10
}

# Verify service is running
$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
if ($sqlService.Status -ne 'Running')
{
Write-Error -Message "Failed to start SQL Server service '$serviceName'"
}
}

Context 'When rebuilding database on a named instance' {
Context 'When specifying only mandatory parameters' {
It 'Should run the rebuild command without throwing (using Force parameter)' {
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
$rebuildSqlDscDatabaseParameters = @{
InstanceName = 'DSCSQLTEST'
SqlSysAdminAccounts = @(
('{0}\SqlAdmin' -f (Get-ComputerName))
)
MediaPath = $env:IsoDrivePath
Verbose = $true
ErrorAction = 'Stop'
Force = $true
}

$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
}

It 'Should have the SQL Server service running after rebuild' {
$serviceName = 'MSSQL$DSCSQLTEST'
Start-Service -Name $serviceName -ErrorAction 'Stop'
Start-Sleep -Seconds 10

$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
$sqlService.Status | Should -Be 'Running'
}

It 'Should be able to connect to the instance after rebuild' {
$computerName = Get-ComputerName
$mockSqlAdministratorUserName = 'SqlAdmin'
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new(
$mockSqlAdministratorUserName,
$mockSqlAdministratorPassword
)

$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'DSCSQLTEST' -Credential $mockSqlAdminCredential -ErrorAction 'Stop'

$serverObject | Should -Not -BeNullOrEmpty
$serverObject.Name | Should -Match 'DSCSQLTEST'

Disconnect-SqlDscDatabaseEngine -ServerObject $serverObject
}
Comment thread
johlju marked this conversation as resolved.
}

Context 'When specifying optional TempDB parameters' {
It 'Should run the rebuild command with custom TempDB file count' {
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
$rebuildSqlDscDatabaseParameters = @{
InstanceName = 'DSCSQLTEST'
SqlSysAdminAccounts = @(
('{0}\SqlAdmin' -f (Get-ComputerName))
)
MediaPath = $env:IsoDrivePath
SqlTempDbFileCount = 8
Verbose = $true
ErrorAction = 'Stop'
Force = $true
}

$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
}

It 'Should have the SQL Server service running after rebuild with TempDB parameters' {
$serviceName = 'MSSQL$DSCSQLTEST'
Start-Service -Name $serviceName -ErrorAction 'Stop'
Start-Sleep -Seconds 10

$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
$sqlService.Status | Should -Be 'Running'
}
Comment thread
johlju marked this conversation as resolved.
}

Context 'When specifying optional SqlCollation parameter' {
It 'Should run the rebuild command with custom collation' {
# Set splatting parameters for Initialize-SqlDscRebuildDatabase
$rebuildSqlDscDatabaseParameters = @{
InstanceName = 'DSCSQLTEST'
SqlSysAdminAccounts = @(
('{0}\SqlAdmin' -f (Get-ComputerName))
)
MediaPath = $env:IsoDrivePath
SqlCollation = 'SQL_Latin1_General_CP1_CI_AS'
Verbose = $true
ErrorAction = 'Stop'
Force = $true
}

$null = Initialize-SqlDscRebuildDatabase @rebuildSqlDscDatabaseParameters
}

It 'Should have the SQL Server service running after rebuild with collation' {
$serviceName = 'MSSQL$DSCSQLTEST'
Start-Service -Name $serviceName -ErrorAction 'Stop'
Start-Sleep -Seconds 10

$sqlService = Get-Service -Name $serviceName -ErrorAction 'Stop'
$sqlService.Status | Should -Be 'Running'
}
Comment thread
johlju marked this conversation as resolved.
}
}
}
3 changes: 2 additions & 1 deletion tests/Integration/Commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ Remove-SqlDscRole | 7 | 4 (New-SqlDscRole) | DSCSQLTEST | -
Remove-SqlDscLogin | 7 | 4 (New-SqlDscLogin) | DSCSQLTEST | -
Remove-SqlDscTraceFlag | 7 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
Repair-SqlDscServer | 8 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
Uninstall-SqlDscServer | 9 | 8 (Repair-SqlDscServer) | - | -
Initialize-SqlDscRebuildDatabase | 8 | 1 (Install-SqlDscServer) | DSCSQLTEST | -
Uninstall-SqlDscServer | 9 | 8 (Repair-SqlDscServer), 8 (Initialize-SqlDscRebuildDatabase) | - | -
<!-- markdownlint-enable MD013 -->

### Integration_Test_Commands_ReportingServices
Expand Down
Loading