forked from microsoft/scalar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchStepWithoutSharedCacheTests.cs
More file actions
252 lines (203 loc) · 11.1 KB
/
FetchStepWithoutSharedCacheTests.cs
File metadata and controls
252 lines (203 loc) · 11.1 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
using NUnit.Framework;
using Scalar.FunctionalTests.FileSystemRunners;
using Scalar.FunctionalTests.Should;
using Scalar.Tests.Should;
using System;
using System.IO;
namespace Scalar.FunctionalTests.Tests.EnlistmentPerFixture
{
[TestFixture]
public class FetchStepWithoutSharedCacheTests : 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 FetchStepWithoutSharedCacheTests()
: base(forcePerRepoObjectCache: true, skipFetchCommitsAndTreesDuringClone: 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 FetchStepCommitsToEmptyCache()
{
this.Enlistment.RunVerb("fetch");
// 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 FetchStepBuildsIdxWhenMissingFromPrefetchPack()
{
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);
// fetch should rebuild the missing idx
this.Enlistment.RunVerb("fetch");
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 FetchStepCleansUpBadPrefetchPack()
{
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);
// fetch should delete the bad pack
this.Enlistment.RunVerb("fetch");
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();
}
// WindowsOnly because the test depends on Windows-specific file sharing behavior
[TestCase, Order(4)]
[Category(Categories.WindowsOnly)]
public void FetchStepFailsWhenItCannotRemoveABadPrefetchPack()
{
this.Enlistment.Unregister();
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 fetch-commits-and-trees from being able to delete it
using (FileStream stream = new FileStream(badPackPath, FileMode.Open, FileAccess.Read, FileShare.None))
{
this.Enlistment.RunVerb("fetch", failOnError: false);
}
badPackPath.ShouldBeAFile(this.fileSystem).WithContents(badContents);
// After handle is closed fetching commits and trees should succeed
this.Enlistment.RunVerb("fetch");
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(5)]
public void FetchCommitsAndTreesCleansUpStaleTempPrefetchPacks()
{
this.Enlistment.Unregister();
// 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.RunVerb("fetch");
// 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);
}
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);
// StringComparison.Ordinal because packs should be created using the case we expect
filename.StartsWith(PrefetchPackPrefix, StringComparison.Ordinal)
.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;
}
}
}