Skip to content
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
@@ -0,0 +1,65 @@
// ----------------------------------------------------------------------------------
//
// 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 RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit;
using Xunit.Abstractions;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.Sql.Test.ScenarioTests
{
public class ManagedInstanceFailoverScenarioTests : SqlTestsBase
{
protected override void SetupManagementClients(RestTestFramework.MockContext context)
{
var sqlClient = GetSqlClient(context);
var newResourcesClient = GetResourcesClient(context);
var networkClient = GetNetworkClient(context);
Helper.SetupSomeOfManagementClients(sqlClient, newResourcesClient, networkClient);
}

public ManagedInstanceFailoverScenarioTests(ITestOutputHelper output) : base(output)
{
}

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

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

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

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestFailoverManagedInstanceReadableSecondary()
{
RunPowerShellTest("Test-FailoverManagedInstanceReadableSecondary");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# ----------------------------------------------------------------------------------
#
# 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.
# ----------------------------------------------------------------------------------

# Location to use for provisioning test managed instances
$instanceLocation = "westcentralus"

<#
.SYNOPSIS
Tests Managed Instance failover.
#>
function Test-FailoverManagedInstance
{
try
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

$job = Invoke-AzSqlInstanceFailover -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance.ManagedInstanceName -AsJob
$job | Wait-Job

try
{
Invoke-AzSqlInstanceFailover -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance.ManagedInstanceName -AsJob
}
catch
{
$ErrorMessage = $_.Exception.Message
Assert-AreEqual True $ErrorMessage.Contains("There was a recent failover on the managed instance")
}
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Managed Instance failover with passthru.
#>
function Test-FailoverManagedInstancePassThru
{
try
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

$output = Invoke-AzSqlInstanceFailover -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance.ManagedInstanceName -PassThru
Assert-True { $output }
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Managed Instance failover using piping.
#>
function Test-FailoverManagedInstancePiping
{
try
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

Get-AzSqlInstance -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance.ManagedInstanceName | Invoke-AzSqlInstanceFailover
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

<#
.SYNOPSIS
Tests Managed Instance failover secondary readable replica.

Managed instance has 3 replicas and intiating failover does not specify which one to failover, so we run test
with -PassThru and assert on the output.
#>
function Test-FailoverManagedInstanceReadableSecondary
{
try
{
# Setup
$rg = Create-ResourceGroupForTest
$vnetName = "cl_initial"
$subnetName = "CooL"

# Setup VNET
$virtualNetwork1 = CreateAndGetVirtualNetworkForManagedInstance $vnetName $subnetName $rg.Location
$subnetId = $virtualNetwork1.Subnets.where({ $_.Name -eq $subnetName })[0].Id

# Initiate sync create of managed instance.
$managedInstance = Create-ManagedInstanceForTest $rg $subnetId

# Change instance edition to BusinessCritical to get secondary replicas to failover
$credentials = Get-ServerCredential
$edition = "BusinessCritical"

$managedInstance1 = Set-AzSqlInstance -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance.ManagedInstanceName `
-Edition $edition -Force

$output = Invoke-AzSqlInstanceFailover -ResourceGroupName $rg.ResourceGroupName -Name $managedInstance1.ManagedInstanceName -ReadableSecondary -PassThru
Assert-True { $output }
}
finally
{
Remove-ResourceGroupForTest $rg
}
}

Loading