@@ -3,6 +3,7 @@ package executor_test
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "os"
6
7
"testing"
7
8
"time"
8
9
@@ -370,6 +371,60 @@ func TestExecutorWorkspaceTooEarly(t *testing.T) {
370
371
require .Equal (t , database .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
371
372
}
372
373
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
+
373
428
func mustProvisionWorkspace (t * testing.T , client * codersdk.Client ) codersdk.Workspace {
374
429
t .Helper ()
375
430
coderdtest .NewProvisionerDaemon (t , client )
0 commit comments