Exemple #1
0
        private async Task <RestorePlanResponse> VerifyRestore(string backupFileName, bool canRestore, bool execute = false, string targetDatabase = null)
        {
            string filePath = GetBackupFilePath(backupFileName);

            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);

                RestoreDatabaseHelper service = new RestoreDatabaseHelper();
                var request = new RestoreParams
                {
                    BackupFilePath = filePath,
                    DatabaseName   = targetDatabase,
                    OwnerUri       = queryTempFile.FilePath
                };

                var restoreDataObject = service.CreateRestoreDatabaseTaskDataObject(request);
                var response          = service.CreateRestorePlanResponse(restoreDataObject);

                Assert.NotNull(response);
                Assert.Equal(response.CanRestore, canRestore);
                if (canRestore)
                {
                    Assert.True(response.DbFiles.Any());
                    if (string.IsNullOrEmpty(targetDatabase))
                    {
                        targetDatabase = response.DatabaseName;
                    }
                    Assert.Equal(response.DatabaseName, targetDatabase);

                    if (execute)
                    {
                        await DropDatabase(targetDatabase);

                        Thread.Sleep(2000);
                        request.RelocateDbFiles = response.RelocateFilesNeeded;
                        service.ExecuteRestore(restoreDataObject);
                        Assert.True(restoreDataObject.Server.Databases.Contains(targetDatabase));
                        await DropDatabase(targetDatabase);
                    }
                }

                return(response);
            }
        }
