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 upgrading a server with result from upgrade hint cmdlet
18
+ #>
19
+ function Test-ServerUpgradeWithUpgradeHint
20
+ {
21
+ # Setup
22
+ $server = Create- ServerForServerUpgradeTest
23
+
24
+ # Create a basic database
25
+ $databaseName = Get-DatabaseName
26
+ $database = New-AzureRmSqlDatabase - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - Edition Basic - MaxSizeBytes 1 GB
27
+ Assert-AreEqual $database.DatabaseName $databaseName
28
+
29
+ try
30
+ {
31
+ $upgrade = Get-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
32
+
33
+ $mapping = Get-AzureRmSqlServerUpgradeHint - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
34
+
35
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - ServerVersion 12.0 - ScheduleUpgradeAfterUtcDateTime ((Get-Date ).AddMinutes(1 ).ToUniversalTime()) - DatabaseCollection $mapping.Databases - ElasticPoolCollection $hint.ElasticPools }
36
+
37
+ while ($true )
38
+ {
39
+ $upgrade = Get-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
40
+ if ($upgrade.Status -eq " Completed" )
41
+ {
42
+ # Upgrade is successful
43
+ $server = Get-AzureRmSqlServer - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
44
+ Assert-AreEqual $server.ServerVersion " 12.0"
45
+ break
46
+ }
47
+ elseif ($upgrade.Status -eq " Stopped" )
48
+ {
49
+ # Upgrade failed
50
+ $server = Get-AzureRmSqlServer - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
51
+ Assert-AreEqual $server.ServerVersion " 2.0"
52
+ break
53
+ }
54
+ Wait-Seconds 10
55
+ }
56
+ }
57
+ finally
58
+ {
59
+ Remove-AzureRmResourceGroup - Name $server.ResourceGroupName - Force
60
+ }
61
+ }
62
+
63
+ <#
64
+ . SYNOPSIS
65
+ Tests upgrading a server and then cancel the upgrade
66
+ #>
67
+ function Test-ServerUpgradeAndCancel
68
+ {
69
+ # Setup
70
+ $server = Create- ServerForServerUpgradeTest
71
+
72
+ try
73
+ {
74
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - ServerVersion 12.0 }
75
+
76
+ $upgrade = Get-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
77
+ # Assert-AreEqual $upgrade.Status "Queued" # If the server was version 2.0 and Start had succeeded
78
+ Assert-AreEqual $upgrade.Status " Completed"
79
+
80
+ Assert-Throws { Stop-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - Force }
81
+
82
+ $upgrade = Get-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName
83
+ # Assert-AreEqual $upgrade.Status "Cancelling" # If the server was version 2.0 and Stop had succeeded
84
+ Assert-AreEqual $upgrade.Status " Completed"
85
+
86
+ # Below would be if the server was version 2.0 and Stop had succeeded
87
+ # while ($true)
88
+ # {
89
+ # $upgrade = Get-AzureRmSqlServerUpgrade -ResourceGroupName $server.ResourceGroupName -ServerName $server.ServerName
90
+ # # if ($upgrade.Status -eq "Stopped")
91
+ # if ($upgrade.Status -eq "Completed")
92
+ # {
93
+ # break
94
+ # }
95
+ # Wait-Seconds 10
96
+ # }
97
+
98
+ # Upgrade is cancelled
99
+ # $server = Get-AzureRmSqlServer -ResourceGroupName $server.ResourceGroupName -ServerName $server.ServerName
100
+ # Assert-AreEqual $server.ServerVersion "2.0"
101
+ }
102
+ finally
103
+ {
104
+ Remove-AzureRmResourceGroup - Name $server.ResourceGroupName - Force
105
+ }
106
+ }
107
+
108
+ <#
109
+ . SYNOPSIS
110
+ Tests upgrading a server with invalid parameters
111
+ #>
112
+ function Test-ServerUpgradeNegative
113
+ {
114
+ # Setup
115
+ $server = Create- ServerForServerUpgradeTest
116
+
117
+ # Create a basic database
118
+ $databaseName = Get-DatabaseName
119
+ $database = New-AzureRmSqlDatabase - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - DatabaseName $databaseName - Edition Basic - MaxSizeBytes 1 GB
120
+ Assert-AreEqual $database.DatabaseName $databaseName
121
+
122
+ try
123
+ {
124
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName }
125
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - ServerVersion 13.0 }
126
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - ScheduleUpgradeAfterUtcDateTime ((Get-Date ).ToUniversalTime())}
127
+
128
+ $recommendedDatabase = New-Object - TypeName Microsoft.Azure.Management.Sql.LegacySdk.Models.RecommendedDatabaseProperties
129
+ $recommendedDatabase.Name = databaseName
130
+ $recommendedDatabase.TargetEdition = " InvalidEdition"
131
+ $recommendedDatabase.TargetServiceLevelObjective = " S0"
132
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - DatabaseCollection ($recommendedDatabase )}
133
+
134
+ $recommendedDatabase.TargetEdition = " Premium"
135
+ $recommendedDatabase.TargetServiceLevelObjective = " S0"
136
+ Assert-Throws { Start-AzureRmSqlServerUpgrade - ResourceGroupName $server.ResourceGroupName - ServerName $server.ServerName - DatabaseCollection ($recommendedDatabase )}
137
+ }
138
+ finally
139
+ {
140
+ Remove-AzureRmResourceGroup - Name $server.ResourceGroupName - Force
141
+ }
142
+ }
143
+
144
+ <#
145
+ . SYNOPSIS
146
+ Creates a resource group and server for server upgrade tests
147
+ #>
148
+ function Create-ServerForServerUpgradeTest ()
149
+ {
150
+ $location = " West US"
151
+ $rgName = Get-ResourceGroupName
152
+
153
+ $rg = New-AzureRmResourceGroup - Name $rgName - Location $location
154
+
155
+ $serverName = Get-ServerName
156
+ $version = " 12.0"
157
+ $serverLogin = " testusername"
158
+ $serverPassword = " t357ingP@s5w0rd!"
159
+ $credentials = New-Object System.Management.Automation.PSCredential($serverLogin , ($serverPassword | ConvertTo-SecureString - asPlainText - Force))
160
+
161
+ $server = New-AzureRmSql ` Server - ResourceGroupName $rgName - ServerName $serverName - Location $location - ServerVersion $version - SqlAdministratorCredentials $credentials
162
+ return $server
163
+ }
0 commit comments