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

Function TestExtraneous

coderd/prebuilds/preset_snapshot_test.go:543–589  ·  view source on GitHub ↗

Additional prebuilds exist for a given preset configuration; these must be deleted.

(t *testing.T)

Source from the content-addressed store, hash-verified

541
542// Additional prebuilds exist for a given preset configuration; these must be deleted.
543func TestExtraneous(t *testing.T) {
544 t.Parallel()
545 current := opts[optionSet0]
546 clock := quartz.NewMock(t)
547
548 // GIVEN: a preset with 1 desired prebuild.
549 presets := []database.GetTemplatePresetsWithPrebuildsRow{
550 preset(true, 1, current),
551 }
552
553 var older uuid.UUID
554 // GIVEN: 2 running prebuilds for the preset.
555 running := []database.GetRunningPrebuiltWorkspacesRow{
556 prebuiltWorkspace(current, clock, func(row database.GetRunningPrebuiltWorkspacesRow) database.GetRunningPrebuiltWorkspacesRow {
557 // The older of the running prebuilds will be deleted in order to maintain freshness.
558 row.CreatedAt = clock.Now().Add(-time.Hour)
559 older = row.ID
560 return row
561 }),
562 prebuiltWorkspace(current, clock, func(row database.GetRunningPrebuiltWorkspacesRow) database.GetRunningPrebuiltWorkspacesRow {
563 row.CreatedAt = clock.Now()
564 return row
565 }),
566 }
567
568 // GIVEN: NO prebuilds in progress.
569 var inProgress []database.CountInProgressPrebuildsRow
570
571 // WHEN: calculating the current preset's state.
572 snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil, nil, quartz.NewMock(t), testutil.Logger(t))
573 ps, err := snapshot.FilterByPreset(current.presetID)
574 require.NoError(t, err)
575
576 // THEN: an extraneous prebuild is detected and marked for deletion.
577 state := ps.CalculateState()
578 actions, err := ps.CalculateActions(backoffInterval)
579 require.NoError(t, err)
580 validateState(t, prebuilds.ReconciliationState{
581 Actual: 2, Desired: 1, Extraneous: 1, Eligible: 2,
582 }, *state)
583 validateActions(t, []*prebuilds.ReconciliationActions{
584 {
585 ActionType: prebuilds.ActionTypeDelete,
586 DeleteIDs: []uuid.UUID{older},
587 },
588 }, actions)
589}
590
591// A prebuild is considered Expired when it has exceeded their time-to-live (TTL)
592// specified in the preset's cache invalidation invalidate_after_secs parameter.

Callers

nothing calls this directly

Calls 10

FilterByPresetMethod · 0.95
NewGlobalSnapshotFunction · 0.92
LoggerFunction · 0.92
presetFunction · 0.85
prebuiltWorkspaceFunction · 0.85
validateStateFunction · 0.85
validateActionsFunction · 0.85
CalculateStateMethod · 0.80
AddMethod · 0.65
CalculateActionsMethod · 0.45

Tested by

no test coverage detected