-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestRestore.cs
More file actions
executable file
·221 lines (180 loc) · 7.37 KB
/
TestRestore.cs
File metadata and controls
executable file
·221 lines (180 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
namespace AgDatabaseMove.Integration
{
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Configuration;
using SmoFacade;
using Xunit;
public class TestRestoreConfig
{
public DatabaseConfig From { get; set; }
public DatabaseConfig To { get; set; }
public string FullBackup { get; set; }
public string TestQuery { get; set; }
}
public class TestRestoreFixture : IDisposable
{
public readonly Dictionary<string, List<LoginProperties>> _preTestLogins = new Dictionary<string, List<LoginProperties>>();
public readonly AgDatabase _source;
public readonly AgDatabase _test;
public TestRestoreConfig _config;
public TestRestoreFixture()
{
var builder = new ConfigurationBuilder().AddJsonFile("config.json", false);
_config = builder.Build().GetSection("TestRestore").Get<TestRestoreConfig>();
_source = new AgDatabase(_config.From);
_test = new AgDatabase(_config.To);
_test.Delete();
// We only snapshot the primary instance's logins. It works for our integration environment, but we could do better and snapshot each instance's.
_test.Listener.ForEachAgInstance(server => {
_preTestLogins.Add(server.Name, server.Logins.Select(l => l.Properties()).ToList());
});
}
public void Dispose()
{
if(_test != null) {
_test.Listener.ForEachAgInstance(CleanupLogins);
_test.Dispose();
}
_source?.Dispose();
}
private void CleanupLogins(Server server)
{
var postTestLogins = server.Logins.ToList();
var newLogins = postTestLogins.Where(post => _preTestLogins[server.Name].All(pre => pre.Name != post.Name));
foreach(var login in newLogins) login.Drop();
}
}
public class TestRestore : IClassFixture<TestRestoreFixture>
{
private readonly TestRestoreFixture _testRestoreFixture;
public TestRestore(TestRestoreFixture testRestoreFixture)
{
_testRestoreFixture = testRestoreFixture;
}
private AgDatabase Test => _testRestoreFixture._test;
private AgDatabase Source => _testRestoreFixture._source;
private TestRestoreConfig Config => _testRestoreFixture._config;
private string RestoreFileRelocator(string name)
{
// Our integration environment is currently mixed.
name = Regex.Replace(name, "MSSQL11", "MSSQL13", RegexOptions.IgnoreCase & RegexOptions.CultureInvariant);
name = Regex.Replace(name, Source.Name, Test.Name, RegexOptions.IgnoreCase & RegexOptions.CultureInvariant);
return name;
}
[Fact]
public void DetectsInitializing()
{
// AgDatabaseMove databases across AG instances
Assert.False(Test.Exists());
var mover = new AgDatabaseMove(new MoveOptions {
Source = Source,
Destination = Test,
Overwrite = false,
Finalize = false,
FileRelocator = RestoreFileRelocator,
RetryDuration = attempt => TimeSpan.FromSeconds(attempt * 10)
});
mover.Move();
// AgDatabaseMove with recovery primary
Test.FinalizePrimary();
// Write sufficient data to the primary
var connectionStringBuilder = new SqlConnectionStringBuilder(_testRestoreFixture._config.To.ConnectionString);
connectionStringBuilder.InitialCatalog = Test.Name;
using var connection = new SqlConnection(connectionStringBuilder.ToString());
connection.Open();
var createTableSql =
"CREATE TABLE TestSync (Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, script VARCHAR(MAX) NULL)";
using var createTable = new SqlCommand(createTableSql, connection);
createTable.ExecuteNonQuery();
var fillTableSql =
"INSERT INTO TestSync (script) (SELECT TOP 100000 sm.[definition] FROM sys.all_sql_modules AS sm CROSS JOIN sys.all_sql_modules AS asm)";
using var fillTable = new SqlCommand(fillTableSql, connection);
fillTable.ExecuteNonQuery();
Test.JoinAg();
Assert.True(Test.IsInitializing());
Test.Delete();
}
/// <summary>
/// This is how I envision the library being used to do our move operation while clients are using the database.
/// </summary>
[Fact]
public void ProgressiveRestore()
{
var mover = new AgDatabaseMove(new MoveOptions {
Source = Source,
Destination = Test,
Overwrite = false,
Finalize = false,
FileRelocator = RestoreFileRelocator,
RetryDuration = attempt => TimeSpan.FromSeconds(attempt * 10)
});
int seconds;
decimal? lastLsn = null;
// AgDatabaseMove the database and subsequent log files
do {
var timer = new Stopwatch();
timer.Start();
lastLsn = mover.Move(lastLsn);
timer.Stop();
seconds = timer.Elapsed.Seconds;
} while(seconds > 5);
// Do things here to disconnect users: Set single user mode, signal the service etc.
Source.RestrictedUserMode();
// Hopefully a quick backup and restore
mover._options.Finalize = true;
mover.Move(lastLsn);
// The database is migrated
Source.MultiUserMode();
Test.Delete();
}
[Fact]
public void RestoreAndCleanup()
{
Assert.False(Test.Exists());
var mover = new AgDatabaseMove(new MoveOptions {
Source = Source,
Destination = Test,
Finalize = true,
CopyLogins = true,
FileRelocator = RestoreFileRelocator,
RetryDuration = attempt => TimeSpan.FromSeconds(attempt * 10)
});
mover.Move();
Assert.True(Test.Exists());
Test.Delete();
Assert.False(Test.Exists());
}
[Fact]
public void TestSimpleRecovery()
{
var fullBackup = new SingleBackup { PhysicalDeviceName = Config.FullBackup, BackupType = BackupFileTools.BackupType.Full};
Func<int, TimeSpan> retryDurationProvider = attempt => TimeSpan.FromSeconds(10 * attempt);
Test.Restore(fullBackup, retryDurationProvider);
Test.JoinAg();
Assert.True(Test.Exists());
// Assert database has data from full backup.
var connectionStringBuilder = new SqlConnectionStringBuilder(Config.To.ConnectionString);
connectionStringBuilder.InitialCatalog = Test.Name;
using var connection = new SqlConnection(connectionStringBuilder.ToString());
connection.Open();
using var testQuery = new SqlCommand(Config.TestQuery, connection);
var reader = testQuery.ExecuteReader();
Assert.True(reader.HasRows);
Test.Delete();
}
[Fact]
public void TestThrowsIfBackupNotFull()
{
var logBackup = new SingleBackup { PhysicalDeviceName = Config.FullBackup, BackupType = BackupFileTools.BackupType.Log };
var diffBackup = new SingleBackup { PhysicalDeviceName = Config.FullBackup, BackupType = BackupFileTools.BackupType.Diff };
Func<int, TimeSpan> retryDurationProvider = attempt => TimeSpan.FromSeconds(10 * attempt);
Assert.Throws<ArgumentException>(() => Test.Restore(logBackup, retryDurationProvider));
Assert.Throws<ArgumentException>(() => Test.Restore(diffBackup, retryDurationProvider));
}
}
}