Skip to content

Commit fb222ec

Browse files
committed
chore: add a unit test to ensure correct behaviour with multiple coderd replicas
1 parent e6168ba commit fb222ec

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

coderd/autobuild/executor/lifecycle_executor_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package executor_test
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"testing"
78
"time"
89

@@ -370,6 +371,60 @@ func TestExecutorWorkspaceTooEarly(t *testing.T) {
370371
require.Equal(t, database.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected workspace to be running")
371372
}
372373

374+
func TestExecutorAutostartMultipleOK(t *testing.T) {
375+
if os.Getenv("DB") == "" {
376+
t.Skip(`This test only really works when using a "real" database, similar to a HA setup`)
377+
}
378+
379+
t.Parallel()
380+
381+
var (
382+
ctx = context.Background()
383+
err error
384+
tickCh = make(chan time.Time)
385+
tickCh2 = make(chan time.Time)
386+
client = coderdtest.New(t, &coderdtest.Options{
387+
LifecycleTicker: tickCh,
388+
})
389+
_ = coderdtest.New(t, &coderdtest.Options{
390+
LifecycleTicker: tickCh2,
391+
})
392+
// Given: we have a user with a workspace
393+
workspace = mustProvisionWorkspace(t, client)
394+
)
395+
// Given: workspace is stopped
396+
workspace = mustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
397+
398+
// Given: the workspace initially has autostart disabled
399+
require.Empty(t, workspace.AutostartSchedule)
400+
401+
// When: we enable workspace autostart
402+
sched, err := schedule.Weekly("* * * * *")
403+
require.NoError(t, err)
404+
require.NoError(t, client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
405+
Schedule: sched.String(),
406+
}))
407+
408+
// When: the autobuild executor ticks
409+
go func() {
410+
tickCh <- time.Now().UTC().Add(time.Minute)
411+
tickCh2 <- time.Now().UTC().Add(time.Minute)
412+
close(tickCh)
413+
close(tickCh2)
414+
}()
415+
416+
// Then: the workspace should be started
417+
<-time.After(5 * time.Second)
418+
ws := mustWorkspace(t, client, workspace.ID)
419+
require.NotEqual(t, workspace.LatestBuild.ID, ws.LatestBuild.ID, "expected a workspace build to occur")
420+
require.Equal(t, codersdk.ProvisionerJobSucceeded, ws.LatestBuild.Job.Status, "expected provisioner job to have succeeded")
421+
require.Equal(t, database.WorkspaceTransitionStart, ws.LatestBuild.Transition, "expected latest transition to be start")
422+
builds, err := client.WorkspaceBuilds(ctx, ws.ID)
423+
require.NoError(t, err, "fetch list of workspace builds from primary")
424+
// One build to start, one stop transition, and one autostart. No more.
425+
require.Len(t, builds, 3, "unexpected number of builds for workspace from primary")
426+
}
427+
373428
func mustProvisionWorkspace(t *testing.T, client *codersdk.Client) codersdk.Workspace {
374429
t.Helper()
375430
coderdtest.NewProvisionerDaemon(t, client)

0 commit comments

Comments
 (0)