Skip to content

Commit fcd7f4c

Browse files
Fix test for Copy-DbaDbMail (#9745)
1 parent 0cb94cf commit fcd7f4c

7 files changed

+128
-86
lines changed

tests/Add-DbaAgDatabase.Tests.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
33
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
45
# $TestConfig has to be set outside of the tests by running: $TestConfig = Get-TestConfig
56
# This will set $TestConfig.Defaults with the parameter defaults, including:
67
# * Confirm = $false
@@ -9,7 +10,7 @@ param(
910
$PSDefaultParameterValues = $TestConfig.Defaults
1011
)
1112

12-
Describe "Add-DbaAgDatabase UnitTests" -Tag "UnitTests" {
13+
Describe $CommandName -Tag "UnitTests" {
1314
Context "Parameter validation" {
1415
BeforeAll {
1516
$command = Get-Command Add-DbaAgDatabase
@@ -43,7 +44,7 @@ Describe "Add-DbaAgDatabase UnitTests" -Tag "UnitTests" {
4344
}
4445
}
4546

46-
Describe "Add-DbaAgDatabase IntegrationTests" -Tag "IntegrationTests" {
47+
Describe $CommandName -Tag "IntegrationTests" {
4748
BeforeAll {
4849
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
4950
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

tests/Add-DbaAgListener.Tests.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
33
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
45
$PSDefaultParameterValues = $TestConfig.Defaults
56
)
67

7-
Describe "Add-DbaAgListener" -Tag "UnitTests" {
8+
Describe $CommandName -Tag "UnitTests" {
89
Context "Parameter validation" {
910
BeforeAll {
1011
$command = Get-Command Add-DbaAgListener
@@ -38,7 +39,7 @@ Describe "Add-DbaAgListener" -Tag "UnitTests" {
3839
}
3940
}
4041

41-
Describe "Add-DbaAgListener" -Tag "IntegrationTests" {
42+
Describe $CommandName -Tag "IntegrationTests" {
4243
BeforeAll {
4344
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
4445

tests/Add-DbaAgReplica.Tests.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
5+
$PSDefaultParameterValues = $TestConfig.Defaults
56
)
67

7-
Describe "Add-DbaAgReplica" -Tag "UnitTests" {
8+
Describe $CommandName -Tag "UnitTests" {
89
Context "Parameter validation" {
910
BeforeAll {
1011
$command = Get-Command Add-DbaAgReplica
@@ -46,7 +47,7 @@ Describe "Add-DbaAgReplica" -Tag "UnitTests" {
4647
}
4748
}
4849

49-
Describe "Add-DbaAgReplica" -Tag "IntegrationTests" {
50+
Describe $CommandName -Tag "IntegrationTests" {
5051
BeforeAll {
5152
$primaryAgName = "dbatoolsci_agroup"
5253
$splatPrimary = @{

tests/Add-DbaComputerCertificate.Tests.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
5+
$PSDefaultParameterValues = $TestConfig.Defaults
56
)
67

7-
Describe "Add-DbaComputerCertificate" -Tag "UnitTests" {
8+
Describe $CommandName -Tag "UnitTests" {
89
Context "Parameter validation" {
910
BeforeAll {
1011
$command = Get-Command Add-DbaComputerCertificate
@@ -35,7 +36,7 @@ Describe "Add-DbaComputerCertificate" -Tag "UnitTests" {
3536
}
3637
}
3738

38-
Describe "Add-DbaComputerCertificate" -Tag "IntegrationTests" {
39+
Describe $CommandName -Tag "IntegrationTests" {
3940
Context "Certificate is added properly" {
4041
BeforeAll {
4142
$certPath = "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt"

tests/Add-DbaDbMirrorMonitor.Tests.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
5+
$PSDefaultParameterValues = $TestConfig.Defaults
56
)
67

7-
Describe "Add-DbaDbMirrorMonitor" -Tag "UnitTests" {
8+
Describe $CommandName -Tag "UnitTests" {
89
Context "Parameter validation" {
910
BeforeAll {
1011
$command = Get-Command Add-DbaDbMirrorMonitor
@@ -29,7 +30,7 @@ Describe "Add-DbaDbMirrorMonitor" -Tag "UnitTests" {
2930
}
3031
}
3132

32-
Describe "Add-DbaDbMirrorMonitor" -Tag "IntegrationTests" {
33+
Describe $CommandName -Tag "IntegrationTests" {
3334
BeforeAll {
3435
$null = Remove-DbaDbMirrorMonitor -SqlInstance $TestConfig.instance2 -WarningAction SilentlyContinue
3536
}

tests/Copy-DbaDbMail.Tests.ps1

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
22
param(
3-
$ModuleName = "dbatools",
4-
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
3+
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
5+
$PSDefaultParameterValues = $TestConfig.Defaults
56
)
67

7-
Describe "Copy-DbaDbMail" -Tag "UnitTests" {
8+
Describe $CommandName -Tag "UnitTests" {
89
Context "Parameter validation" {
910
BeforeAll {
10-
$command = Get-Command Copy-DbaDbMail
11-
$expected = $TestConfig.CommonParameters
12-
$expected += @(
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
1314
"Source",
1415
"Destination",
1516
"Type",
@@ -20,70 +21,55 @@ Describe "Copy-DbaDbMail" -Tag "UnitTests" {
2021
)
2122
}
2223

23-
It "Has parameter: <_>" -ForEach $expected {
24-
$command | Should -HaveParameter $PSItem
25-
}
26-
27-
It "Should have exactly the number of expected parameters ($($expected.Count))" {
28-
$hasparms = $command.Parameters.Values.Name | Where-Object { $_ -notin ('whatif', 'confirm') }
29-
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
24+
It "Should have the expected parameters" {
25+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
3026
}
3127
}
3228
}
3329

34-
Describe "Copy-DbaDbMail" -Tags "IntegrationTests" {
30+
Describe $CommandName -Tags "IntegrationTests" {
3531
BeforeAll {
36-
$servers = Connect-DbaInstance -SqlInstance $TestConfig.instance2, $TestConfig.instance3
37-
foreach ($s in $servers) {
38-
if ( (Get-DbaSpConfigure -SqlInstance $s -Name 'Database Mail XPs').RunningValue -ne 1 ) {
39-
Set-DbaSpConfigure -SqlInstance $s -Name 'Database Mail XPs' -Value 1
40-
}
41-
}
32+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
4233

43-
$accountName = "dbatoolsci_test_$(get-random)"
44-
$account_display_name = 'dbatoolsci mail alerts'
45-
$account_description = 'Mail account for email alerts'
46-
$profilename = "dbatoolsci_test_$(get-random)"
47-
$profile_description = 'Mail profile for email alerts'
34+
# TODO: Maybe remove "-EnableException:$false -WarningAction SilentlyContinue" when we can rely on the setting beeing 0 when entering the test
35+
$null = Set-DbaSpConfigure -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Name 'Database Mail XPs' -Value 1 -EnableException:$false -WarningAction SilentlyContinue
4836

49-
$email_address = '[email protected]'
50-
$mailserver_name = 'smtp.dbatools.io'
51-
$replyto_address = '[email protected]'
52-
$mailaccountpriority = 1
37+
$accountName = "dbatoolsci_test_$(get-random)"
38+
$profileName = "dbatoolsci_test_$(get-random)"
5339

5440
$splat1 = @{
5541
SqlInstance = $TestConfig.instance2
5642
Name = $accountName
57-
Description = $account_description
58-
EmailAddress = $email_address
59-
DisplayName = $account_display_name
60-
ReplyToAddress = $replyto_address
61-
MailServer = $mailserver_name
43+
Description = 'Mail account for email alerts'
44+
EmailAddress = '[email protected]'
45+
DisplayName = 'dbatoolsci mail alerts'
46+
ReplyToAddress = '[email protected]'
47+
MailServer = 'smtp.dbatools.io'
6248
}
6349
$null = New-DbaDbMailAccount @splat1 -Force
6450

6551
$splat2 = @{
6652
SqlInstance = $TestConfig.instance2
67-
Name = $profilename
68-
Description = $profile_description
53+
Name = $profileName
54+
Description = 'Mail profile for email alerts'
6955
MailAccountName = $accountName
70-
MailAccountPriority = $mailaccountpriority
56+
MailAccountPriority = 1
7157
}
7258
$null = New-DbaDbMailProfile @splat2
59+
60+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
7361
}
7462

7563
AfterAll {
76-
$servers = Connect-DbaInstance -SqlInstance $TestConfig.instance2, $TestConfig.instance3
64+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
7765

78-
foreach ($s in $servers) {
79-
$mailAccountSettings = "EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$accountname';"
80-
Invoke-DbaQuery -SqlInstance $s -Query $mailAccountSettings -Database msdb
81-
$mailProfileSettings = "EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = '$profilename';"
82-
Invoke-DbaQuery -SqlInstance $s -Query $mailProfileSettings -Database msdb
83-
}
66+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Query "EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$accountName';"
67+
Invoke-DbaQuery -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Query "EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = '$profileName';"
68+
69+
$null = Set-DbaSpConfigure -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Name 'Database Mail XPs' -Value 0
8470
}
8571

86-
Context "When copying DbMail to $($TestConfig.instance3)" {
72+
Context "When copying DbMail" {
8773
BeforeAll {
8874
$results = Copy-DbaDbMail -Source $TestConfig.instance2 -Destination $TestConfig.instance3
8975
}
@@ -92,9 +78,32 @@ Describe "Copy-DbaDbMail" -Tags "IntegrationTests" {
9278
$results | Should -Not -BeNullOrEmpty
9379
}
9480

95-
It "Should have copied <_.type> from source to destination" -ForEach ($results | Where-Object type -in @('Mail Configuration', 'Mail Account', 'Mail Profile')) {
96-
$PSItem.SourceServer | Should -Be $TestConfig.instance2
97-
$PSItem.DestinationServer | Should -Be $TestConfig.instance3
81+
It "Should have copied Mail Configuration from source to destination" {
82+
$result = $results | Where-Object Type -eq 'Mail Configuration'
83+
$result.SourceServer | Should -Be $TestConfig.instance2
84+
$result.DestinationServer | Should -Be $TestConfig.instance3
85+
$result.Status | Should -Be 'Successful'
86+
}
87+
88+
It "Should have copied Mail Account from source to destination" {
89+
$result = $results | Where-Object Type -eq 'Mail Account'
90+
$result.SourceServer | Should -Be $TestConfig.instance2
91+
$result.DestinationServer | Should -Be $TestConfig.instance3
92+
$result.Status | Should -Be 'Successful'
93+
}
94+
95+
It "Should have copied Mail Profile from source to destination" {
96+
$result = $results | Where-Object Type -eq 'Mail Profile'
97+
$result.SourceServer | Should -Be $TestConfig.instance2
98+
$result.DestinationServer | Should -Be $TestConfig.instance3
99+
$result.Status | Should -Be 'Successful'
100+
}
101+
102+
It "Should have copied Mail Server from source to destination" {
103+
$result = $results | Where-Object Type -eq 'Mail Server'
104+
$result.SourceServer | Should -Be $TestConfig.instance2
105+
$result.DestinationServer | Should -Be $TestConfig.instance3
106+
$result.Status | Should -Be 'Successful'
98107
}
99108
}
100109

@@ -107,10 +116,26 @@ Describe "Copy-DbaDbMail" -Tags "IntegrationTests" {
107116
$results | Should -Not -BeNullOrEmpty
108117
}
109118

110-
It "Should have skipped <_.type> copy operations" -ForEach $results {
111-
$PSItem.SourceServer | Should -Be $TestConfig.instance2
112-
$PSItem.DestinationServer | Should -Be $TestConfig.instance3
113-
$PSItem.Status | Should -Be 'Skipped'
119+
It "Should have not reported on Mail Configuration" {
120+
$result = $results | Where-Object Type -eq 'Mail Configuration'
121+
$result | Should -BeNullOrEmpty
122+
}
123+
124+
It "Should have not reported on Mail Account" {
125+
$result = $results | Where-Object Type -eq 'Mail Account'
126+
$result | Should -BeNullOrEmpty
127+
}
128+
129+
It "Should have not reported on Mail Profile" {
130+
$result = $results | Where-Object Type -eq 'Mail Profile'
131+
$result | Should -BeNullOrEmpty
132+
}
133+
134+
It "Should have skipped Mail Server" {
135+
$result = $results | Where-Object Type -eq 'Mail Server'
136+
$result.SourceServer | Should -Be $TestConfig.instance2
137+
$result.DestinationServer | Should -Be $TestConfig.instance3
138+
$result.Status | Should -Be 'Skipped'
114139
}
115140
}
116141
}

tests/Get-DbaConnection.Tests.ps1

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = [System.IO.Path]::GetFileName($PSCommandPath.Replace('.Tests.ps1', '')),
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
47

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
8+
Describe $CommandName -Tag "UnitTests" {
9+
Context "Parameter validation" {
10+
BeforeAll {
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $_ -notin ('WhatIf', 'Confirm') }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
14+
'SqlInstance',
15+
'SqlCredential',
16+
'EnableException'
17+
)
18+
}
19+
It "Should have the expected parameters" {
20+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1221
}
1322
}
1423
}
1524

1625
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
1726
Context "returns the proper transport" {
18-
$results = Get-DbaConnection -SqlInstance $TestConfig.instance1
19-
foreach ($result in $results) {
20-
It "returns an scheme" {
27+
BeforeAll {
28+
$results = Get-DbaConnection -SqlInstance $TestConfig.instance1
29+
}
30+
31+
It "returns a valid AuthScheme" {
32+
foreach ($result in $results) {
2133
$result.AuthScheme | Should -BeIn 'NTLM', 'Kerberos', 'SQL'
2234
}
2335
}

0 commit comments

Comments
 (0)