/src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DatabaseReplicationTests.ps1

https://gitlab.com/jslee1/azure-powershell · Powershell · 293 lines · 178 code · 30 blank · 85 comment · 0 complexity · 08f2ba807d3e739f59679fe118cefb99 MD5 · raw file

  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. .SYNOPSIS
  16. Tests creating a database copy
  17. #>
  18. function Test-CreateDatabaseCopy
  19. {
  20. Test-CreateCopyInternal "12.0" "North Europe"
  21. }
  22. <#
  23. .SYNOPSIS
  24. Tests creating a database copy
  25. #>
  26. function Test-CreateDatabaseCopyV2
  27. {
  28. Test-CreateCopyInternal "2.0" "North Central US"
  29. }
  30. <#
  31. .SYNOPSIS
  32. Tests creating a database copy
  33. #>
  34. function Test-CreateCopyInternal ($serverVersion, $location = "North Europe")
  35. {
  36. # Setup
  37. $rg = Create-ResourceGroupForTest $location
  38. $server = Create-ServerForTest $rg $serverVersion $location
  39. $database = Create-DatabaseForTest $rg $server "Standard"
  40. $copyRg = Create-ResourceGroupForTest $location
  41. $copyServer = Create-ServerForTest $copyRg $serverVersion $location
  42. $copyDatabaseName = Get-DatabaseName
  43. try
  44. {
  45. # Create a local database copy
  46. $dbLocalCopy = New-AzureRmSqlDatabaseCopy -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  47. -CopyDatabaseName $copyDatabaseName
  48. Assert-AreEqual $dbLocalCopy.ResourceGroupName $rg.ResourceGroupName
  49. Assert-AreEqual $dbLocalCopy.ServerName $server.ServerName
  50. Assert-AreEqual $dbLocalCopy.DatabaseName $database.DatabaseName
  51. Assert-AreEqual $dbLocalCopy.CopyResourceGroupName $rg.ResourceGroupName
  52. Assert-AreEqual $dbLocalCopy.CopyServerName $server.ServerName
  53. Assert-AreEqual $dbLocalCopy.CopyDatabaseName $copyDatabaseName
  54. # Create a cross server copy
  55. $dbCrossServerCopy = New-AzureRmSqlDatabaseCopy -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  56. -CopyResourceGroupName $copyRg.ResourceGroupName -CopyServerName $copyServer.ServerName -CopyDatabaseName $copyDatabaseName
  57. Assert-AreEqual $dbCrossServerCopy.ResourceGroupName $rg.ResourceGroupName
  58. Assert-AreEqual $dbCrossServerCopy.ServerName $server.ServerName
  59. Assert-AreEqual $dbCrossServerCopy.DatabaseName $database.DatabaseName
  60. Assert-AreEqual $dbCrossServerCopy.CopyResourceGroupName $copyRg.ResourceGroupName
  61. Assert-AreEqual $dbCrossServerCopy.CopyServerName $copyServer.ServerName
  62. Assert-AreEqual $dbCrossServerCopy.CopyDatabaseName $copyDatabaseName
  63. }
  64. finally
  65. {
  66. Remove-ResourceGroupForTest $rg
  67. Remove-ResourceGroupForTest $copyRg
  68. }
  69. }
  70. <#
  71. .SYNOPSIS
  72. Tests creating a secondary database
  73. #>
  74. function Test-CreateSecondaryDatabase
  75. {
  76. Test-CreateSecondaryDatabaseInternal "12.0" "North Europe"
  77. }
  78. <#
  79. .SYNOPSIS
  80. Tests creating a secondary database
  81. #>
  82. function Test-CreateSecondaryDatabaseV2
  83. {
  84. Test-CreateSecondaryDatabaseInternal "2.0" "North Central US"
  85. }
  86. <#
  87. .SYNOPSIS
  88. Tests creating a secondary database
  89. #>
  90. function Test-CreateSecondaryDatabaseInternal ($serverVersion, $location = "North Europe")
  91. {
  92. # Setup
  93. $rg = Create-ResourceGroupForTest $location
  94. $server = Create-ServerForTest $rg $serverVersion $location
  95. $database = Create-DatabaseForTest $rg $server
  96. $partRg = Create-ResourceGroupForTest $location
  97. $partServer = Create-ServerForTest $partRg $serverVersion $location
  98. try
  99. {
  100. # Create Readable Secondary
  101. $readSecondary = New-AzureRmSqlDatabaseSecondary -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  102. -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName -AllowConnections All
  103. Assert-NotNull $readSecondary.LinkId
  104. Assert-AreEqual $readSecondary.ResourceGroupName $rg.ResourceGroupName
  105. Assert-AreEqual $readSecondary.ServerName $server.ServerName
  106. Assert-AreEqual $readSecondary.DatabaseName $database.DatabaseName
  107. Assert-AreEqual $readSecondary.Role "Primary"
  108. Assert-AreEqual $readSecondary.Location $location
  109. Assert-AreEqual $readSecondary.PartnerResourceGroupName $partRg.ResourceGroupName
  110. Assert-AreEqual $readSecondary.PartnerServerName $partServer.ServerName
  111. Assert-NotNull $readSecondary.PartnerRole
  112. Assert-AreEqual $readSecondary.PartnerLocation $location
  113. Assert-NotNull $readSecondary.AllowConnections
  114. Assert-NotNull $readSecondary.ReplicationState
  115. Assert-NotNull $readSecondary.PercentComplete
  116. }
  117. finally
  118. {
  119. Remove-ResourceGroupForTest $rg
  120. Remove-ResourceGroupForTest $partRg
  121. }
  122. }
  123. <#
  124. .SYNOPSIS
  125. Tests getting a secondary database
  126. #>
  127. function Test-GetReplicationLink
  128. {
  129. Test-GetReplicationLinkInternal "12.0" "North Europe"
  130. }
  131. <#
  132. .SYNOPSIS
  133. Tests getting a secondary database
  134. #>
  135. function Test-GetReplicationLinkV2
  136. {
  137. Test-GetReplicationLinkInternal "2.0" "North Central US"
  138. }
  139. <#
  140. .SYNOPSIS
  141. Tests getting a secondary database
  142. #>
  143. function Test-GetReplicationLinkInternal ($serverVersion, $location = "North Europe")
  144. {
  145. # Setup
  146. $rg = Create-ResourceGroupForTest $location
  147. $server = Create-ServerForTest $rg $serverVersion $location
  148. $database = Create-DatabaseForTest $rg $server
  149. $partRg = Create-ResourceGroupForTest $location
  150. $partServer = Create-ServerForTest $partRg $serverVersion $location
  151. try
  152. {
  153. # Get Secondary
  154. New-AzureRmSqlDatabaseSecondary -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  155. -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName -AllowConnections All
  156. $secondary = Get-AzureRmSqlDatabaseReplicationLink -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName `
  157. -DatabaseName $database.DatabaseName -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName
  158. Assert-NotNull $secondary.LinkId
  159. Assert-AreEqual $secondary.ResourceGroupName $rg.ResourceGroupName
  160. Assert-AreEqual $secondary.ServerName $server.ServerName
  161. Assert-AreEqual $secondary.DatabaseName $database.DatabaseName
  162. Assert-AreEqual $secondary.Role Primary
  163. Assert-AreEqual $secondary.Location $location
  164. Assert-AreEqual $secondary.PartnerResourceGroupName $partRg.ResourceGroupName
  165. Assert-AreEqual $secondary.PartnerServerName $partServer.ServerName
  166. Assert-NotNull $secondary.PartnerRole
  167. Assert-AreEqual $secondary.PartnerLocation $location
  168. Assert-NotNull $secondary.AllowConnections
  169. Assert-NotNull $secondary.ReplicationState
  170. Assert-NotNull $secondary.PercentComplete
  171. }
  172. finally
  173. {
  174. Remove-ResourceGroupForTest $rg
  175. Remove-ResourceGroupForTest $partRg
  176. }
  177. }
  178. <#
  179. .SYNOPSIS
  180. Tests removing a secondary database
  181. #>
  182. function Test-RemoveSecondaryDatabase
  183. {
  184. Test-RemoveSecondaryDatabaseInternal "12.0" "North Europe"
  185. }
  186. <#
  187. .SYNOPSIS
  188. Tests removing a secondary database
  189. #>
  190. function Test-RemoveSecondaryDatabaseV2
  191. {
  192. Test-RemoveSecondaryDatabaseInternal "2.0" "North Central US"
  193. }
  194. <#
  195. .SYNOPSIS
  196. Tests removing a secondary database
  197. #>
  198. function Test-RemoveSecondaryDatabaseInternal ($serverVersion, $location = "North Europe")
  199. {
  200. # Setup
  201. $rg = Create-ResourceGroupForTest $location
  202. $server = Create-ServerForTest $rg $serverVersion $location
  203. $database = Create-DatabaseForTest $rg $server
  204. $partRg = Create-ResourceGroupForTest $location
  205. $partServer = Create-ServerForTest $partRg $serverVersion $location
  206. try
  207. {
  208. # remove Secondary
  209. New-AzureRmSqlDatabaseSecondary -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  210. -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName -AllowConnections All
  211. Remove-AzureRmSqlDatabaseSecondary -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  212. -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName
  213. }
  214. finally
  215. {
  216. Remove-ResourceGroupForTest $rg
  217. Remove-ResourceGroupForTest $partRg
  218. }
  219. }
  220. <#
  221. .SYNOPSIS
  222. Tests removing a secondary database
  223. #>
  224. function Test-FailoverSecondaryDatabase
  225. {
  226. #v12 only
  227. Test-FailoverSecondaryDatabaseInternal "12.0" "North Europe"
  228. }
  229. <#
  230. .SYNOPSIS
  231. Tests failing over a secondary database
  232. #>
  233. function Test-FailoverSecondaryDatabaseInternal ($serverVersion, $location = "North Europe")
  234. {
  235. # Setup
  236. $rg = Create-ResourceGroupForTest $location
  237. $server = Create-ServerForTest $rg $serverVersion $location
  238. $database = Create-DatabaseForTest $rg $server
  239. $partRg = Create-ResourceGroupForTest $location
  240. $partServer = Create-ServerForTest $partRg $serverVersion $location
  241. try
  242. {
  243. # failover Secondary
  244. New-AzureRmSqlDatabaseSecondary -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $database.DatabaseName `
  245. -PartnerResourceGroupName $partRg.ResourceGroupName -PartnerServerName $partServer.ServerName -AllowConnections All
  246. $secondary = Get-AzureRmSqlDatabaseReplicationLink -ResourceGroupName $partRg.ResourceGroupName -ServerName $partServer.ServerName -DatabaseName $database.DatabaseName -PartnerResourceGroupName $rg.ResourceGroupName -PartnerServerName $server.ServerName
  247. $secondary | Set-AzureRmSqlDatabaseSecondary -PartnerResourceGroupName $rg.ResourceGroupName -Failover
  248. }
  249. finally
  250. {
  251. Remove-ResourceGroupForTest $rg
  252. Remove-ResourceGroupForTest $partRg
  253. }
  254. }
  255. <#
  256. .SYNOPSIS
  257. Creates test database
  258. #>
  259. function Create-DatabaseForTest ($rg, $server, $edition = "Premium")
  260. {
  261. $databaseName = Get-DatabaseName
  262. New-AzureRmSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -Edition $edition
  263. }