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
+ <#
16
+ . SYNOPSIS
17
+ Tests database failover. Issues command with both -AsJob and without extra parameters
18
+
19
+ Also tests failing over twice to verify first failover went through which causes second failover to hit a recent failover
20
+ exception (aka too many failovers in the given period of time).
21
+ #>
22
+ function Test-FailoverDatabase
23
+ {
24
+ # Setup
25
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
26
+ $rg = Create- ResourceGroupForTest $location
27
+ $server = Create- ServerForTest $rg $location
28
+
29
+ try
30
+ {
31
+ # Create database
32
+ $databaseName = Get-DatabaseName
33
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName
34
+
35
+ # Failover database with -AsJob
36
+ $job = Invoke-AzSqlDatabaseFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - AsJob
37
+ $job | Wait-Job
38
+
39
+ # Failover database with no extra switch parameters. Tests for exception to verify the first failover went through correctly as this second
40
+ # failover will cause a recent failover exception
41
+ try {
42
+ Invoke-AzSqlDatabaseFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName
43
+ } catch {
44
+ $ErrorMessage = $_.Exception.Message
45
+ Assert-AreEqual True $ErrorMessage.Contains (" There was a recent failover on the database or pool" )
46
+ }
47
+ }
48
+ finally
49
+ {
50
+ Remove-ResourceGroupForTest $rg
51
+ }
52
+ }
53
+
54
+ <#
55
+ . SYNOPSIS
56
+ Tests database failover with passthru
57
+ #>
58
+ function Test-FailoverDatabasePassThru
59
+ {
60
+ # Setup
61
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
62
+ $rg = Create- ResourceGroupForTest $location
63
+ $server = Create- ServerForTest $rg $location
64
+
65
+ try
66
+ {
67
+ # Create database
68
+ $databaseName = Get-DatabaseName
69
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName
70
+
71
+ # Failover database with -PassThru
72
+ $output = Invoke-AzSqlDatabaseFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - PassThru
73
+ Assert-True { $output }
74
+ }
75
+ finally
76
+ {
77
+ Remove-ResourceGroupForTest $rg
78
+ }
79
+ }
80
+
81
+ <#
82
+ . SYNOPSIS
83
+ Tests database failover using piping for database.
84
+ #>
85
+ function Test-FailoverDatabaseWithDatabasePiping
86
+ {
87
+ # Setup
88
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
89
+ $rg = Create- ResourceGroupForTest $location
90
+ $server = Create- ServerForTest $rg $location
91
+
92
+ try
93
+ {
94
+ # Create database
95
+ $databaseName = Get-DatabaseName
96
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName
97
+
98
+ # Failover database using piping for database
99
+ Get-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName | Invoke-AzSqlDatabaseFailover
100
+ }
101
+ finally
102
+ {
103
+ Remove-ResourceGroupForTest $rg
104
+ }
105
+ }
106
+
107
+ <#
108
+ . SYNOPSIS
109
+ Tests database failover using piping for server.
110
+ #>
111
+ function Test-FailoverDatabaseWithServerPiping
112
+ {
113
+ # Setup
114
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
115
+ $rg = Create- ResourceGroupForTest $location
116
+ $server = Create- ServerForTest $rg $location
117
+
118
+ try
119
+ {
120
+ # Create database
121
+ $databaseName = Get-DatabaseName
122
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName
123
+
124
+ # Failover database using piping for server
125
+ Get-AzSqlServer - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName | Invoke-AzSqlDatabaseFailover - DatabaseName $databaseName
126
+ }
127
+ finally
128
+ {
129
+ Remove-ResourceGroupForTest $rg
130
+ }
131
+ }
132
+
133
+ <#
134
+ . SYNOPSIS
135
+ Tests elastic pool failover. Issues command with both -AsJob and without extra parameters
136
+
137
+ Also tests failing over twice to verify first failover went through which causes second failover to hit a recent failover
138
+ exception (aka too many failovers in the given period of time).
139
+ #>
140
+ function Test-FailoverElasticPool
141
+ {
142
+ # Setup
143
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
144
+ $rg = Create- ResourceGroupForTest $location
145
+ $server = Create- ServerForTest $rg $location
146
+
147
+ try
148
+ {
149
+ # Create elastic pool
150
+ $poolName = Get-ElasticPoolName
151
+ New-AzSqlElasticPool - ServerName $server.ServerName - ResourceGroupName $rg.ResourceGroupName - ElasticPoolName $poolName
152
+
153
+ # Create database in elastic pool
154
+ $databaseName = Get-DatabaseName
155
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - ElasticPoolName $poolName
156
+
157
+ # Failover elastic pool with -AsJob
158
+ $job = Invoke-AzSqlElasticPoolFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - ElasticPoolName $poolName - AsJob
159
+ $job | Wait-Job
160
+
161
+ # Failover elastic pool with no extra switch parameters. Tests for exception to verify the first failover went through correctly as this second
162
+ # failover will cause a recent failover exception
163
+ try {
164
+ Invoke-AzSqlElasticPoolFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - ElasticPoolName $poolName
165
+ } catch {
166
+ $ErrorMessage = $_.Exception.Message
167
+ Assert-AreEqual True $ErrorMessage.Contains (" There was a recent failover on the elastic pool" )
168
+ }
169
+ }
170
+ finally
171
+ {
172
+ Remove-ResourceGroupForTest $rg
173
+ }
174
+ }
175
+
176
+ <#
177
+ . SYNOPSIS
178
+ Tests elastic pool failover with passthru
179
+ #>
180
+ function Test-FailoverElasticPoolPassThru
181
+ {
182
+ # Setup
183
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
184
+ $rg = Create- ResourceGroupForTest $location
185
+ $server = Create- ServerForTest $rg $location
186
+
187
+ try
188
+ {
189
+ # Create elastic pool
190
+ $poolName = Get-ElasticPoolName
191
+ New-AzSqlElasticPool - ServerName $server.ServerName - ResourceGroupName $rg.ResourceGroupName - ElasticPoolName $poolName
192
+
193
+ # Create database in elastic pool
194
+ $databaseName = Get-DatabaseName
195
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - ElasticPoolName $poolName
196
+
197
+ # Failover elastic pool with -PassThru
198
+ $output = Invoke-AzSqlElasticPoolFailover - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - ElasticPoolName $poolName - PassThru
199
+ Assert-True { $output }
200
+ }
201
+ finally
202
+ {
203
+ Remove-ResourceGroupForTest $rg
204
+ }
205
+ }
206
+
207
+ <#
208
+ . SYNOPSIS
209
+ Tests elastic pool failover using piping for pool.
210
+ #>
211
+ function Test-FailoverElasticPoolWithPoolPiping
212
+ {
213
+ # Setup
214
+ $location = Get-Location " Microsoft.Sql" " operations" " Southeast Asia"
215
+ $rg = Create- ResourceGroupForTest $location
216
+ $server = Create- ServerForTest $rg $location
217
+
218
+ try
219
+ {
220
+ # Create elastic pool
221
+ $poolName = Get-ElasticPoolName
222
+ New-AzSqlElasticPool - ServerName $server.ServerName - ResourceGroupName $rg.ResourceGroupName - ElasticPoolName $poolName
223
+
224
+ # Create database in elastic pool
225
+ $databaseName = Get-DatabaseName
226
+ New-AzSqlDatabase - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - ElasticPoolName $poolName
227
+
228
+ # Failover elastic pool using piping for pool
229
+ Get-AzSqlElasticPool - ResourceGroupName $rg.ResourceGroupName - ServerName $server.ServerName - ElasticPoolName $poolName | Invoke-AzSqlElasticPoolFailover
230
+ }
231
+ finally
232
+ {
233
+ Remove-ResourceGroupForTest $rg
234
+ }
235
+ }
0 commit comments