-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathDocsSyncTests.cs
More file actions
320 lines (281 loc) · 13.4 KB
/
DocsSyncTests.cs
File metadata and controls
320 lines (281 loc) · 13.4 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
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.Diagnostics;
using System.IO.Abstractions.TestingHelpers;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using AwesomeAssertions;
using Elastic.Documentation.Assembler;
using Elastic.Documentation.Assembler.Deploying.Synchronization;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Configuration.Assembler;
using Elastic.Documentation.Diagnostics;
using Elastic.Documentation.Integrations.S3;
using Elastic.Documentation.ServiceDefaults.Telemetry;
using FakeItEasy;
using Microsoft.Extensions.Logging;
using Nullean.ScopedFileSystem;
using OpenTelemetry;
using OpenTelemetry.Trace;
namespace Elastic.Documentation.IntegrationTests;
public class DocsSyncTests
{
[Fact]
public async Task TestPlan()
{
// Arrange
IReadOnlyCollection<IDiagnosticsOutput> diagnosticsOutputs = [];
var collector = new DiagnosticsCollector(diagnosticsOutputs);
var mockS3Client = A.Fake<IAmazonS3>();
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "docs/add1.md", new MockFileData("# New Document 1") },
{ "docs/add2.md", new MockFileData("# New Document 2") },
{ "docs/add3.md", new MockFileData("# New Document 3") },
{ "docs/skip.md", new MockFileData("# Skipped Document") },
{ "docs/update.md", new MockFileData("# Existing Document") },
}, new MockFileSystemOptions
{
CurrentDirectory = Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"),
});
var configurationContext = TestHelpers.CreateConfigurationContext(fileSystem);
var config = AssemblyConfiguration.Create(configurationContext.ConfigurationFileProvider);
var scopedFs = FileSystemFactory.ScopeCurrentWorkingDirectory(fileSystem);
var scopedWriteFs = FileSystemFactory.ScopeCurrentWorkingDirectoryForWrite(fileSystem);
var context = new AssembleContext(config, configurationContext, "dev", collector, scopedFs, scopedWriteFs, null, Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"));
A.CallTo(() => mockS3Client.ListObjectsV2Async(A<ListObjectsV2Request>._, A<Cancel>._))
.Returns(new ListObjectsV2Response
{
S3Objects =
[
new S3Object { Key = "docs/delete.md" },
new S3Object
{
Key = "docs/skip.md",
ETag = "\"69048c0964c9577a399b138b706a467a\""
}, // This is the result of CalculateS3ETag
new S3Object
{
Key = "docs/update.md",
ETag = "\"existing-etag\""
}
]
});
var planStrategy = new AwsS3SyncPlanStrategy(new LoggerFactory(), mockS3Client, "fake", context);
// Act
var plan = await planStrategy.Plan(null, Cancel.None);
// Assert
plan.TotalRemoteFiles.Should().Be(3);
plan.TotalSourceFiles.Should().Be(5);
plan.TotalSyncRequests.Should().Be(6); //including skip on server
plan.AddRequests.Count.Should().Be(3);
plan.AddRequests.Should().Contain(i => i.DestinationPath == "docs/add1.md");
plan.AddRequests.Should().Contain(i => i.DestinationPath == "docs/add2.md");
plan.AddRequests.Should().Contain(i => i.DestinationPath == "docs/add3.md");
plan.UpdateRequests.Count.Should().Be(1);
plan.UpdateRequests.Should().Contain(i => i.DestinationPath == "docs/update.md");
plan.SkipRequests.Count.Should().Be(1);
plan.SkipRequests.Should().Contain(i => i.DestinationPath == "docs/skip.md");
plan.DeleteRequests.Count.Should().Be(1);
plan.DeleteRequests.Should().Contain(i => i.DestinationPath == "docs/delete.md");
}
[Theory]
[InlineData(0, 10_000, 10_000, 0, 10_000, 0.2, false)]
[InlineData(8_000, 10_000, 10_000, 0, 2000, 0.2, true)]
[InlineData(7900, 10_000, 10_000, 0, 2100, 0.2, false)]
[InlineData(10_000, 0, 10_000, 10_000, 0, 0.2, true)]
[InlineData(2000, 0, 2000, 2000, 0, 0.2, true)]
// When total files to sync is lower than 100 we enforce a minimum ratio of 0.8
[InlineData(20, 40, 40, 0, 20, 0.2, true)]
[InlineData(19, 100, 100, 0, 81, 0.2, false)]
// When total files to sync is lower than 1000 we enforce a minimum ratio of 0.5
[InlineData(200, 400, 400, 0, 200, 0.2, true)]
[InlineData(199, 1000, 1000, 0, 801, 0.2, false)]
public async Task ValidateAdditionsPlan(
int localFiles,
int remoteFiles,
int totalFilesToSync,
int totalFilesToAdd,
int totalFilesToRemove,
float deleteThreshold,
bool valid
)
{
var (validator, _, plan) = await SetupS3SyncContextSetup(localFiles, remoteFiles, deleteThreshold);
// Assert
plan.TotalSourceFiles.Should().Be(localFiles);
plan.TotalSyncRequests.Should().Be(totalFilesToSync);
plan.AddRequests.Count.Should().Be(totalFilesToAdd);
plan.DeleteRequests.Count.Should().Be(totalFilesToRemove);
var validationResult = validator.Validate(plan);
if (plan.TotalSyncRequests <= 100)
validationResult.DeleteThreshold.Should().Be(Math.Max(deleteThreshold, 0.8f));
else if (plan.TotalSyncRequests <= 1000)
validationResult.DeleteThreshold.Should().Be(Math.Max(deleteThreshold, 0.5f));
validationResult.Valid.Should().Be(valid, $"Delete ratio is {validationResult.DeleteRatio} when maximum is {validationResult.DeleteThreshold}");
}
[Theory]
[InlineData(10_000, 0, 10_000, 0, 0, 0.2, true)]
[InlineData(2000, 0, 2000, 0, 0, 0.2, true)]
[InlineData(0, 10_000, 10_000, 0, 10_000, 0.2, false)]
[InlineData(0, 10_000, 10_000, 0, 10_000, 1.0, false)]
[InlineData(20, 10_000, 10_000, 20, 9980, 0.2, false)]
[InlineData(20, 10_000, 10_000, 20, 9980, 1.0, true)]
[InlineData(8_000, 10_000, 10_000, 8000, 2000, 0.2, true)]
[InlineData(7900, 10_000, 10_000, 7900, 2100, 0.2, false)]
public async Task ValidateUpdatesPlan(
int localFiles,
int remoteFiles,
int totalFilesToSync,
int totalFilesToUpdate,
int totalFilesToRemove,
float deleteThreshold,
bool valid
)
{
var (validator, _, plan) = await SetupS3SyncContextSetup(localFiles, remoteFiles, deleteThreshold, "different-etag");
// Assert
plan.TotalSourceFiles.Should().Be(localFiles);
plan.TotalSyncRequests.Should().Be(totalFilesToSync);
plan.UpdateRequests.Count.Should().Be(totalFilesToUpdate);
plan.DeleteRequests.Count.Should().Be(totalFilesToRemove);
var validationResult = validator.Validate(plan);
if (plan.TotalSyncRequests <= 100)
validationResult.DeleteThreshold.Should().Be(Math.Max(deleteThreshold, 0.8f));
else if (plan.TotalSyncRequests <= 1000)
validationResult.DeleteThreshold.Should().Be(Math.Max(deleteThreshold, 0.5f));
validationResult.Valid.Should().Be(valid, $"Delete ratio is {validationResult.DeleteRatio} when maximum is {validationResult.DeleteThreshold}");
}
private static async Task<(DocsSyncPlanValidator validator, AwsS3SyncPlanStrategy planStrategy, SyncPlan plan)> SetupS3SyncContextSetup(
int localFiles, int remoteFiles, float? deleteThreshold = null, string etag = "etag")
{
// Arrange
IReadOnlyCollection<IDiagnosticsOutput> diagnosticsOutputs = [];
var collector = new DiagnosticsCollector(diagnosticsOutputs);
var mockS3Client = A.Fake<IAmazonS3>();
var fileSystem = new MockFileSystem(new MockFileSystemOptions
{
CurrentDirectory = Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"),
});
foreach (var i in Enumerable.Range(0, localFiles))
fileSystem.AddFile($"docs/file-{i}.md", new MockFileData($"# Local Document {i}"));
var configurationContext = TestHelpers.CreateConfigurationContext(fileSystem);
var config = AssemblyConfiguration.Create(configurationContext.ConfigurationFileProvider);
var scopedFs2 = FileSystemFactory.ScopeCurrentWorkingDirectory(fileSystem);
var scopedWriteFs2 = FileSystemFactory.ScopeCurrentWorkingDirectoryForWrite(fileSystem);
var context = new AssembleContext(config, configurationContext, "dev", collector, scopedFs2, scopedWriteFs2, null, Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"));
var s3Objects = new List<S3Object>();
foreach (var i in Enumerable.Range(0, remoteFiles))
{
s3Objects.Add(new S3Object
{
Key = $"docs/file-{i}.md",
ETag = etag
});
}
A.CallTo(() => mockS3Client.ListObjectsV2Async(A<ListObjectsV2Request>._, A<Cancel>._))
.Returns(new ListObjectsV2Response
{
S3Objects = s3Objects
});
var mockEtagCalculator = A.Fake<IS3EtagCalculator>();
A.CallTo(() => mockEtagCalculator.CalculateS3ETag(A<string>._, A<Cancel>._)).Returns("etag");
var planStrategy = new AwsS3SyncPlanStrategy(new LoggerFactory(), mockS3Client, "fake", context, mockEtagCalculator);
// Act
var plan = await planStrategy.Plan(deleteThreshold, Cancel.None);
var validator = new DocsSyncPlanValidator(new LoggerFactory());
return (validator, planStrategy, plan);
}
[Fact]
public async Task TestApply()
{
// Arrange
IReadOnlyCollection<IDiagnosticsOutput> diagnosticsOutputs = [];
var collector = new DiagnosticsCollector(diagnosticsOutputs);
var moxS3Client = A.Fake<IAmazonS3>();
var moxTransferUtility = A.Fake<ITransferUtility>();
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "docs/add1.md", new MockFileData("# New Document 1") },
{ "docs/add2.md", new MockFileData("# New Document 2") },
{ "docs/add3.md", new MockFileData("# New Document 3") },
{ "docs/skip.md", new MockFileData("# Skipped Document") },
{ "docs/update.md", new MockFileData("# Existing Document") },
}, new MockFileSystemOptions
{
CurrentDirectory = Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly"),
});
var configurationContext = TestHelpers.CreateConfigurationContext(fileSystem);
var config = AssemblyConfiguration.Create(configurationContext.ConfigurationFileProvider);
var checkoutDirectory = Path.Join(Paths.WorkingDirectoryRoot.FullName, ".artifacts", "assembly");
var scopedFs3 = FileSystemFactory.ScopeCurrentWorkingDirectory(fileSystem);
var scopedWriteFs3 = FileSystemFactory.ScopeCurrentWorkingDirectoryForWrite(fileSystem);
var context = new AssembleContext(config, configurationContext, "dev", collector, scopedFs3, scopedWriteFs3, null, checkoutDirectory);
var plan = new SyncPlan
{
RemoteListingCompleted = true,
DeleteThresholdDefault = null,
TotalRemoteFiles = 0,
TotalSourceFiles = 5,
TotalSyncRequests = 6,
AddRequests = [
new AddRequest { LocalPath = "docs/add1.md", DestinationPath = "docs/add1.md" },
new AddRequest { LocalPath = "docs/add2.md", DestinationPath = "docs/add2.md" },
new AddRequest { LocalPath = "docs/add3.md", DestinationPath = "docs/add3.md" }
],
UpdateRequests = [
new UpdateRequest
{ LocalPath = "docs/update.md", DestinationPath = "docs/update.md" }
],
SkipRequests = [
new SkipRequest
{ LocalPath = "docs/skip.md", DestinationPath = "docs/skip.md" }
],
DeleteRequests = [
new DeleteRequest
{ DestinationPath = "docs/delete.md" }
]
};
A.CallTo(() => moxS3Client.DeleteObjectsAsync(A<DeleteObjectsRequest>._, A<Cancel>._))
.Returns(new DeleteObjectsResponse
{
HttpStatusCode = System.Net.HttpStatusCode.OK
});
var transferredFiles = Array.Empty<string>();
A.CallTo(() => moxTransferUtility.UploadDirectoryAsync(A<TransferUtilityUploadDirectoryRequest>._, A<Cancel>._))
.Invokes((TransferUtilityUploadDirectoryRequest request, Cancel _) =>
{
transferredFiles = fileSystem.Directory.GetFiles(request.Directory, request.SearchPattern, request.SearchOption);
});
// Configure OpenTelemetry to capture telemetry
var exportedActivities = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(TelemetryConstants.AssemblerSyncInstrumentationName)
.AddInMemoryExporter(exportedActivities)
.Build();
var applier = new AwsS3SyncApplyStrategy(new LoggerFactory(), moxS3Client, moxTransferUtility, "fake", context, collector);
// Act
await applier.Apply(plan, Cancel.None);
// Assert - File operations
transferredFiles.Length.Should().Be(4); // 3 add requests + 1 update request
transferredFiles.Should().NotContain("docs/skip.md");
A.CallTo(() => moxS3Client.DeleteObjectsAsync(A<DeleteObjectsRequest>._, A<Cancel>._))
.MustHaveHappenedOnceExactly();
A.CallTo(() => moxTransferUtility.UploadDirectoryAsync(A<TransferUtilityUploadDirectoryRequest>._, A<Cancel>._))
.MustHaveHappenedOnceExactly();
// Assert - Telemetry spans are created
exportedActivities.Should().Contain(a => a.DisplayName == "sync apply");
exportedActivities.Should().Contain(a => a.DisplayName == "upload files");
exportedActivities.Should().Contain(a => a.DisplayName == "delete files");
// Assert - Telemetry tags contain correct aggregate counts
var syncActivity = exportedActivities.First(a => a.DisplayName == "sync apply");
var tagObjects = syncActivity.TagObjects.ToList();
tagObjects.Should().Contain(t => t.Key == "docs.sync.files.added" && Convert.ToInt64(t.Value, System.Globalization.CultureInfo.InvariantCulture) == 3);
tagObjects.Should().Contain(t => t.Key == "docs.sync.files.updated" && Convert.ToInt64(t.Value, System.Globalization.CultureInfo.InvariantCulture) == 1);
tagObjects.Should().Contain(t => t.Key == "docs.sync.files.deleted" && Convert.ToInt64(t.Value, System.Globalization.CultureInfo.InvariantCulture) == 1);
tagObjects.Should().Contain(t => t.Key == "docs.sync.files.skipped" && Convert.ToInt64(t.Value, System.Globalization.CultureInfo.InvariantCulture) == 1);
tagObjects.Should().Contain(t => t.Key == "docs.sync.files.total" && Convert.ToInt64(t.Value, System.Globalization.CultureInfo.InvariantCulture) == 6);
}
}