diff --git a/CHANGELOG.md b/CHANGELOG.md index 284b9a04d1..763cb238dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added integration tests for `Remove-SqlDscAudit` command to ensure it functions correctly in real environments [issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241). +- Added integration tests for `Disconnect-SqlDscDatabaseEngine` command to ensure + command reliability in real environments + [issue #2224](https://github.com/dsccommunity/SqlServerDsc/issues/2224). - Added integration tests for `Invoke-SqlDscQuery` command to ensure it functions correctly in real environments [issue #2227](https://github.com/dsccommunity/SqlServerDsc/issues/2227). diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 249230d064..93c1660514 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -291,6 +291,7 @@ stages: # Group 1 'tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1' 'tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1' + 'tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1' # Group 2 'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1' 'tests/Integration/Commands/New-SqlDscLogin.Integration.Tests.ps1' diff --git a/tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1 b/tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1 new file mode 100644 index 0000000000..88a6cc0683 --- /dev/null +++ b/tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1 @@ -0,0 +1,156 @@ +[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 'Disconnect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { + BeforeAll { + Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose + } + + Context 'When disconnecting from the default instance' { + It 'Should have the default instance SQL Server service started' { + $getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop' + + $getServiceResult.Status | Should -Be 'Running' + } + + Context 'When disconnecting using Force parameter' { + It 'Should disconnect successfully without confirmation' { + { + $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.Status.ToString() | Should -Match '^Online$' + + # Test the disconnect functionality + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' + + # After disconnect, the connection should be closed + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse + } | Should -Not -Throw + } + } + + Context 'When disconnecting using pipeline input' { + It 'Should disconnect successfully via pipeline' { + { + $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.Status.ToString() | Should -Match '^Online$' + + # Test the disconnect functionality via pipeline + $sqlServerObject | Disconnect-SqlDscDatabaseEngine -Force -ErrorAction 'Stop' + + # After disconnect, the connection should be closed + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse + } | Should -Not -Throw + } + } + } + + Context 'When disconnecting from a named instance' { + 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 disconnecting using Windows authentication' { + It 'Should disconnect successfully from named instance' { + { + $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' + } + + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters + + $sqlServerObject.Status.ToString() | Should -Match '^Online$' + + # Test the disconnect functionality + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' + + # After disconnect, the connection should be closed + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse + } | Should -Not -Throw + } + } + + Context 'When disconnecting using SQL authentication' { + It 'Should disconnect successfully from named instance with SQL login' { + { + $sqlAdministratorUserName = 'sa' + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force + + $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$' + + # Test the disconnect functionality + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' + + # After disconnect, the connection should be closed + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse + } | Should -Not -Throw + } + } + } +} diff --git a/tests/Integration/Commands/README.md b/tests/Integration/Commands/README.md index 7bea01d892..551e58cd02 100644 --- a/tests/Integration/Commands/README.md +++ b/tests/Integration/Commands/README.md @@ -46,6 +46,7 @@ ConvertTo-SqlDscEditionName | 0 | - | - | - Import-SqlDscPreferredModule | 0 | - | - | - Install-SqlDscServer | 1 | 0 (Prerequisites) | - | DSCSQLTEST instance Connect-SqlDscDatabaseEngine | 1 | 0 (Prerequisites) | DSCSQLTEST | - +Disconnect-SqlDscDatabaseEngine | 1 | 0 (Prerequisites) | DSCSQLTEST | - Assert-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - New-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | IntegrationTestSqlLogin, SqlIntegrationTestGroup login Get-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -