diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ce004bfcc..0a93c624c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,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 `Get-SqlDscTraceFlag` command to ensure it functions + correctly in real environments + [issue #2216](https://github.com/dsccommunity/SqlServerDsc/issues/2216). - Added integration tests for `Get-SqlDscPreferredModule` command to ensure it functions correctly in real environments [issue #2218](https://github.com/dsccommunity/SqlServerDsc/issues/2218). diff --git a/azure-pipelines.yml b/azure-pipelines.yml index d350b67bde..7a5b366192 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -309,6 +309,7 @@ stages: 'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1' 'tests/Integration/Commands/ConvertTo-SqlDscEditionName.Integration.Tests.ps1' 'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1' + 'tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1' 'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1' 'tests/Integration/Commands/Enable-SqlDscLogin.Integration.Tests.ps1' 'tests/Integration/Commands/Test-SqlDscIsLogin.Integration.Tests.ps1' diff --git a/tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1 b/tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1 new file mode 100644 index 0000000000..c07413ef17 --- /dev/null +++ b/tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1 @@ -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 + } + } + } + } +} diff --git a/tests/Integration/Commands/README.md b/tests/Integration/Commands/README.md index d9c7b6ce58..f4c936c349 100644 --- a/tests/Integration/Commands/README.md +++ b/tests/Integration/Commands/README.md @@ -59,6 +59,7 @@ Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisit Get-SqlDscManagedComputerService | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - +Get-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Set-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | - Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -