Skip to content

Commit eaffdcd

Browse files
authored
Merge branch 'main' into copilot/fix-2217
2 parents dc199a0 + ae7fcc1 commit eaffdcd

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Added integration tests for `Get-SqlDscStartupParameter` command to ensure it
2020
functions correctly in real environments
2121
[issue #2217](https://github.com/dsccommunity/SqlServerDsc/issues/2217).
22+
- Added integration tests for `Get-SqlDscTraceFlag` command to ensure it functions
23+
correctly in real environments
24+
[issue #2216](https://github.com/dsccommunity/SqlServerDsc/issues/2216).
2225
- Added integration tests for `Get-SqlDscPreferredModule` command to ensure it
2326
functions correctly in real environments
2427
[issue #2218](https://github.com/dsccommunity/SqlServerDsc/issues/2218).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ stages:
309309
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
310310
'tests/Integration/Commands/ConvertTo-SqlDscEditionName.Integration.Tests.ps1'
311311
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'
312+
'tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1'
312313
'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1'
313314
'tests/Integration/Commands/Enable-SqlDscLogin.Integration.Tests.ps1'
314315
'tests/Integration/Commands/Test-SqlDscIsLogin.Integration.Tests.ps1'
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisit
5959
Get-SqlDscManagedComputerService | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6060
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6161
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
62+
Get-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6263
Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6364
Set-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6465
Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)