Skip to content

Commit b6fcb20

Browse files
committed
autobuild: fixup unit tests, clarify behaviour
1 parent 597fbd3 commit b6fcb20

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

coderd/autobuild/executor/lifecycle_executor.go

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (e *Executor) Run() {
5050
func (e *Executor) runOnce(t time.Time) error {
5151
currentTick := t.Truncate(time.Minute)
5252
return e.db.InTx(func(db database.Store) error {
53+
// XXX: if a workspace build is created when ttl != null and then ttl is unset,
54+
// autostop will still happen for this workspace build. Whether this is
55+
// expected behavior from a user's perspective is not yet known.
5356
eligibleWorkspaces, err := db.GetWorkspacesAutostart(e.ctx)
5457
if err != nil {
5558
return xerrors.Errorf("get eligible workspaces for autostart or autostop: %w", err)

coderd/autobuild/executor/lifecycle_executor_test.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,14 @@ func TestExecutorAutostopAlreadyStopped(t *testing.T) {
271271
workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) {
272272
cwr.AutostartSchedule = nil
273273
})
274-
ttl = *workspace.TTL
275274
)
276275

277276
// Given: workspace is stopped
278277
workspace = mustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
279278

280279
// When: the autobuild executor ticks past the TTL
281280
go func() {
282-
tickCh <- time.Now().UTC().Add(ttl + time.Minute)
281+
tickCh <- workspace.LatestBuild.Deadline.Add(time.Minute)
283282
close(tickCh)
284283
}()
285284

@@ -313,7 +312,7 @@ func TestExecutorAutostopNotEnabled(t *testing.T) {
313312

314313
// When: the autobuild executor ticks past the TTL
315314
go func() {
316-
tickCh <- time.Now().UTC().Add(time.Minute)
315+
tickCh <- workspace.LatestBuild.Deadline.Add(time.Minute)
317316
close(tickCh)
318317
}()
319318

@@ -401,7 +400,7 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
401400
require.Equal(t, codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running")
402401
}
403402

404-
func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
403+
func TestExecutorWorkspaceAutostopBeforeDeadline(t *testing.T) {
405404
t.Parallel()
406405

407406
var (
@@ -416,7 +415,7 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
416415

417416
// When: the autobuild executor ticks before the TTL
418417
go func() {
419-
tickCh <- time.Now().UTC()
418+
tickCh <- workspace.LatestBuild.Deadline.Add(-1 * time.Minute)
420419
close(tickCh)
421420
}()
422421

@@ -427,6 +426,38 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
427426
require.Equal(t, codersdk.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running")
428427
}
429428

429+
func TestExecutorWorkspaceAutostopNoWaitChangedMyMind(t *testing.T) {
430+
t.Parallel()
431+
432+
var (
433+
ctx = context.Background()
434+
tickCh = make(chan time.Time)
435+
client = coderdtest.New(t, &coderdtest.Options{
436+
AutobuildTicker: tickCh,
437+
IncludeProvisionerD: true,
438+
})
439+
// Given: we have a user with a workspace
440+
workspace = mustProvisionWorkspace(t, client)
441+
)
442+
443+
// Given: the user changes their mind and decides their workspace should not auto-stop
444+
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{TTL: nil})
445+
require.NoError(t, err)
446+
447+
// When: the autobuild executor ticks after the deadline
448+
go func() {
449+
tickCh <- workspace.LatestBuild.Deadline.Add(time.Minute)
450+
close(tickCh)
451+
}()
452+
453+
// Then: the workspace should still stop - sorry!
454+
<-time.After(5 * time.Second)
455+
ws := mustWorkspace(t, client, workspace.ID)
456+
require.NotEqual(t, workspace.LatestBuild.ID, ws.LatestBuild.ID, "expected a workspace build to occur")
457+
require.Equal(t, codersdk.ProvisionerJobSucceeded, ws.LatestBuild.Job.Status, "expected provisioner job to have succeeded")
458+
require.Equal(t, codersdk.WorkspaceTransitionStop, ws.LatestBuild.Transition, "expected workspace not to be running")
459+
}
460+
430461
func TestExecutorAutostartMultipleOK(t *testing.T) {
431462
if os.Getenv("DB") == "" {
432463
t.Skip(`This test only really works when using a "real" database, similar to a HA setup`)

0 commit comments

Comments
 (0)