Skip to content

Add import/export cmdlets for Azure Sql database #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</Reference>
<Reference Include="Microsoft.Azure.Management.Sql">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.45.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Sql.0.46.0-prerelease\lib\net40\Microsoft.Azure.Management.Sql.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.Storage">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.Storage.2.4.0-preview\lib\net40\Microsoft.Azure.Management.Storage.dll</HintPath>
Expand Down Expand Up @@ -230,6 +230,9 @@
</None>
<None Include="ScenarioTests\ElasticPoolCrudTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\ImportExportTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Include="ScenarioTests\LocationCapabilitiesTests.cs" />
<Compile Include="ScenarioTests\ServerActiveDirectoryAdministratorTest.cs" />
Expand All @@ -245,6 +248,8 @@
<Compile Include="ScenarioTests\SqlTestsBase.cs" />
<Compile Include="ScenarioTests\TransparentDataEncryptionCrudTests.cs" />
<Compile Include="ScenarioTests\IndexRecommendationTests.cs" />
<Compile Include="ScenarioTests\ImportExportTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseImportExportTests.cs" />
<Compile Include="UnitTests\AzureSqlCmdletBaseAttributeTests.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseActivationAttributeTest.cs" />
<Compile Include="UnitTests\AzureSqlDatabaseAttributeTests.cs" />
Expand Down Expand Up @@ -599,6 +604,12 @@
</None>
<None Include="Templates\sql-ddm-test-env-setup.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ImportExportTests\TestExportDatabase.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ImportExportTests\TestImportDatabase.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup />
Expand Down
46 changes: 46 additions & 0 deletions src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,50 @@ function Remove-ThreatDetectionTestEnvironment ($testSuffix)
catch
{
}
}

<#
.SYNOPSIS
Gets the parameters for import/export tests
#>
function Get-SqlDatabaseImportExportTestEnvironmentParameters ($testSuffix)
{
$databaseName = "sql-ie-cmdlet-db" + $testSuffix;
$password = [Microsoft.Azure.Test.TestUtilities]::GenerateName("IEp@ssw0rd");
#Fake storage account data. Used for playback mode
$exportBacpacUri = "http://test.blob.core.windows.net/bacpacs"
$importBacpacUri = "http://test.blob.core.windows.net/bacpacs/test.bacpac"
$storageKey = "StorageKey"

$testMode = [System.Environment]::GetEnvironmentVariable("AZURE_TEST_MODE")
if($testMode -eq "Record"){
$exportBacpacUri = [System.Environment]::GetEnvironmentVariable("TEST_EXPORT_BACPAC")
$importBacpacUri = [System.Environment]::GetEnvironmentVariable("TEST_IMPORT_BACPAC")
$storageKey = [System.Environment]::GetEnvironmentVariable("TEST_STORAGE_KEY")

if ([System.string]::IsNullOrEmpty($exportBacpacUri)){
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)"
}
if ([System.string]::IsNullOrEmpty($importBacpacUri)){
throw "The TEST_IMPORT_BACPAC environment variable should point to an Azure blob storage ('e.g.' https://test.blob.core.windows.net/bacpacs)"
}
if ([System.string]::IsNullOrEmpty($storageKey)){
throw "The TEST_STORAGE_KEY environment variable should point to a valid storage key for an existing Azure storage account"
}
}

return @{
rgname = "sql-ie-cmdlet-test-rg" +$testSuffix;
serverName = "sql-ie-cmdlet-server" +$testSuffix;
databaseName = $databaseName;
userName = "testuser";
firewallRuleName = "sql-ie-fwrule" +$testSuffix;
password = $password;
storageKeyType = "StorageAccessKey";
storageKey = $storageKey;
exportBacpacUri = $exportBacpacUri + "/" + $databaseName + ".bacpac";
importBacpacUri = $importBacpacUri;
location = "Australia East";
version = "12.0";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.ScenarioTest.SqlTests;
using Microsoft.Azure.Test;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
{
public class ImportExportTests : SqlTestsBase
{
[Fact]
[Trait(Category.AcceptanceType, Category.Sql)]
public void TestExportDatabase()
{
RunPowerShellTest("Test-ExportDatabase");
}

[Fact]
[Trait(Category.AcceptanceType, Category.Sql)]
public void TestImportDatabase()
{
RunPowerShellTest("Test-ImportDatabase");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------


<#
.SYNOPSIS
Tests creating a database
#>
function Test-ExportDatabase
{
# Setup
$testSuffix = 90063
$createServer = $true
$createDatabase = $true
$createFirewallRule = $true
$operationName = "Export"
$succeeded = $true

Verify-ImportExport $testSuffix $createServer $createDatabase $createFirewallRule $operationName $succeeded
}

function Test-ImportDatabase
{
# Setup
$testSuffix = 90062
$createServer = $true
$createDatabase = $false
$createFirewallRule = $true
$operationName = "Import"
$succeeded = $true

Verify-ImportExport $testSuffix $createServer $createDatabase $createFirewallRule $operationName $succeeded
}

function Verify-ImportExport($testSuffix, $createServer, $createDatabase, $createFirewallRule, $operationName, $succeeded)
{
# Setup
$params = Get-SqlDatabaseImportExportTestEnvironmentParameters $testSuffix
$rg = New-AzureRmResourceGroup -Name $params.rgname -Location $params.location
$export = "Export"
$import = "Import"

try
{
Assert-NotNull $params.storageKey
Assert-NotNull $params.importBacpacUri
Assert-NotNull $params.exportBacpacUri

$password = $params.password
$secureString = ($password | ConvertTo-SecureString -asPlainText -Force)
$credentials = new-object System.Management.Automation.PSCredential($params.userName, $secureString)
if($createServer -eq $true){
$server = New-AzureRmSqlServer -ResourceGroupName $params.rgname -ServerName $params.serverName -ServerVersion $params.version -Location $params.location -SqlAdministratorCredentials $credentials
}

if($createDatabase -eq $true){
$standarddb = New-AzureRmSqlDatabase -ResourceGroupName $params.rgname -ServerName $params.serverName -DatabaseName $params.databaseName
}

if($createFirewallRule -eq $true){
New-AzureRmSqlServerFirewallRule -ResourceGroupName $params.rgname -ServerName $params.serverName -AllowAllAzureIPs
}

$operationStatusLink = ""

if($operationName -eq $export){
# Export database.
$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
Assert-NotNull $exportResponse
$operationStatusLink = $exportResponse.OperationStatusLink
}

if($operationName -eq $import){
$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
Assert-NotNull $importResponse
$operationStatusLink = $importResponse.OperationStatusLink
}

Assert-NotNull $operationStatusLink

#Get status
$statusInProgress = "InProgress"
$statusSucceeded = "Succeeded"
$status = "InProgress"

if($succeeded -eq $true){
Write-Output "Getting Status"
while($status -eq $statusInProgress){
$statusResponse = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $operationStatusLink
Write-Output "Import Export Status Message:" + $statusResponse.StatusMessage
$status = $statusResponse.Status
}
Assert-AreEqual $status $statusSucceeded
Write-Output "ImportExportStatus:" + $status
}
}
finally
{
Remove-ResourceGroupForTest $rg
}
}
Loading