1
+ # ----------------------------------------------------------------------------------
2
+ #
3
+ # Copyright Microsoft Corporation
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ----------------------------------------------------------------------------------
14
+
15
+ function Handle-InstanceFailoverGroupTest ($scriptBlock , $rg = " testclrg" , $primaryLocation = " southeastasia" , $secondaryLocation = " southeastasia" , $mi1 = $null , $mi2 = $null , $cleanup = $false )
16
+ {
17
+ try
18
+ {
19
+ $rg = if ($rg -eq $null ) { " testclrg" } else { $rg }
20
+ $miName1 = if ($mi1 -eq $null ) { " tdstage-haimb-dont-delete-3" } else { " " }
21
+ $miName2 = if ($mi1 -eq $null ) { " threat-detection-test-1" } else { " " }
22
+
23
+ Invoke-Command - ScriptBlock $scriptBlock - ArgumentList $primaryLocation , $secondaryLocation , $rg , $miName1 , $miName2
24
+ }
25
+ finally
26
+ {
27
+ }
28
+ }
29
+
30
+ function Handle-InstanceFailoverGroupTestWithInstanceFailoverGroup ($scriptBlock , $failoverPolicy = " Automatic" )
31
+ {
32
+ Handle- InstanceFailoverGroupTest {
33
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
34
+
35
+ $fgName = Get-FailoverGroupName
36
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - Name $fgName - Location $location - ResourceGroupName $rg - PrimaryManagedInstanceName $managedInstanceName - PartnerRegion $partnerRegion - PartnerResourceGroupName $rg - PartnerManagedInstanceName $partnerManagedInstanceName
37
+ Invoke-Command - ScriptBlock $scriptBlock - ArgumentList $fg
38
+
39
+ }.GetNewClosure()
40
+ }
41
+
42
+ function Validate-InstanceFailoverGroup ($rg , $name , $miName1 , $miName2 , $role , $partnerRole , $failoverPolicy , $gracePeriod , $readOnlyFailoverPolicy , $fg , $message = " no context provided" )
43
+ {
44
+ Assert-NotNull $fg.Id " `$ fg.Id ($message )"
45
+ Assert-NotNull $fg.PartnerRegion " `$ fg.PartnerRegion ($message )"
46
+ Assert-AreEqual $miName1 $fg.PrimaryManagedInstanceName " `$ fg.PrimaryManagedInstanceName ($message )"
47
+ Assert-AreEqual $miName2 $fg.PartnerManagedInstanceName " `$ fg.PartnerManagedInstanceName ($message )"
48
+ Assert-AreEqual $name $fg.Name " `$ fg.Name ($message )"
49
+ Assert-AreEqual $rg $fg.ResourceGroupName " `$ fg.ResourceGroupName ($message )"
50
+ Assert-AreEqual $rg $fg.PartnerResourceGroupName " `$ fg.PartnerResourceGroupName ($message )"
51
+ Assert-AreEqual $role $fg.ReplicationRole " `$ fg.ReplicationRole ($message )"
52
+ Assert-AreEqual $failoverPolicy $fg.ReadWriteFailoverPolicy " `$ fg.ReadWriteFailoverPolicy ($message )"
53
+ Assert-AreEqual $gracePeriod $fg.FailoverWithDataLossGracePeriodHours " `$ fg.FailoverWithGracePeriodHours ($message )"
54
+ Assert-AreEqual $readOnlyFailoverPolicy $fg.ReadOnlyFailoverPolicy " `$ fg.ReadOnlyFailoverPolicy ($message )"
55
+ Assert-AreEqual $true @ (' CATCH_UP' , ' SUSPENDED' , ' SEEDING' ).Contains($fg.ReplicationState ) " `$ fg.ReplicationState ($message )"
56
+ }
57
+
58
+ function Assert-InstanceFailoverGroupsEqual ($expected , $actual , $role = $null , $failoverPolicy = $null , $gracePeriod = $null , $readOnlyFailoverPolicy = $null , $message = " no context provided" )
59
+ {
60
+ $failoverPolicy = if ($failoverPolicy -eq $null ) { $expected.ReadWriteFailoverPolicy } else { $failoverPolicy }
61
+ $gracePeriod = if ($gracePeriod -eq $null -and $failoverPolicy -ne " Manual" ) { $expected.FailoverWithDataLossGracePeriodHours } else { $gracePeriod }
62
+ $readOnlyFailoverPolicy = if ($readOnlyFailoverPolicy -eq $null ) { $expected.ReadOnlyFailoverPolicy } else { $readOnlyFailoverPolicy }
63
+ $role = if ($role -eq $null ) { $expected.ReplicationRole } else { $role }
64
+
65
+ $partnerRole = if ($role -eq " Primary" ) { " Secondary" } else { " Primary" }
66
+
67
+ Validate- InstanceFailoverGroup `
68
+ $expected.ResourceGroupName `
69
+ $expected.Name `
70
+ $expected.PrimaryManagedInstanceName `
71
+ $expected.PartnerManagedInstanceName `
72
+ $role `
73
+ $partnerRole `
74
+ $failoverPolicy `
75
+ $gracePeriod `
76
+ $readOnlyFailoverPolicy `
77
+ $actual `
78
+ $message
79
+ }
80
+
81
+ function Validate-InstanceFailoverGroupWithGet ($fg , $message = " no context provided" )
82
+ {
83
+ $actual = Get-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $fg.ResourceGroupName - Location $fg.Location - Name $fg.Name
84
+ Assert-InstanceFailoverGroupsEqual $fg $actual - message $message
85
+ }
86
+
87
+ <#
88
+ . SYNOPSIS
89
+ Tests create and update a failover group
90
+ #>
91
+
92
+ function Test-CreateInstanceFailoverGroup-Named ()
93
+ {
94
+ Handle- InstanceFailoverGroupTest {
95
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
96
+
97
+ $fgName = Get-FailoverGroupName
98
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - Name $fgName - Location $location - ResourceGroupName $rg - PrimaryManagedInstanceName $managedInstanceName - PartnerRegion $partnerRegion - PartnerResourceGroupName $rg - PartnerManagedInstanceName $partnerManagedInstanceName
99
+ Validate- InstanceFailoverGroup $rg $fgName $managedInstanceName $partnerManagedInstanceName Primary Secondary Automatic 1 Disabled $fg
100
+ Validate- InstanceFailoverGroupWithGet $fg
101
+
102
+ Remove-AzSqlDatabaseInstanceFailoverGroup - Name $fgName - Location $location - ResourceGroupName $rg - Force
103
+ }
104
+ }
105
+
106
+ function Test-CreateInstanceFailoverGroup-Positional ()
107
+ {
108
+ Handle- InstanceFailoverGroupTest {
109
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
110
+
111
+ $fgName = Get-FailoverGroupName
112
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $rg - PrimaryManagedInstanceName $managedInstanceName - Name $fgName - Location $location - PartnerRegion $partnerRegion - PartnerManagedInstanceName $partnerManagedInstanceName
113
+ Validate- InstanceFailoverGroup $rg $fgName $managedInstanceName $partnerManagedInstanceName Primary Secondary Automatic 1 Disabled $fg
114
+ Validate- InstanceFailoverGroupWithGet $fg
115
+
116
+ $fg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
117
+ }
118
+ }
119
+
120
+ function Test-CreateInstanceFailoverGroup-AutomaticPolicy ()
121
+ {
122
+ Handle- InstanceFailoverGroupTest {
123
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
124
+
125
+ $fgName = Get-FailoverGroupName
126
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $rg - Location $location - PrimaryManagedInstanceName $managedInstanceName - Name $fgName - PartnerRegion $partnerRegion - PartnerManagedInstanceName $partnerManagedInstanceName - FailoverPolicy Automatic
127
+ Validate- InstanceFailoverGroup $rg $fgName $managedInstanceName $partnerManagedInstanceName Primary Secondary Automatic 1 Disabled $fg
128
+ Validate- InstanceFailoverGroupWithGet $fg
129
+
130
+ $fg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
131
+ }
132
+ }
133
+
134
+ function Test-CreateInstanceFailoverGroup-AutomaticPolicyGracePeriodReadOnlyFailover ()
135
+ {
136
+ Handle- InstanceFailoverGroupTest {
137
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
138
+
139
+ $fgName = Get-FailoverGroupName
140
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $rg - Location $location - PrimaryManagedInstanceName $managedInstanceName - Name $fgName - PartnerRegion $partnerRegion - PartnerManagedInstanceName $partnerManagedInstanceName - FailoverPolicy Automatic - GracePeriodWithDataLossHours 123 - AllowReadOnlyFailoverToPrimary Enabled
141
+ Validate- InstanceFailoverGroup $rg $fgName $managedInstanceName $partnerManagedInstanceName Primary Secondary Automatic 123 Enabled $fg
142
+ Validate- InstanceFailoverGroupWithGet $fg
143
+
144
+ $fg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
145
+ }
146
+ }
147
+
148
+ function Test-CreateInstanceFailoverGroup-ManualPolicy ()
149
+ {
150
+ Handle- InstanceFailoverGroupTest {
151
+ Param ($location , $partnerRegion , $rg , $managedInstanceName , $partnerManagedInstanceName )
152
+
153
+ $fgName = Get-FailoverGroupName
154
+ $fg = New-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $rg - Location $location - PrimaryManagedInstanceName $managedInstanceName - Name $fgName - PartnerRegion $partnerRegion - PartnerManagedInstanceName $partnerManagedInstanceName - FailoverPolicy Manual
155
+ Validate- InstanceFailoverGroup $rg $fgName $managedInstanceName $partnerManagedInstanceName Primary Secondary Manual $null Disabled $fg
156
+ Validate- InstanceFailoverGroupWithGet $fg
157
+
158
+ $fg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
159
+ }
160
+ }
161
+
162
+ function Test-SetInstanceFailoverGroup-Named ()
163
+ {
164
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
165
+ Param ($fg )
166
+
167
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup
168
+ Assert-InstanceFailoverGroupsEqual $fg $newFg
169
+ Validate- InstanceFailoverGroupWithGet $newFg
170
+
171
+ $newFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
172
+ }
173
+ }
174
+
175
+ function Test-SetInstanceFailoverGroup-Positional ()
176
+ {
177
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
178
+ Param ($fg )
179
+
180
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup
181
+ Assert-InstanceFailoverGroupsEqual $fg $newFg
182
+ Validate- InstanceFailoverGroupWithGet $newFg
183
+
184
+ $newFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
185
+ }
186
+ }
187
+
188
+ function Test-SetInstanceFailoverGroup-AutomaticWithGracePeriodReadOnlyFailover ()
189
+ {
190
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
191
+ Param ($fg )
192
+
193
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup - FailoverPolicy Automatic - GracePeriodWithDataLossHours 123 - AllowReadOnlyFailoverToPrimary Enabled
194
+ Assert-InstanceFailoverGroupsEqual $fg $newFg - failoverPolicy Automatic - gracePeriod 123 - readOnlyFailoverPolicy Enabled
195
+ Validate- InstanceFailoverGroupWithGet $newFg
196
+
197
+ $newFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
198
+ } - failoverPolicy Manual
199
+ }
200
+
201
+ function Test-SetInstanceFailoverGroup-AutomaticToManual ()
202
+ {
203
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
204
+ Param ($fg )
205
+
206
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup - FailoverPolicy Manual
207
+ Assert-InstanceFailoverGroupsEqual $fg $newFg - failoverPolicy Manual - gracePeriod $null
208
+ Validate- InstanceFailoverGroupWithGet $newFg
209
+
210
+ $newFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
211
+ }
212
+ }
213
+
214
+ function Test-SetInstanceFailoverGroup-ManualToAutomaticNoGracePeriod ()
215
+ {
216
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
217
+ Param ($fg )
218
+
219
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup - FailoverPolicy Manual
220
+ Assert-InstanceFailoverGroupsEqual $fg $newFg - failoverPolicy Manual - gracePeriod $null
221
+
222
+ $newFg = $fg | Set-AzSqlDatabaseInstanceFailoverGroup - FailoverPolicy Automatic
223
+ Assert-InstanceFailoverGroupsEqual $fg $newFg - failoverPolicy Automatic - gracePeriod 1
224
+ Validate- InstanceFailoverGroupWithGet $newFg
225
+
226
+ $newFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
227
+ } - failoverPolicy Manual
228
+ }
229
+
230
+ function Test-SwitchInstanceFailoverGroup ()
231
+ {
232
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
233
+ Param ($fg )
234
+
235
+ $fg | Switch-AzSqlDatabaseInstanceFailoverGroup
236
+
237
+ $newPrimaryFg = Get-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $fg.ResourceGroupName - Location $fg.Location - Name $fg.Name
238
+
239
+ Validate- InstanceFailoverGroupWithGet $newPrimaryFg
240
+
241
+ $newPrimaryFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
242
+ }
243
+ }
244
+
245
+ function Test-SwitchInstanceFailoverGroupAllowDataLoss ()
246
+ {
247
+ Handle- InstanceFailoverGroupTestWithInstanceFailoverGroup {
248
+ Param ($fg )
249
+
250
+ $fg | Switch-AzSqlDatabaseInstanceFailoverGroup - AllowDataLoss
251
+ $newPrimaryFg = Get-AzSqlDatabaseInstanceFailoverGroup - ResourceGroupName $fg.ResourceGroupName - Location $fg.Location - Name $fg.Name
252
+
253
+ Validate- InstanceFailoverGroupWithGet $newPrimaryFg
254
+
255
+ $newPrimaryFg | Remove-AzSqlDatabaseInstanceFailoverGroup - Force
256
+ }
257
+ }
0 commit comments