@@ -182,13 +182,12 @@ func containsWorkspaceAgentStat(stats []database.GetWorkspaceAgentStatsRow, need
182
182
183
183
//nolint:paralleltest // It uses LockIDDBPurge.
184
184
func TestDeleteOldWorkspaceAgentLogs (t * testing.T ) {
185
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
186
- defer cancel ()
185
+ ctx := testutil .Context (t , testutil .WaitShort )
187
186
clk := quartz .NewMock (t )
188
187
now := dbtime .Now ()
189
188
threshold := now .Add (- 7 * 24 * time .Hour )
190
- beforeThreshold := threshold .Add (- time .Hour )
191
- afterThreshold := threshold .Add (time .Hour )
189
+ beforeThreshold := threshold .Add (- 24 * time .Hour )
190
+ afterThreshold := threshold .Add (24 * time .Hour )
192
191
clk .Set (now ).MustWait (ctx )
193
192
194
193
db , _ := dbtestutil .NewDB (t , dbtestutil .WithDumpOnFailure ())
@@ -202,44 +201,48 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
202
201
203
202
// Given the following:
204
203
205
- // Workspace A was built once before the threshold, and never connected.
206
- wsA := dbgen .Workspace (t , db , database.Workspace {OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
204
+ // Workspace A was built twice before the threshold, and never connected on
205
+ // either attempt.
206
+ wsA := dbgen .Workspace (t , db , database.Workspace {Name : "a" , OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
207
207
wbA1 := mustCreateWorkspaceBuild (t , db , org , tv , wsA .ID , beforeThreshold , 1 )
208
+ wbA2 := mustCreateWorkspaceBuild (t , db , org , tv , wsA .ID , beforeThreshold , 2 )
208
209
agentA1 := mustCreateAgent (t , db , wbA1 )
209
- mustCreateAgentLogs (ctx , t , db , agentA1 .ID , nil , "agent a1 logs should be deleted" )
210
+ agentA2 := mustCreateAgent (t , db , wbA2 )
211
+ mustCreateAgentLogs (ctx , t , db , agentA1 , nil , "agent a1 logs should be deleted" )
212
+ mustCreateAgentLogs (ctx , t , db , agentA2 , nil , "agent a2 logs should be retained" )
210
213
211
214
// Workspace B was built twice before the threshold.
212
- wsB := dbgen .Workspace (t , db , database.Workspace {OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
215
+ wsB := dbgen .Workspace (t , db , database.Workspace {Name : "b" , OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
213
216
wbB1 := mustCreateWorkspaceBuild (t , db , org , tv , wsB .ID , beforeThreshold , 1 )
214
217
wbB2 := mustCreateWorkspaceBuild (t , db , org , tv , wsB .ID , beforeThreshold , 2 )
215
218
agentB1 := mustCreateAgent (t , db , wbB1 )
216
219
agentB2 := mustCreateAgent (t , db , wbB2 )
217
- mustCreateAgentLogs (ctx , t , db , agentB1 . ID , & beforeThreshold , "agent b1 logs should be deleted" )
218
- mustCreateAgentLogs (ctx , t , db , agentB2 . ID , & beforeThreshold , "agent b2 logs should be retained" )
220
+ mustCreateAgentLogs (ctx , t , db , agentB1 , & beforeThreshold , "agent b1 logs should be deleted" )
221
+ mustCreateAgentLogs (ctx , t , db , agentB2 , & beforeThreshold , "agent b2 logs should be retained" )
219
222
220
223
// Workspace C was built once before the threshold, and once after.
221
- wsC := dbgen .Workspace (t , db , database.Workspace {OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
224
+ wsC := dbgen .Workspace (t , db , database.Workspace {Name : "c" , OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
222
225
wbC1 := mustCreateWorkspaceBuild (t , db , org , tv , wsC .ID , beforeThreshold , 1 )
223
226
wbC2 := mustCreateWorkspaceBuild (t , db , org , tv , wsC .ID , afterThreshold , 2 )
224
227
agentC1 := mustCreateAgent (t , db , wbC1 )
225
228
agentC2 := mustCreateAgent (t , db , wbC2 )
226
- mustCreateAgentLogs (ctx , t , db , agentC1 . ID , & beforeThreshold , "agent c1 logs should be deleted" )
227
- mustCreateAgentLogs (ctx , t , db , agentC2 . ID , & afterThreshold , "agent c2 logs should be retained" )
229
+ mustCreateAgentLogs (ctx , t , db , agentC1 , & beforeThreshold , "agent c1 logs should be deleted" )
230
+ mustCreateAgentLogs (ctx , t , db , agentC2 , & afterThreshold , "agent c2 logs should be retained" )
228
231
229
232
// Workspace D was built twice after the threshold.
230
- wsD := dbgen .Workspace (t , db , database.Workspace {OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
233
+ wsD := dbgen .Workspace (t , db , database.Workspace {Name : "d" , OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
231
234
wbD1 := mustCreateWorkspaceBuild (t , db , org , tv , wsD .ID , afterThreshold , 1 )
232
235
wbD2 := mustCreateWorkspaceBuild (t , db , org , tv , wsD .ID , afterThreshold , 2 )
233
236
agentD1 := mustCreateAgent (t , db , wbD1 )
234
237
agentD2 := mustCreateAgent (t , db , wbD2 )
235
- mustCreateAgentLogs (ctx , t , db , agentD1 . ID , & afterThreshold , "agent d1 logs should be retained" )
236
- mustCreateAgentLogs (ctx , t , db , agentD2 . ID , & afterThreshold , "agent d2 logs should be retained" )
238
+ mustCreateAgentLogs (ctx , t , db , agentD1 , & afterThreshold , "agent d1 logs should be retained" )
239
+ mustCreateAgentLogs (ctx , t , db , agentD2 , & afterThreshold , "agent d2 logs should be retained" )
237
240
238
241
// Workspace E was build once after threshold but never connected.
239
- wsE := dbgen .Workspace (t , db , database.Workspace {OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
242
+ wsE := dbgen .Workspace (t , db , database.Workspace {Name : "e" , OwnerID : user .ID , OrganizationID : org .ID , TemplateID : tmpl .ID })
240
243
wbE1 := mustCreateWorkspaceBuild (t , db , org , tv , wsE .ID , beforeThreshold , 1 )
241
244
agentE1 := mustCreateAgent (t , db , wbE1 )
242
- mustCreateAgentLogs (ctx , t , db , agentE1 . ID , nil , "agent e1 logs should be retained" )
245
+ mustCreateAgentLogs (ctx , t , db , agentE1 , nil , "agent e1 logs should be retained" )
243
246
244
247
// when dbpurge runs
245
248
@@ -260,14 +263,17 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
260
263
w .MustWait (ctx )
261
264
262
265
// then logs related to the following agents should be deleted:
263
- // Agent A1 never connected and was created before the threshold.
266
+ // Agent A1 never connected, was created before the threshold, and is not the
267
+ // latest build.
264
268
assertNoWorkspaceAgentLogs (ctx , t , db , agentA1 .ID )
265
269
// Agent B1 is not the latest build and the logs are from before threshold.
266
270
assertNoWorkspaceAgentLogs (ctx , t , db , agentB1 .ID )
267
271
// Agent C1 is not the latest build and the logs are from before threshold.
268
272
assertNoWorkspaceAgentLogs (ctx , t , db , agentC1 .ID )
269
273
270
274
// then logs related to the following agents should be retained:
275
+ // Agent A2 is the latest build.
276
+ assertWorkspaceAgentLogs (ctx , t , db , agentA2 .ID , "agent a2 logs should be retained" )
271
277
// Agent B2 is the latest build.
272
278
assertWorkspaceAgentLogs (ctx , t , db , agentB2 .ID , "agent b2 logs should be retained" )
273
279
// Agent C2 is the latest build.
@@ -331,7 +337,11 @@ func mustCreateAgent(t *testing.T, db database.Store, wb database.WorkspaceBuild
331
337
CreatedAt : wb .CreatedAt ,
332
338
})
333
339
340
+ ws , err := db .GetWorkspaceByID (context .Background (), wb .WorkspaceID )
341
+ require .NoError (t , err )
342
+
334
343
wa := dbgen .WorkspaceAgent (t , db , database.WorkspaceAgent {
344
+ Name : fmt .Sprintf ("%s%d" , ws .Name , wb .BuildNumber ),
335
345
ResourceID : resource .ID ,
336
346
CreatedAt : wb .CreatedAt ,
337
347
FirstConnectedAt : sql.NullTime {},
@@ -342,28 +352,27 @@ func mustCreateAgent(t *testing.T, db database.Store, wb database.WorkspaceBuild
342
352
return wa
343
353
}
344
354
345
- func mustCreateAgentLogs (ctx context.Context , t * testing.T , db database.Store , agentID uuid. UUID , agentLastConnectedAt * time.Time , output string ) uuid. UUID {
355
+ func mustCreateAgentLogs (ctx context.Context , t * testing.T , db database.Store , agent database. WorkspaceAgent , agentLastConnectedAt * time.Time , output string ) {
346
356
t .Helper ()
347
357
if agentLastConnectedAt != nil {
348
358
require .NoError (t , db .UpdateWorkspaceAgentConnectionByID (ctx , database.UpdateWorkspaceAgentConnectionByIDParams {
349
- ID : agentID ,
359
+ ID : agent . ID ,
350
360
LastConnectedAt : sql.NullTime {Time : * agentLastConnectedAt , Valid : true },
351
361
}))
352
362
}
353
363
_ , err := db .InsertWorkspaceAgentLogs (ctx , database.InsertWorkspaceAgentLogsParams {
354
- AgentID : agentID ,
355
- // CreatedAt: agentLastConnectedAt ,
356
- Output : []string {output },
357
- Level : []database.LogLevel {database .LogLevelDebug },
364
+ AgentID : agent . ID ,
365
+ CreatedAt : agent . CreatedAt ,
366
+ Output : []string {output },
367
+ Level : []database.LogLevel {database .LogLevelDebug },
358
368
})
359
369
require .NoError (t , err )
360
370
// Make sure that agent logs have been collected.
361
371
agentLogs , err := db .GetWorkspaceAgentLogsAfter (ctx , database.GetWorkspaceAgentLogsAfterParams {
362
- AgentID : agentID ,
372
+ AgentID : agent . ID ,
363
373
})
364
374
require .NoError (t , err )
365
375
require .NotEmpty (t , agentLogs , "agent logs must be present" )
366
- return agentID
367
376
}
368
377
369
378
//nolint:paralleltest // It uses LockIDDBPurge.
0 commit comments