|
| 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 build" first.' |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +BeforeAll { |
| 27 | + $script:moduleName = 'SqlServerDsc' |
| 28 | + |
| 29 | + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' |
| 30 | +} |
| 31 | + |
| 32 | +Describe 'Remove-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 removing an audit using ServerObject parameter set' { |
| 56 | + BeforeEach { |
| 57 | + # Create a test audit for each test |
| 58 | + $script:testAuditName = 'SqlDscTestRemoveAudit_' + (Get-Random) |
| 59 | + $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop |
| 60 | + } |
| 61 | + |
| 62 | + It 'Should remove an audit successfully' { |
| 63 | + # Verify audit exists before removal |
| 64 | + $existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop |
| 65 | + $existingAudit | Should -Not -BeNullOrEmpty |
| 66 | + |
| 67 | + # Remove the audit |
| 68 | + $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop |
| 69 | + |
| 70 | + # Verify audit no longer exists |
| 71 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue' |
| 72 | + $removedAudit | Should -BeNullOrEmpty |
| 73 | + } |
| 74 | + |
| 75 | + It 'Should throw error when trying to remove non-existent audit' { |
| 76 | + { Remove-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -Force -ErrorAction Stop } | |
| 77 | + Should -Throw |
| 78 | + } |
| 79 | + |
| 80 | + It 'Should support the Refresh parameter' { |
| 81 | + # Verify audit exists before removal |
| 82 | + $existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop |
| 83 | + $existingAudit | Should -Not -BeNullOrEmpty |
| 84 | + |
| 85 | + # Remove the audit with Refresh parameter |
| 86 | + $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Refresh -Force -ErrorAction Stop |
| 87 | + |
| 88 | + # Verify audit no longer exists |
| 89 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue' |
| 90 | + $removedAudit | Should -BeNullOrEmpty |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Context 'When removing an audit using AuditObject parameter set' { |
| 95 | + BeforeEach { |
| 96 | + # Create a test audit for each test |
| 97 | + $script:testAuditNameForObject = 'SqlDscTestRemoveAuditObj_' + (Get-Random) |
| 98 | + $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -LogType 'ApplicationLog' -Force -ErrorAction Stop |
| 99 | + } |
| 100 | + |
| 101 | + It 'Should remove an audit using audit object' { |
| 102 | + $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop |
| 103 | + $auditObject | Should -Not -BeNullOrEmpty |
| 104 | + |
| 105 | + # Remove the audit using audit object |
| 106 | + $null = Remove-SqlDscAudit -AuditObject $auditObject -Force -ErrorAction Stop |
| 107 | + |
| 108 | + # Verify audit no longer exists |
| 109 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction 'SilentlyContinue' |
| 110 | + $removedAudit | Should -BeNullOrEmpty |
| 111 | + } |
| 112 | + |
| 113 | + It 'Should support pipeline input with audit object' { |
| 114 | + $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop |
| 115 | + $auditObject | Should -Not -BeNullOrEmpty |
| 116 | + |
| 117 | + # Remove the audit using pipeline |
| 118 | + $auditObject | Remove-SqlDscAudit -Force -ErrorAction Stop |
| 119 | + |
| 120 | + # Verify audit no longer exists |
| 121 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction 'SilentlyContinue' |
| 122 | + $removedAudit | Should -BeNullOrEmpty |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + Context 'When removing an audit using ServerObject parameter set with pipeline' { |
| 127 | + BeforeEach { |
| 128 | + # Create a test audit for each test |
| 129 | + $script:testAuditNameForPipeline = 'SqlDscTestRemoveAuditPipe_' + (Get-Random) |
| 130 | + $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -LogType 'ApplicationLog' -Force -ErrorAction Stop |
| 131 | + } |
| 132 | + |
| 133 | + It 'Should support pipeline input with server object' { |
| 134 | + # Verify audit exists before removal |
| 135 | + $existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop |
| 136 | + $existingAudit | Should -Not -BeNullOrEmpty |
| 137 | + |
| 138 | + # Remove the audit using pipeline with server object |
| 139 | + $script:serverObject | Remove-SqlDscAudit -Name $script:testAuditNameForPipeline -Force -ErrorAction Stop |
| 140 | + |
| 141 | + # Verify audit no longer exists |
| 142 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction 'SilentlyContinue' |
| 143 | + $removedAudit | Should -BeNullOrEmpty |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + Context 'When removing multiple audits' { |
| 148 | + BeforeAll { |
| 149 | + # Create multiple test audits |
| 150 | + $script:testAuditNames = @( |
| 151 | + 'SqlDscTestMultiRemove1_' + (Get-Random), |
| 152 | + 'SqlDscTestMultiRemove2_' + (Get-Random) |
| 153 | + ) |
| 154 | + |
| 155 | + foreach ($auditName in $script:testAuditNames) |
| 156 | + { |
| 157 | + $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + It 'Should remove multiple audits successfully' { |
| 162 | + # Verify audits exist before removal |
| 163 | + foreach ($auditName in $script:testAuditNames) |
| 164 | + { |
| 165 | + $existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop |
| 166 | + $existingAudit | Should -Not -BeNullOrEmpty |
| 167 | + } |
| 168 | + |
| 169 | + # Remove the audits |
| 170 | + foreach ($auditName in $script:testAuditNames) |
| 171 | + { |
| 172 | + $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction Stop |
| 173 | + } |
| 174 | + |
| 175 | + # Verify audits no longer exist |
| 176 | + foreach ($auditName in $script:testAuditNames) |
| 177 | + { |
| 178 | + $removedAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction 'SilentlyContinue' |
| 179 | + $removedAudit | Should -BeNullOrEmpty |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments