Skip to content

Commit 8c1efad

Browse files
committed
Enhance integration tests for SQL DSC agent alerts by starting and stopping the SQL Server service to ensure a clean test environment and save resources.
1 parent ad21960 commit 8c1efad

6 files changed

+37
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ applyTo: "**"
2525

2626
## Test Requirements
2727
- Unit tests: Add `$env:SqlServerDscCI = $true` in `BeforeAll`, remove in `AfterAll`
28-
- Integration tests: Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
29-
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md
30-
- Integration test script files must be added to a group
31-
within the test stage in ./azure-pipelines.yml.
32-
- Choose the appropriate group number based on the required dependencies
28+
- Integration tests:
29+
- If requiring SQL Server DB, start the Windows service in `BeforeAll`, stop it in `AfterAll`.
30+
- Use `Connect-SqlDscDatabaseEngine` for SQL Server DB session, and always with correct CI credentials
31+
- Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
32+
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md
33+
- Integration test script files must be added to a group within the test stage in ./azure-pipelines.yml.
34+
- Choose the appropriate group number based on the required dependencies
3335

3436
## Unit tests
3537
- When unit test uses SMO types, ensure they are properly stubbed in SMO.cs

tests/Integration/Commands/Get-SqlDscAgentAlert.Integration.Tests.ps1

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

4444
Describe 'Get-SqlDscAgentAlert' -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+
4649
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
4750
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4851

@@ -62,6 +65,9 @@ Describe 'Get-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019
6265

6366
# Disconnect from the SQL Server
6467
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
68+
69+
# Stop the named instance SQL Server service to save memory on the build worker.
70+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
6571
}
6672

6773
It 'Should get all alerts' {

tests/Integration/Commands/New-SqlDscAgentAlert.Integration.Tests.ps1

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

4444
Describe 'New-SqlDscAgentAlert' -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+
4649
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
4750
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4851

@@ -70,6 +73,9 @@ Describe 'New-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019
7073

7174
# Disconnect from the SQL Server
7275
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
76+
77+
# Stop the named instance SQL Server service to save memory on the build worker.
78+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
7379
}
7480

7581
It 'Should create alert with severity' {

tests/Integration/Commands/Remove-SqlDscAgentAlert.Integration.Tests.ps1

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

4444
Describe 'Remove-SqlDscAgentAlert' -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+
4649
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
4750
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4851

@@ -56,6 +59,9 @@ Describe 'Remove-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2
5659
AfterAll {
5760
# Disconnect from the SQL Server
5861
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
62+
63+
# Stop the named instance SQL Server service to save memory on the build worker.
64+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
5965
}
6066

6167
Context 'When removing by ServerObject' {

tests/Integration/Commands/Set-SqlDscAgentAlert.Integration.Tests.ps1

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

4444
Describe 'Set-SqlDscAgentAlert' -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+
4649
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
4750
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4851

@@ -60,6 +63,9 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019
6063

6164
# Disconnect from the SQL Server
6265
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'
6369
}
6470

6571
It 'Should update alert severity using ServerObject parameter set' {

tests/Integration/Commands/Test-SqlDscAgentAlert.Integration.Tests.ps1

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

4444
Describe 'Test-SqlDscAgentAlert' -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+
4649
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
4750
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4851

@@ -63,6 +66,9 @@ Describe 'Test-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL201
6366

6467
# Disconnect from the SQL Server
6568
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject
69+
70+
# Stop the named instance SQL Server service to save memory on the build worker.
71+
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
6672
}
6773

6874
Context 'When checking existence only' {

0 commit comments

Comments
 (0)