Skip to content

Commit 6a016b8

Browse files
committed
fix test
1 parent 0d2c132 commit 6a016b8

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

coderd/database/dbpurge/dbpurge_test.go

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,12 @@ func containsWorkspaceAgentStat(stats []database.GetWorkspaceAgentStatsRow, need
182182

183183
//nolint:paralleltest // It uses LockIDDBPurge.
184184
func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
185-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
186-
defer cancel()
185+
ctx := testutil.Context(t, testutil.WaitShort)
187186
clk := quartz.NewMock(t)
188187
now := dbtime.Now()
189188
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)
192191
clk.Set(now).MustWait(ctx)
193192

194193
db, _ := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure())
@@ -202,44 +201,48 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
202201

203202
// Given the following:
204203

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})
207207
wbA1 := mustCreateWorkspaceBuild(t, db, org, tv, wsA.ID, beforeThreshold, 1)
208+
wbA2 := mustCreateWorkspaceBuild(t, db, org, tv, wsA.ID, beforeThreshold, 2)
208209
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")
210213

211214
// 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})
213216
wbB1 := mustCreateWorkspaceBuild(t, db, org, tv, wsB.ID, beforeThreshold, 1)
214217
wbB2 := mustCreateWorkspaceBuild(t, db, org, tv, wsB.ID, beforeThreshold, 2)
215218
agentB1 := mustCreateAgent(t, db, wbB1)
216219
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")
219222

220223
// 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})
222225
wbC1 := mustCreateWorkspaceBuild(t, db, org, tv, wsC.ID, beforeThreshold, 1)
223226
wbC2 := mustCreateWorkspaceBuild(t, db, org, tv, wsC.ID, afterThreshold, 2)
224227
agentC1 := mustCreateAgent(t, db, wbC1)
225228
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")
228231

229232
// 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})
231234
wbD1 := mustCreateWorkspaceBuild(t, db, org, tv, wsD.ID, afterThreshold, 1)
232235
wbD2 := mustCreateWorkspaceBuild(t, db, org, tv, wsD.ID, afterThreshold, 2)
233236
agentD1 := mustCreateAgent(t, db, wbD1)
234237
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")
237240

238241
// 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})
240243
wbE1 := mustCreateWorkspaceBuild(t, db, org, tv, wsE.ID, beforeThreshold, 1)
241244
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")
243246

244247
// when dbpurge runs
245248

@@ -260,14 +263,17 @@ func TestDeleteOldWorkspaceAgentLogs(t *testing.T) {
260263
w.MustWait(ctx)
261264

262265
// 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.
264268
assertNoWorkspaceAgentLogs(ctx, t, db, agentA1.ID)
265269
// Agent B1 is not the latest build and the logs are from before threshold.
266270
assertNoWorkspaceAgentLogs(ctx, t, db, agentB1.ID)
267271
// Agent C1 is not the latest build and the logs are from before threshold.
268272
assertNoWorkspaceAgentLogs(ctx, t, db, agentC1.ID)
269273

270274
// 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")
271277
// Agent B2 is the latest build.
272278
assertWorkspaceAgentLogs(ctx, t, db, agentB2.ID, "agent b2 logs should be retained")
273279
// Agent C2 is the latest build.
@@ -331,7 +337,11 @@ func mustCreateAgent(t *testing.T, db database.Store, wb database.WorkspaceBuild
331337
CreatedAt: wb.CreatedAt,
332338
})
333339

340+
ws, err := db.GetWorkspaceByID(context.Background(), wb.WorkspaceID)
341+
require.NoError(t, err)
342+
334343
wa := dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{
344+
Name: fmt.Sprintf("%s%d", ws.Name, wb.BuildNumber),
335345
ResourceID: resource.ID,
336346
CreatedAt: wb.CreatedAt,
337347
FirstConnectedAt: sql.NullTime{},
@@ -342,28 +352,27 @@ func mustCreateAgent(t *testing.T, db database.Store, wb database.WorkspaceBuild
342352
return wa
343353
}
344354

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) {
346356
t.Helper()
347357
if agentLastConnectedAt != nil {
348358
require.NoError(t, db.UpdateWorkspaceAgentConnectionByID(ctx, database.UpdateWorkspaceAgentConnectionByIDParams{
349-
ID: agentID,
359+
ID: agent.ID,
350360
LastConnectedAt: sql.NullTime{Time: *agentLastConnectedAt, Valid: true},
351361
}))
352362
}
353363
_, 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},
358368
})
359369
require.NoError(t, err)
360370
// Make sure that agent logs have been collected.
361371
agentLogs, err := db.GetWorkspaceAgentLogsAfter(ctx, database.GetWorkspaceAgentLogsAfterParams{
362-
AgentID: agentID,
372+
AgentID: agent.ID,
363373
})
364374
require.NoError(t, err)
365375
require.NotEmpty(t, agentLogs, "agent logs must be present")
366-
return agentID
367376
}
368377

369378
//nolint:paralleltest // It uses LockIDDBPurge.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy