Skip to content

Commit a694dca

Browse files
committed
Refactor integration tests for SQL DSC agent alerts to use mock credentials and streamline connection setup
1 parent 3582c0d commit a694dca

5 files changed

+29
-53
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ AfterAll {
4343

4444
Describe 'Get-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
47+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
48+
49+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
50+
4651
# Connect to the SQL Server instance
47-
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance
52+
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
4853

4954
# Create test alerts for getting
5055
$script:sqlServerObject | New-SqlDscAgentAlert -Name 'IntegrationTest_GetAlert1' -Severity 16 -ErrorAction Stop

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,13 @@ AfterAll {
4343

4444
Describe 'New-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46-
# Connect to the SQL Server instance
47-
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance
48-
}
46+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
47+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4948

50-
BeforeEach {
51-
# Clean up any test alerts that might exist from previous runs
52-
$testAlerts = @(
53-
'IntegrationTest_SeverityAlert',
54-
'IntegrationTest_MessageIdAlert',
55-
'IntegrationTest_PassThruAlert',
56-
'IntegrationTest_DuplicateAlert',
57-
'IntegrationTest_InvalidAlert'
58-
)
49+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
5950

60-
foreach ($alertName in $testAlerts)
61-
{
62-
$null = $script:sqlServerObject |
63-
Remove-SqlDscAgentAlert -Name $alertName -Force -ErrorAction 'SilentlyContinue'
64-
}
51+
# Connect to the SQL Server instance
52+
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
6553
}
6654

6755
AfterAll {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ AfterAll {
4343

4444
Describe 'Remove-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
47+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
48+
49+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
50+
4651
# Connect to the SQL Server instance
47-
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance
52+
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
4853
}
4954

5055

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

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

4444
Describe 'Set-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46-
# Connect to the SQL Server instance
47-
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance
46+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
47+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
4848

49-
# Clean up any test alerts that might exist from previous runs
50-
$testAlerts = @(
51-
'IntegrationTest_UpdateAlert'
52-
)
49+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
5350

54-
foreach ($alertName in $testAlerts)
55-
{
56-
$existingAlert = $script:sqlServerObject | Get-SqlDscAgentAlert -Name $alertName -ErrorAction 'SilentlyContinue'
57-
if ($existingAlert)
58-
{
59-
$existingAlert | Remove-SqlDscAgentAlert -Force
60-
}
61-
}
51+
# Connect to the SQL Server instance
52+
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
6253

6354
# Create a test alert for updating
6455
$script:sqlServerObject | New-SqlDscAgentAlert -Name 'IntegrationTest_UpdateAlert' -Severity 14 -ErrorAction Stop
6556
}
6657

6758
AfterAll {
68-
# Clean up test alerts
69-
$testAlerts = @(
70-
'IntegrationTest_UpdateAlert'
71-
)
72-
73-
foreach ($alertName in $testAlerts)
74-
{
75-
$existingAlert = $script:sqlServerObject | Get-SqlDscAgentAlert -Name $alertName -ErrorAction 'SilentlyContinue'
76-
if ($existingAlert)
77-
{
78-
$existingAlert | Remove-SqlDscAgentAlert -Force
79-
}
80-
}
59+
$null = $script:sqlServerObject | Remove-SqlDscAgentAlert -Name 'IntegrationTest_UpdateAlert' -Force -ErrorAction 'SilentlyContinue'
8160

8261
# Disconnect from the SQL Server
8362
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject

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

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

4444
Describe 'Test-SqlDscAgentAlert' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
4545
BeforeAll {
46-
# Connect to the SQL Server instance
47-
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:sqlServerInstance
46+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
47+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
48+
49+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
4850

49-
# Clean up any test alerts that might exist from previous runs
50-
$testAlerts = @(
51-
'IntegrationTest_SeverityAlert',
52-
'IntegrationTest_MessageIdAlert'
53-
)
51+
# Connect to the SQL Server instance
52+
$script:sqlServerObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential
5453

5554
# Create test alerts for testing
5655
$script:sqlServerObject | New-SqlDscAgentAlert -Name 'IntegrationTest_SeverityAlert' -Severity 16 -ErrorAction Stop

0 commit comments

Comments
 (0)