Exemple #2
0
        private async Task <RestorePlanResponse> VerifyRestore(
            string[] backupFileNames             = null,
            string sourceDbName                  = null,
            bool canRestore                      = true,
            TaskExecutionModeFlag executionMode  = TaskExecutionModeFlag.None,
            string targetDatabase                = null,
            string[] selectedBackupSets          = null,
            Dictionary <string, object> options  = null,
            Func <Database, bool> verifyDatabase = null,
            bool shouldFail                      = false)
        {
            string backUpFilePath = string.Empty;

            if (backupFileNames != null)
            {
                var filePaths = backupFileNames.Select(x => GetBackupFilePath(x));
                backUpFilePath = filePaths.Aggregate((current, next) => current + " ," + next);
            }

            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);

                RestoreDatabaseHelper service = new RestoreDatabaseHelper();

                // If source database is sepecified verfiy it's part of source db list
                if (!string.IsNullOrEmpty(sourceDbName))
                {
                    RestoreConfigInfoResponse configInfoResponse = service.CreateConfigInfoResponse(new RestoreConfigInfoRequestParams
                    {
                        OwnerUri = queryTempFile.FilePath
                    });
                    IEnumerable <string> dbNames = configInfoResponse.ConfigInfo[RestoreOptionsHelper.SourceDatabaseNamesWithBackupSets] as IEnumerable <string>;
                    Assert.True(dbNames.Any(x => x == sourceDbName));
                }
                var request = new RestoreParams
                {
                    BackupFilePaths    = backUpFilePath,
                    TargetDatabaseName = targetDatabase,
                    OwnerUri           = queryTempFile.FilePath,
                    SelectedBackupSets = selectedBackupSets,
                    SourceDatabaseName = sourceDbName
                };
                request.Options[RestoreOptionsHelper.ReadHeaderFromMedia] = backupFileNames != null;

                if (options != null)
                {
                    foreach (var item in options)
                    {
                        if (!request.Options.ContainsKey(item.Key))
                        {
                            request.Options.Add(item.Key, item.Value);
                        }
                    }
                }

                var restoreDataObject = service.CreateRestoreDatabaseTaskDataObject(request);
                restoreDataObject.ConnectionInfo = connectionResult.ConnectionInfo;
                var response = service.CreateRestorePlanResponse(restoreDataObject);

                Assert.NotNull(response);
                Assert.False(string.IsNullOrWhiteSpace(response.SessionId));
                Assert.Equal(response.CanRestore, canRestore);
                if (canRestore)
                {
                    Assert.True(response.DbFiles.Any());
                    if (string.IsNullOrEmpty(targetDatabase))
                    {
                        targetDatabase = response.DatabaseName;
                    }
                    Assert.Equal(response.DatabaseName, targetDatabase);
                    Assert.NotNull(response.PlanDetails);
                    Assert.True(response.PlanDetails.Any());
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.BackupTailLog]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.TailLogBackupFile]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DataFileFolder]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.LogFileFolder]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.StandbyFile]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.StandbyFile]);

                    if (executionMode != TaskExecutionModeFlag.None)
                    {
                        try
                        {
                            request.SessionId = response.SessionId;
                            restoreDataObject = service.CreateRestoreDatabaseTaskDataObject(request);
                            Assert.Equal(response.SessionId, restoreDataObject.SessionId);
                            request.RelocateDbFiles = !restoreDataObject.DbFilesLocationAreValid();
                            restoreDataObject.Execute((TaskExecutionMode)Enum.Parse(typeof(TaskExecutionMode), executionMode.ToString()));

                            if (executionMode.HasFlag(TaskExecutionModeFlag.Execute))
                            {
                                Assert.True(restoreDataObject.Server.Databases.Contains(targetDatabase));

                                if (verifyDatabase != null)
                                {
                                    Assert.True(verifyDatabase(restoreDataObject.Server.Databases[targetDatabase]));
                                }

                                //To verify the backupset that are restored, verifying the database is a better options.
                                //Some tests still verify the number of backup sets that are executed which in some cases can be less than the selected list
                                if (verifyDatabase == null && selectedBackupSets != null)
                                {
                                    Assert.Equal(selectedBackupSets.Count(), restoreDataObject.RestorePlanToExecute.RestoreOperations.Count());
                                }
                            }
                            if (executionMode.HasFlag(TaskExecutionModeFlag.Script))
                            {
                                Assert.False(string.IsNullOrEmpty(restoreDataObject.ScriptContent));
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!shouldFail)
                            {
                                Assert.False(true, ex.Message);
                            }
                        }
                        finally
                        {
                            await DropDatabase(targetDatabase);
                        }
                    }
                }

                return(response);
            }
        }
        private async Task <RestorePlanResponse> VerifyRestore(
            string[] backupFileNames,
            bool canRestore,
            bool execute                         = false,
            string targetDatabase                = null,
            string[] selectedBackupSets          = null,
            Dictionary <string, object> options  = null,
            Func <Database, bool> verifyDatabase = null)
        {
            var    filePaths      = backupFileNames.Select(x => GetBackupFilePath(x));
            string backUpFilePath = filePaths.Aggregate((current, next) => current + " ," + next);


            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                TestConnectionResult connectionResult = await LiveConnectionHelper.InitLiveConnectionInfoAsync("master", queryTempFile.FilePath);

                RestoreDatabaseHelper service = new RestoreDatabaseHelper();
                var request = new RestoreParams
                {
                    BackupFilePaths    = backUpFilePath,
                    TargetDatabaseName = targetDatabase,
                    OwnerUri           = queryTempFile.FilePath,
                    SelectedBackupSets = selectedBackupSets
                };

                if (options != null)
                {
                    foreach (var item in options)
                    {
                        if (!request.Options.ContainsKey(item.Key))
                        {
                            request.Options.Add(item.Key, item.Value);
                        }
                    }
                }

                var restoreDataObject = service.CreateRestoreDatabaseTaskDataObject(request);
                var response          = service.CreateRestorePlanResponse(restoreDataObject);

                Assert.NotNull(response);
                Assert.False(string.IsNullOrWhiteSpace(response.SessionId));
                Assert.Equal(response.CanRestore, canRestore);
                if (canRestore)
                {
                    Assert.True(response.DbFiles.Any());
                    if (string.IsNullOrEmpty(targetDatabase))
                    {
                        targetDatabase = response.DatabaseName;
                    }
                    Assert.Equal(response.DatabaseName, targetDatabase);
                    Assert.NotNull(response.PlanDetails);
                    Assert.True(response.PlanDetails.Any());
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultBackupTailLog]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultTailLogBackupFile]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultDataFileFolder]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultLogFileFolder]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultStandbyFile]);
                    Assert.NotNull(response.PlanDetails[RestoreOptionsHelper.DefaultStandbyFile]);

                    if (execute)
                    {
                        request.SessionId = response.SessionId;
                        restoreDataObject = service.CreateRestoreDatabaseTaskDataObject(request);
                        Assert.Equal(response.SessionId, restoreDataObject.SessionId);
                        request.RelocateDbFiles = !restoreDataObject.DbFilesLocationAreValid();
                        service.ExecuteRestore(restoreDataObject);
                        Assert.True(restoreDataObject.Server.Databases.Contains(targetDatabase));

                        if (verifyDatabase != null)
                        {
                            Assert.True(verifyDatabase(restoreDataObject.Server.Databases[targetDatabase]));
                        }

                        //To verify the backupset that are restored, verifying the database is a better options.
                        //Some tests still verify the number of backup sets that are executed which in some cases can be less than the selected list
                        if (verifyDatabase == null && selectedBackupSets != null)
                        {
                            Assert.Equal(selectedBackupSets.Count(), restoreDataObject.RestorePlanToExecute.RestoreOperations.Count());
                        }

                        await DropDatabase(targetDatabase);
                    }
                }

                return(response);
            }
        }