Skip to content

Commit c8bbdda

Browse files
author
Hovsep
committed
Merge pull request #2077 from llali/dev2
Add import/export cmdlets for Azure Sql database
2 parents ed13af4 + afad939 commit c8bbdda

23 files changed

+13380
-5
lines changed

src/ResourceManager/Sql/Commands.Sql.Test/Commands.Sql.Test.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</Reference>
7272
<Reference Include="Microsoft.Azure.Management.Sql">
7373
<SpecificVersion>False</SpecificVersion>
74-
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.45.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
74+
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.46.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
7575
</Reference>
7676
<Reference Include="Microsoft.Azure.Management.Storage">
7777
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
@@ -230,6 +230,9 @@
230230
</None>
231231
<None Include="ScenarioTests\ElasticPoolCrudTests.ps1">
232232
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
233+
</None>
234+
<None Include="ScenarioTests\ImportExportTests.ps1">
235+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
233236
</None>
234237
<Compile Include="ScenarioTests\LocationCapabilitiesTests.cs" />
235238
<Compile Include="ScenarioTests\ServerActiveDirectoryAdministratorTest.cs" />
@@ -245,6 +248,8 @@
245248
<Compile Include="ScenarioTests\SqlTestsBase.cs" />
246249
<Compile Include="ScenarioTests\TransparentDataEncryptionCrudTests.cs" />
247250
<Compile Include="ScenarioTests\IndexRecommendationTests.cs" />
251+
<Compile Include="ScenarioTests\ImportExportTests.cs" />
252+
<Compile Include="UnitTests\AzureSqlDatabaseImportExportTests.cs" />
248253
<Compile Include="UnitTests\AzureSqlCmdletBaseAttributeTests.cs" />
249254
<Compile Include="UnitTests\AzureSqlDatabaseActivationAttributeTest.cs" />
250255
<Compile Include="UnitTests\AzureSqlDatabaseAttributeTests.cs" />
@@ -599,6 +604,12 @@
599604
</None>
600605
<None Include="Templates\sql-ddm-test-env-setup.json">
601606
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
607+
</None>
608+
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ImportExportTests\TestExportDatabase.json">
609+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
610+
</None>
611+
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ImportExportTests\TestImportDatabase.json">
612+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
602613
</None>
603614
</ItemGroup>
604615
<ItemGroup />

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,50 @@ function Remove-ThreatDetectionTestEnvironment ($testSuffix)
250250
catch
251251
{
252252
}
253+
}
254+
255+
<#
256+
.SYNOPSIS
257+
Gets the parameters for import/export tests
258+
#>
259+
function Get-SqlDatabaseImportExportTestEnvironmentParameters ($testSuffix)
260+
{
261+
$databaseName = "sql-ie-cmdlet-db" + $testSuffix;
262+
$password = [Microsoft.Azure.Test.TestUtilities]::GenerateName("IEp@ssw0rd");
263+
#Fake storage account data. Used for playback mode
264+
$exportBacpacUri = "http://test.blob.core.windows.net/bacpacs"
265+
$importBacpacUri = "http://test.blob.core.windows.net/bacpacs/test.bacpac"
266+
$storageKey = "StorageKey"
267+
268+
$testMode = [System.Environment]::GetEnvironmentVariable("AZURE_TEST_MODE")
269+
if($testMode -eq "Record"){
270+
$exportBacpacUri = [System.Environment]::GetEnvironmentVariable("TEST_EXPORT_BACPAC")
271+
$importBacpacUri = [System.Environment]::GetEnvironmentVariable("TEST_IMPORT_BACPAC")
272+
$storageKey = [System.Environment]::GetEnvironmentVariable("TEST_STORAGE_KEY")
273+
274+
if ([System.string]::IsNullOrEmpty($exportBacpacUri)){
275+
throw "The TEST_EXPORT_BACPAC environment variable should point to a bacpac that has been uploaded to Azure blob storage ('e.g.' https://test.blob.core.windows.net/bacpacs/empty.bacpac)"
276+
}
277+
if ([System.string]::IsNullOrEmpty($importBacpacUri)){
278+
throw "The TEST_IMPORT_BACPAC environment variable should point to an Azure blob storage ('e.g.' https://test.blob.core.windows.net/bacpacs)"
279+
}
280+
if ([System.string]::IsNullOrEmpty($storageKey)){
281+
throw "The TEST_STORAGE_KEY environment variable should point to a valid storage key for an existing Azure storage account"
282+
}
283+
}
284+
285+
return @{
286+
rgname = "sql-ie-cmdlet-test-rg" +$testSuffix;
287+
serverName = "sql-ie-cmdlet-server" +$testSuffix;
288+
databaseName = $databaseName;
289+
userName = "testuser";
290+
firewallRuleName = "sql-ie-fwrule" +$testSuffix;
291+
password = $password;
292+
storageKeyType = "StorageAccessKey";
293+
storageKey = $storageKey;
294+
exportBacpacUri = $exportBacpacUri + "/" + $databaseName + ".bacpac";
295+
importBacpacUri = $importBacpacUri;
296+
location = "Australia East";
297+
version = "12.0";
298+
}
253299
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
16+
using Microsoft.Azure.Test;
17+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
18+
using Xunit;
19+
20+
namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
21+
{
22+
public class ImportExportTests : SqlTestsBase
23+
{
24+
[Fact]
25+
[Trait(Category.AcceptanceType, Category.Sql)]
26+
public void TestExportDatabase()
27+
{
28+
RunPowerShellTest("Test-ExportDatabase");
29+
}
30+
31+
[Fact]
32+
[Trait(Category.AcceptanceType, Category.Sql)]
33+
public void TestImportDatabase()
34+
{
35+
RunPowerShellTest("Test-ImportDatabase");
36+
}
37+
}
38+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
<#
17+
.SYNOPSIS
18+
Tests creating a database
19+
#>
20+
function Test-ExportDatabase
21+
{
22+
# Setup
23+
$testSuffix = 90063
24+
$createServer = $true
25+
$createDatabase = $true
26+
$createFirewallRule = $true
27+
$operationName = "Export"
28+
$succeeded = $true
29+
30+
Verify-ImportExport $testSuffix $createServer $createDatabase $createFirewallRule $operationName $succeeded
31+
}
32+
33+
function Test-ImportDatabase
34+
{
35+
# Setup
36+
$testSuffix = 90062
37+
$createServer = $true
38+
$createDatabase = $false
39+
$createFirewallRule = $true
40+
$operationName = "Import"
41+
$succeeded = $true
42+
43+
Verify-ImportExport $testSuffix $createServer $createDatabase $createFirewallRule $operationName $succeeded
44+
}
45+
46+
function Verify-ImportExport($testSuffix, $createServer, $createDatabase, $createFirewallRule, $operationName, $succeeded)
47+
{
48+
# Setup
49+
$params = Get-SqlDatabaseImportExportTestEnvironmentParameters $testSuffix
50+
$rg = New-AzureRmResourceGroup -Name $params.rgname -Location $params.location
51+
$export = "Export"
52+
$import = "Import"
53+
54+
try
55+
{
56+
Assert-NotNull $params.storageKey
57+
Assert-NotNull $params.importBacpacUri
58+
Assert-NotNull $params.exportBacpacUri
59+
60+
$password = $params.password
61+
$secureString = ($password | ConvertTo-SecureString -asPlainText -Force)
62+
$credentials = new-object System.Management.Automation.PSCredential($params.userName, $secureString)
63+
if($createServer -eq $true){
64+
$server = New-AzureRmSqlServer -ResourceGroupName $params.rgname -ServerName $params.serverName -ServerVersion $params.version -Location $params.location -SqlAdministratorCredentials $credentials
65+
}
66+
67+
if($createDatabase -eq $true){
68+
$standarddb = New-AzureRmSqlDatabase -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
69+
}
70+
71+
if($createFirewallRule -eq $true){
72+
New-AzureRmSqlServerFirewallRule -ResourceGroupName $params.rgname -ServerName $params.serverName -AllowAllAzureIPs
73+
}
74+
75+
$operationStatusLink = ""
76+
77+
if($operationName -eq $export){
78+
# Export database.
79+
$exportResponse = New-AzureRmSqlDatabaseExport -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName -StorageKeyType $params.storageKeyType -StorageKey $params.storageKey -StorageUri $params.exportBacpacUri -AdministratorLogin $params.userName -AdministratorLoginPassword $secureString -AuthenticationType Sql
80+
Assert-NotNull $exportResponse
81+
$operationStatusLink = $exportResponse.OperationStatusLink
82+
}
83+
84+
if($operationName -eq $import){
85+
$importResponse = New-AzureRmSqlDatabaseImport -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName -StorageKeyType $params.storageKeyType -StorageKey $params.storageKey -StorageUri $params.importBacpacUri -AdministratorLogin $params.userName -AdministratorLoginPassword $secureString -Edition Standard -ServiceObjectiveName S0 -DatabaseMaxSizeBytes 5000000 -AuthenticationType Sql
86+
Assert-NotNull $importResponse
87+
$operationStatusLink = $importResponse.OperationStatusLink
88+
}
89+
90+
Assert-NotNull $operationStatusLink
91+
92+
#Get status
93+
$statusInProgress = "InProgress"
94+
$statusSucceeded = "Succeeded"
95+
$status = "InProgress"
96+
97+
if($succeeded -eq $true){
98+
Write-Output "Getting Status"
99+
while($status -eq $statusInProgress){
100+
$statusResponse = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $operationStatusLink
101+
Write-Output "Import Export Status Message:" + $statusResponse.StatusMessage
102+
$status = $statusResponse.Status
103+
}
104+
Assert-AreEqual $status $statusSucceeded
105+
Write-Output "ImportExportStatus:" + $status
106+
}
107+
}
108+
finally
109+
{
110+
Remove-ResourceGroupForTest $rg
111+
}
112+
}

0 commit comments

Comments
 (0)