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