|
| 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 | +} |
0 commit comments