(t *testing.T)
| 1232 | } |
| 1233 | |
| 1234 | func TestDeleteOldConnectionLogs(t *testing.T) { |
| 1235 | t.Parallel() |
| 1236 | |
| 1237 | now := time.Date(2025, 1, 15, 7, 30, 0, 0, time.UTC) |
| 1238 | retentionPeriod := 30 * 24 * time.Hour |
| 1239 | afterThreshold := now.Add(-retentionPeriod).Add(-24 * time.Hour) // 31 days ago (older than threshold) |
| 1240 | beforeThreshold := now.Add(-15 * 24 * time.Hour) // 15 days ago (newer than threshold) |
| 1241 | |
| 1242 | testCases := []struct { |
| 1243 | name string |
| 1244 | retentionConfig codersdk.RetentionConfig |
| 1245 | oldLogTime time.Time |
| 1246 | recentLogTime *time.Time // nil means no recent log created |
| 1247 | expectOldDeleted bool |
| 1248 | expectedLogsRemaining int |
| 1249 | }{ |
| 1250 | { |
| 1251 | name: "RetentionEnabled", |
| 1252 | retentionConfig: codersdk.RetentionConfig{ |
| 1253 | ConnectionLogs: serpent.Duration(retentionPeriod), |
| 1254 | }, |
| 1255 | oldLogTime: afterThreshold, |
| 1256 | recentLogTime: &beforeThreshold, |
| 1257 | expectOldDeleted: true, |
| 1258 | expectedLogsRemaining: 1, // only recent log remains |
| 1259 | }, |
| 1260 | { |
| 1261 | name: "RetentionDisabled", |
| 1262 | retentionConfig: codersdk.RetentionConfig{ |
| 1263 | ConnectionLogs: serpent.Duration(0), |
| 1264 | }, |
| 1265 | oldLogTime: now.Add(-365 * 24 * time.Hour), // 1 year ago |
| 1266 | recentLogTime: nil, |
| 1267 | expectOldDeleted: false, |
| 1268 | expectedLogsRemaining: 1, // old log is kept |
| 1269 | }, |
| 1270 | } |
| 1271 | |
| 1272 | for _, tc := range testCases { |
| 1273 | t.Run(tc.name, func(t *testing.T) { |
| 1274 | t.Parallel() |
| 1275 | |
| 1276 | ctx := testutil.Context(t, testutil.WaitShort) |
| 1277 | clk := quartz.NewMock(t) |
| 1278 | clk.Set(now).MustWait(ctx) |
| 1279 | |
| 1280 | db, _ := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure()) |
| 1281 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) |
| 1282 | |
| 1283 | // Setup test fixtures. |
| 1284 | user := dbgen.User(t, db, database.User{}) |
| 1285 | org := dbgen.Organization(t, db, database.Organization{}) |
| 1286 | _ = dbgen.OrganizationMember(t, db, database.OrganizationMember{UserID: user.ID, OrganizationID: org.ID}) |
| 1287 | tv := dbgen.TemplateVersion(t, db, database.TemplateVersion{OrganizationID: org.ID, CreatedBy: user.ID}) |
| 1288 | tmpl := dbgen.Template(t, db, database.Template{OrganizationID: org.ID, ActiveVersionID: tv.ID, CreatedBy: user.ID}) |
| 1289 | workspace := dbgen.Workspace(t, db, database.WorkspaceTable{ |
| 1290 | OwnerID: user.ID, |
| 1291 | OrganizationID: org.ID, |
nothing calls this directly
no test coverage detected