forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefetchVerbWithoutSharedCacheTests.cs
More file actions
395 lines (322 loc) · 18.9 KB
/
PrefetchVerbWithoutSharedCacheTests.cs
File metadata and controls
395 lines (322 loc) · 18.9 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using GVFS.FunctionalTests.FileSystemRunners;
using GVFS.FunctionalTests.Should;
using GVFS.FunctionalTests.Tools;
using GVFS.Tests.Should;
using NUnit.Framework;
using System;
using System.IO;
using System.Threading;
namespace GVFS.FunctionalTests.Tests.EnlistmentPerFixture
{
// TODO(Mac): Before these tests can be enabled PostFetchJobShouldComplete needs
// to work on Mac (where post-fetch.lock is not removed from disk)
[TestFixture]
[Category(Categories.ExtraCoverage)]
[Category(Categories.MacTODO.M4)]
public class PrefetchVerbWithoutSharedCacheTests : TestsWithEnlistmentPerFixture
{
private const string PrefetchPackPrefix = "prefetch";
private const string TempPackFolder = "tempPacks";
private FileSystemRunner fileSystem;
// Set forcePerRepoObjectCache to true to avoid any of the tests inadvertently corrupting
// the cache
public PrefetchVerbWithoutSharedCacheTests()
: base(forcePerRepoObjectCache: true, skipPrefetchDuringClone: true)
{
this.fileSystem = new SystemIORunner();
}
private string PackRoot
{
get
{
return this.Enlistment.GetPackRoot(this.fileSystem);
}
}
private string TempPackRoot
{
get
{
return Path.Combine(this.PackRoot, TempPackFolder);
}
}
[TestCase, Order(1)]
public void PrefetchCommitsToEmptyCache()
{
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
// Verify prefetch pack(s) are in packs folder and have matching idx file
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
this.AllPrefetchPacksShouldHaveIdx(prefetchPacks);
// Verify tempPacks is empty
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(2)]
public void PrefetchBuildsIdxWhenMissingFromPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack");
string idxPath = Path.ChangeExtension(prefetchPacks[0], ".idx");
idxPath.ShouldBeAFile(this.fileSystem);
File.SetAttributes(idxPath, FileAttributes.Normal);
this.fileSystem.DeleteFile(idxPath);
idxPath.ShouldNotExistOnDisk(this.fileSystem);
// Prefetch should rebuild the missing idx
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
idxPath.ShouldBeAFile(this.fileSystem);
// All of the original prefetch packs should still be present
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
newPrefetchPacks.ShouldContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(3)]
public void PrefetchCleansUpBadPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
long mostRecentPackTimestamp = this.GetMostRecentPackTimestamp(prefetchPacks);
// Create a bad pack that is newer than the most recent pack
string badContents = "BADPACK";
string badPackPath = Path.Combine(this.PackRoot, $"{PrefetchPackPrefix}-{mostRecentPackTimestamp + 1}-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(badPackPath, badContents);
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
// Prefetch should delete the bad pack
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
// All of the original prefetch packs should still be present
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
newPrefetchPacks.ShouldContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(4)]
public void PrefetchCleansUpOldPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
long oldestPackTimestamp = this.GetOldestPackTimestamp(prefetchPacks);
// Create a bad pack that is older than the oldest pack
string badContents = "BADPACK";
string badPackPath = Path.Combine(this.PackRoot, $"{PrefetchPackPrefix}-{oldestPackTimestamp - 1}-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(badPackPath, badContents);
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
// Prefetch should delete the bad pack and all packs after it
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
foreach (string packPath in prefetchPacks)
{
string idxPath = Path.ChangeExtension(packPath, ".idx");
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
idxPath.ShouldNotExistOnDisk(this.fileSystem);
}
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(5)]
public void PrefetchFailsWhenItCannotRemoveABadPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
long mostRecentPackTimestamp = this.GetMostRecentPackTimestamp(prefetchPacks);
// Create a bad pack that is newer than the most recent pack
string badContents = "BADPACK";
string badPackPath = Path.Combine(this.PackRoot, $"{PrefetchPackPrefix}-{mostRecentPackTimestamp + 1}-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(badPackPath, badContents);
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
// Open a handle to the bad pack that will prevent prefetch from being able to delete it
using (FileStream stream = new FileStream(badPackPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
string output = this.Enlistment.Prefetch("--commits", failOnError: false);
output.ShouldContain($"Unable to delete {badPackPath}");
}
// After handle is closed prefetch should succeed
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
newPrefetchPacks.ShouldContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(6)]
public void PrefetchFailsWhenItCannotRemoveAPrefetchPackNewerThanBadPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
long oldestPackTimestamp = this.GetOldestPackTimestamp(prefetchPacks);
// Create a bad pack that is older than the oldest pack
string badContents = "BADPACK";
string badPackPath = Path.Combine(this.PackRoot, $"{PrefetchPackPrefix}-{oldestPackTimestamp - 1}-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(badPackPath, badContents);
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
// Open a handle to a good pack that is newer than the bad pack, which will prevent prefetch from being able to delete it
using (FileStream stream = new FileStream(prefetchPacks[0], FileMode.Open, FileAccess.Read, FileShare.None))
{
string output = this.Enlistment.Prefetch("--commits", failOnError: false);
output.ShouldContain($"Unable to delete {prefetchPacks[0]}");
}
// After handle is closed prefetch should succeed
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
// The bad pack and all packs newer than it should not be on disk
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
newPrefetchPacks.ShouldNotContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(7)]
public void PrefetchFailsWhenItCannotRemoveAPrefetchIdxNewerThanBadPrefetchPack()
{
string[] prefetchPacks = this.ReadPrefetchPackFileNames();
long oldestPackTimestamp = this.GetOldestPackTimestamp(prefetchPacks);
// Create a bad pack that is older than the oldest pack
string badContents = "BADPACK";
string badPackPath = Path.Combine(this.PackRoot, $"{PrefetchPackPrefix}-{oldestPackTimestamp - 1}-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(badPackPath, badContents);
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
string newerIdxPath = Path.ChangeExtension(prefetchPacks[0], ".idx");
newerIdxPath.ShouldBeAFile(this.fileSystem);
// Open a handle to a good idx that is newer than the bad pack, which will prevent prefetch from being able to delete it
using (FileStream stream = new FileStream(newerIdxPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
string output = this.Enlistment.Prefetch("--commits", failOnError: false);
output.ShouldContain($"Unable to delete {newerIdxPath}");
}
// After handle is closed prefetch should succeed
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
// The bad pack and all packs newer than it should not be on disk
badPackPath.ShouldNotExistOnDisk(this.fileSystem);
newerIdxPath.ShouldNotExistOnDisk(this.fileSystem);
string[] newPrefetchPacks = this.ReadPrefetchPackFileNames();
newPrefetchPacks.ShouldNotContain(prefetchPacks, (item, expectedValue) => { return string.Equals(item, expectedValue); });
this.AllPrefetchPacksShouldHaveIdx(newPrefetchPacks);
this.TempPackRoot.ShouldBeADirectory(this.fileSystem).WithNoItems();
}
[TestCase, Order(8)]
public void PrefetchCleansUpStaleTempPrefetchPacks()
{
// Create stale packs and idxs in the temp folder
string stalePackContents = "StalePack";
string stalePackPath = Path.Combine(this.TempPackRoot, $"{PrefetchPackPrefix}-123456-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(stalePackPath, stalePackContents);
stalePackPath.ShouldBeAFile(this.fileSystem).WithContents(stalePackContents);
string staleIdxContents = "StaleIdx";
string staleIdxPath = Path.ChangeExtension(stalePackPath, ".idx");
this.fileSystem.WriteAllText(staleIdxPath, staleIdxContents);
staleIdxPath.ShouldBeAFile(this.fileSystem).WithContents(staleIdxContents);
string stalePackPath2 = Path.Combine(this.TempPackRoot, $"{PrefetchPackPrefix}-123457-{Guid.NewGuid().ToString("N")}.pack");
this.fileSystem.WriteAllText(stalePackPath2, stalePackContents);
stalePackPath2.ShouldBeAFile(this.fileSystem).WithContents(stalePackContents);
string stalePack2TempIdx = Path.ChangeExtension(stalePackPath2, ".tempidx");
this.fileSystem.WriteAllText(stalePack2TempIdx, staleIdxContents);
stalePack2TempIdx.ShouldBeAFile(this.fileSystem).WithContents(staleIdxContents);
// Create other unrelated file in the temp folder
string otherFileContents = "Test file, don't delete me!";
string otherFilePath = Path.Combine(this.TempPackRoot, "ReadmeAndDontDeleteMe.txt");
this.fileSystem.WriteAllText(otherFilePath, otherFileContents);
otherFilePath.ShouldBeAFile(this.fileSystem).WithContents(otherFileContents);
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
// Validate stale prefetch packs are cleaned up
Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.pack").ShouldBeEmpty("There should be no .pack files in the tempPack folder");
Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.idx").ShouldBeEmpty("There should be no .idx files in the tempPack folder");
Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.tempidx").ShouldBeEmpty("There should be no .tempidx files in the tempPack folder");
// Validate other files are not impacted
otherFilePath.ShouldBeAFile(this.fileSystem).WithContents(otherFileContents);
}
[TestCase, Order(9)]
public void PrefetchCleansUpOphanedLockFiles()
{
// Multi-pack-index write happens even if the prefetch downloads nothing, while
// the commit-graph write happens only when the prefetch downloads at least one pack
string graphPath = Path.Combine(this.Enlistment.GetObjectRoot(this.fileSystem), "info", "commit-graph");
string graphLockPath = graphPath + ".lock";
string midxPath = Path.Combine(this.PackRoot, "multi-pack-index");
string midxLockPath = midxPath + ".lock";
this.fileSystem.CreateEmptyFile(graphLockPath);
// Force deleting the prefetch packs to make the prefetch non-trivial.
this.fileSystem.DeleteDirectory(this.PackRoot);
this.fileSystem.CreateDirectory(this.PackRoot);
this.fileSystem.CreateEmptyFile(midxLockPath);
this.Enlistment.Prefetch("--commits");
this.PostFetchJobShouldComplete();
this.fileSystem.FileExists(graphLockPath).ShouldBeFalse();
this.fileSystem.FileExists(midxLockPath).ShouldBeFalse();
this.fileSystem.FileExists(graphPath).ShouldBeTrue();
this.fileSystem.FileExists(midxPath).ShouldBeTrue();
}
private void PackShouldHaveIdxFile(string pathPath)
{
string idxPath = Path.ChangeExtension(pathPath, ".idx");
idxPath.ShouldBeAFile(this.fileSystem).WithContents().Length.ShouldBeAtLeast(1, $"{idxPath} is unexepectedly empty");
}
private void AllPrefetchPacksShouldHaveIdx(string[] prefetchPacks)
{
prefetchPacks.Length.ShouldBeAtLeast(1, "There should be at least one prefetch pack");
foreach (string prefetchPack in prefetchPacks)
{
this.PackShouldHaveIdxFile(prefetchPack);
}
}
private string[] ReadPrefetchPackFileNames()
{
return Directory.GetFiles(this.PackRoot, $"{PrefetchPackPrefix}*.pack");
}
private long GetTimestamp(string preFetchPackName)
{
string filename = Path.GetFileName(preFetchPackName);
filename.StartsWith(PrefetchPackPrefix).ShouldBeTrue($"'{preFetchPackName}' does not start with '{PrefetchPackPrefix}'");
string[] parts = filename.Split('-');
long parsed;
parts.Length.ShouldBeAtLeast(1, $"'{preFetchPackName}' has less parts ({parts.Length}) than expected (1)");
long.TryParse(parts[1], out parsed).ShouldBeTrue($"Failed to parse long from '{parts[1]}'");
return parsed;
}
private long GetMostRecentPackTimestamp(string[] prefetchPacks)
{
prefetchPacks.Length.ShouldBeAtLeast(1, "prefetchPacks should have at least one item");
long mostRecentPackTimestamp = -1;
foreach (string prefetchPack in prefetchPacks)
{
long timestamp = this.GetTimestamp(prefetchPack);
if (timestamp > mostRecentPackTimestamp)
{
mostRecentPackTimestamp = timestamp;
}
}
mostRecentPackTimestamp.ShouldBeAtLeast(1, "Failed to find the most recent pack");
return mostRecentPackTimestamp;
}
private long GetOldestPackTimestamp(string[] prefetchPacks)
{
prefetchPacks.Length.ShouldBeAtLeast(1, "prefetchPacks should have at least one item");
long oldestPackTimestamp = long.MaxValue;
foreach (string prefetchPack in prefetchPacks)
{
long timestamp = this.GetTimestamp(prefetchPack);
if (timestamp < oldestPackTimestamp)
{
oldestPackTimestamp = timestamp;
}
}
oldestPackTimestamp.ShouldBeAtMost(long.MaxValue - 1, "Failed to find the oldest pack");
return oldestPackTimestamp;
}
private void PostFetchJobShouldComplete()
{
string objectDir = this.Enlistment.GetObjectRoot(this.fileSystem);
string postFetchLock = Path.Combine(objectDir, "git-maintenance-step.lock");
while (this.fileSystem.FileExists(postFetchLock))
{
Thread.Sleep(500);
}
ProcessResult midxResult = GitProcess.InvokeProcess(this.Enlistment.RepoRoot, "multi-pack-index verify --object-dir=\"" + objectDir + "\"");
midxResult.ExitCode.ShouldEqual(0);
ProcessResult graphResult = GitProcess.InvokeProcess(this.Enlistment.RepoRoot, "commit-graph read --object-dir=\"" + objectDir + "\"");
graphResult.ExitCode.ShouldEqual(0);
graphResult.Output.ShouldContain("43475048"); // Header from commit-graph file.
}
}
}