|
| 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 | +Describe 'Get-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { |
| 33 | + BeforeAll { |
| 34 | + $script:mockInstanceName = 'DSCSQLTEST' |
| 35 | + $script:mockComputerName = Get-ComputerName |
| 36 | + |
| 37 | + $mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception. |
| 38 | + $mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force |
| 39 | + |
| 40 | + $script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword) |
| 41 | + |
| 42 | + # Get the service object for testing the ByServiceObject parameter set |
| 43 | + $script:serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ErrorAction 'Stop' |
| 44 | + } |
| 45 | + |
| 46 | + Context 'When getting trace flags using InstanceName parameter' { |
| 47 | + It 'Should return an array of UInt32 values or empty array' { |
| 48 | + $result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName |
| 49 | + |
| 50 | + # The result should be either null/empty or an array of UInt32 values |
| 51 | + if ($result) { |
| 52 | + @($result) | Should -BeOfType [System.UInt32] |
| 53 | + } else { |
| 54 | + $result | Should -BeNullOrEmpty |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Context 'When getting trace flags using ServerName and InstanceName parameters' { |
| 60 | + It 'Should return an array of UInt32 values or empty array when specifying server and instance' { |
| 61 | + $result = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName |
| 62 | + |
| 63 | + # The result should be either null/empty or an array of UInt32 values |
| 64 | + if ($result) { |
| 65 | + @($result) | Should -BeOfType [System.UInt32] |
| 66 | + } else { |
| 67 | + $result | Should -BeNullOrEmpty |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + It 'Should return an array of UInt32 values or empty array when specifying only instance name' { |
| 72 | + $result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName |
| 73 | + |
| 74 | + # The result should be either null/empty or an array of UInt32 values |
| 75 | + if ($result) { |
| 76 | + @($result) | Should -BeOfType [System.UInt32] |
| 77 | + } else { |
| 78 | + $result | Should -BeNullOrEmpty |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Context 'When getting trace flags using ServiceObject parameter' { |
| 84 | + It 'Should return an array of UInt32 values or empty array when using service object' { |
| 85 | + $result = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject |
| 86 | + |
| 87 | + # The result should be either null/empty or an array of UInt32 values |
| 88 | + if ($result) { |
| 89 | + @($result) | Should -BeOfType [System.UInt32] |
| 90 | + } else { |
| 91 | + $result | Should -BeNullOrEmpty |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Context 'When there are no trace flags configured' { |
| 97 | + It 'Should return empty result when no trace flags are set' { |
| 98 | + # This test validates the command works when no trace flags are configured |
| 99 | + # We cannot control the trace flag state in CI, so we just verify the command executes without error |
| 100 | + { Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName -ErrorAction 'Stop' } | |
| 101 | + Should -Not -Throw |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Context 'When testing error handling' { |
| 106 | + It 'Should throw an error when specifying an invalid instance name' { |
| 107 | + { Get-SqlDscTraceFlag -InstanceName 'InvalidInstance' -ErrorAction 'Stop' } | |
| 108 | + Should -Throw |
| 109 | + } |
| 110 | + |
| 111 | + It 'Should return empty result when specifying an invalid instance name with SilentlyContinue' { |
| 112 | + $result = Get-SqlDscTraceFlag -InstanceName 'InvalidInstance' -ErrorAction 'SilentlyContinue' |
| 113 | + |
| 114 | + $result | Should -BeNullOrEmpty |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Context 'When comparing different parameter sets' { |
| 119 | + It 'Should return consistent results between ByServerName and ByServiceObject parameter sets' { |
| 120 | + $resultByServerName = Get-SqlDscTraceFlag -ServerName $script:mockComputerName -InstanceName $script:mockInstanceName -ErrorAction 'SilentlyContinue' |
| 121 | + $resultByServiceObject = Get-SqlDscTraceFlag -ServiceObject $script:serviceObject -ErrorAction 'SilentlyContinue' |
| 122 | + |
| 123 | + # Both results should be of the same type (both null or both arrays) |
| 124 | + if ($null -eq $resultByServerName) { |
| 125 | + $resultByServiceObject | Should -BeNullOrEmpty |
| 126 | + } else { |
| 127 | + $resultByServiceObject | Should -Not -BeNullOrEmpty |
| 128 | + @($resultByServerName).Count | Should -Be @($resultByServiceObject).Count |
| 129 | + |
| 130 | + # If both have trace flags, they should be the same |
| 131 | + if (@($resultByServerName).Count -gt 0) { |
| 132 | + Compare-Object -ReferenceObject @($resultByServerName) -DifferenceObject @($resultByServiceObject) | Should -BeNullOrEmpty |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments