Skip to content
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

[Synapse] - refine restore sqlpool #13888

Merged
merged 6 commits into from
Jan 8, 2021
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
45 changes: 45 additions & 0 deletions src/Synapse/Synapse.Test/ScenarioTests/SqlPoolBackupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Azure.ServiceManagement.Common.Models;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace Microsoft.Azure.Commands.Synapse.Test.ScenarioTests
{
public class SqlPoolBackupTests: SynapseTestBase
{
public XunitTracingInterceptor _logger;

public SqlPoolBackupTests(Xunit.Abstractions.ITestOutputHelper output)
{
_logger = new XunitTracingInterceptor(output);
XunitTracingInterceptor.AddToContext(_logger);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSqlPoolRestorePoint(){
NewInstance.RunPsTest(
_logger,
"Test-SqlPoolRestorePoint");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestRestoreFromRestorePoint()
{
NewInstance.RunPsTest(
_logger,
"Test-RestoreFromRestorePoint");
}

[Fact(Skip = "Currently the test case cannot pass due to some backend issues.")]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestRestoreFromBackup(){
NewInstance.RunPsTest(
_logger,
"Test-RestoreFromBackup");
}
}
}
219 changes: 219 additions & 0 deletions src/Synapse/Synapse.Test/ScenarioTests/SqlPoolBackupTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# ----------------------------------------------------------------------------------
#
# 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 for getting restore points from SQL pools
#>
function Test-SqlPoolRestorePoint
{
# Setup
$testSuffix = getAssetName
Create-SqlPoolBackupTestEnvironment $testSuffix
$params = Get-SqlPoolBackupTestEnvironmentParameters $testSuffix

try
{
# Get restore points
$restorePoints = Get-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName

# Create a new restore point
$restorePointLabelToSet = 'ContosoRestorePoint'
New-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName -RestorePointLabel $restorePointLabelToSet

# Get restore points and compare with what we sent
$restorePoints = Get-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName

Assert-AreEqual 1 $restorePoints.Count
Assert-AreEqual $restorePointLabelToSet $restorePoints[0].RestorePointLabel
Assert-AreEqual 'DISCRETE' $restorePoints[0].RestorePointType
Assert-Null $restorePoints[0].EarliestRestoreDate
Assert-NotNull $restorePoints[0].RestorePointCreationDate

# Remove restore point
Remove-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName `
-RestorePointCreationDate $restorePoints[0].RestorePointCreationDate -Force

# Get restore points
$restorePoints = Get-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName

# Check the SQL pool doesn't have any restore point
Assert-AreEqual 0 $restorePoints.Count

# Remove the system restore point
$restorePointCannotBeDeletedErrorMessage = "Cannot delete system restore point"
Assert-ThrowsContains -script { Remove-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName `
-RestorePointCreationDate $restorePoints[0].RestorePointCreationDate -Force } -message $restorePointCannotBeDeletedErrorMessage

# piping scenario
$pool = Get-AzSynapseSqlPool -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName

# Create a restore point
$pool | New-AzSynapseSqlPoolRestorePoint -RestorePointLabel $restorePointLabelToSet

# Get restore points and compare with what we sent
$restorePoints = $pool | Get-AzSynapseSqlPoolRestorePoint
Assert-AreEqual 1 $restorePoints.Count
Assert-AreEqual $restorePointLabelToSet $restorePoints[0].RestorePointLabel
Assert-AreEqual 'DISCRETE' $restorePoints[0].RestorePointType
Assert-Null $restorePoints[0].EarliestRestoreDate
Assert-NotNull $restorePoints[0].RestorePointCreationDate

# Remove restore point
$pool | Remove-AzSynapseSqlPoolRestorePoint -RestorePointCreationDate $restorePoints[0].RestorePointCreationDate -Force

# Get restore points
$restorePoints = $pool | Get-AzSynapseSqlPoolRestorePoint

# Check the SQL pool only has one system restore point
Assert-AreEqual 0 $restorePoints.Count

# Create a restore point
$pool | New-AzSynapseSqlPoolRestorePoint -RestorePointLabel $restorePointLabelToSet

# Get restore points and compare with what we sent
$restorePoints = $pool | Get-AzSynapseSqlPoolRestorePoint
Assert-AreEqual 1 $restorePoints.Count
Assert-AreEqual $restorePointLabelToSet $restorePoints[0].RestorePointLabel
Assert-AreEqual 'DISCRETE' $restorePoints[0].RestorePointType
Assert-Null $restorePoints[0].EarliestRestoreDate
Assert-NotNull $restorePoints[0].RestorePointCreationDate

$restorePoints[0] | Remove-AzSynapseSqlPoolRestorePoint -Force

# Get restore points
$restorePoints = $pool | Get-AzSynapseSqlPoolRestorePoint

# Check the SQL pool only has one system restore point
Assert-AreEqual 0 $restorePoints.Count
}
finally
{
# Cleanup
Remove-SqlPoolBackupTestEnvironment $testSuffix
}
}

<#
.SYNOPSIS
Tests for restoring from restore point
#>
function Test-RestoreFromRestorePoint
{
# Setup
$testSuffix = getAssetName
Create-SqlPoolBackupTestEnvironment $testSuffix
$params = Get-SqlPoolBackupTestEnvironmentParameters $testSuffix

try
{
# Create a new restore point
$restorePointLabelToSet = 'ContosoRestorePoint'
$restorePoint = New-AzSynapseSqlPoolRestorePoint -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName -RestorePointLabel $restorePointLabelToSet

# Transform Synapse SQL pool resource ID to SQL database ID because
# currently the command only accepts the SQL databse ID
$pool = Get-AzSynapseSqlPool -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName
$databaseId = $pool.Id -replace "Microsoft.Synapse", "Microsoft.Sql" `
-replace "workspaces", "servers" `
-replace "sqlPools", "databases"

# Restore to same workspace with source SQL database
$restoredPool = Restore-AzSynapseSqlPool -FromRestorePoint -RestorePoint $restorePoint.RestorePointCreationDate -TargetSqlPoolName $params.restoredSqlPoolName -ResourceGroupName $params.rgname `
-WorkspaceName $params.workspaceName -ResourceId $databaseId -PerformanceLevel $params.perfLevel

Assert-AreEqual $params.rgname $restoredPool.ResourceGroupName
Assert-AreEqual $params.workspaceName $restoredPool.WorkspaceName
Assert-AreEqual $params.restoredSqlPoolName $restoredPool.SqlPoolName
}
finally
{
# Cleanup
Remove-SqlPoolBackupTestEnvironment $testSuffix
}
}

<#
.SYNOPSIS
Tests for restoring from backup
#>
function Test-RestoreFromBackup
{
# Setup
$testSuffix = getAssetName
Create-SqlPoolBackupTestEnvironment $testSuffix
$params = Get-SqlPoolBackupTestEnvironmentParameters $testSuffix

try
{
# Transform Synapse SQL pool resource ID to SQL database ID because
# currently the command only accepts the SQL databse ID
$pool = Get-AzSynapseSqlPool -ResourceGroupName $params.rgname -WorkspaceName $params.workspaceName -Name $params.sqlPoolName
$databaseId = $pool.Id -replace "Microsoft.Synapse", "Microsoft.Sql" `
-replace "workspaces", "servers" `
-replace "sqlPools", "databases"

# Restore to same workspace with source SQL database
$restoredPool = Restore-AzSynapseSqlPool -FromBackup -TargetSqlPoolName $params.restoredSqlPoolName -ResourceGroupName $params.rgname `
-WorkspaceName $params.workspaceName -ResourceId $databaseId

Assert-AreEqual $params.rgname $restoredPool.ResourceGroupName
Assert-AreEqual $params.workspaceName $restoredPool.WorkspaceName
Assert-AreEqual $params.restoredSqlPoolName $restoredPool.SqlPoolName
}
finally
{
# Cleanup
Remove-SqlPoolBackupTestEnvironment $testSuffix
}
}

<#
.SYNOPSIS
Creates the test environment needed to perform the tests
#>
function Create-SqlPoolBackupTestEnvironment ($testSuffix)
{
$params = Get-SqlPoolBackupTestEnvironmentParameters $testSuffix
Create-TestEnvironmentWithParams $params $params.location
}

<#
.SYNOPSIS
Gets the values of the parameters used at the tests
#>
function Get-SqlPoolBackupTestEnvironmentParameters ($testSuffix)
{
return @{ rgname = "sql-bk-cmdlet-test-rg" +$testSuffix;
workspaceName = "sqlbkws" +$testSuffix;
sqlPoolName = "sqlbkpool" + $testSuffix;
storageAccountName = "sqlbkstorage" + $testSuffix;
fileSystemName = "sqlbkcmdletfs" + $testSuffix;
loginName = "testlogin";
pwd = "testp@ssMakingIt1007Longer";
perfLevel = 'DW200c';
location = "westcentralus";
restoredSqlPoolName = "dwrestore" + $testSuffix;
}
}

<#
.SYNOPSIS
Removes the test environment that was needed to perform the tests
#>
function Remove-SqlPoolBackupTestEnvironment ($testSuffix)
{
$params = Get-SqlPoolBackupTestEnvironmentParameters $testSuffix
Remove-AzResourceGroup -Name $params.rgname -Force
}
41 changes: 2 additions & 39 deletions src/Synapse/Synapse.Test/ScenarioTests/SqlPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,18 @@ public SqlPoolTests(Xunit.Abstractions.ITestOutputHelper output)
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSynapseSqlPool()
{
string testResourceGroupName = SynapseTestBase.TestResourceGroupName;
if (string.IsNullOrEmpty(testResourceGroupName))
{
testResourceGroupName = nameof(TestResourceGroupName);
}

string testWorkspaceName = SynapseTestBase.TestWorkspaceName;
if (string.IsNullOrEmpty(testWorkspaceName))
{
testWorkspaceName = nameof(TestWorkspaceName);
}

SynapseTestBase.NewInstance.RunPsTest(
_logger,
string.Format(
"Test-SynapseSqlPool -resourceGroupName '{0}' -workspaceName '{1}'",
testResourceGroupName,
testWorkspaceName));
"Test-SynapseSqlPool");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSynapseSqlPoolSecurity()
{
string testResourceGroupName = SynapseTestBase.TestResourceGroupName;
if (string.IsNullOrEmpty(testResourceGroupName))
{
testResourceGroupName = nameof(TestResourceGroupName);
}

string testWorkspaceName = SynapseTestBase.TestWorkspaceName;
if (string.IsNullOrEmpty(testWorkspaceName))
{
testWorkspaceName = nameof(TestWorkspaceName);
}

string testSqlPoolName = SynapseTestBase.TestSqlPoolName;
if (string.IsNullOrEmpty(testWorkspaceName))
{
testSqlPoolName = nameof(TestSqlPoolName);
}

SynapseTestBase.NewInstance.RunPsTest(
_logger,
string.Format(
"Test-SynapseSqlPool-Security -resourceGroupName '{0}' -workspaceName '{1}' -sqlPoolName '{2}'",
testResourceGroupName,
testWorkspaceName,
testSqlPoolName));
"Test-SynapseSqlPool-Security");
}
}
}
Loading