Skip to content

Commit 10519d9

Browse files
authored
Add integration test for ConvertFrom-SqlDscServerPermission command (#2272)
1 parent 52e707f commit 10519d9

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

CHANGELOG.md

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

88
### Added
99

10+
- Added integration tests for `Remove-SqlDscAudit` command to ensure it functions
11+
correctly in real environments
12+
[issue #2241](https://github.com/dsccommunity/SqlServerDsc/issues/2241).
13+
- Added integration tests for `ConvertFrom-SqlDscServerPermission` command to
14+
ensure it functions correctly in real environments
15+
[issue #2210](https://github.com/dsccommunity/SqlServerDsc/issues/2210).
1016
- `Remove-SqlDscTraceFlag`
1117
- Added missing integration test to ensure command reliability ([issue #2239](https://github.com/dsccommunity/SqlServerDsc/issues/2239)).
1218
- `Remove-SqlDscAudit`

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ stages:
307307
'tests/Integration/Commands/Test-SqlDscIsRole.Integration.Tests.ps1'
308308
'tests/Integration/Commands/Grant-SqlDscServerPermission.Integration.Tests.ps1'
309309
'tests/Integration/Commands/Get-SqlDscServerPermission.Integration.Tests.ps1'
310+
'tests/Integration/Commands/ConvertFrom-SqlDscServerPermission.Integration.Tests.ps1'
310311
'tests/Integration/Commands/Test-SqlDscServerPermission.Integration.Tests.ps1'
311312
'tests/Integration/Commands/Deny-SqlDscServerPermission.Integration.Tests.ps1'
312313
'tests/Integration/Commands/Revoke-SqlDscServerPermission.Integration.Tests.ps1'
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
# NOTE: This integration test focuses on validating the ConvertFrom-SqlDscServerPermission command
33+
# in a realistic environment. Since this is a conversion utility that doesn't directly interact
34+
# with SQL Server, it tests the command's functionality with real ServerPermission objects
35+
# rather than requiring SQL Server connectivity.
36+
Describe 'ConvertFrom-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
37+
Context 'When converting ServerPermission objects in integration environment' {
38+
It 'Should convert single permission correctly' {
39+
# Use the module scope to create ServerPermission object properly
40+
$serverPermission = & (Get-Module -Name $script:moduleName) {
41+
[ServerPermission] @{
42+
State = 'Grant'
43+
Permission = @('ConnectSql')
44+
}
45+
}
46+
47+
$result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop'
48+
49+
$result | Should -Not -BeNullOrEmpty
50+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
51+
$result.ConnectSql | Should -BeTrue
52+
$result.ViewServerState | Should -BeFalse
53+
}
54+
55+
It 'Should convert multiple permissions correctly' {
56+
$serverPermission = & (Get-Module -Name $script:moduleName) {
57+
[ServerPermission] @{
58+
State = 'Grant'
59+
Permission = @('ConnectSql', 'ViewServerState', 'AlterAnyLogin')
60+
}
61+
}
62+
63+
$result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop'
64+
65+
$result | Should -Not -BeNullOrEmpty
66+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
67+
$result.ConnectSql | Should -BeTrue
68+
$result.ViewServerState | Should -BeTrue
69+
$result.AlterAnyLogin | Should -BeTrue
70+
$result.ControlServer | Should -BeFalse
71+
}
72+
73+
It 'Should convert permission using pipeline input' {
74+
$serverPermission = & (Get-Module -Name $script:moduleName) {
75+
[ServerPermission] @{
76+
State = 'Grant'
77+
Permission = @('ViewAnyDatabase', 'CreateAnyDatabase')
78+
}
79+
}
80+
81+
$result = $serverPermission | ConvertFrom-SqlDscServerPermission -ErrorAction 'Stop'
82+
83+
$result | Should -Not -BeNullOrEmpty
84+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
85+
$result.ViewAnyDatabase | Should -BeTrue
86+
$result.CreateAnyDatabase | Should -BeTrue
87+
$result.ConnectSql | Should -BeFalse
88+
}
89+
90+
It 'Should convert permissions correctly regardless of state' {
91+
# Test with GrantWithGrant state
92+
$serverPermission = & (Get-Module -Name $script:moduleName) {
93+
[ServerPermission] @{
94+
State = 'GrantWithGrant'
95+
Permission = @('AlterSettings', 'CreateEndpoint')
96+
}
97+
}
98+
99+
$result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop'
100+
101+
$result | Should -Not -BeNullOrEmpty
102+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
103+
$result.AlterSettings | Should -BeTrue
104+
$result.CreateEndpoint | Should -BeTrue
105+
$result.ConnectSql | Should -BeFalse
106+
}
107+
108+
It 'Should handle empty permission array correctly' {
109+
$serverPermission = & (Get-Module -Name $script:moduleName) {
110+
[ServerPermission] @{
111+
State = 'Grant'
112+
Permission = @()
113+
}
114+
}
115+
116+
$result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop'
117+
118+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
119+
$result.ConnectSql | Should -BeFalse
120+
$result.ViewServerState | Should -BeFalse
121+
$result.ControlServer | Should -BeFalse
122+
}
123+
124+
It 'Should convert multiple ServerPermission objects through pipeline into single combined set' {
125+
$serverPermissions = & (Get-Module -Name $script:moduleName) {
126+
@(
127+
[ServerPermission] @{
128+
State = 'Grant'
129+
Permission = @('ConnectSql')
130+
},
131+
[ServerPermission] @{
132+
State = 'Deny'
133+
Permission = @('ViewServerState', 'AlterTrace')
134+
}
135+
)
136+
}
137+
138+
$result = $serverPermissions | ConvertFrom-SqlDscServerPermission -ErrorAction 'Stop'
139+
140+
# The command combines all permissions into a single ServerPermissionSet
141+
$result | Should -Not -BeNullOrEmpty
142+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
143+
144+
# All permissions from both objects should be set to true
145+
$result.ConnectSql | Should -BeTrue
146+
$result.ViewServerState | Should -BeTrue
147+
$result.AlterTrace | Should -BeTrue
148+
}
149+
150+
It 'Should create compatible ServerPermissionSet for SQL Server permission operations' {
151+
# Test with realistic permissions commonly used in SQL Server administration
152+
$serverPermission = & (Get-Module -Name $script:moduleName) {
153+
[ServerPermission] @{
154+
State = 'Grant'
155+
Permission = @('ConnectSql', 'ViewServerState', 'CreateAnyDatabase', 'AlterAnyLogin')
156+
}
157+
}
158+
159+
$result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop'
160+
161+
# Verify this creates a valid ServerPermissionSet that could be used with SQL Server SMO
162+
$result | Should -Not -BeNullOrEmpty
163+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet]
164+
165+
# Verify all specified permissions are set to true
166+
$result.ConnectSql | Should -BeTrue
167+
$result.ViewServerState | Should -BeTrue
168+
$result.CreateAnyDatabase | Should -BeTrue
169+
$result.AlterAnyLogin | Should -BeTrue
170+
171+
# Verify unspecified permissions remain false
172+
$result.ControlServer | Should -BeFalse
173+
$result.Shutdown | Should -BeFalse
174+
$result.ViewAnyDefinition | Should -BeFalse
175+
}
176+
}
177+
}

tests/Integration/Commands/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Get-SqlDscRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST |
5959
Test-SqlDscIsRole | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6060
Grant-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Grants CreateEndpoint permission to role
6161
Get-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
62+
ConvertFrom-SqlDscServerPermission | 2 | 0 (Prerequisites) | - | -
6263
Test-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -
6364
Deny-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | Denies AlterTrace permission to login (persistent)
6465
Revoke-SqlDscServerPermission | 2 | 1 (Install-SqlDscServer), 0 (Prerequisites) | DSCSQLTEST | -

0 commit comments

Comments
 (0)