Skip to content

Commit 90a7c9d

Browse files
authored
Refactor integration tests to optimize SQL Server service management (#2291)
1 parent 65a1f9a commit 90a7c9d

File tree

59 files changed

+39
-404
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+39
-404
lines changed

.github/instructions/SqlServerDsc-guidelines.instructions.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ applyTo: "**"
3737
## Tests Requirements
3838
- Unit tests: Add `$env:SqlServerDscCI = $true` in `BeforeAll`, remove in `AfterAll`
3939
- Integration tests:
40-
- If requiring SQL Server DB, start the Windows service in `BeforeAll`, stop it in `AfterAll`.
4140
- Use `Connect-SqlDscDatabaseEngine` for SQL Server DB session, and always with correct CI credentials
4241
- Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
4342
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md

CHANGELOG.md

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

227227
### Changed
228228

229+
- Optimized integration test performance by leaving the DSCSQLTEST service running
230+
for subsequent tests, significantly improving CI build times.
229231
- Improved code quality by ensuring all function invocations in the private
230232
and public functions use named parameters instead of positional parameters.
231233
- SqlServerDsc

tests/Integration/Commands/Assert-SqlDscAgentOperator.Integration.Tests.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ AfterAll {
4343

4444
Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46-
# Starting the named instance SQL Server service prior to running tests.
47-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
48-
4946
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
5047
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
5148

@@ -63,9 +60,6 @@ Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_S
6360

6461
# Disconnect from the SQL Server
6562
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
66-
67-
# Stop the named instance SQL Server service to save memory on the build worker.
68-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
6963
}
7064

7165
Context 'When operator exists' {

tests/Integration/Commands/Assert-SqlDscLogin.Integration.Tests.ps1

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ BeforeAll {
3131

3232
Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:instanceName = 'DSCSQLTEST'
3835
$script:computerName = Get-ComputerName
3936
}
4037

41-
AfterAll {
42-
# Stop the named instance SQL Server service to save memory on the build worker.
43-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
44-
}
45-
4638
Context 'When connecting to SQL Server instance' {
4739
BeforeAll {
4840
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.

tests/Integration/Commands/Connect-SqlDscDatabaseEngine.Integration.Tests.ps1

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integrati
8484
}
8585

8686
Context 'When connecting to a named instance' {
87-
BeforeAll {
88-
# Starting the named instance SQL Server service prior to running tests.
89-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
90-
}
91-
92-
AfterAll {
93-
# Stop the named instance SQL Server service to save memory on the build worker.
94-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
95-
}
96-
9787
It 'Should have the named instance SQL Server service started' {
9888
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'
9989

tests/Integration/Commands/ConvertTo-SqlDscDatabasePermission.Integration.Tests.ps1

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ BeforeAll {
3131

3232
Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:mockInstanceName = 'DSCSQLTEST'
3835

3936
$mockSqlAdministratorUserName = 'SqlAdmin'
@@ -46,23 +43,19 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
4643

4744
AfterAll {
4845
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
49-
50-
# Stop the named instance SQL Server service to save memory on the build worker.
51-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
5246
}
53-
5447
Context 'When converting empty collection of DatabasePermissionInfo' {
5548
It 'Should return empty array for empty DatabasePermissionInfo collection' {
5649
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()
57-
50+
5851
$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $emptyCollection
5952

6053
$result | Should -BeNullOrEmpty
6154
}
6255

6356
It 'Should accept empty collection through pipeline' {
6457
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()
65-
58+
6659
$result = $emptyCollection | ConvertTo-SqlDscDatabasePermission
6760

6861
$result | Should -BeNullOrEmpty
@@ -80,12 +73,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
8073

8174
# Validate the result structure
8275
$result | Should -Not -BeNullOrEmpty
83-
76+
8477
# Each result should have State and Permission properties
8578
foreach ($permission in $result) {
8679
$permission.State | Should -Not -BeNullOrEmpty
8780
$permission.Permission | Should -Not -BeNullOrEmpty
88-
81+
8982
# Validate that permission state is one of the expected values
9083
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
9184
}
@@ -102,12 +95,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
10295

10396
# Validate the result structure
10497
$result | Should -Not -BeNullOrEmpty
105-
98+
10699
# Each result should have State and Permission properties
107100
foreach ($permission in $result) {
108101
$permission.State | Should -Not -BeNullOrEmpty
109102
$permission.Permission | Should -Not -BeNullOrEmpty
110-
103+
111104
# Validate that permission state is one of the expected values
112105
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
113106
}
@@ -126,12 +119,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
126119

127120
# Validate the result structure
128121
$result | Should -Not -BeNullOrEmpty
129-
122+
130123
# Each result should have State and Permission properties
131124
foreach ($permission in $result) {
132125
$permission.State | Should -Not -BeNullOrEmpty
133126
$permission.Permission | Should -Not -BeNullOrEmpty
134-
127+
135128
# Validate that permission state is one of the expected values
136129
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
137130
}
@@ -151,7 +144,7 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
151144
if ($result) {
152145
# Validate that permissions are properly grouped by state
153146
$uniqueStates = $result.State | Sort-Object -Unique
154-
147+
155148
foreach ($state in $uniqueStates) {
156149
$permissionsForState = $result | Where-Object { $_.State -eq $state }
157150
$permissionsForState | Should -HaveCount 1

tests/Integration/Commands/Deny-SqlDscServerPermission.Integration.Tests.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ BeforeAll {
3131

3232
Describe 'Deny-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:mockInstanceName = 'DSCSQLTEST'
3835

3936
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
@@ -50,9 +47,6 @@ Describe 'Deny-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integratio
5047

5148
AfterAll {
5249
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
53-
54-
# Stop the named instance SQL Server service to save memory on the build worker.
55-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
5650
}
5751

5852
Context 'When denying server permissions to login' {

tests/Integration/Commands/Disable-SqlDscAgentOperator.Integration.Tests.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ AfterAll {
4343

4444
Describe 'Disable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46-
# Starting the named instance SQL Server service prior to running tests.
47-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
48-
4946
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
5047
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
5148
$mockSqlAdministratorCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
@@ -61,9 +58,6 @@ Describe 'Disable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_
6158
Set-SqlDscConfigurationOption -ServerObject $script:serverObject -Name 'Agent XPs' -Value 0 -Force -Verbose -ErrorAction 'Stop'
6259

6360
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
64-
65-
# Stopping the named instance SQL Server service after running tests.
66-
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
6761
}
6862

6963
Context 'When disabling an existing agent operator' {

tests/Integration/Commands/Disable-SqlDscAudit.Integration.Tests.ps1

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ BeforeAll {
3131

3232
Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:mockInstanceName = 'DSCSQLTEST'
3835
$script:mockComputerName = Get-ComputerName
3936

@@ -47,9 +44,6 @@ Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL201
4744

4845
AfterAll {
4946
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'
5347
}
5448

5549
Context 'When disabling an audit using ServerObject parameter set' {
@@ -206,7 +200,7 @@ Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL201
206200
'SqlDscTestMultiDisable1_' + (Get-Random),
207201
'SqlDscTestMultiDisable2_' + (Get-Random)
208202
)
209-
203+
210204
foreach ($auditName in $script:testAuditNames)
211205
{
212206
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop

tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ BeforeAll {
3131

3232
Describe 'Disable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
3333
BeforeAll {
34-
# Starting the named instance SQL Server service prior to running tests.
35-
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
36-
3734
$script:mockInstanceName = 'DSCSQLTEST'
3835

3936
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
@@ -49,9 +46,6 @@ Describe 'Disable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL201
4946

5047
AfterAll {
5148
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'
5549
}
5650

5751
Context 'When disabling a login using ServerObject parameter set' {

0 commit comments

Comments
 (0)