Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -10,6 +10,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 `Test-SqlDscIsRole` command to ensure it functions
correctly in real environments
[issue #2229](https://github.com/dsccommunity/SqlServerDsc/issues/2229).

### Fixed

Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ stages:
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscIsRole.Integration.Tests.ps1'
'tests/Integration/Commands/Grant-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
Expand Down
111 changes: 111 additions & 0 deletions tests/Integration/Commands/Test-SqlDscIsRole.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
[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 build" first.'
}
}

BeforeAll {
$script:moduleName = 'SqlServerDsc'

Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
}

Describe 'Test-SqlDscIsRole' -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'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockSqlAdministratorCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $mockSqlAdministratorUserName, $mockSqlAdministratorPassword

$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $mockSqlAdministratorCredential -ErrorAction 'Stop'

# Shared test roles created by New-SqlDscRole integration tests
$script:sharedTestRoleForIntegrationTests = 'SharedTestRole_ForIntegrationTests'
$script:persistentTestRole = 'SqlDscIntegrationTestRole_Persistent'
}

AfterAll {
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 testing for existing system roles' {
It 'Should return true for built-in sysadmin role' {
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'sysadmin' -ErrorAction 'Stop'
$result | Should -BeTrue
}

It 'Should return true for built-in serveradmin role' {
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'serveradmin' -ErrorAction 'Stop'
$result | Should -BeTrue
}

It 'Should return true for built-in securityadmin role' {
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'securityadmin' -ErrorAction 'Stop'
$result | Should -BeTrue
}
}

Context 'When testing for existing user-created roles' {
It 'Should return true for shared test role created by New-SqlDscRole integration test' {
# This role should be created by New-SqlDscRole integration tests
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name $script:sharedTestRoleForIntegrationTests -ErrorAction 'Stop'
$result | Should -BeTrue
}

It 'Should return true for persistent test role created by New-SqlDscRole integration test' {
# This role should be created by New-SqlDscRole integration tests
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name $script:persistentTestRole -ErrorAction 'Stop'
$result | Should -BeTrue
}
}

Context 'When testing for non-existing roles' {
It 'Should return false for non-existing role' {
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'NonExistentRole' -ErrorAction 'Stop'
$result | Should -BeFalse
}

It 'Should return false for role with special characters that does not exist' {
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'Role$WithSpecial@Characters' -ErrorAction 'Stop'
$result | Should -BeFalse
}
}

Context 'When using pipeline input' {
It 'Should accept ServerObject from pipeline for existing role' {
$result = $script:serverObject | Test-SqlDscIsRole -Name 'sysadmin' -ErrorAction 'Stop'
$result | Should -BeTrue
}

It 'Should accept ServerObject from pipeline for non-existing role' {
$result = $script:serverObject | Test-SqlDscIsRole -Name 'NonExistentRole' -ErrorAction 'Stop'
$result | Should -BeFalse
}
}
}