-
-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathCleanupDataJobTests.cs
More file actions
351 lines (279 loc) · 17.9 KB
/
Copy pathCleanupDataJobTests.cs
File metadata and controls
351 lines (279 loc) · 17.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
using Exceptionless.Core;
using Exceptionless.Core.Billing;
using Exceptionless.Core.Jobs;
using Exceptionless.Core.Repositories;
using Exceptionless.Core.Services;
using Exceptionless.DateTimeExtensions;
using Exceptionless.Tests.Utility;
using Foundatio.Repositories;
using Foundatio.Repositories.Utility;
using Xunit;
namespace Exceptionless.Tests.Jobs;
public class CleanupDataJobTests : IntegrationTestsBase
{
private readonly CleanupDataJob _job;
private readonly UsageService _usageService;
private readonly IOrganizationRepository _organizationRepository;
private readonly OrganizationData _organizationData;
private readonly IProjectRepository _projectRepository;
private readonly ProjectData _projectData;
private readonly IStackRepository _stackRepository;
private readonly StackData _stackData;
private readonly IEventRepository _eventRepository;
private readonly EventData _eventData;
private readonly ITokenRepository _tokenRepository;
private readonly TokenData _tokenData;
private readonly BillingManager _billingManager;
private readonly BillingPlans _plans;
public CleanupDataJobTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory)
{
_job = GetService<CleanupDataJob>();
_usageService = GetService<UsageService>();
_organizationData = GetService<OrganizationData>();
_organizationRepository = GetService<IOrganizationRepository>();
_projectData = GetService<ProjectData>();
_projectRepository = GetService<IProjectRepository>();
_stackData = GetService<StackData>();
_stackRepository = GetService<IStackRepository>();
_eventData = GetService<EventData>();
_eventRepository = GetService<IEventRepository>();
_tokenData = GetService<TokenData>();
_tokenRepository = GetService<ITokenRepository>();
_billingManager = GetService<BillingManager>();
_plans = GetService<BillingPlans>();
}
[Fact]
public async Task CanCleanupSuspendedTokens()
{
var organization = _organizationData.GenerateSampleOrganization(_billingManager, _plans);
organization.IsSuspended = true;
organization.SuspensionDate = DateTime.UtcNow;
organization.SuspendedByUserId = TestConstants.UserId;
organization.SuspensionCode = Core.Models.SuspensionCode.Billing;
organization.SuspensionNotes = "blah";
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject());
var token = await _tokenRepository.AddAsync(_tokenData.GenerateSampleApiKeyToken(), o => o.ImmediateConsistency());
Assert.False(token.IsSuspended);
await _job.RunAsync(TestCancellationToken);
token = await _tokenRepository.GetByIdAsync(token.Id);
Assert.NotNull(token);
Assert.True(token.IsSuspended);
}
[Fact]
public async Task CanCleanupSoftDeletedOrganization()
{
var organization = _organizationData.GenerateSampleOrganization(_billingManager, _plans);
organization.IsDeleted = true;
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
var persistentEvent = await _eventRepository.AddAsync(_eventData.GenerateEvent(organization.Id, project.Id, stack.Id), o => o.ImmediateConsistency());
await _job.RunAsync(TestCancellationToken);
Assert.Null(await _organizationRepository.GetByIdAsync(organization.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _projectRepository.GetByIdAsync(project.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _stackRepository.GetByIdAsync(stack.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _eventRepository.GetByIdAsync(persistentEvent.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task CanCleanupSoftDeletedProject()
{
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = _projectData.GenerateSampleProject();
project.IsDeleted = true;
await _projectRepository.AddAsync(project, o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
var persistentEvent = await _eventRepository.AddAsync(_eventData.GenerateEvent(organization.Id, project.Id, stack.Id), o => o.ImmediateConsistency());
await _job.RunAsync(TestCancellationToken);
Assert.NotNull(await _organizationRepository.GetByIdAsync(organization.Id));
Assert.Null(await _projectRepository.GetByIdAsync(project.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _stackRepository.GetByIdAsync(stack.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _eventRepository.GetByIdAsync(persistentEvent.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task CanCleanupSoftDeletedStack()
{
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = _stackData.GenerateSampleStack();
stack.IsDeleted = true;
await _stackRepository.AddAsync(stack, o => o.ImmediateConsistency());
var persistentEvent = await _eventRepository.AddAsync(_eventData.GenerateEvent(organization.Id, project.Id, stack.Id), o => o.ImmediateConsistency());
await _job.RunAsync(TestCancellationToken);
Assert.NotNull(await _organizationRepository.GetByIdAsync(organization.Id));
Assert.NotNull(await _projectRepository.GetByIdAsync(project.Id));
Assert.Null(await _stackRepository.GetByIdAsync(stack.Id, o => o.IncludeSoftDeletes()));
Assert.Null(await _eventRepository.GetByIdAsync(persistentEvent.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task CanCleanupEventsOutsideOfRetentionPeriod()
{
var organization = _organizationData.GenerateSampleOrganization(_billingManager, _plans);
_billingManager.ApplyBillingPlan(organization, _plans.FreePlan);
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
var options = GetService<AppOptions>();
var date = DateTimeOffset.UtcNow.SubtractDays(options.MaximumRetentionDays);
var persistentEvent = await _eventRepository.AddAsync(_eventData.GenerateEvent(organization.Id, project.Id, stack.Id, date, date, date), o => o.ImmediateConsistency());
await _job.RunAsync(TestCancellationToken);
Assert.NotNull(await _organizationRepository.GetByIdAsync(organization.Id));
Assert.NotNull(await _projectRepository.GetByIdAsync(project.Id));
Assert.NotNull(await _stackRepository.GetByIdAsync(stack.Id));
Assert.Null(await _eventRepository.GetByIdAsync(persistentEvent.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task CanDeleteOrphanedEventsByStack()
{
var organization = _organizationData.GenerateSampleOrganization(_billingManager, _plans);
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
await _eventRepository.AddAsync(_eventData.GenerateEvents(5000, organization.Id, project.Id, stack.Id), o => o.ImmediateConsistency());
var orphanedEvents = _eventData.GenerateEvents(10000, organization.Id, project.Id).ToList();
orphanedEvents.ForEach(e => e.StackId = ObjectId.GenerateNewId().ToString());
await _eventRepository.AddAsync(orphanedEvents, o => o.ImmediateConsistency());
var eventCount = await _eventRepository.CountAsync(o => o.IncludeSoftDeletes().ImmediateConsistency());
Assert.Equal(15000, eventCount);
await GetService<CleanupOrphanedDataJob>().RunAsync(TestCancellationToken);
eventCount = await _eventRepository.CountAsync(o => o.IncludeSoftDeletes().ImmediateConsistency());
Assert.Equal(5000, eventCount);
}
[Fact]
public async Task CanDeleteOnlyRecentOrphanedEventsByStack()
{
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
var validEvent = _eventData.GenerateEvent(organization.Id, project.Id, stack.Id);
var recentOrphanedEvent = _eventData.GenerateEvent(organization.Id, project.Id);
var olderOrphanedEvent = _eventData.GenerateEvent(organization.Id, project.Id);
string missingStackId = ObjectId.GenerateNewId().ToString();
recentOrphanedEvent.StackId = missingStackId;
recentOrphanedEvent.CreatedUtc = TimeProvider.GetUtcNow().UtcDateTime;
olderOrphanedEvent.StackId = missingStackId;
olderOrphanedEvent.CreatedUtc = TimeProvider.GetUtcNow().SubtractDays(4).UtcDateTime;
await _eventRepository.AddAsync([validEvent, recentOrphanedEvent, olderOrphanedEvent], o => o.ImmediateConsistency());
await GetService<CleanupOrphanedDataJob>().RunAsync(TestCancellationToken);
var events = await _eventRepository.GetAllAsync(o => o.PageLimit(10).ImmediateConsistency());
Assert.Equal(2, events.Total);
Assert.Contains(events.Documents, e => e.Id == validEvent.Id);
Assert.Contains(events.Documents, e => e.Id == olderOrphanedEvent.Id);
Assert.DoesNotContain(events.Documents, e => e.Id == recentOrphanedEvent.Id);
}
[Fact]
public async Task RemoveProjectsAsync_SoftDeletedProjectWithEvents_IncrementsDeletedUsage()
{
// Arrange
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = _projectData.GenerateSampleProject();
project.IsDeleted = true;
await _projectRepository.AddAsync(project, o => o.ImmediateConsistency());
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
var events = _eventData.GenerateEvents(5, organization.Id, project.Id, stack.Id).ToList();
await _eventRepository.AddAsync(events, o => o.ImmediateConsistency());
// Act
await _job.RunAsync(TestCancellationToken);
// Assert
var orgUsage = await _usageService.GetUsageAsync(organization.Id, null);
Assert.Equal(5, orgUsage.CurrentUsage.Deleted);
Assert.Equal(5, orgUsage.CurrentHourUsage.Deleted);
TimeProvider.Advance(TimeSpan.FromMinutes(10));
await _usageService.SavePendingUsageAsync();
var savedOrg = await _organizationRepository.GetByIdAsync(organization.Id);
Assert.NotNull(savedOrg);
Assert.Equal(5, savedOrg.Usage.Sum(u => u.Deleted));
Assert.Null(await _projectRepository.GetByIdAsync(project.Id, o => o.IncludeSoftDeletes()));
var allEvents = await _eventRepository.GetAllAsync();
Assert.DoesNotContain(allEvents.Documents, e => String.Equals(e.ProjectId, project.Id, StringComparison.Ordinal));
}
[Fact]
public async Task RemoveProjectsAsync_SoftDeletedEmptyProject_DoesNotIncrementDeletedUsage()
{
// Arrange
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = _projectData.GenerateSampleProject();
project.IsDeleted = true;
await _projectRepository.AddAsync(project, o => o.ImmediateConsistency());
// Act
await _job.RunAsync(TestCancellationToken);
// Assert
var orgUsage = await _usageService.GetUsageAsync(organization.Id, null);
Assert.Equal(0, orgUsage.CurrentUsage.Deleted);
Assert.Equal(0, orgUsage.CurrentHourUsage.Deleted);
Assert.Null(await _projectRepository.GetByIdAsync(project.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task RemoveStacksAsync_SoftDeletedStack_IncrementsDeletedUsage()
{
// Arrange
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var stack = _stackData.GenerateSampleStack();
stack.IsDeleted = true;
await _stackRepository.AddAsync(stack, o => o.ImmediateConsistency());
var events = _eventData.GenerateEvents(3, organization.Id, project.Id, stack.Id).ToList();
await _eventRepository.AddAsync(events, o => o.ImmediateConsistency());
// Act
await _job.RunAsync(TestCancellationToken);
// Assert
var usageResponse = await _usageService.GetUsageAsync(organization.Id, project.Id);
Assert.Equal(3, usageResponse.CurrentUsage.Deleted);
Assert.Equal(3, usageResponse.CurrentHourUsage.Deleted);
TimeProvider.Advance(TimeSpan.FromMinutes(10));
await _usageService.SavePendingUsageAsync();
var savedOrg = await _organizationRepository.GetByIdAsync(organization.Id);
Assert.NotNull(savedOrg);
Assert.Equal(3, savedOrg.Usage.Sum(u => u.Deleted));
Assert.Null(await _stackRepository.GetByIdAsync(stack.Id, o => o.IncludeSoftDeletes()));
}
[Fact]
public async Task RemoveStacksAsync_MultipleProjectsSoftDeleted_TracksExactDeletedUsagePerProject()
{
// Arrange
var organization = await _organizationRepository.AddAsync(_organizationData.GenerateSampleOrganization(_billingManager, _plans), o => o.ImmediateConsistency());
var project1 = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var project2 = await _projectRepository.AddAsync(_projectData.GenerateProject(generateId: true, organizationId: organization.Id), o => o.ImmediateConsistency());
var stack1a = _stackData.GenerateStack(generateId: true, organizationId: organization.Id, projectId: project1.Id);
stack1a.IsDeleted = true;
var stack1b = _stackData.GenerateStack(generateId: true, organizationId: organization.Id, projectId: project1.Id);
stack1b.IsDeleted = true;
var stack2 = _stackData.GenerateStack(generateId: true, organizationId: organization.Id, projectId: project2.Id);
stack2.IsDeleted = true;
await _stackRepository.AddAsync([stack1a, stack1b, stack2], o => o.ImmediateConsistency());
await _eventRepository.AddAsync(_eventData.GenerateEvents(4, organization.Id, project1.Id, stack1a.Id), o => o.ImmediateConsistency());
await _eventRepository.AddAsync(_eventData.GenerateEvents(2, organization.Id, project1.Id, stack1b.Id), o => o.ImmediateConsistency());
await _eventRepository.AddAsync(_eventData.GenerateEvents(3, organization.Id, project2.Id, stack2.Id), o => o.ImmediateConsistency());
// Act
await _job.RunAsync(TestCancellationToken);
// Assert
var usageProject1 = await _usageService.GetUsageAsync(organization.Id, project1.Id);
var usageProject2 = await _usageService.GetUsageAsync(organization.Id, project2.Id);
Assert.Equal(6, usageProject1.CurrentUsage.Deleted);
Assert.Equal(3, usageProject2.CurrentUsage.Deleted);
TimeProvider.Advance(TimeSpan.FromMinutes(10));
await _usageService.SavePendingUsageAsync();
var savedOrg = await _organizationRepository.GetByIdAsync(organization.Id);
Assert.NotNull(savedOrg);
Assert.Equal(9, savedOrg.Usage.Sum(u => u.Deleted));
}
[Fact]
public async Task EnforceRetentionAsync_ExpiredEvents_DoesNotIncrementDeletedUsage()
{
// Arrange
var organization = _organizationData.GenerateSampleOrganization(_billingManager, _plans);
_billingManager.ApplyBillingPlan(organization, _plans.FreePlan);
await _organizationRepository.AddAsync(organization, o => o.ImmediateConsistency());
var project = await _projectRepository.AddAsync(_projectData.GenerateSampleProject(), o => o.ImmediateConsistency());
var options = GetService<AppOptions>();
var expiredDate = DateTimeOffset.UtcNow.SubtractDays(options.MaximumRetentionDays);
var stack = await _stackRepository.AddAsync(_stackData.GenerateSampleStack(), o => o.ImmediateConsistency());
await _eventRepository.AddAsync(_eventData.GenerateEvent(organization.Id, project.Id, stack.Id, expiredDate, expiredDate, expiredDate), o => o.ImmediateConsistency());
// Act
await _job.RunAsync(TestCancellationToken);
// Assert
var usageResponse = await _usageService.GetUsageAsync(organization.Id, project.Id);
Assert.Equal(0, usageResponse.CurrentUsage.Deleted);
Assert.Equal(0, usageResponse.CurrentHourUsage.Deleted);
}
}