-
Notifications
You must be signed in to change notification settings - Fork 227
Add integration test for ConvertFrom-SqlDscServerPermission command #2272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e47deef
Initial plan
Copilot 2a8c14a
Add integration test for ConvertFrom-SqlDscServerPermission command
Copilot e529da4
Merge branch 'main' into copilot/fix-2210
johlju 2a9cbf8
Fix integration test formatting and documentation issues
Copilot 16feb22
Merge branch 'main' into copilot/fix-2210
johlju File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
tests/Integration/Commands/ConvertFrom-SqlDscServerPermission.Integration.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| [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 noop" first.' | ||
| } | ||
| } | ||
|
|
||
| BeforeAll { | ||
| $script:moduleName = 'SqlServerDsc' | ||
|
|
||
| Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' | ||
| } | ||
|
|
||
| # NOTE: This integration test focuses on validating the ConvertFrom-SqlDscServerPermission command | ||
| # in a realistic environment. Since this is a conversion utility that doesn't directly interact | ||
| # with SQL Server, it tests the command's functionality with real ServerPermission objects | ||
| # rather than requiring SQL Server connectivity. | ||
| Describe 'ConvertFrom-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') { | ||
| Context 'When converting ServerPermission objects in integration environment' { | ||
| It 'Should convert single permission correctly' { | ||
| # Use the module scope to create ServerPermission object properly | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('ConnectSql') | ||
| } | ||
| } | ||
|
|
||
| $result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop' | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
| $result.ConnectSql | Should -BeTrue | ||
| $result.ViewServerState | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should convert multiple permissions correctly' { | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('ConnectSql', 'ViewServerState', 'AlterAnyLogin') | ||
| } | ||
| } | ||
|
|
||
| $result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop' | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
| $result.ConnectSql | Should -BeTrue | ||
| $result.ViewServerState | Should -BeTrue | ||
| $result.AlterAnyLogin | Should -BeTrue | ||
| $result.ControlServer | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should convert permission using pipeline input' { | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('ViewAnyDatabase', 'CreateAnyDatabase') | ||
| } | ||
| } | ||
|
|
||
| $result = $serverPermission | ConvertFrom-SqlDscServerPermission -ErrorAction 'Stop' | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
| $result.ViewAnyDatabase | Should -BeTrue | ||
| $result.CreateAnyDatabase | Should -BeTrue | ||
| $result.ConnectSql | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should convert permissions correctly regardless of state' { | ||
| # Test with GrantWithGrant state | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'GrantWithGrant' | ||
| Permission = @('AlterSettings', 'CreateEndpoint') | ||
| } | ||
| } | ||
|
|
||
| $result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop' | ||
|
|
||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
| $result.AlterSettings | Should -BeTrue | ||
| $result.CreateEndpoint | Should -BeTrue | ||
| $result.ConnectSql | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should handle empty permission array correctly' { | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @() | ||
| } | ||
| } | ||
|
|
||
| $result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop' | ||
|
|
||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
| $result.ConnectSql | Should -BeFalse | ||
| $result.ViewServerState | Should -BeFalse | ||
| $result.ControlServer | Should -BeFalse | ||
| } | ||
|
|
||
| It 'Should convert multiple ServerPermission objects through pipeline into single combined set' { | ||
| $serverPermissions = & (Get-Module -Name $script:moduleName) { | ||
| @( | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('ConnectSql') | ||
| }, | ||
| [ServerPermission] @{ | ||
| State = 'Deny' | ||
| Permission = @('ViewServerState', 'AlterTrace') | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| $result = $serverPermissions | ConvertFrom-SqlDscServerPermission -ErrorAction 'Stop' | ||
|
|
||
| # The command combines all permissions into a single ServerPermissionSet | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
|
|
||
| # All permissions from both objects should be set to true | ||
| $result.ConnectSql | Should -BeTrue | ||
| $result.ViewServerState | Should -BeTrue | ||
| $result.AlterTrace | Should -BeTrue | ||
| } | ||
|
|
||
| It 'Should create compatible ServerPermissionSet for SQL Server permission operations' { | ||
| # Test with realistic permissions commonly used in SQL Server administration | ||
| $serverPermission = & (Get-Module -Name $script:moduleName) { | ||
| [ServerPermission] @{ | ||
| State = 'Grant' | ||
| Permission = @('ConnectSql', 'ViewServerState', 'CreateAnyDatabase', 'AlterAnyLogin') | ||
| } | ||
| } | ||
|
|
||
| $result = ConvertFrom-SqlDscServerPermission -Permission $serverPermission -ErrorAction 'Stop' | ||
|
|
||
| # Verify this creates a valid ServerPermissionSet that could be used with SQL Server SMO | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.ServerPermissionSet] | ||
|
|
||
| # Verify all specified permissions are set to true | ||
| $result.ConnectSql | Should -BeTrue | ||
| $result.ViewServerState | Should -BeTrue | ||
| $result.CreateAnyDatabase | Should -BeTrue | ||
| $result.AlterAnyLogin | Should -BeTrue | ||
|
|
||
| # Verify unspecified permissions remain false | ||
| $result.ControlServer | Should -BeFalse | ||
| $result.Shutdown | Should -BeFalse | ||
| $result.ViewAnyDefinition | Should -BeFalse | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.