Skip to content

Commit bd29e3c

Browse files
Copilotjohlju
andcommitted
Fix newlines and add integration tests for role management commands
Co-authored-by: johlju <7189721+johlju@users.noreply.github.com>
1 parent 35a6468 commit bd29e3c

10 files changed

Lines changed: 428 additions & 6 deletions

azure-pipelines.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ stages:
283283
# Group 2
284284
'tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1'
285285
'tests/Integration/Commands/Get-SqlDscLogin.Integration.Tests.ps1'
286+
'tests/Integration/Commands/Get-SqlDscRole.Integration.Tests.ps1'
287+
'tests/Integration/Commands/New-SqlDscRole.Integration.Tests.ps1'
288+
'tests/Integration/Commands/Remove-SqlDscRole.Integration.Tests.ps1'
286289
# Group 9
287290
'tests/Integration/Commands/Uninstall-SqlDscServer.Integration.Tests.ps1'
288291
)

source/Public/Get-SqlDscRole.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ function Get-SqlDscRole
8989
return , $ServerObject.Roles
9090
}
9191
}
92-
}
92+
}

source/Public/New-SqlDscRole.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ function New-SqlDscRole
117117
}
118118
}
119119
}
120-
}
120+
}

source/Public/Remove-SqlDscRole.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,4 @@ function Remove-SqlDscRole
130130
}
131131
}
132132
}
133-
}
133+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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:dscModuleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:dscModuleName
30+
}
31+
32+
Describe 'Get-SqlDscRole' -Tag @('Integration_SQL2016', '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+
$script:mockComputerName = Get-ComputerName
39+
40+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
42+
43+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
44+
45+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
46+
}
47+
48+
AfterAll {
49+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
50+
51+
# Stop the named instance SQL Server service to save memory on the build worker.
52+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
53+
}
54+
55+
Context 'When getting all SQL Server roles' {
56+
It 'Should return an array of ServerRole objects' {
57+
$result = Get-SqlDscRole -ServerObject $script:serverObject
58+
59+
<#
60+
Casting to array to ensure we get the count on Windows PowerShell
61+
when there is only one role.
62+
#>
63+
@($result).Count | Should -BeGreaterOrEqual 1
64+
@($result)[0] | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
65+
}
66+
67+
It 'Should return system roles including sysadmin' {
68+
$result = Get-SqlDscRole -ServerObject $script:serverObject
69+
70+
$result.Name | Should -Contain 'sysadmin'
71+
}
72+
}
73+
74+
Context 'When getting a specific SQL Server role' {
75+
It 'Should return the specified role when it exists' {
76+
$result = Get-SqlDscRole -ServerObject $script:serverObject -Name 'sysadmin'
77+
78+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
79+
$result.Name | Should -Be 'sysadmin'
80+
$result.IsFixedRole | Should -BeTrue
81+
}
82+
83+
It 'Should throw an error when the role does not exist' {
84+
{ Get-SqlDscRole -ServerObject $script:serverObject -Name 'NonExistentRole' -ErrorAction 'Stop' } |
85+
Should -Throw -ExpectedMessage 'There is no role with the name ''NonExistentRole''.'
86+
}
87+
88+
It 'Should return null when the role does not exist and error action is SilentlyContinue' {
89+
$result = Get-SqlDscRole -ServerObject $script:serverObject -Name 'NonExistentRole' -ErrorAction 'SilentlyContinue'
90+
91+
$result | Should -BeNullOrEmpty
92+
}
93+
}
94+
95+
Context 'When using the Refresh parameter' {
96+
It 'Should return the same results with and without Refresh' {
97+
$resultWithoutRefresh = Get-SqlDscRole -ServerObject $script:serverObject
98+
$resultWithRefresh = Get-SqlDscRole -ServerObject $script:serverObject -Refresh
99+
100+
@($resultWithoutRefresh).Count | Should -Be @($resultWithRefresh).Count
101+
}
102+
}
103+
104+
Context 'When using pipeline input' {
105+
It 'Should accept ServerObject from pipeline' {
106+
$result = $script:serverObject | Get-SqlDscRole -Name 'sysadmin'
107+
108+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
109+
$result.Name | Should -Be 'sysadmin'
110+
}
111+
}
112+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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:dscModuleName = 'SqlServerDsc'
28+
29+
Import-Module -Name $script:dscModuleName
30+
}
31+
32+
Describe 'New-SqlDscRole' -Tag @('Integration_SQL2016', '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+
$script:mockComputerName = Get-ComputerName
39+
40+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
42+
43+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
44+
45+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
46+
47+
# Test role names that will be created and cleaned up
48+
$script:testRoleName = 'TestRole_' + (Get-Random)
49+
$script:testRoleNameWithOwner = 'TestRoleOwner_' + (Get-Random)
50+
}
51+
52+
AfterAll {
53+
# Clean up any test roles that might have been created
54+
try
55+
{
56+
$existingRole = Get-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleName -ErrorAction 'SilentlyContinue'
57+
if ($existingRole)
58+
{
59+
Remove-SqlDscRole -RoleObject $existingRole -Force
60+
}
61+
62+
$existingRoleWithOwner = Get-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleNameWithOwner -ErrorAction 'SilentlyContinue'
63+
if ($existingRoleWithOwner)
64+
{
65+
Remove-SqlDscRole -RoleObject $existingRoleWithOwner -Force
66+
}
67+
}
68+
catch
69+
{
70+
# Ignore cleanup errors
71+
}
72+
73+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
74+
75+
# Stop the named instance SQL Server service to save memory on the build worker.
76+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
77+
}
78+
79+
Context 'When creating a new SQL Server role' {
80+
It 'Should create a role and return a ServerRole object' {
81+
$result = New-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleName -Force
82+
83+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
84+
$result.Name | Should -Be $script:testRoleName
85+
$result.IsFixedRole | Should -BeFalse
86+
87+
# Verify the role exists in the server
88+
$verifyRole = Get-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleName
89+
$verifyRole | Should -Not -BeNullOrEmpty
90+
$verifyRole.Name | Should -Be $script:testRoleName
91+
}
92+
93+
It 'Should create a role with a specified owner' {
94+
$result = New-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleNameWithOwner -Owner 'sa' -Force
95+
96+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
97+
$result.Name | Should -Be $script:testRoleNameWithOwner
98+
$result.Owner | Should -Be 'sa'
99+
$result.IsFixedRole | Should -BeFalse
100+
}
101+
102+
It 'Should throw an error when creating a role that already exists' {
103+
# First, create the role
104+
New-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleName -Force
105+
106+
# Then try to create it again, should fail
107+
{ New-SqlDscRole -ServerObject $script:serverObject -Name $script:testRoleName -Force -ErrorAction 'Stop' } |
108+
Should -Throw
109+
}
110+
}
111+
112+
Context 'When using pipeline input' {
113+
It 'Should accept ServerObject from pipeline' {
114+
$uniqueRoleName = 'PipelineTestRole_' + (Get-Random)
115+
116+
try
117+
{
118+
$result = $script:serverObject | New-SqlDscRole -Name $uniqueRoleName -Force
119+
120+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ServerRole'
121+
$result.Name | Should -Be $uniqueRoleName
122+
}
123+
finally
124+
{
125+
# Clean up
126+
try
127+
{
128+
$roleToCleanup = Get-SqlDscRole -ServerObject $script:serverObject -Name $uniqueRoleName -ErrorAction 'SilentlyContinue'
129+
if ($roleToCleanup)
130+
{
131+
Remove-SqlDscRole -RoleObject $roleToCleanup -Force
132+
}
133+
}
134+
catch
135+
{
136+
# Ignore cleanup errors
137+
}
138+
}
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)