Skip to content

Commit abc0854

Browse files
committed
add test to ensure workspaces are not autostarted before time
1 parent faebe2e commit abc0854

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

coderd/lifecycle/executor/lifecycle_executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (e *Executor) runOnce(t time.Time) error {
109109
)
110110

111111
if err := doBuild(e.ctx, db, ws, validTransition); err != nil {
112-
e.log.Error(e.ctx, "transition workspace",
112+
e.log.Error(e.ctx, "unable to transition workspace",
113113
slog.F("workspace_id", ws.ID),
114114
slog.F("transition", validTransition),
115115
slog.Error(err),

coderd/lifecycle/executor/lifecycle_executor_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package executor_test
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
"time"
78

@@ -274,6 +275,45 @@ func Test_Executor_Workspace_Deleted(t *testing.T) {
274275
}, 5*time.Second, 250*time.Millisecond)
275276
}
276277

278+
func Test_Executor_Workspace_TooEarly(t *testing.T) {
279+
t.Parallel()
280+
281+
var (
282+
ctx = context.Background()
283+
err error
284+
tickCh = make(chan time.Time)
285+
client = coderdtest.New(t, &coderdtest.Options{
286+
LifecycleTicker: tickCh,
287+
})
288+
// Given: we have a user with a workspace
289+
workspace = mustProvisionWorkspace(t, client)
290+
)
291+
292+
// Given: the workspace initially has autostart disabled
293+
require.Empty(t, workspace.AutostopSchedule)
294+
295+
// When: we enable workspace autostart with some time in the future
296+
futureTime := time.Now().Add(time.Hour)
297+
futureTimeCron := fmt.Sprintf("%d %d * * *", futureTime.Minute(), futureTime.Hour())
298+
sched, err := schedule.Weekly(futureTimeCron)
299+
require.NoError(t, err)
300+
require.NoError(t, client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
301+
Schedule: sched.String(),
302+
}))
303+
304+
// When: the lifecycle executor ticks
305+
go func() {
306+
tickCh <- time.Now().UTC()
307+
close(tickCh)
308+
}()
309+
310+
// Then: nothing should happen
311+
require.Never(t, func() bool {
312+
ws := mustWorkspace(t, client, workspace.ID)
313+
return ws.LatestBuild.Transition != database.WorkspaceTransitionStart
314+
}, 5*time.Second, 250*time.Millisecond)
315+
}
316+
277317
func mustProvisionWorkspace(t *testing.T, client *codersdk.Client) codersdk.Workspace {
278318
t.Helper()
279319
coderdtest.NewProvisionerDaemon(t, client)

0 commit comments

Comments
 (0)