Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
correctly in real environments
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
- Added integration tests for `Get-SqlDscAudit` command to ensure it functions
correctly in real environments
[issue #2222](https://github.com/dsccommunity/SqlServerDsc/issues/2222).
- Added integration tests for `Disconnect-SqlDscDatabaseEngine` command to ensure
command reliability in real environments
[issue #2224](https://github.com/dsccommunity/SqlServerDsc/issues/2224).
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ stages:
# Group 8
'tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscAgentOperator.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscAudit.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscAudit.Integration.Tests.ps1'
'tests/Integration/Commands/Set-SqlDscAudit.Integration.Tests.ps1'
'tests/Integration/Commands/Remove-SqlDscDatabase.Integration.Tests.ps1'
Expand Down
165 changes: 165 additions & 0 deletions tests/Integration/Commands/Get-SqlDscAudit.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
[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 'Get-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 test audits for the tests
$script:testAuditName1 = 'SqlDscTestGetAudit1_' + (Get-Random)
$script:testAuditName2 = 'SqlDscTestGetAudit2_' + (Get-Random)

$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName1 -LogType 'ApplicationLog' -Force -ErrorAction Stop
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName2 -LogType 'ApplicationLog' -Force -ErrorAction Stop
}

AfterAll {
# Clean up test audits
$testAuditsToRemove = @($script:testAuditName1, $script:testAuditName2)

foreach ($auditName in $testAuditsToRemove)
{
try
{
$existingAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -ErrorAction 'SilentlyContinue'
if ($existingAudit)
{
$null = Remove-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -Force -ErrorAction 'SilentlyContinue'
}
}
catch
{
# Ignore cleanup errors
}
}

Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When getting all SQL Server audits' {
It 'Should return an array of Audit objects' {
$result = Get-SqlDscAudit -ServerObject $script:serverObject

<#
Casting to array to ensure we get the count on Windows PowerShell
when there is only one audit.
#>
@($result).Count | Should -BeGreaterOrEqual 2
@($result)[0] | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit'
}

It 'Should return test audits that were created' {
$result = Get-SqlDscAudit -ServerObject $script:serverObject

$result.Name | Should -Contain $script:testAuditName1
$result.Name | Should -Contain $script:testAuditName2
}
}

Context 'When getting a specific SQL Server audit' {
It 'Should return the specified audit when it exists' {
$result = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName1

$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit'
$result.Name | Should -Be $script:testAuditName1
}

It 'Should throw an error when the audit does not exist' {
{ Get-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -ErrorAction 'Stop' } |
Should -Throw
}

It 'Should return null when the audit does not exist and error action is SilentlyContinue' {
$result = Get-SqlDscAudit -ServerObject $script:serverObject -Name 'NonExistentAudit' -ErrorAction 'SilentlyContinue'

$result | Should -BeNullOrEmpty
}
}

Context 'When using the Refresh parameter' {
It 'Should return the same results with and without Refresh for all audits' {
$resultWithoutRefresh = Get-SqlDscAudit -ServerObject $script:serverObject
$resultWithRefresh = Get-SqlDscAudit -ServerObject $script:serverObject -Refresh

@($resultWithoutRefresh).Count | Should -Be @($resultWithRefresh).Count
}

It 'Should return the same result with and without Refresh for specific audit' {
$resultWithoutRefresh = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName1
$resultWithRefresh = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName1 -Refresh

$resultWithoutRefresh.Name | Should -Be $resultWithRefresh.Name
$resultWithoutRefresh.Name | Should -Be $script:testAuditName1
}
}

Context 'When using pipeline input' {
It 'Should accept ServerObject from pipeline for all audits' {
$result = $script:serverObject | Get-SqlDscAudit

@($result).Count | Should -BeGreaterOrEqual 2
$result.Name | Should -Contain $script:testAuditName1
$result.Name | Should -Contain $script:testAuditName2
}

It 'Should accept ServerObject from pipeline for specific audit' {
$result = $script:serverObject | Get-SqlDscAudit -Name $script:testAuditName1

$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit'
$result.Name | Should -Be $script:testAuditName1
}
}

Context 'When testing audit properties' {
It 'Should return audits with expected properties' {
$result = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName1

$result.Name | Should -Be $script:testAuditName1
$result.Parent | Should -Be $script:serverObject
$result.Enabled | Should -Not -BeNull
$result.DestinationType | Should -Not -BeNull
}
}
}
1 change: 1 addition & 0 deletions tests/Integration/Commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Test-SqlDscIsAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | D
Assert-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Enable-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Disable-SqlDscAgentOperator | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Get-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Disable-SqlDscAudit | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Add-SqlDscTraceFlag | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
Remove-SqlDscAgentAlert | 8 | 2 (New-SqlDscAgentAlert) | DSCSQLTEST | -
Expand Down
Loading