-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for Get-SqlDscTraceFlag command #2247
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
5 commits
Select commit
Hold shift + click to select a range
0a1d663
Initial plan
Copilot c5e6a40
Add integration test for Get-SqlDscTraceFlag command
Copilot c57c16d
Refactor Get-SqlDscTraceFlag integration tests by removing unnecessar…
johlju 7ebce2e
Merge branch 'main' into copilot/fix-2216
johlju af2d523
Fix task name and add Get-SqlDscTraceFlag to README.md dependency table
Copilot 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
137 changes: 137 additions & 0 deletions
137
tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1
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,137 @@ | ||
| [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' | ||
| } | ||
|
|
||
| Describe 'Get-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { | ||
| BeforeAll { | ||
| $script:mockInstanceName = 'DSCSQLTEST' | ||
| $script:mockComputerName = Get-ComputerName | ||
|
|
||
| $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. | ||
| $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force | ||
|
|
||
| $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) | ||
|
|
||
| # Get the service object for testing the ByServiceObject parameter set | ||
| $script:serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| Context 'When getting trace flags using InstanceName parameter' { | ||
| It 'Should return an array of UInt32 values or empty array' { | ||
| $result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName | ||
|
|
||
| # The result should be either null/empty or an array of UInt32 values | ||
| if ($result) { | ||
| @($result) | Should -BeOfType [System.UInt32] | ||
| } else { | ||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'When getting trace flags using ServerName and InstanceName parameters' { | ||
| It 'Should return an array of UInt32 values or empty array when specifying server and instance' { | ||
| $result = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName | ||
|
|
||
| # The result should be either null/empty or an array of UInt32 values | ||
| if ($result) { | ||
| @($result) | Should -BeOfType [System.UInt32] | ||
| } else { | ||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| It 'Should return an array of UInt32 values or empty array when specifying only instance name' { | ||
| $result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName | ||
|
|
||
| # The result should be either null/empty or an array of UInt32 values | ||
| if ($result) { | ||
| @($result) | Should -BeOfType [System.UInt32] | ||
| } else { | ||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'When getting trace flags using ServiceObject parameter' { | ||
| It 'Should return an array of UInt32 values or empty array when using service object' { | ||
| $result = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject | ||
|
|
||
| # The result should be either null/empty or an array of UInt32 values | ||
| if ($result) { | ||
| @($result) | Should -BeOfType [System.UInt32] | ||
| } else { | ||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Context 'When there are no trace flags configured' { | ||
| It 'Should return empty result when no trace flags are set' { | ||
| # This test validates the command works when no trace flags are configured | ||
| # We cannot control the trace flag state in CI, so we just verify the command executes without error | ||
| { Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName -ErrorAction 'Stop' } | | ||
| Should -Not -Throw | ||
| } | ||
| } | ||
|
|
||
| Context 'When testing error handling' { | ||
| It 'Should throw an error when specifying an invalid instance name' { | ||
| { Get-SqlDscTraceFlag -InstanceName 'InvalidInstance' -ErrorAction 'Stop' } | | ||
| Should -Throw | ||
| } | ||
|
|
||
| It 'Should return empty result when specifying an invalid instance name with SilentlyContinue' { | ||
| $result = Get-SqlDscTraceFlag -InstanceName 'InvalidInstance' -ErrorAction 'SilentlyContinue' | ||
|
|
||
| $result | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
|
|
||
| Context 'When comparing different parameter sets' { | ||
| It 'Should return consistent results between ByServerName and ByServiceObject parameter sets' { | ||
| $resultByServerName = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'SilentlyContinue' | ||
| $resultByServiceObject = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject -ErrorAction 'SilentlyContinue' | ||
|
|
||
| # Both results should be of the same type (both null or both arrays) | ||
| if ($null -eq $resultByServerName) { | ||
| $resultByServiceObject | Should -BeNullOrEmpty | ||
| } else { | ||
| $resultByServiceObject | Should -Not -BeNullOrEmpty | ||
| @($resultByServerName).Count | Should -Be @($resultByServiceObject).Count | ||
|
|
||
| # If both have trace flags, they should be the same | ||
| if (@($resultByServerName).Count -gt 0) { | ||
| Compare-Object -ReferenceObject @($resultByServerName) -DifferenceObject @($resultByServiceObject) | Should -BeNullOrEmpty | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.