Skip to content

Commit 62a5c31

Browse files
authored
Add integration test for Disable-SqlDscAudit command (#2270)
1 parent af4a70a commit 62a5c31

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ 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 `Disable-SqlDscAudit` command to ensure it functions
17+
correctly in real environments
18+
[issue #2206](https://github.com/dsccommunity/SqlServerDsc/issues/2206).
1619
- Added integration tests for `ConvertTo-SqlDscDatabasePermission` command to
1720
ensure command reliability
1821
[issue #2209](https://github.com/dsccommunity/SqlServerDsc/issues/2209).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ stages:
334334
'tests/Integration/Commands/Assert-SqlDscAgentOperator.Integration.Tests.ps1'
335335
'tests/Integration/Commands/Enable-SqlDscAgentOperator.Integration.Tests.ps1'
336336
'tests/Integration/Commands/Disable-SqlDscAgentOperator.Integration.Tests.ps1'
337+
'tests/Integration/Commands/Disable-SqlDscAudit.Integration.Tests.ps1'
337338
'tests/Integration/Commands/Add-SqlDscTraceFlag.Integration.Tests.ps1'
338339
# Group 8
339340
'tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1'
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
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 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
# Starting the named instance SQL Server service prior to running tests.
35+
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36+
37+
$script:mockInstanceName = 'DSCSQLTEST'
38+
$script:mockComputerName = Get-ComputerName
39+
40+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
42+
43+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
44+
45+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction Stop
46+
}
47+
48+
AfterAll {
49+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
50+
51+
# Stop the named instance SQL Server service to save memory on the build worker.
52+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
53+
}
54+
55+
Context 'When disabling an audit using ServerObject parameter set' {
56+
BeforeEach {
57+
# Create and enable a test audit for each test
58+
$script:testAuditName = 'SqlDscTestDisableAudit_' + (Get-Random)
59+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
60+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop
61+
}
62+
63+
AfterEach {
64+
# Clean up: Disable and remove the test audit if it still exists
65+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
66+
if ($existingAudit)
67+
{
68+
# Disable the audit first if it's enabled (required before removal)
69+
if ($existingAudit.Enabled)
70+
{
71+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction 'SilentlyContinue'
72+
}
73+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction 'SilentlyContinue'
74+
}
75+
}
76+
77+
It 'Should disable an enabled audit successfully' {
78+
# Verify audit exists and is enabled before disabling
79+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
80+
$existingAudit | Should -Not -BeNullOrEmpty
81+
$existingAudit.Enabled | Should -BeTrue
82+
83+
# Disable the audit
84+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop
85+
86+
# Verify audit is now disabled
87+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
88+
$disabledAudit | Should -Not -BeNullOrEmpty
89+
$disabledAudit.Enabled | Should -BeFalse
90+
}
91+
92+
It 'Should throw error when trying to disable non-existent audit' {
93+
{ Disable-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -Force -ErrorAction Stop } |
94+
Should -Throw
95+
}
96+
97+
It 'Should support the Refresh parameter' {
98+
# Verify audit exists and is enabled before disabling
99+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
100+
$existingAudit | Should -Not -BeNullOrEmpty
101+
$existingAudit.Enabled | Should -BeTrue
102+
103+
# Disable the audit with Refresh parameter
104+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Refresh -Force -ErrorAction Stop
105+
106+
# Verify audit is now disabled
107+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
108+
$disabledAudit | Should -Not -BeNullOrEmpty
109+
$disabledAudit.Enabled | Should -BeFalse
110+
}
111+
}
112+
113+
Context 'When disabling an audit using AuditObject parameter set' {
114+
BeforeEach {
115+
# Create and enable a test audit for each test
116+
$script:testAuditNameForObject = 'SqlDscTestDisableAuditObj_' + (Get-Random)
117+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -LogType 'ApplicationLog' -Force -ErrorAction Stop
118+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -Force -ErrorAction Stop
119+
}
120+
121+
AfterEach {
122+
# Clean up: Disable and remove the test audit if it still exists
123+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction 'SilentlyContinue'
124+
if ($existingAudit)
125+
{
126+
# Disable the audit first if it's enabled (required before removal)
127+
if ($existingAudit.Enabled)
128+
{
129+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -Force -ErrorAction 'SilentlyContinue'
130+
}
131+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -Force -ErrorAction 'SilentlyContinue'
132+
}
133+
}
134+
135+
It 'Should disable an audit using audit object' {
136+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
137+
$auditObject | Should -Not -BeNullOrEmpty
138+
$auditObject.Enabled | Should -BeTrue
139+
140+
# Disable the audit using audit object
141+
$null = Disable-SqlDscAudit -AuditObject $auditObject -Force -ErrorAction Stop
142+
143+
# Verify audit is now disabled
144+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
145+
$disabledAudit | Should -Not -BeNullOrEmpty
146+
$disabledAudit.Enabled | Should -BeFalse
147+
}
148+
149+
It 'Should support pipeline input with audit object' {
150+
$auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
151+
$auditObject | Should -Not -BeNullOrEmpty
152+
$auditObject.Enabled | Should -BeTrue
153+
154+
# Disable the audit using pipeline
155+
$auditObject | Disable-SqlDscAudit -Force -ErrorAction Stop
156+
157+
# Verify audit is now disabled
158+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop
159+
$disabledAudit | Should -Not -BeNullOrEmpty
160+
$disabledAudit.Enabled | Should -BeFalse
161+
}
162+
}
163+
164+
Context 'When disabling an audit using ServerObject parameter set with pipeline' {
165+
BeforeEach {
166+
# Create and enable a test audit for each test
167+
$script:testAuditNameForPipeline = 'SqlDscTestDisableAuditPipe_' + (Get-Random)
168+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -LogType 'ApplicationLog' -Force -ErrorAction Stop
169+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -Force -ErrorAction Stop
170+
}
171+
172+
AfterEach {
173+
# Clean up: Disable and remove the test audit if it still exists
174+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction 'SilentlyContinue'
175+
if ($existingAudit)
176+
{
177+
# Disable the audit first if it's enabled (required before removal)
178+
if ($existingAudit.Enabled)
179+
{
180+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -Force -ErrorAction 'SilentlyContinue'
181+
}
182+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -Force -ErrorAction 'SilentlyContinue'
183+
}
184+
}
185+
186+
It 'Should support pipeline input with server object' {
187+
# Verify audit exists and is enabled before disabling
188+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop
189+
$existingAudit | Should -Not -BeNullOrEmpty
190+
$existingAudit.Enabled | Should -BeTrue
191+
192+
# Disable the audit using pipeline with server object
193+
$script:serverObject | Disable-SqlDscAudit -Name $script:testAuditNameForPipeline -Force -ErrorAction Stop
194+
195+
# Verify audit is now disabled
196+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop
197+
$disabledAudit | Should -Not -BeNullOrEmpty
198+
$disabledAudit.Enabled | Should -BeFalse
199+
}
200+
}
201+
202+
Context 'When disabling multiple audits' {
203+
BeforeAll {
204+
# Create and enable multiple test audits
205+
$script:testAuditNames = @(
206+
'SqlDscTestMultiDisable1_' + (Get-Random),
207+
'SqlDscTestMultiDisable2_' + (Get-Random)
208+
)
209+
210+
foreach ($auditName in $script:testAuditNames)
211+
{
212+
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
213+
$null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction Stop
214+
}
215+
}
216+
217+
AfterAll {
218+
# Clean up: Disable and remove all test audits
219+
foreach ($auditName in $script:testAuditNames)
220+
{
221+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction 'SilentlyContinue'
222+
if ($existingAudit)
223+
{
224+
# Disable the audit first if it's enabled (required before removal)
225+
if ($existingAudit.Enabled)
226+
{
227+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction 'SilentlyContinue'
228+
}
229+
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction 'SilentlyContinue'
230+
}
231+
}
232+
}
233+
234+
It 'Should disable multiple audits successfully' {
235+
# Verify audits exist and are enabled before disabling
236+
foreach ($auditName in $script:testAuditNames)
237+
{
238+
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop
239+
$existingAudit | Should -Not -BeNullOrEmpty
240+
$existingAudit.Enabled | Should -BeTrue
241+
}
242+
243+
# Disable the audits
244+
foreach ($auditName in $script:testAuditNames)
245+
{
246+
$null = Disable-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction Stop
247+
}
248+
249+
# Verify audits are now disabled
250+
foreach ($auditName in $script:testAuditNames)
251+
{
252+
$disabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop
253+
$disabledAudit | Should -Not -BeNullOrEmpty
254+
$disabledAudit.Enabled | Should -BeFalse
255+
}
256+
}
257+
}
258+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Test-SqlDscIsAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | D
8686
Assert-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8787
Enable-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8888
Disable-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
89+
Disable-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8990
Add-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
9091
Remove-SqlDscAgentAlert | 8 | 2 (New-SqlDscAgentAlert) | DSCSQLTEST | -
9192
Remove-SqlDscAgentOperator | 8 | 2 (New-SqlDscAgentOperator) | DSCSQLTEST | -

0 commit comments

Comments
 (0)