MCPcopy Create free account
hub / github.com/coder/coder / TestDeleteOldAuditLogs

Function TestDeleteOldAuditLogs

coderd/database/dbpurge/dbpurge_test.go:1593–1786  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1591}
1592
1593func TestDeleteOldAuditLogs(t *testing.T) {
1594 t.Parallel()
1595
1596 now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC)
1597 retentionPeriod := 30 * 24 * time.Hour
1598 afterThreshold := now.Add(-retentionPeriod).Add(-24 * time.Hour) // 31 days ago (older than threshold)
1599 beforeThreshold := now.Add(-15 * 24 * time.Hour) // 15 days ago (newer than threshold)
1600
1601 testCases := []struct {
1602 name string
1603 retentionConfig codersdk.RetentionConfig
1604 oldLogTime time.Time
1605 recentLogTime *time.Time // nil means no recent log created
1606 expectOldDeleted bool
1607 expectedLogsRemaining int
1608 }{
1609 {
1610 name: "RetentionEnabled",
1611 retentionConfig: codersdk.RetentionConfig{
1612 AuditLogs: serpent.Duration(retentionPeriod),
1613 },
1614 oldLogTime: afterThreshold,
1615 recentLogTime: &beforeThreshold,
1616 expectOldDeleted: true,
1617 expectedLogsRemaining: 1, // only recent log remains
1618 },
1619 {
1620 name: "RetentionDisabled",
1621 retentionConfig: codersdk.RetentionConfig{
1622 AuditLogs: serpent.Duration(0),
1623 },
1624 oldLogTime: now.Add(-365 * 24 * time.Hour), // 1 year ago
1625 recentLogTime: nil,
1626 expectOldDeleted: false,
1627 expectedLogsRemaining: 1, // old log is kept
1628 },
1629 }
1630
1631 for _, tc := range testCases {
1632 t.Run(tc.name, func(t *testing.T) {
1633 t.Parallel()
1634
1635 ctx := testutil.Context(t, testutil.WaitShort)
1636 clk := quartz.NewMock(t)
1637 clk.Set(now).MustWait(ctx)
1638
1639 db, _ := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure())
1640 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})
1641
1642 // Setup test fixtures.
1643 user := dbgen.User(t, db, database.User{})
1644 org := dbgen.Organization(t, db, database.Organization{})
1645
1646 // Create old audit log.
1647 oldLog := dbgen.AuditLog(t, db, database.AuditLog{
1648 UserID: user.ID,
1649 OrganizationID: org.ID,
1650 Time: tc.oldLogTime,

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
NewDBFunction · 0.92
WithDumpOnFailureFunction · 0.92
UserFunction · 0.92
OrganizationFunction · 0.92
AuditLogFunction · 0.92
NewFunction · 0.92
WithClockFunction · 0.92
TryReceiveFunction · 0.92
awaitDoTickFunction · 0.85
nopAuditorPtrFunction · 0.85
DurationMethod · 0.80

Tested by

no test coverage detected