|
| 1 | +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] |
| 2 | +param () |
| 3 | + |
| 4 | +BeforeDiscovery { |
| 5 | + try |
| 6 | + { |
| 7 | + if (-not (Get-Module -Name 'DscResource.Test')) |
| 8 | + { |
| 9 | + # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task. |
| 10 | + if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable)) |
| 11 | + { |
| 12 | + # Redirect all streams to $null, except the error stream (stream 2) |
| 13 | + & "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null |
| 14 | + } |
| 15 | + |
| 16 | + # If the dependencies have not been resolved, this will throw an error. |
| 17 | + Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop' |
| 18 | + } |
| 19 | + } |
| 20 | + catch [System.IO.FileNotFoundException] |
| 21 | + { |
| 22 | + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:moduleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' |
| 30 | +} |
| 31 | + |
| 32 | +# cSpell: ignore DSCSQLTEST |
| 33 | +Describe 'Disconnect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 34 | + BeforeAll { |
| 35 | + Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose |
| 36 | + } |
| 37 | + |
| 38 | + Context 'When disconnecting from the default instance' { |
| 39 | + It 'Should have the default instance SQL Server service started' { |
| 40 | + $getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop' |
| 41 | + |
| 42 | + $getServiceResult.Status | Should -Be 'Running' |
| 43 | + } |
| 44 | + |
| 45 | + Context 'When disconnecting using Force parameter' { |
| 46 | + It 'Should disconnect successfully without confirmation' { |
| 47 | + { |
| 48 | + $sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 49 | + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 50 | + |
| 51 | + $connectSqlDscDatabaseEngineParameters = @{ |
| 52 | + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) |
| 53 | + Verbose = $true |
| 54 | + ErrorAction = 'Stop' |
| 55 | + } |
| 56 | + |
| 57 | + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters |
| 58 | + |
| 59 | + $sqlServerObject.Status.ToString() | Should -Match '^Online$' |
| 60 | + |
| 61 | + # Test the disconnect functionality |
| 62 | + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' |
| 63 | + |
| 64 | + # After disconnect, the connection should be closed |
| 65 | + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse |
| 66 | + } | Should -Not -Throw |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Context 'When disconnecting using pipeline input' { |
| 71 | + It 'Should disconnect successfully via pipeline' { |
| 72 | + { |
| 73 | + $sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 74 | + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 75 | + |
| 76 | + $connectSqlDscDatabaseEngineParameters = @{ |
| 77 | + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) |
| 78 | + Verbose = $true |
| 79 | + ErrorAction = 'Stop' |
| 80 | + } |
| 81 | + |
| 82 | + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters |
| 83 | + |
| 84 | + $sqlServerObject.Status.ToString() | Should -Match '^Online$' |
| 85 | + |
| 86 | + # Test the disconnect functionality via pipeline |
| 87 | + $sqlServerObject | Disconnect-SqlDscDatabaseEngine -Force -ErrorAction 'Stop' |
| 88 | + |
| 89 | + # After disconnect, the connection should be closed |
| 90 | + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse |
| 91 | + } | Should -Not -Throw |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Context 'When disconnecting from a named instance' { |
| 97 | + It 'Should have the named instance SQL Server service started' { |
| 98 | + $getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop' |
| 99 | + |
| 100 | + $getServiceResult.Status | Should -Be 'Running' |
| 101 | + } |
| 102 | + |
| 103 | + Context 'When disconnecting using Windows authentication' { |
| 104 | + It 'Should disconnect successfully from named instance' { |
| 105 | + { |
| 106 | + $sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 107 | + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 108 | + |
| 109 | + $connectSqlDscDatabaseEngineParameters = @{ |
| 110 | + InstanceName = 'DSCSQLTEST' |
| 111 | + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) |
| 112 | + Verbose = $true |
| 113 | + ErrorAction = 'Stop' |
| 114 | + } |
| 115 | + |
| 116 | + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters |
| 117 | + |
| 118 | + $sqlServerObject.Status.ToString() | Should -Match '^Online$' |
| 119 | + |
| 120 | + # Test the disconnect functionality |
| 121 | + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' |
| 122 | + |
| 123 | + # After disconnect, the connection should be closed |
| 124 | + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse |
| 125 | + } | Should -Not -Throw |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + Context 'When disconnecting using SQL authentication' { |
| 130 | + It 'Should disconnect successfully from named instance with SQL login' { |
| 131 | + { |
| 132 | + $sqlAdministratorUserName = 'sa' |
| 133 | + $sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 134 | + |
| 135 | + $connectSqlDscDatabaseEngineParameters = @{ |
| 136 | + InstanceName = 'DSCSQLTEST' # cSpell: disable-line |
| 137 | + LoginType = 'SqlLogin' |
| 138 | + Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword) |
| 139 | + Verbose = $true |
| 140 | + ErrorAction = 'Stop' |
| 141 | + } |
| 142 | + |
| 143 | + $sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters |
| 144 | + |
| 145 | + $sqlServerObject.Status.ToString() | Should -Match '^Online$' |
| 146 | + |
| 147 | + # Test the disconnect functionality |
| 148 | + Disconnect-SqlDscDatabaseEngine -ServerObject $sqlServerObject -Force -ErrorAction 'Stop' |
| 149 | + |
| 150 | + # After disconnect, the connection should be closed |
| 151 | + $sqlServerObject.ConnectionContext.IsOpen | Should -BeFalse |
| 152 | + } | Should -Not -Throw |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments