-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for Enable-SqlDscAudit command #2252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
040cd62
Initial plan
Copilot 335804f
Add integration test for Enable-SqlDscAudit command
Copilot 7cb41e2
Fix PSScriptAnalyzer warnings and update CHANGELOG for Enable-SqlDscA…
Copilot 57dfcf9
Address PR feedback: update build task, remove service management, fi…
Copilot 7630734
Merge branch 'main' into copilot/fix-2223
johlju fe9423d
Add service management for default SQL Server instance in Disconnect-…
johlju a07cbed
Refactor Enable-SqlDscAudit tests to use 'Enabled' property instead o…
johlju 35d8b33
Remove service start/stop commands from Get-SqlDscAudit integration t…
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
tests/Integration/Commands/Enable-SqlDscAudit.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,262 @@ | ||
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')] | ||
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'Using ConvertTo-SecureString with plaintext is allowed in tests.')] | ||
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingEmptyCatchBlock', '', Justification = 'Empty catch blocks are used intentionally for cleanup in test teardown.')] | ||
| 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 'Enable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { | ||
| BeforeAll { | ||
| $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 | ||
| } | ||
|
|
||
| AfterAll { | ||
| Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject | ||
| } | ||
|
|
||
| Context 'When enabling an audit using ServerObject parameter set' { | ||
| BeforeEach { | ||
| # Create a test audit for each test (disabled by default) | ||
| $script:testAuditName = 'SqlDscTestEnableAudit_' + (Get-Random) | ||
| $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'ApplicationLog' -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is created but disabled | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop | ||
| $auditObject.Enabled | Should -BeFalse | ||
| } | ||
|
|
||
| AfterEach { | ||
| # Clean up: disable and remove the test audit | ||
| try { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction 'SilentlyContinue' | ||
| if ($auditObject) { | ||
| if ($auditObject.Enabled) { | ||
| $auditObject.Disable() | ||
| } | ||
| $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
| } | ||
| catch { | ||
| # Ignore cleanup errors | ||
| } | ||
| } | ||
|
|
||
| It 'Should enable an audit successfully' { | ||
| # Enable the audit | ||
| $null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is now enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should throw error when trying to enable non-existent audit' { | ||
| { Enable-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -Force -ErrorAction Stop } | | ||
| Should -Throw | ||
| } | ||
|
|
||
| It 'Should support the Refresh parameter' { | ||
| # Enable the audit with Refresh parameter | ||
| $null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Refresh -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is now enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should not fail when enabling an already enabled audit' { | ||
| # Enable the audit first time | ||
| $null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
|
|
||
| # Enable the audit again - should not fail | ||
| { Enable-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -Force -ErrorAction Stop } | | ||
| Should -Not -Throw | ||
|
|
||
| # Verify audit is still enabled | ||
| $stillEnabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop | ||
| $stillEnabledAudit.Enabled | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When enabling an audit using AuditObject parameter set' { | ||
| BeforeEach { | ||
| # Create a test audit for each test (disabled by default) | ||
| $script:testAuditNameForObject = 'SqlDscTestEnableAuditObj_' + (Get-Random) | ||
| $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -LogType 'ApplicationLog' -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is created but disabled | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop | ||
| $auditObject.Enabled | Should -BeFalse | ||
| } | ||
|
|
||
| AfterEach { | ||
| # Clean up: disable and remove the test audit | ||
| try { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction 'SilentlyContinue' | ||
| if ($auditObject) { | ||
| if ($auditObject.Enabled) { | ||
| $auditObject.Disable() | ||
| } | ||
| $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
| } | ||
| catch { | ||
| # Ignore cleanup errors | ||
| } | ||
| } | ||
|
|
||
| It 'Should enable an audit using audit object' { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop | ||
| $auditObject | Should -Not -BeNullOrEmpty | ||
| $auditObject.Enabled | Should -BeFalse | ||
|
|
||
| # Enable the audit using audit object | ||
| $null = Enable-SqlDscAudit -AuditObject $auditObject -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is now enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should support pipeline input with audit object' { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop | ||
| $auditObject | Should -Not -BeNullOrEmpty | ||
| $auditObject.Enabled | Should -BeFalse | ||
|
|
||
| # Enable the audit using pipeline | ||
| $auditObject | Enable-SqlDscAudit -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is now enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForObject -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When enabling an audit using ServerObject parameter set with pipeline' { | ||
| BeforeEach { | ||
| # Create a test audit for each test (disabled by default) | ||
| $script:testAuditNameForPipeline = 'SqlDscTestEnableAuditPipe_' + (Get-Random) | ||
| $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -LogType 'ApplicationLog' -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is created but disabled | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop | ||
| $auditObject.Enabled | Should -BeFalse | ||
| } | ||
|
|
||
| AfterEach { | ||
| # Clean up: disable and remove the test audit | ||
| try { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction 'SilentlyContinue' | ||
| if ($auditObject) { | ||
| if ($auditObject.Enabled) { | ||
| $auditObject.Disable() | ||
| } | ||
| $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
| } | ||
| catch { | ||
| # Ignore cleanup errors | ||
| } | ||
| } | ||
|
|
||
| It 'Should support pipeline input with server object' { | ||
| # Enable the audit using pipeline with server object | ||
| $script:serverObject | Enable-SqlDscAudit -Name $script:testAuditNameForPipeline -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is now enabled | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditNameForPipeline -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
| } | ||
|
|
||
| Context 'When enabling multiple audits' { | ||
| BeforeAll { | ||
| # Create multiple test audits (disabled by default) | ||
| $script:testAuditNames = @( | ||
| 'SqlDscTestMultiEnable1_' + (Get-Random), | ||
| 'SqlDscTestMultiEnable2_' + (Get-Random) | ||
| ) | ||
|
|
||
| foreach ($auditName in $script:testAuditNames) | ||
| { | ||
| $null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop | ||
|
|
||
| # Verify audit is created but disabled | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop | ||
| $auditObject.Enabled | Should -BeFalse | ||
| } | ||
| } | ||
|
|
||
| AfterAll { | ||
| # Clean up: disable and remove all test audits | ||
| foreach ($auditName in $script:testAuditNames) | ||
| { | ||
| try { | ||
| $auditObject = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction 'SilentlyContinue' | ||
| if ($auditObject) { | ||
| if ($auditObject.Enabled) { | ||
| $auditObject.Disable() | ||
| } | ||
| $null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction 'SilentlyContinue' | ||
| } | ||
| } | ||
| catch { | ||
| # Ignore cleanup errors | ||
| } | ||
| } | ||
| } | ||
|
|
||
| It 'Should enable multiple audits successfully' { | ||
| # Enable the audits | ||
| foreach ($auditName in $script:testAuditNames) | ||
| { | ||
| $null = Enable-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction Stop | ||
| } | ||
|
|
||
| # Verify audits are now enabled | ||
| foreach ($auditName in $script:testAuditNames) | ||
| { | ||
| $enabledAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction Stop | ||
| $enabledAudit.Enabled | Should -BeTrue | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.