Skip to content
Merged
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Refactored integration tests to remove `finally` blocks from `It`-blocks and
use Pester `BeforeEach`/`AfterEach` blocks instead, following DSC Community
coding guidelines. This improves test cleanup reliability and maintainability
across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)):
- `New-SqlDscLogin.Integration.Tests.ps1`
- `New-SqlDscAudit.Integration.Tests.ps1`
- `Get-SqlDscDatabasePermission.Integration.Tests.ps1`
- `Get-SqlDscPreferredModule.Integration.Tests.ps1`
- `New-SqlDscDatabase.Integration.Tests.ps1`
- Refactored unit tests to remove `finally` blocks from `It`-blocks and
use Pester `AfterEach` blocks instead, following DSC Community coding
guidelines. This improves test cleanup reliability and maintainability
across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)):
- `Set-SqlDscConfigurationOption.Tests.ps1`
- `Get-SqlDscConfigurationOption.Tests.ps1`
- `Test-SqlDscConfigurationOption.Tests.ps1`
- `Test-SqlDscIsDatabasePrincipal` and `Get-SqlDscDatabasePermission`
- Added `Refresh` parameter to refresh SMO collections before checking
database principals, addressing issues with custom database roles created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati
}

Context 'When working with built-in database roles' {
BeforeEach {
$script:customRoleName = $null
}

AfterEach {
# Clean up the custom role if it was created
if ($script:customRoleName)
{
$dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$script:customRoleName];"
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue'
}
}

It 'Should return permissions for db_datareader role' {
# Note: The command excludes fixed roles by default, so this should return null or empty
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ErrorAction 'SilentlyContinue'
Expand All @@ -173,33 +186,24 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati

It 'Should work with non-fixed database roles when they exist' {
# Create a custom database role for testing
$customRoleName = 'TestRole_' + (Get-Random)
$createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$customRoleName];"
$script:customRoleName = 'TestRole_' + (Get-Random)
$createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$script:customRoleName];"
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $createRoleSql -Force -ErrorAction 'Stop'

try
{
# Grant a permission to the custom role
$grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$customRoleName];"
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop'
# Grant a permission to the custom role
$grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$script:customRoleName];"
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop'

# Test getting permissions for the custom role
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $customRoleName -Refresh
# Test getting permissions for the custom role
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:customRoleName -Refresh

$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo]
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo]

# Verify the Connect permission we granted is present
$connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true }
$connectPermission | Should -Not -BeNullOrEmpty
$connectPermission.PermissionState | Should -Be 'Grant'
}
finally
{
# Clean up the custom role
$dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$customRoleName];"
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue'
}
# Verify the Connect permission we granted is present
$connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true }
$connectPermission | Should -Not -BeNullOrEmpty
$connectPermission.PermissionState | Should -Be 'Grant'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,28 @@ Describe 'Get-SqlDscPreferredModule' -Tag @('Integration_SQL2017', 'Integration_
}
}

It 'Should throw an error when specified version does not exist' {
# Backup original environment variable
$originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion

try {
# Set to a non-existent version
$env:SMODefaultModuleVersion = '999.999.999'

{ Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
Context 'When testing error handling' {
BeforeEach {
# Capture the current environment variable value
$script:originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion
}
finally {
# Restore original environment variable
if ($originalSMODefaultModuleVersion) {
$env:SMODefaultModuleVersion = $originalSMODefaultModuleVersion

AfterEach {
# Restore the environment variable
if ($script:originalSMODefaultModuleVersion) {
$env:SMODefaultModuleVersion = $script:originalSMODefaultModuleVersion
}
else {
Remove-Item -Path 'env:SMODefaultModuleVersion' -ErrorAction 'SilentlyContinue'
}
}

It 'Should throw an error when specified version does not exist' {
# Set to a non-existent version
$env:SMODefaultModuleVersion = '999.999.999'

{ Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,18 @@ Describe 'New-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019',
}

It 'Should create a security log audit successfully' {
$securityLogAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random)
$script:testAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random)

try
{
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop

$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $securityLogAuditName
$result.DestinationType | Should -Be 'SecurityLog'
$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:testAuditName
$result.DestinationType | Should -Be 'SecurityLog'

# Verify the audit exists in the server
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
$createdAudit.DestinationType | Should -Be 'SecurityLog'
}
finally
{
# Clean up
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction 'SilentlyContinue'
if ($auditToRemove)
{
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
}
}
# Verify the audit exists in the server
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
$createdAudit | Should -Not -BeNullOrEmpty
$createdAudit.DestinationType | Should -Be 'SecurityLog'
}

It 'Should support PassThru parameter' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,29 @@ Describe 'New-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019
}

Context 'When using the Refresh parameter' {
It 'Should refresh the database collection before creating' {
$uniqueName = 'SqlDscTestRefresh_' + (Get-Random)

try
{
$result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -Refresh -Force -ErrorAction Stop
BeforeEach {
$script:refreshTestDbName = $null
}

$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $uniqueName
}
finally
AfterEach {
# Clean up the refresh test database if it was created
if ($script:refreshTestDbName)
{
# Clean up
$dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -ErrorAction 'SilentlyContinue'
$dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -ErrorAction 'SilentlyContinue'
if ($dbToRemove)
{
$null = Remove-SqlDscDatabase -DatabaseObject $dbToRemove -Force
}
}
}

It 'Should refresh the database collection before creating' {
$script:refreshTestDbName = 'SqlDscTestRefresh_' + (Get-Random)

$result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -Refresh -Force -ErrorAction Stop

$result | Should -Not -BeNullOrEmpty
$result.Name | Should -Be $script:refreshTestDbName
}
}
}
Loading