-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for Disconnect-SqlDscDatabaseEngine command #2255
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0cb0f5
Initial plan
Copilot 465d38b
Initial analysis complete - planning integration test for Disconnect-…
Copilot 776bf24
Add integration test for Disconnect-SqlDscDatabaseEngine command
Copilot a82746e
Address feedback: revert build.ps1, fix error message, add README ent…
Copilot 7d16282
Fix build.ps1 file mode to match main branch (remove executable permi…
Copilot 36ba010
Merge branch 'main' into copilot/fix-2224
johlju 028da07
Merge branch 'main' into copilot/fix-2224
johlju de117cc
Remove redundant service start/stop in Disconnect-SqlDscDatabaseEngin…
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
156 changes: 156 additions & 0 deletions
156
tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1
johlju marked this conversation as resolved.
Show resolved
Hide 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
| } | ||
| } | ||
| } |
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.