Skip to content

Commit 22af3ec

Browse files
Copilotjohlju
andcommitted
Add integration test for Test-SqlDscIsRole command
Co-authored-by: johlju <[email protected]>
1 parent 9b8b4f6 commit 22af3ec

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
1111
correctly in real environments
1212
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
13+
- Added integration tests for `Test-SqlDscIsRole` command to ensure it functions
14+
correctly in real environments
15+
[issue #2229](https://github.com/dsccommunity/SqlServerDsc/issues/2229).
1316

1417
### Fixed
1518

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ stages:
304304
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'
305305
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
306306
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
307+
'tests/Integration/Commands/Test-SqlDscIsRole.Integration.Tests.ps1'
307308
'tests/Integration/Commands/Grant-SqlDscServerPermission.Integration.Tests.ps1'
308309
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
309310
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 'Test-SqlDscIsRole' -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+
39+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
40+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
41+
$mockSqlAdministratorCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $mockSqlAdministratorUserName, $mockSqlAdministratorPassword
42+
43+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $mockSqlAdministratorCredential -ErrorAction 'Stop'
44+
45+
# Shared test roles created by New-SqlDscRole integration tests
46+
$script:sharedTestRoleForIntegrationTests = 'SharedTestRole_ForIntegrationTests'
47+
$script:persistentTestRole = 'SqlDscIntegrationTestRole_Persistent'
48+
}
49+
50+
AfterAll {
51+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
52+
53+
# Stop the named instance SQL Server service to save memory on the build worker.
54+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
55+
}
56+
57+
Context 'When testing for existing system roles' {
58+
It 'Should return true for built-in sysadmin role' {
59+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'sysadmin' -ErrorAction 'Stop'
60+
$result | Should -BeTrue
61+
}
62+
63+
It 'Should return true for built-in serveradmin role' {
64+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'serveradmin' -ErrorAction 'Stop'
65+
$result | Should -BeTrue
66+
}
67+
68+
It 'Should return true for built-in securityadmin role' {
69+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'securityadmin' -ErrorAction 'Stop'
70+
$result | Should -BeTrue
71+
}
72+
}
73+
74+
Context 'When testing for existing user-created roles' {
75+
It 'Should return true for shared test role created by New-SqlDscRole integration test' {
76+
# This role should be created by New-SqlDscRole integration tests
77+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name $script:sharedTestRoleForIntegrationTests -ErrorAction 'Stop'
78+
$result | Should -BeTrue
79+
}
80+
81+
It 'Should return true for persistent test role created by New-SqlDscRole integration test' {
82+
# This role should be created by New-SqlDscRole integration tests
83+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name $script:persistentTestRole -ErrorAction 'Stop'
84+
$result | Should -BeTrue
85+
}
86+
}
87+
88+
Context 'When testing for non-existing roles' {
89+
It 'Should return false for non-existing role' {
90+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'NonExistentRole' -ErrorAction 'Stop'
91+
$result | Should -BeFalse
92+
}
93+
94+
It 'Should return false for role with special characters that does not exist' {
95+
$result = Test-SqlDscIsRole -ServerObject $script:serverObject -Name 'Role$WithSpecial@Characters' -ErrorAction 'Stop'
96+
$result | Should -BeFalse
97+
}
98+
}
99+
100+
Context 'When using pipeline input' {
101+
It 'Should accept ServerObject from pipeline for existing role' {
102+
$result = $script:serverObject | Test-SqlDscIsRole -Name 'sysadmin' -ErrorAction 'Stop'
103+
$result | Should -BeTrue
104+
}
105+
106+
It 'Should accept ServerObject from pipeline for non-existing role' {
107+
$result = $script:serverObject | Test-SqlDscIsRole -Name 'NonExistentRole' -ErrorAction 'Stop'
108+
$result | Should -BeFalse
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)