MCPcopy Index your code
hub / github.com/coder/coder / TestExecutorPrebuilds

Function TestExecutorPrebuilds

coderd/autobuild/lifecycle_executor_test.go:1347–1553  ·  view source on GitHub ↗

TestExecutorPrebuilds verifies AGPL behavior for prebuilt workspaces. It ensures that workspace schedules do not trigger while the workspace is still in a prebuilt state. Scheduling behavior only applies after the workspace has been claimed and becomes a regular user workspace. For enterprise-relate

(t *testing.T)

Source from the content-addressed store, hash-verified

1345// workspace has been claimed and becomes a regular user workspace.
1346// For enterprise-related functionality, see enterprise/coderd/workspaces_test.go.
1347func TestExecutorPrebuilds(t *testing.T) {
1348 t.Parallel()
1349
1350 // Prebuild workspaces should not be autostopped when the deadline is reached.
1351 // After being claimed, the workspace should stop at the deadline.
1352 t.Run("OnlyStopsAfterClaimed", func(t *testing.T) {
1353 t.Parallel()
1354
1355 // Setup
1356 ctx := testutil.Context(t, testutil.WaitShort)
1357 clock := quartz.NewMock(t)
1358 db, pb := dbtestutil.NewDB(t, dbtestutil.WithDumpOnFailure())
1359 var (
1360 tickCh = make(chan time.Time)
1361 statsCh = make(chan autobuild.Stats)
1362 client = coderdtest.New(t, &coderdtest.Options{
1363 Database: db,
1364 Pubsub: pb,
1365 AutobuildTicker: tickCh,
1366 IncludeProvisionerDaemon: true,
1367 AutobuildStats: statsCh,
1368 })
1369 )
1370
1371 // Setup user, template and template version
1372 owner := coderdtest.CreateFirstUser(t, client)
1373 _, user := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleMember())
1374 version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
1375 coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
1376 template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
1377
1378 // Database setup of a preset with a prebuild instance
1379 preset := setupTestDBPreset(t, db, version.ID, int32(1))
1380
1381 // Given: a running prebuilt workspace with a deadline and ready to be claimed
1382 dbPrebuild := setupTestDBPrebuiltWorkspace(
1383 ctx, t, clock, db, pb,
1384 owner.OrganizationID,
1385 template.ID,
1386 version.ID,
1387 preset.ID,
1388 )
1389 prebuild := coderdtest.MustWorkspace(t, client, dbPrebuild.ID)
1390 require.Equal(t, codersdk.WorkspaceTransitionStart, prebuild.LatestBuild.Transition)
1391 require.NotZero(t, prebuild.LatestBuild.Deadline)
1392
1393 p, err := coderdtest.GetProvisionerForTags(db, time.Now(), prebuild.OrganizationID, nil)
1394 require.NoError(t, err)
1395
1396 // When: the autobuild executor ticks *after* the deadline:
1397 go func() {
1398 tickTime := prebuild.LatestBuild.Deadline.Time.Add(time.Minute)
1399 coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime)
1400 tickCh <- tickTime
1401 }()
1402
1403 // Then: the prebuilt workspace should remain in a start transition
1404 prebuildStats := testutil.RequireReceive(ctx, t, statsCh)

Callers

nothing calls this directly

Calls 15

ContextFunction · 0.92
NewDBFunction · 0.92
WithDumpOnFailureFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
RoleMemberFunction · 0.92
CreateTemplateVersionFunction · 0.92
CreateTemplateFunction · 0.92
MustWorkspaceFunction · 0.92
GetProvisionerForTagsFunction · 0.92

Tested by

no test coverage detected