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