-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathNew-SqlDscAudit.Integration.Tests.ps1
More file actions
348 lines (280 loc) · 16.1 KB
/
New-SqlDscAudit.Integration.Tests.ps1
File metadata and controls
348 lines (280 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
[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 'New-SqlDscAudit' -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: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)
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction Stop
# Create a temporary directory for file audits if it doesn't exist
$script:testAuditPath = 'C:\Temp\SqlDscTestAudits'
if (-not (Test-Path -Path $script:testAuditPath))
{
$null = New-Item -Path $script:testAuditPath -ItemType Directory -Force
}
}
AfterAll {
# Clean up any test audits that might remain
$testAudits = Get-SqlDscAudit -ServerObject $script:serverObject -ErrorAction 'SilentlyContinue' |
Where-Object { $_.Name -like 'SqlDscTestAudit*' }
foreach ($audit in $testAudits)
{
try
{
Remove-SqlDscAudit -AuditObject $audit -Force -ErrorAction 'SilentlyContinue'
}
catch
{
# Ignore cleanup errors
}
}
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
# Clean up temporary directory
if (Test-Path -Path $script:testAuditPath)
{
Remove-Item -Path $script:testAuditPath -Recurse -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 creating a new application log audit' {
BeforeEach {
$script:testAuditName = 'SqlDscTestAudit_AppLog_' + (Get-Random)
}
AfterEach {
# Clean up the audit created in this test
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
It 'Should create an application log audit successfully' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:testAuditName
$result.DestinationType | Should -Be 'ApplicationLog'
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit'
# Verify the audit exists in the server
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
$createdAudit.Name | Should -Be $script:testAuditName
$createdAudit.DestinationType | Should -Be 'ApplicationLog'
}
It 'Should create a security log audit successfully' {
$securityLogAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random)
try
{
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $securityLogAuditName
$result.DestinationType | Should -Be 'SecurityLog'
# Verify the audit exists in the server
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
$createdAudit.DestinationType | Should -Be 'SecurityLog'
}
finally
{
# Clean up
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
}
It 'Should support PassThru parameter' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit'
$result.Name | Should -Be $script:testAuditName
}
}
Context 'When creating a new file audit' {
BeforeEach {
$script:testAuditName = 'SqlDscTestAudit_File_' + (Get-Random)
}
AfterEach {
# Clean up the audit created in this test
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
It 'Should create a file audit successfully' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Path $script:testAuditPath -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:testAuditName
$result.DestinationType | Should -Be 'File'
$result.FilePath | Should -Be $script:testAuditPath
# Verify the audit exists in the server
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
$createdAudit.DestinationType | Should -Be 'File'
$createdAudit.FilePath | Should -Be $script:testAuditPath
}
It 'Should create a file audit with maximum file size' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Path $script:testAuditPath -MaximumFileSize 100 -MaximumFileSizeUnit 'Megabyte' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.MaximumFileSize | Should -Be 100
$result.MaximumFileSizeUnit | Should -Be 'MB'
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.MaximumFileSize | Should -Be 100
$createdAudit.MaximumFileSizeUnit | Should -Be 'MB'
}
It 'Should create a file audit with maximum files and reserve disk space' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Path $script:testAuditPath -MaximumFiles 5 -MaximumFileSize 50 -MaximumFileSizeUnit 'Megabyte' -ReserveDiskSpace -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.MaximumFiles | Should -Be 5
$result.MaximumFileSize | Should -Be 50
$result.MaximumFileSizeUnit | Should -Be 'MB'
$result.ReserveDiskSpace | Should -BeTrue
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.MaximumFiles | Should -Be 5
$createdAudit.MaximumFileSize | Should -Be 50
$createdAudit.MaximumFileSizeUnit | Should -Be 'MB'
$createdAudit.ReserveDiskSpace | Should -BeTrue
}
It 'Should create a file audit with maximum rollover files' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Path $script:testAuditPath -MaximumRolloverFiles 10 -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.MaximumRolloverFiles | Should -Be 10
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.MaximumRolloverFiles | Should -Be 10
}
}
Context 'When creating an audit with advanced options' {
BeforeEach {
$script:testAuditName = 'SqlDscTestAudit_Advanced_' + (Get-Random)
}
AfterEach {
# Clean up the audit created in this test
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
It 'Should create an audit with OnFailure setting' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -OnFailure 'Continue' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.OnFailure | Should -Be 'Continue'
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.OnFailure | Should -Be 'Continue'
}
It 'Should create an audit with QueueDelay setting' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -QueueDelay 5000 -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.QueueDelay | Should -Be 5000
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.QueueDelay | Should -Be 5000
}
It 'Should create an audit with AuditGuid setting' {
$testGuid = [System.Guid]::NewGuid().ToString()
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -AuditGuid $testGuid -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Guid | Should -Be $testGuid
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.Guid | Should -Be $testGuid
}
It 'Should create an audit with AuditFilter setting' {
$testFilter = "([database_name] = 'master')"
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -AuditFilter $testFilter -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Filter | Should -Be $testFilter
# Verify the audit exists with correct properties
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit.Filter | Should -Be $testFilter
}
It 'Should support Refresh parameter' {
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Refresh -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:testAuditName
# Verify the audit exists
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
}
}
Context 'When creating an audit using pipeline input' {
BeforeEach {
$script:testAuditName = 'SqlDscTestAudit_Pipeline_' + (Get-Random)
}
AfterEach {
# Clean up the audit created in this test
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
It 'Should support pipeline input with server object' {
$result = $script:serverObject | New-SqlDscAudit -Name $script:testAuditName -LogType 'ApplicationLog' -PassThru -Force -ErrorAction Stop
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:testAuditName
# Verify the audit exists
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
}
}
Context 'When handling error conditions' {
BeforeEach {
$script:testAuditName = 'SqlDscTestAudit_Error_' + (Get-Random)
}
AfterEach {
# Clean up the audit created in this test
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
It 'Should throw error when trying to create an audit that already exists' {
# First, create an audit
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
# Then try to create another audit with the same name
{ New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop } |
Should -Throw
}
It 'Should throw error when path does not exist for file audit' {
$nonExistentPath = Join-Path -Path $env:TEMP -ChildPath 'NonExistentPath'
{ New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Path $nonExistentPath -Force -ErrorAction Stop } |
Should -Throw
}
}
}