Skip to content

Commit a3a74f0

Browse files
authored
Add integration test for Enable-SqlDscAudit command (#2252)
1 parent 77d1c88 commit a3a74f0

File tree

6 files changed

+276
-6
lines changed

6 files changed

+276
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1414
correctly in real environments
1515
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
16+
- Added integration tests for `Enable-SqlDscAudit` command to ensure command
17+
reliability [issue #2223](https://github.com/dsccommunity/SqlServerDsc/issues/2223).
1618
- Added integration tests for `Get-SqlDscAudit` command to ensure it functions
1719
correctly in real environments
1820
[issue #2222](https://github.com/dsccommunity/SqlServerDsc/issues/2222).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ stages:
347347
# Group 8
348348
'tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1'
349349
'tests/Integration/Commands/Remove-SqlDscAgentOperator.Integration.Tests.ps1'
350+
'tests/Integration/Commands/Enable-SqlDscAudit.Integration.Tests.ps1'
350351
'tests/Integration/Commands/Get-SqlDscAudit.Integration.Tests.ps1'
351352
'tests/Integration/Commands/Remove-SqlDscAudit.Integration.Tests.ps1'
352353
'tests/Integration/Commands/Set-SqlDscAudit.Integration.Tests.ps1'

tests/Integration/Commands/Disconnect-SqlDscDatabaseEngine.Integration.Tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ Describe 'Disconnect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integr
3636
}
3737

3838
Context 'When disconnecting from the default instance' {
39+
BeforeAll {
40+
# Starting the default instance SQL Server service prior to running tests.
41+
Start-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
42+
}
43+
44+
AfterAll {
45+
# Stop the default instance SQL Server service to save memory on the build worker.
46+
Stop-Service -Name 'MSSQLSERVER' -Verbose -ErrorAction 'Stop'
47+
}
48+
3949
It 'Should have the default instance SQL Server service started' {
4050
$getServiceResult = Get-Service -Name 'MSSQLSERVER' -ErrorAction 'Stop'
4151

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
2+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Using ConvertTo-SecureString with plaintext is allowed in tests.')]
3+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingEmptyCatchBlock', '', Justification = 'Empty catch blocks are used intentionally for cleanup in test teardown.')]
4+
param ()
5+
6+
BeforeDiscovery {
7+
try
8+
{
9+
if (-not (Get-Module -Name 'DscResource.Test'))
10+
{
11+
# Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
12+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
13+
{
14+
# Redirect all streams to $null, except the error stream (stream 2)
15+
& "$PSScriptRoot/../../../build.ps1" -Tasks 'noop' 3>&1 4>&1 5>&1 6>&1 > $null
16+
}
17+
18+
# If the dependencies have not been resolved, this will throw an error.
19+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
20+
}
21+
}
22+
catch [System.IO.FileNotFoundException]
23+
{
24+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks noop" first.'
25+
}
26+
}
27+
28+
BeforeAll {
29+
$script:moduleName = 'SqlServerDsc'
30+
31+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
32+
}
33+
34+
Describe 'Enable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
35+
BeforeAll {
36+
$script:mockInstanceName = 'DSCSQLTEST'
37+
$script:mockComputerName = Get-ComputerName
38+
39+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
40+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
41+
42+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
43+
44+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction Stop
45+
}
46+
47+
AfterAll {
48+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
49+
}
50+
51+
Context 'When enabling an audit using ServerObject parameter set' {
52+
BeforeEach {
53+
# Create a test audit for each test (disabled by default)
54+
$script:testAuditName = 'SqlDscTestEnableAudit_' + (Get-Random)
55+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
56+
57+
# Verify audit is created but disabled
58+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
59+
$auditObject.Enabled | Should -BeFalse
60+
}
61+
62+
AfterEach {
63+
# Clean up: disable and remove the test audit
64+
try {
65+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
66+
if ($auditObject) {
67+
if ($auditObject.Enabled) {
68+
$auditObject.Disable()
69+
}
70+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction 'SilentlyContinue'
71+
}
72+
}
73+
catch {
74+
# Ignore cleanup errors
75+
}
76+
}
77+
78+
It 'Should enable an audit successfully' {
79+
# Enable the audit
80+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop
81+
82+
# Verify audit is now enabled
83+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
84+
$enabledAudit.Enabled | Should -BeTrue
85+
}
86+
87+
It 'Should throw error when trying to enable non-existent audit' {
88+
{ Enable-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -Force -ErrorAction Stop } |
89+
Should -Throw
90+
}
91+
92+
It 'Should support the Refresh parameter' {
93+
# Enable the audit with Refresh parameter
94+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Refresh -Force -ErrorAction Stop
95+
96+
# Verify audit is now enabled
97+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
98+
$enabledAudit.Enabled | Should -BeTrue
99+
}
100+
101+
It 'Should not fail when enabling an already enabled audit' {
102+
# Enable the audit first time
103+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop
104+
105+
# Verify audit is enabled
106+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
107+
$enabledAudit.Enabled | Should -BeTrue
108+
109+
# Enable the audit again - should not fail
110+
{ Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop } |
111+
Should -Not -Throw
112+
113+
# Verify audit is still enabled
114+
$stillEnabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
115+
$stillEnabledAudit.Enabled | Should -BeTrue
116+
}
117+
}
118+
119+
Context 'When enabling an audit using AuditObject parameter set' {
120+
BeforeEach {
121+
# Create a test audit for each test (disabled by default)
122+
$script:testAuditNameForObject = 'SqlDscTestEnableAuditObj_' + (Get-Random)
123+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -LogType 'ApplicationLog' -Force -ErrorAction Stop
124+
125+
# Verify audit is created but disabled
126+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
127+
$auditObject.Enabled | Should -BeFalse
128+
}
129+
130+
AfterEach {
131+
# Clean up: disable and remove the test audit
132+
try {
133+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction 'SilentlyContinue'
134+
if ($auditObject) {
135+
if ($auditObject.Enabled) {
136+
$auditObject.Disable()
137+
}
138+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -Force -ErrorAction 'SilentlyContinue'
139+
}
140+
}
141+
catch {
142+
# Ignore cleanup errors
143+
}
144+
}
145+
146+
It 'Should enable an audit using audit object' {
147+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
148+
$auditObject | Should -Not -BeNullOrEmpty
149+
$auditObject.Enabled | Should -BeFalse
150+
151+
# Enable the audit using audit object
152+
$null = Enable-SqlDscAudit -AuditObject $auditObject -Force -ErrorAction Stop
153+
154+
# Verify audit is now enabled
155+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
156+
$enabledAudit.Enabled | Should -BeTrue
157+
}
158+
159+
It 'Should support pipeline input with audit object' {
160+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
161+
$auditObject | Should -Not -BeNullOrEmpty
162+
$auditObject.Enabled | Should -BeFalse
163+
164+
# Enable the audit using pipeline
165+
$auditObject | Enable-SqlDscAudit -Force -ErrorAction Stop
166+
167+
# Verify audit is now enabled
168+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
169+
$enabledAudit.Enabled | Should -BeTrue
170+
}
171+
}
172+
173+
Context 'When enabling an audit using ServerObject parameter set with pipeline' {
174+
BeforeEach {
175+
# Create a test audit for each test (disabled by default)
176+
$script:testAuditNameForPipeline = 'SqlDscTestEnableAuditPipe_' + (Get-Random)
177+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -LogType 'ApplicationLog' -Force -ErrorAction Stop
178+
179+
# Verify audit is created but disabled
180+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop
181+
$auditObject.Enabled | Should -BeFalse
182+
}
183+
184+
AfterEach {
185+
# Clean up: disable and remove the test audit
186+
try {
187+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction 'SilentlyContinue'
188+
if ($auditObject) {
189+
if ($auditObject.Enabled) {
190+
$auditObject.Disable()
191+
}
192+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -Force -ErrorAction 'SilentlyContinue'
193+
}
194+
}
195+
catch {
196+
# Ignore cleanup errors
197+
}
198+
}
199+
200+
It 'Should support pipeline input with server object' {
201+
# Enable the audit using pipeline with server object
202+
$script:serverObject | Enable-SqlDscAudit -Name $script:testAuditNameForPipeline -Force -ErrorAction Stop
203+
204+
# Verify audit is now enabled
205+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop
206+
$enabledAudit.Enabled | Should -BeTrue
207+
}
208+
}
209+
210+
Context 'When enabling multiple audits' {
211+
BeforeAll {
212+
# Create multiple test audits (disabled by default)
213+
$script:testAuditNames = @(
214+
'SqlDscTestMultiEnable1_' + (Get-Random),
215+
'SqlDscTestMultiEnable2_' + (Get-Random)
216+
)
217+
218+
foreach ($auditName in $script:testAuditNames)
219+
{
220+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
221+
222+
# Verify audit is created but disabled
223+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop
224+
$auditObject.Enabled | Should -BeFalse
225+
}
226+
}
227+
228+
AfterAll {
229+
# Clean up: disable and remove all test audits
230+
foreach ($auditName in $script:testAuditNames)
231+
{
232+
try {
233+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction 'SilentlyContinue'
234+
if ($auditObject) {
235+
if ($auditObject.Enabled) {
236+
$auditObject.Disable()
237+
}
238+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction 'SilentlyContinue'
239+
}
240+
}
241+
catch {
242+
# Ignore cleanup errors
243+
}
244+
}
245+
}
246+
247+
It 'Should enable multiple audits successfully' {
248+
# Enable the audits
249+
foreach ($auditName in $script:testAuditNames)
250+
{
251+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction Stop
252+
}
253+
254+
# Verify audits are now enabled
255+
foreach ($auditName in $script:testAuditNames)
256+
{
257+
$enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop
258+
$enabledAudit.Enabled | Should -BeTrue
259+
}
260+
}
261+
}
262+
}

tests/Integration/Commands/Get-SqlDscAudit.Integration.Tests.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ BeforeAll {
3131

3232
Describe 'Get-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:mockInstanceName = 'DSCSQLTEST'
3835
$script:mockComputerName = Get-ComputerName
3936

@@ -73,9 +70,6 @@ Describe 'Get-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019',
7370
}
7471

7572
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
76-
77-
# Stop the named instance SQL Server service to save memory on the build worker.
78-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
7973
}
8074

8175
Context 'When getting all SQL Server audits' {

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Set-SqlDscStartupParameter | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | D
6363
Set-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6464
Disable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6565
Enable-SqlDscLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
66+
Enable-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6667
Test-SqlDscIsLogin | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6768
Test-SqlDscIsLoginEnabled | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6869
New-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | SqlDscIntegrationTestRole_Persistent role

0 commit comments

Comments
 (0)