Skip to content

Commit c52ea50

Browse files
authored
Add integration test for ConvertTo-SqlDscServerPermission command (#2305)
1 parent 8ec0a9a commit c52ea50

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- Added integration tests for `ConvertTo-SqlDscServerPermission` command to ensure
11+
command reliability [issue #2207](https://github.com/dsccommunity/SqlServerDsc/issues/2207).
1012
- Added post-installation configuration integration test to configure SSL certificate
1113
support for SQL Server instance DSCSQLTEST in CI environment, enabling testing
1214
of encryption-related functionality. The new `PostInstallationConfiguration`

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ stages:
327327
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
328328
'tests/Integration/Commands/Set-SqlDscServerPermission.Integration.Tests.ps1'
329329
'tests/Integration/Commands/ConvertFrom-SqlDscServerPermission.Integration.Tests.ps1'
330+
'tests/Integration/Commands/ConvertTo-SqlDscServerPermission.Integration.Tests.ps1'
330331
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
331332
'tests/Integration/Commands/Deny-SqlDscServerPermission.Integration.Tests.ps1'
332333
'tests/Integration/Commands/Revoke-SqlDscServerPermission.Integration.Tests.ps1'
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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 noop" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:moduleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
}
31+
32+
Describe 'ConvertTo-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
$script:mockInstanceName = 'DSCSQLTEST'
35+
36+
$mockSqlAdministratorUserName = 'SqlAdmin'
37+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
38+
39+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
40+
41+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
42+
}
43+
44+
AfterAll {
45+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject -ErrorAction 'Stop'
46+
}
47+
48+
Context 'When converting empty collection of ServerPermissionInfo' {
49+
It 'Should return empty array for empty ServerPermissionInfo collection' {
50+
$emptyCollection = [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]] @()
51+
52+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $emptyCollection
53+
54+
$result | Should -BeNullOrEmpty
55+
}
56+
57+
It 'Should accept empty collection through pipeline' {
58+
$emptyCollection = [Microsoft.SqlServer.Management.Smo.ServerPermissionInfo[]] @()
59+
60+
$result = $emptyCollection | ConvertTo-SqlDscServerPermission
61+
62+
$result | Should -BeNullOrEmpty
63+
}
64+
}
65+
66+
Context 'When converting ServerPermissionInfo from SQL Server' {
67+
It 'Should convert ServerPermissionInfo to ServerPermission objects for sa principal' {
68+
# Get permissions for the sa login
69+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -ErrorAction 'Stop'
70+
71+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo
72+
73+
# Validate the result structure
74+
$result | Should -Not -BeNullOrEmpty
75+
76+
# Each result should have State and Permission properties
77+
foreach ($permission in $result)
78+
{
79+
$permission.State | Should -Not -BeNullOrEmpty
80+
$permission.Permission | Should -Not -BeNullOrEmpty
81+
82+
# Validate that permission state is one of the expected values
83+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
84+
}
85+
}
86+
87+
It 'Should accept ServerPermissionInfo through pipeline' {
88+
# Get permissions for the sa login
89+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'sa' -ErrorAction 'Stop'
90+
91+
$result = $serverPermissionInfo | ConvertTo-SqlDscServerPermission
92+
93+
# Validate the result structure
94+
$result | Should -Not -BeNullOrEmpty
95+
96+
# Each result should have State and Permission properties
97+
foreach ($permission in $result)
98+
{
99+
$permission.State | Should -Not -BeNullOrEmpty
100+
$permission.Permission | Should -Not -BeNullOrEmpty
101+
102+
# Validate that permission state is one of the expected values
103+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
104+
}
105+
}
106+
}
107+
108+
Context 'When converting ServerPermissionInfo for system logins' {
109+
It 'Should handle NT AUTHORITY\SYSTEM permissions correctly' {
110+
# Get permissions for NT AUTHORITY\SYSTEM
111+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'NT AUTHORITY\SYSTEM' -ErrorAction 'SilentlyContinue'
112+
113+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo
114+
115+
# Validate the result structure
116+
$result | Should -Not -BeNullOrEmpty
117+
118+
# Each result should have State and Permission properties
119+
foreach ($permission in $result)
120+
{
121+
$permission.State | Should -Not -BeNullOrEmpty
122+
$permission.Permission | Should -Not -BeNullOrEmpty
123+
124+
# Validate that permission state is one of the expected values
125+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
126+
}
127+
}
128+
}
129+
130+
Context 'When converting ServerPermissionInfo for server roles' {
131+
It 'Should handle public server role permissions correctly' {
132+
# Get permissions for the public server role
133+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'public' -ErrorAction 'Stop'
134+
135+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo
136+
137+
# Validate the result structure
138+
$result | Should -Not -BeNullOrEmpty
139+
140+
# Each result should have State and Permission properties
141+
foreach ($permission in $result)
142+
{
143+
$permission.State | Should -Not -BeNullOrEmpty
144+
$permission.Permission | Should -Not -BeNullOrEmpty
145+
146+
# Validate that permission state is one of the expected values
147+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
148+
}
149+
}
150+
151+
It 'Should handle SqlDscIntegrationTestRole_Persistent server role permissions correctly' {
152+
# Get permissions for the SqlDscIntegrationTestRole_Persistent server role
153+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'SqlDscIntegrationTestRole_Persistent' -ErrorAction 'Stop'
154+
155+
# Only proceed if we have permission data to work with
156+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo
157+
158+
# Validate the result structure
159+
$result | Should -Not -BeNullOrEmpty
160+
161+
# Each result should have State and Permission properties
162+
foreach ($permission in $result)
163+
{
164+
$permission.State | Should -Not -BeNullOrEmpty
165+
$permission.Permission | Should -Not -BeNullOrEmpty
166+
167+
# Validate that permission state is one of the expected values
168+
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
169+
}
170+
171+
# Verify that the CreateEndpoint permission granted by Grant-SqlDscServerPermission test is present
172+
$grantPermission = $result | Where-Object { $_.State -eq 'Grant' }
173+
if ($grantPermission)
174+
{
175+
$grantPermission.Permission | Should -Contain 'CreateEndpoint' -Because 'CreateEndpoint permission should have been granted by Grant-SqlDscServerPermission integration test'
176+
}
177+
}
178+
}
179+
180+
Context 'When testing conversion functionality with multiple permission states' {
181+
It 'Should group permissions by state correctly' {
182+
# Get permissions from a principal that might have various permission states
183+
$serverPermissionInfo = Get-SqlDscServerPermission -ServerObject $script:serverObject -Name 'SqlDscIntegrationTestRole_Persistent' -ErrorAction 'SilentlyContinue'
184+
185+
$result = ConvertTo-SqlDscServerPermission -ServerPermissionInfo $serverPermissionInfo
186+
187+
# Validate that permissions are properly grouped by state
188+
$uniqueStates = $result.State | Sort-Object -Unique
189+
190+
foreach ($state in $uniqueStates)
191+
{
192+
$permissionsForState = $result | Where-Object { $_.State -eq $state }
193+
$permissionsForState | Should -HaveCount 1
194+
$permissionsForState[0].Permission | Should -Not -BeNullOrEmpty
195+
}
196+
}
197+
}
198+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Test-SqlDscIsDatabasePrincipal | 4 | 1 (Install-SqlDscServer), 0 (Prerequisites)
7878
Grant-SqlDscServerPermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Grants CreateEndpoint permission to role
7979
Get-SqlDscServerPermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8080
Set-SqlDscServerPermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
81+
ConvertTo-SqlDscServerPermission | 2 | 2 (Grant-SqlDscServerPermission), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8182
ConvertFrom-SqlDscServerPermission | 4 | 0 (Prerequisites) | - | -
8283
Test-SqlDscServerPermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
8384
Deny-SqlDscServerPermission | 4 | 4 (New-SqlDscLogin), 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Denies AlterTrace permission to login (persistent)

0 commit comments

Comments
 (0)