Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
137 changes: 137 additions & 0 deletions tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1
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
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/Integration/Commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | -
Expand Down
Loading