1
1
# Requires -Module @ { ModuleName = " Pester" ; ModuleVersion = " 5.0" }
2
2
param (
3
- $ModuleName = " dbatools" ,
4
- $PSDefaultParameterValues = ($TestConfig = Get-TestConfig ).Defaults
3
+ $ModuleName = " dbatools" ,
4
+ # $TestConfig has to be set outside of the tests by running: $TestConfig = Get-TestConfig
5
+ # This will set $TestConfig.Defaults with the parameter defaults, including:
6
+ # * Confirm = $false
7
+ # * WarningVariable = 'WarnVar'
8
+ # So you don't have to use -Confirm:$false and you can always use $WarnVar to test for warnings.
9
+ $PSDefaultParameterValues = $TestConfig.Defaults
5
10
)
6
11
7
- Describe " Add-DbaAgDatabase" - Tag " UnitTests" {
12
+ Describe " Add-DbaAgDatabase UnitTests " - Tag " UnitTests" {
8
13
Context " Parameter validation" {
9
14
BeforeAll {
10
15
$command = Get-Command Add-DbaAgDatabase
@@ -38,49 +43,122 @@ Describe "Add-DbaAgDatabase" -Tag "UnitTests" {
38
43
}
39
44
}
40
45
41
- Describe " Add-DbaAgDatabase" - Tag " IntegrationTests" {
46
+ Describe " Add-DbaAgDatabase IntegrationTests " - Tag " IntegrationTests" {
42
47
BeforeAll {
43
- $null = Get-DbaProcess - SqlInstance $TestConfig.instance3 - Program ' dbatools PowerShell module - dbatools.io' | Stop-DbaProcess - WarningAction SilentlyContinue
44
- $server = Connect-DbaInstance - SqlInstance $TestConfig.instance3
45
- $agname = " dbatoolsci_addagdb_agroup"
46
- $dbname = " dbatoolsci_addagdb_agroupdb"
47
- $newdbname = " dbatoolsci_addag_agroupdb_2"
48
- $server.Query (" create database $dbname " )
49
- $backup = Get-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $dbname | Backup-DbaDatabase
50
- $splatNewAg = @ {
51
- Primary = $TestConfig.instance3
52
- Name = $agname
53
- ClusterType = " None"
48
+ # We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
49
+ $PSDefaultParameterValues [' *-Dba*:EnableException' ] = $true
50
+
51
+ # Collect all the created files to be able to remove them in the AfterAll block.
52
+ $filesToRemove = @ ( )
53
+
54
+ # Explain what needs to be set up for the test:
55
+ # To add a database to an availablity group, we need an availability group and a database that has been backed up.
56
+ # For negative tests, we need a database without a backup and a non existing database.
57
+
58
+ # Set variables. They are available in all the It blocks.
59
+ $agName = " addagdb_group"
60
+ $existingDbWithBackup = " dbWithBackup"
61
+ $existingDbWithoutBackup = " dbWithoutBackup"
62
+ $nonexistingDb = " dbdoesnotexist"
63
+
64
+ # Create the objects.
65
+ $splat = @ {
66
+ Primary = $TestConfig.instance3
67
+ Name = $agName
68
+ ClusterType = " None"
54
69
FailoverMode = " Manual"
55
- Database = $dbname
56
- Confirm = $false
57
- Certificate = " dbatoolsci_AGCert"
70
+ Certificate = " dbatoolsci_AGCert"
58
71
}
59
- $ag = New-DbaAvailabilityGroup @splatNewAg
72
+ $null = New-DbaAvailabilityGroup @splat
73
+
74
+ $null = New-DbaDatabase - SqlInstance $TestConfig.instance3 - Name $existingDbWithBackup
75
+ $backup = Backup-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $existingDbWithBackup - Path $TestConfig.Temp
76
+ $filesToRemove += $backup.Path
77
+
78
+ $null = New-DbaDatabase - SqlInstance $TestConfig.instance3 - Name $existingDbWithoutBackup
79
+
80
+ # We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
81
+ $PSDefaultParameterValues.Remove (' *-Dba*:EnableException' )
60
82
}
61
83
62
84
AfterAll {
63
- $null = Remove-DbaAvailabilityGroup - SqlInstance $server - AvailabilityGroup $agname - Confirm:$false
64
- $null = Remove-DbaDatabase - SqlInstance $server - Database $dbname , $newdbname - Confirm:$false
85
+ # We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
86
+ $PSDefaultParameterValues [' *-Dba*:EnableException' ] = $true
87
+
88
+ # Cleanup all created object.
89
+ $null = Remove-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName
90
+ $null = Get-DbaEndpoint - SqlInstance $TestConfig.instance3 - Type DatabaseMirroring | Remove-DbaEndpoint
91
+ $null = Remove-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $existingDbWithBackup , $existingDbWithoutBackup
92
+
93
+ # Remove all created files.
94
+ Remove-Item - Path $filesToRemove
95
+
96
+ # As this is the last block we do not need to reset the $PSDefaultParameterValues.
65
97
}
66
98
67
99
Context " When adding AG database" {
100
+ # We use the BeforeAll to run the test itself.
101
+ # Results are saved in $results.
102
+ BeforeAll {
103
+ $splat = @ {
104
+ SqlInstance = $TestConfig.instance3
105
+ AvailabilityGroup = $agName
106
+ Database = $existingDbWithBackup
107
+ }
108
+ $results = Add-DbaAgDatabase @splat
109
+ }
110
+
111
+ # Always include this test to be sure that the command runs without warnings.
112
+ It " Does not warn" {
113
+ $WarnVar | Should - BeNullOrEmpty
114
+ }
115
+
116
+ It " Returns proper results" {
117
+ $results.AvailabilityGroup | Should - Be $agName
118
+ $results.Name | Should - Be $existingDbWithBackup
119
+ $results.IsJoined | Should - BeTrue
120
+ }
121
+ }
122
+
123
+ Context " When adding AG database that does not have a backup" {
68
124
BeforeAll {
69
- $server.Query (" create database $newdbname " )
70
- $backup = Get-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $newdbname | Backup-DbaDatabase
71
125
$splatAddAgDb = @ {
72
- SqlInstance = $TestConfig.instance3
73
- AvailabilityGroup = $agname
74
- Database = $newdbname
75
- Confirm = $false
126
+ SqlInstance = $TestConfig.instance3
127
+ AvailabilityGroup = $agName
128
+ Database = $existingDbWithoutBackup
129
+ # As we don't want an output, we suppress the warning.
130
+ # But we can still test the warning because WarningVariable is set globally to WarnVar.
131
+ WarningAction = ' SilentlyContinue'
76
132
}
77
133
$results = Add-DbaAgDatabase @splatAddAgDb
78
134
}
79
135
80
- It " Returns proper results" {
81
- $results.AvailabilityGroup | Should - Be $agname
82
- $results.Name | Should - Be $newdbname
83
- $results.IsJoined | Should - Be $true
136
+ It " Does warn" {
137
+ $WarnVar | Should -Match " Failed to add database $existingDbWithoutBackup to Availability Group $agName "
138
+ }
139
+
140
+ It " Does not return results" {
141
+ $results | Should - BeNullOrEmpty
142
+ }
143
+ }
144
+
145
+ Context " When adding AG database that does not exists" {
146
+ BeforeAll {
147
+ $splatAddAgDb = @ {
148
+ SqlInstance = $TestConfig.instance3
149
+ AvailabilityGroup = $agName
150
+ Database = $nonexistingDb
151
+ WarningAction = ' SilentlyContinue'
152
+ }
153
+ $results = Add-DbaAgDatabase @splatAddAgDb
154
+ }
155
+
156
+ It " Does warn" {
157
+ $WarnVar | Should -Match ([regex ]::Escape(" Database [$nonexistingDb ] is not found" ))
158
+ }
159
+
160
+ It " Does not return results" {
161
+ $results | Should - BeNullOrEmpty
84
162
}
85
163
}
86
- } # $TestConfig.instance2 for appveyor
164
+ } # $TestConfig.instance2 for appveyor
0 commit comments