Skip to content

Commit

Permalink
Uninstall-SqlDscServer: Add integration test (#2033)
Browse files Browse the repository at this point in the history
- `Uninstall-SqlDscServer`
  - Added integration test for the command.
  • Loading branch information
johlju authored May 29, 2024
1 parent c5e7fe7 commit 55926f1
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `Connect-SqlDscDatabaseEngine`
- Added integration test for the command.
- `Uninstall-SqlDscServer`
- Added integration test for the command.

### Changed

Expand Down
3 changes: 2 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ stages:
# Group 1
'tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1'
'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1'
#'tests/Integration/Commands/Install-SqlDscReportingServices.Integration.Tests.ps1'
# Group 9
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
)
name: test
displayName: 'Run Integration Test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BeforeDiscovery {
}
}

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

Context 'When connecting to the default instance impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
BeforeAll {
# Starting the default instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
}

AfterAll {
# Stop the default instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
}

It 'Should have the default instance SQL Server service started' {
$getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop'

$connectSqlDscDatabaseEngineParameters = @{
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
$getServiceResult.Status | Should -Be 'Running'
}

Context 'When impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$connectSqlDscDatabaseEngineParameters = @{
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}

$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}
}

Context 'When connecting to the named instance impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
Context 'When connecting to a named instance' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

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

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

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
$getServiceResult.Status | Should -Be 'Running'
}
}

Context 'When connecting to the named instance using a SQL login' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'sa'
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
Context 'When impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}

$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
LoginType = 'SqlLogin'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}

Context 'When using a SQL login' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'sa'
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
LoginType = 'SqlLogin'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ BeforeDiscovery {
}
}

# cSpell: ignore SQLSERVERAGENT, DSCSQLTEST
Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose
Expand Down Expand Up @@ -150,6 +151,22 @@ Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL20
$sqlServerService | Should -Not -BeNullOrEmpty
$sqlServerService.Status | Should -Be 'Running'
}

It 'Should stop the default instance SQL Server service' {
# Stop the default instance SQL Server service to save memory on the build worker.
$stopServiceResult = Stop-Service -Name 'MSSQLSERVER' -Force -PassThru -Verbose -ErrorAction 'Stop'

write-verbose -Message ($stopServiceResult | Out-String) -Verbose

(
<#
Filter services. This will also have stopped the dependent
service 'SQLSERVERAGENT'
#>
$stopServiceResult |
Where-Object -FilterScript { $_.Name -eq 'MSSQLSERVER'}
).Status | Should -Be 'Stopped'
}
}

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

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

$sqlServerService | Should -Not -BeNullOrEmpty
$sqlServerService.Status | Should -Be 'Running'
}

It 'Should stop the named instance SQL Server service' {
# Stop the named instance SQL Server service to save memory on the build worker.
$stopServiceResult = Stop-Service -Name 'MSSQL$DSCSQLTEST' -Force -PassThru -Verbose -ErrorAction 'Stop'

(
<#
Filter services. This will also have stopped the dependent
service 'SQL Server Agent (DSCSQLTEST)'.
#>
$stopServiceResult |
Where-Object -FilterScript { $_.Name -eq 'MSSQL$DSCSQLTEST' }
).Status | Should -Be 'Stopped'
}
}

# # Enable this to debugging the last installation by output the Summary.txt.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[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 has 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' 2>&1 4>&1 5>&1 6>&1 > $null
}

# If the dependencies has 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 build" first.'
}
}

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

# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$computerName = Get-ComputerName
}

It 'Should have the named instance SQL Server service started' {
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'

$getServiceResult.Status | Should -Be 'Running'
}

Context 'When uninstalling a named instance' {
It 'Should run the command without throwing' {
{
# Set splatting parameters for Uninstall-SqlDscServer
$uninstallSqlDscServerParameters = @{
InstanceName = 'DSCSQLTEST'
Features = 'SQLENGINE'
MediaPath = $env:IsoDrivePath
Verbose = $true
ErrorAction = 'Stop'
Force = $true
}

Uninstall-SqlDscServer @uninstallSqlDscServerParameters
} | Should -Not -Throw
}

It 'Should not have a named instance SQL Server service' {
Get-Service -Name 'SQL Server (DSCSQLTEST)' -ErrorAction 'Ignore' | Should -BeNullOrEmpty
}
}
}

0 comments on commit 55926f1

Please sign in to comment.