nolint:paralleltest // It uses LockIDDBPurge.
(t *testing.T)
| 505 | |
| 506 | //nolint:paralleltest // It uses LockIDDBPurge. |
| 507 | func TestDeleteOldWorkspaceAgentLogs(t *testing.T) { |
| 508 | ctx := testutil.Context(t, testutil.WaitShort) |
| 509 | clk := quartz.NewMock(t) |
| 510 | now := dbtime.Now() |
| 511 | threshold := now.Add(-7 * 24 * time.Hour) |
| 512 | beforeThreshold := threshold.Add(-24 * time.Hour) |
| 513 | afterThreshold := threshold.Add(24 * time.Hour) |
| 514 | clk.Set(now).MustWait(ctx) |
| 515 | |
| 516 | db, _ := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure()) |
| 517 | org := dbgen.Organization(t, db, database.Organization{}) |
| 518 | user := dbgen.User(t, db, database.User{}) |
| 519 | _ = dbgen.OrganizationMember(t, db, database.OrganizationMember{UserID: user.ID, OrganizationID: org.ID}) |
| 520 | tv := dbgen.TemplateVersion(t, db, database.TemplateVersion{OrganizationID: org.ID, CreatedBy: user.ID}) |
| 521 | tmpl := dbgen.Template(t, db, database.Template{OrganizationID: org.ID, ActiveVersionID: tv.ID, CreatedBy: user.ID}) |
| 522 | |
| 523 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) |
| 524 | |
| 525 | // Given the following: |
| 526 | |
| 527 | // Workspace A was built twice before the threshold, and never connected on |
| 528 | // either attempt. |
| 529 | wsA := dbgen.Workspace(t, db, database.WorkspaceTable{Name: "a", OwnerID: user.ID, OrganizationID: org.ID, TemplateID: tmpl.ID}) |
| 530 | wbA1 := mustCreateWorkspaceBuild(t, db, org, tv, wsA.ID, beforeThreshold, 1) |
| 531 | wbA2 := mustCreateWorkspaceBuild(t, db, org, tv, wsA.ID, beforeThreshold, 2) |
| 532 | agentA1 := mustCreateAgent(t, db, wbA1) |
| 533 | agentA2 := mustCreateAgent(t, db, wbA2) |
| 534 | mustCreateAgentLogs(ctx, t, db, agentA1, nil, "agent a1 logs should be deleted") |
| 535 | mustCreateAgentLogs(ctx, t, db, agentA2, nil, "agent a2 logs should be retained") |
| 536 | |
| 537 | // Workspace B was built twice before the threshold. |
| 538 | wsB := dbgen.Workspace(t, db, database.WorkspaceTable{Name: "b", OwnerID: user.ID, OrganizationID: org.ID, TemplateID: tmpl.ID}) |
| 539 | wbB1 := mustCreateWorkspaceBuild(t, db, org, tv, wsB.ID, beforeThreshold, 1) |
| 540 | wbB2 := mustCreateWorkspaceBuild(t, db, org, tv, wsB.ID, beforeThreshold, 2) |
| 541 | agentB1 := mustCreateAgent(t, db, wbB1) |
| 542 | agentB2 := mustCreateAgent(t, db, wbB2) |
| 543 | mustCreateAgentLogs(ctx, t, db, agentB1, &beforeThreshold, "agent b1 logs should be deleted") |
| 544 | mustCreateAgentLogs(ctx, t, db, agentB2, &beforeThreshold, "agent b2 logs should be retained") |
| 545 | |
| 546 | // Workspace C was built once before the threshold, and once after. |
| 547 | wsC := dbgen.Workspace(t, db, database.WorkspaceTable{Name: "c", OwnerID: user.ID, OrganizationID: org.ID, TemplateID: tmpl.ID}) |
| 548 | wbC1 := mustCreateWorkspaceBuild(t, db, org, tv, wsC.ID, beforeThreshold, 1) |
| 549 | wbC2 := mustCreateWorkspaceBuild(t, db, org, tv, wsC.ID, afterThreshold, 2) |
| 550 | agentC1 := mustCreateAgent(t, db, wbC1) |
| 551 | agentC2 := mustCreateAgent(t, db, wbC2) |
| 552 | mustCreateAgentLogs(ctx, t, db, agentC1, &beforeThreshold, "agent c1 logs should be deleted") |
| 553 | mustCreateAgentLogs(ctx, t, db, agentC2, &afterThreshold, "agent c2 logs should be retained") |
| 554 | |
| 555 | // Workspace D was built twice after the threshold. |
| 556 | wsD := dbgen.Workspace(t, db, database.WorkspaceTable{Name: "d", OwnerID: user.ID, OrganizationID: org.ID, TemplateID: tmpl.ID}) |
| 557 | wbD1 := mustCreateWorkspaceBuild(t, db, org, tv, wsD.ID, afterThreshold, 1) |
| 558 | wbD2 := mustCreateWorkspaceBuild(t, db, org, tv, wsD.ID, afterThreshold, 2) |
| 559 | agentD1 := mustCreateAgent(t, db, wbD1) |
| 560 | agentD2 := mustCreateAgent(t, db, wbD2) |
| 561 | mustCreateAgentLogs(ctx, t, db, agentD1, &afterThreshold, "agent d1 logs should be retained") |
| 562 | mustCreateAgentLogs(ctx, t, db, agentD2, &afterThreshold, "agent d2 logs should be retained") |
| 563 | |
| 564 | // Workspace E was build once after threshold but never connected. |
nothing calls this directly
no test coverage detected