From 3bf562acf03542236207c4d84c2da78b65595fd2 Mon Sep 17 00:00:00 2001 From: Kate Shin Date: Fri, 2 Jun 2017 18:11:35 -0700 Subject: [PATCH] Create backup service tests (#368) * Add backup test * Add backup service tests --- .../DisasterRecoveryService.cs | 10 +- .../DisasterRecovery/BackupTests.cs | 97 +++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/DisasterRecovery/BackupTests.cs diff --git a/src/Microsoft.SqlTools.ServiceLayer/DisasterRecovery/DisasterRecoveryService.cs b/src/Microsoft.SqlTools.ServiceLayer/DisasterRecovery/DisasterRecoveryService.cs index 384805a9..555fcbba 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/DisasterRecovery/DisasterRecoveryService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/DisasterRecovery/DisasterRecoveryService.cs @@ -145,22 +145,22 @@ namespace Microsoft.SqlTools.ServiceLayer.DisasterRecovery return null; } - private void InitializeBackup(CDataContainer dataContainer, SqlConnection sqlConnection) + internal void InitializeBackup(CDataContainer dataContainer, SqlConnection sqlConnection) { this.backupUtilities.Initialize(dataContainer, sqlConnection); } - private void SetBackupInput(BackupInfo input) + internal void SetBackupInput(BackupInfo input) { this.backupUtilities.SetBackupInput(input); } - private void PerformBackup() + internal void PerformBackup() { this.backupUtilities.PerformBackup(); } - - private BackupConfigInfo GetBackupConfigInfo(string databaseName) + + internal BackupConfigInfo GetBackupConfigInfo(string databaseName) { return this.backupUtilities.GetBackupConfigInfo(databaseName); } diff --git a/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/DisasterRecovery/BackupTests.cs b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/DisasterRecovery/BackupTests.cs new file mode 100644 index 00000000..bcc11910 --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.IntegrationTests/DisasterRecovery/BackupTests.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.IO; +using System.Threading.Tasks; +using Microsoft.SqlServer.Management.Smo; +using Microsoft.SqlTools.Hosting.Protocol; +using Microsoft.SqlTools.ServiceLayer.Admin; +using Microsoft.SqlTools.ServiceLayer.Admin.Contracts; +using Microsoft.SqlTools.ServiceLayer.DisasterRecovery; +using Microsoft.SqlTools.ServiceLayer.DisasterRecovery.Contracts; +using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility; +using Microsoft.SqlTools.ServiceLayer.Test.Common; +using Moq; +using Xunit; + +namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.DisasterRecovery +{ + public class BackupTests + { + /// + /// Get backup configuration info + /// + [Fact] + public async void GetBackupConfigInfoTest() + { + string databaseName = "testbackup_" + new Random().Next(10000000, 99999999); + SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName); + var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName); + + var requestContext = new Mock>(); + requestContext.Setup(x => x.SendResult(It.IsAny())) + .Returns(Task.FromResult(new object())); + + var dbParams = new DefaultDatabaseInfoParams + { + OwnerUri = liveConnection.ConnectionInfo.OwnerUri + }; + + await DisasterRecoveryService.HandleBackupConfigInfoRequest(dbParams, requestContext.Object); + + requestContext.Verify(x => x.SendResult(It.Is + (p => p.BackupConfigInfo.RecoveryModel != string.Empty + && p.BackupConfigInfo.DefaultBackupFolder != string.Empty + && p.BackupConfigInfo.DatabaseInfo != null))); + + testDb.Cleanup(); + } + + [Fact] + public void CreateBackupTest() + { + string databaseName = "testbackup_" + new Random().Next(10000000, 99999999); + SqlTestDb testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName); + var liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName); + + // Initialize backup service + DatabaseTaskHelper helper = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true); + SqlConnection sqlConn = DisasterRecoveryService.GetSqlConnection(liveConnection.ConnectionInfo); + DisasterRecoveryService.Instance.InitializeBackup(helper.DataContainer, sqlConn); + + // Get default backup path + BackupConfigInfo backupConfigInfo = DisasterRecoveryService.Instance.GetBackupConfigInfo(sqlConn.Database); + string backupPath = backupConfigInfo.DefaultBackupFolder + "\\" + databaseName + ".bak"; + + var backupInfo = new BackupInfo(); + backupInfo.BackupComponent = (int)BackupComponent.Database; + backupInfo.BackupDeviceType = (int)BackupDeviceType.Disk; + backupInfo.BackupPathDevices = new Dictionary() { { backupPath, (int)DeviceType.File } }; + backupInfo.BackupPathList = new List(new string[] { backupPath }); + backupInfo.BackupsetName = "default_backup"; + backupInfo.BackupType = (int)BackupType.Full; + backupInfo.DatabaseName = databaseName; + backupInfo.SelectedFileGroup = null; + backupInfo.SelectedFiles = ""; + + var backupParams = new BackupParams + { + OwnerUri = liveConnection.ConnectionInfo.OwnerUri, + BackupInfo = backupInfo + }; + + // Backup the database + DisasterRecoveryService.Instance.SetBackupInput(backupParams.BackupInfo); + DisasterRecoveryService.Instance.PerformBackup(); + + // Remove the backup file + if (File.Exists(backupPath)) + { + File.Delete(backupPath); + } + + // Clean up the database + testDb.Cleanup(); + } + } +} \ No newline at end of file