mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-17 02:51:45 -05:00
* fixing the order of setting backup properties so that they are correctly propogated to actual action * Adding a backup type specific test and enabling 'few' old ones which seem to work consistentenly * Adding finally block, close connection and name change as per PR comments
This commit is contained in:
@@ -172,11 +172,12 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Execute()
|
public override void Execute()
|
||||||
{
|
{
|
||||||
|
// set the operation properties before using them to create backup obejct
|
||||||
|
this.SetBackupProps();
|
||||||
this.backup = new Backup();
|
this.backup = new Backup();
|
||||||
this.backup.Database = this.backupInfo.DatabaseName;
|
this.backup.Database = this.backupInfo.DatabaseName;
|
||||||
this.backup.Action = this.backupActionType;
|
this.backup.Action = this.backupActionType;
|
||||||
this.backup.Incremental = this.isBackupIncremental;
|
this.backup.Incremental = this.isBackupIncremental;
|
||||||
this.SetBackupProps();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -71,12 +71,15 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create simple backup test
|
/// Create simple backup test
|
||||||
/// </summary>
|
/// </summary>
|
||||||
//[Fact]
|
[Fact]
|
||||||
public void CreateBackupTest()
|
public void CreateBackupTest()
|
||||||
{
|
{
|
||||||
DisasterRecoveryService service = new DisasterRecoveryService();
|
DisasterRecoveryService service = new DisasterRecoveryService();
|
||||||
string databaseName = "testbackup_" + new Random().Next(10000000, 99999999);
|
string databaseName = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);
|
||||||
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
||||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
||||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
||||||
@@ -93,20 +96,29 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
service.PerformBackup(backupOperation);
|
service.PerformBackup(backupOperation);
|
||||||
|
|
||||||
VerifyAndCleanBackup(backupPath);
|
VerifyAndCleanBackup(backupPath);
|
||||||
|
sqlConn.Close();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
testDb.Cleanup();
|
testDb.Cleanup();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//[Fact]
|
[Fact]
|
||||||
public void ScriptBackupTest()
|
public void ScriptBackupTest()
|
||||||
{
|
{
|
||||||
DisasterRecoveryService service = new DisasterRecoveryService();
|
DisasterRecoveryService service = new DisasterRecoveryService();
|
||||||
string databaseName = "testbackup_" + new Random().Next(10000000, 99999999);
|
string databaseName = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);
|
||||||
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
||||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
||||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
||||||
string backupPath = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);
|
string backupPath = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);
|
||||||
|
|
||||||
|
|
||||||
BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
|
BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
|
||||||
BackupType.Full,
|
BackupType.Full,
|
||||||
new List<string>() { backupPath },
|
new List<string>() { backupPath },
|
||||||
@@ -122,19 +134,26 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
testDb.RunQuery(script);
|
testDb.RunQuery(script);
|
||||||
|
|
||||||
VerifyAndCleanBackup(backupPath);
|
VerifyAndCleanBackup(backupPath);
|
||||||
|
sqlConn.Close();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
testDb.Cleanup();
|
testDb.Cleanup();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test creating backup with advanced options set.
|
/// Test creating backup with advanced options set.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
//[Fact]
|
[Fact]
|
||||||
public void CreateBackupWithAdvancedOptionsTest()
|
public void CreateBackupWithAdvancedOptionsTest()
|
||||||
{
|
{
|
||||||
DisasterRecoveryService service = new DisasterRecoveryService();
|
DisasterRecoveryService service = new DisasterRecoveryService();
|
||||||
string databaseName = "testbackup_" + new Random().Next(10000000, 99999999);
|
string databaseName = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);
|
||||||
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
||||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
||||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
||||||
@@ -173,20 +192,28 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
Console.WriteLine("Remove certificate and master key..");
|
Console.WriteLine("Remove certificate and master key..");
|
||||||
testDb.RunQuery(cleanupCertificateQuery);
|
testDb.RunQuery(cleanupCertificateQuery);
|
||||||
|
|
||||||
|
sqlConn.Close();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
// Clean up the database
|
// Clean up the database
|
||||||
Console.WriteLine("Clean up database..");
|
Console.WriteLine("Clean up database..");
|
||||||
testDb.Cleanup();
|
testDb.Cleanup();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test creating backup with advanced options set.
|
/// Test creating backup with advanced options set.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
//[Fact]
|
[Fact]
|
||||||
public void ScriptBackupWithAdvancedOptionsTest()
|
public void ScriptBackupWithAdvancedOptionsTest()
|
||||||
{
|
{
|
||||||
DisasterRecoveryService service = new DisasterRecoveryService();
|
DisasterRecoveryService service = new DisasterRecoveryService();
|
||||||
string databaseName = "testbackup_" + new Random().Next(10000000, 99999999);
|
string databaseName = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);
|
||||||
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
||||||
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
||||||
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
||||||
@@ -226,11 +253,58 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
// Delete certificate and master key
|
// Delete certificate and master key
|
||||||
Console.WriteLine("Remove certificate and master key..");
|
Console.WriteLine("Remove certificate and master key..");
|
||||||
testDb.RunQuery(cleanupCertificateQuery);
|
testDb.RunQuery(cleanupCertificateQuery);
|
||||||
|
sqlConn.Close();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
// Clean up the database
|
// Clean up the database
|
||||||
Console.WriteLine("Clean up database..");
|
Console.WriteLine("Clean up database..");
|
||||||
testDb.Cleanup();
|
testDb.Cleanup();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test the correct script generation for different backup action types
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void ScriptBackupWithDifferentActionTypesTest()
|
||||||
|
{
|
||||||
|
string databaseName = "SqlToolsService_TestBackup_" + new Random().Next(10000000, 99999999);
|
||||||
|
SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Create Full backup script
|
||||||
|
string script = GenerateScriptForBackupType(BackupType.Full, databaseName);
|
||||||
|
|
||||||
|
// Validate Full backup script
|
||||||
|
Assert.Contains("BACKUP DATABASE", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.DoesNotContain("BACKUP LOG", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.DoesNotContain("DIFFERENTIAL", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
// Create log backup script
|
||||||
|
script = GenerateScriptForBackupType(BackupType.TransactionLog, databaseName);
|
||||||
|
|
||||||
|
// Validate Log backup script
|
||||||
|
Assert.Contains("BACKUP LOG", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.DoesNotContain("BACKUP DATABASE", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.DoesNotContain("DIFFERENTIAL", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
// Create differential backup script
|
||||||
|
script = GenerateScriptForBackupType(BackupType.Differential, databaseName);
|
||||||
|
|
||||||
|
// Validate differential backup script
|
||||||
|
Assert.Contains("BACKUP DATABASE", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.DoesNotContain("BACKUP LOG", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
Assert.Contains("WITH DIFFERENTIAL", script, StringComparison.OrdinalIgnoreCase);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// Clean up the database
|
||||||
|
Console.WriteLine("Clean up database..");
|
||||||
|
testDb.Cleanup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//[Fact]
|
//[Fact]
|
||||||
public async void BackupFileBrowserTest()
|
public async void BackupFileBrowserTest()
|
||||||
@@ -393,6 +467,37 @@ CREATE CERTIFICATE {1} WITH SUBJECT = 'Backup Encryption Certificate'; ";
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GenerateScriptForBackupType(BackupType backupType, string databaseName)
|
||||||
|
{
|
||||||
|
DisasterRecoveryService service = new DisasterRecoveryService();
|
||||||
|
var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
|
||||||
|
DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
|
||||||
|
SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
|
||||||
|
string backupPath = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);
|
||||||
|
|
||||||
|
BackupInfo backupInfoLog = CreateDefaultBackupInfo(databaseName,
|
||||||
|
backupType,
|
||||||
|
new List<string>() { backupPath },
|
||||||
|
new Dictionary<string, int>() { { backupPath, (int)DeviceType.File } });
|
||||||
|
backupInfoLog.FormatMedia = true;
|
||||||
|
backupInfoLog.SkipTapeHeader = true;
|
||||||
|
backupInfoLog.Initialize = true;
|
||||||
|
backupInfoLog.MediaName = "backup test media";
|
||||||
|
backupInfoLog.MediaDescription = "backup test";
|
||||||
|
BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfoLog, helper.DataContainer, sqlConn);
|
||||||
|
|
||||||
|
// Generate Script
|
||||||
|
Console.WriteLine("Generate script for backup operation..");
|
||||||
|
service.ScriptBackup(backupOperation);
|
||||||
|
string script = backupOperation.ScriptContent;
|
||||||
|
|
||||||
|
// There shouldnt be any backup file created
|
||||||
|
Assert.True(!File.Exists(backupPath), "Backup file is not expected to be created");
|
||||||
|
|
||||||
|
sqlConn.Close();
|
||||||
|
return script;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user