Skip to content

Failover database and elastic pool new powershell cmdlets #9750

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 9 commits into from
Aug 7, 2019
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
2 changes: 1 addition & 1 deletion src/Sql/Sql.LegacySdk/Sql.LegacySdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" PrivateAssets="all"/>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" PrivateAssets="all" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/FailoverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ----------------------------------------------------------------------------------
//
// 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.WindowsAzure.Commands.ScenarioTest;
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
{
public class FailoverTests : SqlTestsBase
{
public FailoverTests(ITestOutputHelper output) : base(output)
{
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverDatabase()
{
RunPowerShellTest("Test-FailoverDatabase");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverDatabasePassThru()
{
RunPowerShellTest("Test-FailoverDatabasePassThru");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverDatabaseWithDatabasePiping()
{
RunPowerShellTest("Test-FailoverDatabaseWithDatabasePiping");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverDatabaseWithServerPiping()
{
RunPowerShellTest("Test-FailoverDatabaseWithServerPiping");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverElasticPool()
{
RunPowerShellTest("Test-FailoverElasticPool");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverElasticPoolPassThru()
{
RunPowerShellTest("Test-FailoverElasticPoolPassThru");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverElasticPoolWithPoolPiping()
{
RunPowerShellTest("Test-FailoverElasticPoolWithPoolPiping");
}
}
}
235 changes: 235 additions & 0 deletions src/Sql/Sql.Test/ScenarioTests/FailoverTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
# ----------------------------------------------------------------------------------
#
# 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 database failover. Issues command with both -AsJob and without extra parameters

Also tests failing over twice to verify first failover went through which causes second failover to hit a recent failover
exception (aka too many failovers in the given period of time).
#>
function Test-FailoverDatabase
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create database
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName

# Failover database with -AsJob
$job = Invoke-AzSqlDatabaseFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -AsJob
$job | Wait-Job

# Failover database with no extra switch parameters. Tests for exception to verify the first failover went through correctly as this second
# failover will cause a recent failover exception
try {
Invoke-AzSqlDatabaseFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName
} catch {
$ErrorMessage = $_.Exception.Message
Assert-AreEqual True $ErrorMessage.Contains("There was a recent failover on the database or pool")
}
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests database failover with passthru
#>
function Test-FailoverDatabasePassThru
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create database
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName

# Failover database with -PassThru
$output = Invoke-AzSqlDatabaseFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -PassThru
Assert-True { $output }
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests database failover using piping for database.
#>
function Test-FailoverDatabaseWithDatabasePiping
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create database
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName

# Failover database using piping for database
Get-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName | Invoke-AzSqlDatabaseFailover
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests database failover using piping for server.
#>
function Test-FailoverDatabaseWithServerPiping
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create database
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName

# Failover database using piping for server
Get-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName | Invoke-AzSqlDatabaseFailover -DatabaseName $databaseName
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests elastic pool failover. Issues command with both -AsJob and without extra parameters

Also tests failing over twice to verify first failover went through which causes second failover to hit a recent failover
exception (aka too many failovers in the given period of time).
#>
function Test-FailoverElasticPool
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create elastic pool
$poolName = Get-ElasticPoolName
New-AzSqlElasticPool -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -ElasticPoolName $poolName

# Create database in elastic pool
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -ElasticPoolName $poolName

# Failover elastic pool with -AsJob
$job = Invoke-AzSqlElasticPoolFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -ElasticPoolName $poolName -AsJob
$job | Wait-Job

# Failover elastic pool with no extra switch parameters. Tests for exception to verify the first failover went through correctly as this second
# failover will cause a recent failover exception
try {
Invoke-AzSqlElasticPoolFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -ElasticPoolName $poolName
} catch {
$ErrorMessage = $_.Exception.Message
Assert-AreEqual True $ErrorMessage.Contains("There was a recent failover on the elastic pool")
}
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests elastic pool failover with passthru
#>
function Test-FailoverElasticPoolPassThru
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create elastic pool
$poolName = Get-ElasticPoolName
New-AzSqlElasticPool -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -ElasticPoolName $poolName

# Create database in elastic pool
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -ElasticPoolName $poolName

# Failover elastic pool with -PassThru
$output = Invoke-AzSqlElasticPoolFailover -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -ElasticPoolName $poolName -PassThru
Assert-True { $output }
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests elastic pool failover using piping for pool.
#>
function Test-FailoverElasticPoolWithPoolPiping
{
# Setup
$location = Get-Location "Microsoft.Sql" "operations" "Southeast Asia"
$rg = Create-ResourceGroupForTest $location
$server = Create-ServerForTest $rg $location

try
{
# Create elastic pool
$poolName = Get-ElasticPoolName
New-AzSqlElasticPool -ServerName $server.ServerName -ResourceGroupName $rg.ResourceGroupName -ElasticPoolName $poolName

# Create database in elastic pool
$databaseName = Get-DatabaseName
New-AzSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName -ElasticPoolName $poolName

# Failover elastic pool using piping for pool
Get-AzSqlElasticPool -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -ElasticPoolName $poolName | Invoke-AzSqlElasticPoolFailover
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Sql/Sql/Az.Sql.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ CmdletsToExport = 'Get-AzSqlDatabaseTransparentDataEncryption',
'Remove-AzSqlServerAudit', 'Remove-AzSqlDatabaseAudit',
'Get-AzSqlInstancePool', 'Set-AzSqlInstancePool',
'New-AzSqlInstancePool', 'Remove-AzSqlInstancePool',
'Get-AzSqlInstancePoolUsage'
'Get-AzSqlInstancePoolUsage',
'Invoke-AzSqlDatabaseFailover',
'Invoke-AzSqlElasticPoolFailover'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
1 change: 1 addition & 0 deletions src/Sql/Sql/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Add Azure Sql Instance pool usages cmdlets
* Update Azure Sql Managed instance cmdlets to support instance pools
* Fixed miscellaneous typos across module
* Add failover database and elastic pool new cmdlets.
* Add optional resource group parameter to Get-DatabaseLongTermRetentionBackup and Remove-DatabaseLongTermRetentionBackup cmdlets

## Version 1.13.1
Expand Down
Loading