From 55926f11785e85845ba6c13e39cff2391307c32d Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Wed, 29 May 2024 09:23:53 +0200 Subject: [PATCH] `Uninstall-SqlDscServer`: Add integration test (#2033) - `Uninstall-SqlDscServer` - Added integration test for the command. --- CHANGELOG.md | 2 + azure-pipelines.yml | 3 +- ...SqlDscDatabaseEngine.Integration.Tests.ps1 | 120 +++++++++++------- ...Install-SqlDscServer.Integration.Tests.ps1 | 33 ++++- ...install-SqlDscServer.Integration.Tests.ps1 | 64 ++++++++++ 5 files changed, 177 insertions(+), 45 deletions(-) create mode 100644 tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1b57c8e..7327f8665 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/azure-pipelines.yml b/azure-pipelines.yml index f76f12291..5bc43106e 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -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' diff --git a/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 b/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 index 44141ab4f..c2764332a 100644 --- a/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1 @@ -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 @@ -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 + } } } } diff --git a/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 b/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 index 846785b10..a74ab0417 100644 --- a/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 +++ b/tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1 @@ -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 @@ -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' { @@ -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. diff --git a/tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1 b/tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1 new file mode 100644 index 000000000..424081b5e --- /dev/null +++ b/tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1 @@ -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 + } + } +}