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 @@ -10,6 +10,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 `Set-SqlDscTraceFlag` command to ensure it functions
correctly in real environments
[issue #2232](https://github.com/dsccommunity/SqlServerDsc/issues/2232).
- Added integration tests for `ConvertFrom-SqlDscServerPermission` command to
ensure it functions correctly in real environments
[issue #2210](https://github.com/dsccommunity/SqlServerDsc/issues/2210).
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ stages:
'tests/Integration/Commands/Get-SqlDscConfigurationOption.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscConfigurationOption.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscConfigurationOption.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscManagedComputerInstance.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Get-SqlDscManagedComputerInstance | 2 | 1 (Install-SqlDscServer), 0 (Prerequisit
Get-SqlDscServerProtocolName | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Get-SqlDscServerProtocol | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Set-SqlDscConfigurationOption | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Disable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Enable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
New-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestRole_Persistent role
Get-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Expand Down
183 changes: 183 additions & 0 deletions tests/Integration/Commands/Set-SqlDscTraceFlag.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
[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 'Set-SqlDscTraceFlag' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'
$script:mockServerName = Get-ComputerName

# Store the original trace flags to restore them later
$script:originalTraceFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
}

AfterAll {
# Restore the original trace flags
if ($script:originalTraceFlags)
{
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $script:originalTraceFlags -Force -ErrorAction 'SilentlyContinue'
}
else
{
# Clear all trace flags if there were none originally
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @() -Force -ErrorAction 'SilentlyContinue'
}

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When setting trace flags' {
It 'Should set a single trace flag and verify the change' {
# Set trace flag 3226 (suppress successful backup messages)
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'

# Verify it's set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 3226
}

It 'Should set multiple trace flags and verify the change' {
# Set multiple trace flags
$testTraceFlags = @(3226, 1222)
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag $testTraceFlags -Force -ErrorAction 'Stop'

# Verify they're set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
foreach ($flag in $testTraceFlags)
{
$result | Should -Contain $flag
}
}

It 'Should replace existing trace flags with new ones' {
# First, set trace flag 3226
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'

# Verify it's set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 3226

# Now replace with different trace flag
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 1222 -Force -ErrorAction 'Stop'

# Verify the old flag is gone and new one is present
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 1222
$result | Should -Not -Contain 3226
}

It 'Should clear all trace flags when passing empty array' {
# First, set some trace flags
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @(3226, 1222) -Force -ErrorAction 'Stop'

# Verify they're set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 3226
$result | Should -Contain 1222

# Now clear all trace flags
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag @() -Force -ErrorAction 'Stop'

# Verify they're cleared
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -BeNullOrEmpty
}
}

Context 'When using ShouldProcess with WhatIf' {
It 'Should not actually change the trace flags when using WhatIf' {
# Get current trace flags
$originalFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName

# Use WhatIf to simulate setting different trace flags
Set-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -TraceFlag 4199 -WhatIf

# Verify the trace flags haven't changed
$currentFlags = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
if ($originalFlags)
{
$currentFlags | Should -HaveCount $originalFlags.Count
foreach ($flag in $originalFlags)
{
$currentFlags | Should -Contain $flag
}
}
else
{
$currentFlags | Should -BeNullOrEmpty
}
}
}

Context 'When using ServiceObject parameter' {
It 'Should accept ServiceObject from pipeline and set trace flags' {
# Get the service object
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ServerName $script:mockServerName -ErrorAction 'Stop'
$serviceObject | Should -Not -BeNullOrEmpty

# Set trace flag using pipeline
$serviceObject | Set-SqlDscTraceFlag -TraceFlag 3226 -Force -ErrorAction 'Stop'

# Verify it's set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 3226
}

It 'Should accept ServiceObject parameter directly' {
# Get the service object
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine' -InstanceName $script:mockInstanceName -ServerName $script:mockServerName -ErrorAction 'Stop'
$serviceObject | Should -Not -BeNullOrEmpty

# Set trace flag using direct parameter
Set-SqlDscTraceFlag -ServiceObject $serviceObject -TraceFlag 1222 -Force -ErrorAction 'Stop'

# Verify it's set
$result = Get-SqlDscTraceFlag -ServerName $script:mockServerName -InstanceName $script:mockInstanceName
$result | Should -Contain 1222
}
}

Context 'When using default parameters' {
It 'Should work with default server name and instance name' {
# This test assumes the command is run on the same server as the SQL instance
# and uses MSSQLSERVER as default instance, but our test instance is DSCSQLTEST
# so we need to specify the instance name
Set-SqlDscTraceFlag -InstanceName $script:mockInstanceName -TraceFlag 3226 -Force -ErrorAction 'Stop'

# Verify it's set
$result = Get-SqlDscTraceFlag -InstanceName $script:mockInstanceName
$result | Should -Contain 3226
}
}
}
Loading