-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration tests for Initialize-SqlDscRebuildDatabase #2309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3a97cb1
Add integration tests for Initialize-SqlDscRebuildDatabase command
johlju 85d06fa
Add error handling and verbose logging for Initialize-SqlDscRebuildDa…
johlju 0b7efb7
Add SAPwd parameter to Initialize-SqlDscRebuildDatabase integration t…
johlju cc4e485
Update Repair-SqlDscServer integration tests to skip due to CI enviro…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
tests/Integration/Commands/Initialize-SqlDscRebuildDatabase.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
|
|
||
| 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' | ||
| } | ||
|
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' | ||
| } | ||
|
johlju marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.