@@ -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
@@ -25,7 +26,7 @@ func TestExecutorAutostartOK(t *testing.T) {
25
26
err error
26
27
tickCh = make (chan time.Time )
27
28
client = coderdtest .New (t , & coderdtest.Options {
28
- LifecycleTicker : tickCh ,
29
+ AutobuildTicker : tickCh ,
29
30
})
30
31
// Given: we have a user with a workspace
31
32
workspace = mustProvisionWorkspace (t , client )
@@ -65,7 +66,7 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
65
66
err error
66
67
tickCh = make (chan time.Time )
67
68
client = coderdtest .New (t , & coderdtest.Options {
68
- LifecycleTicker : tickCh ,
69
+ AutobuildTicker : tickCh ,
69
70
})
70
71
// Given: we have a user with a workspace
71
72
workspace = mustProvisionWorkspace (t , client )
@@ -117,7 +118,7 @@ func TestExecutorAutostartAlreadyRunning(t *testing.T) {
117
118
err error
118
119
tickCh = make (chan time.Time )
119
120
client = coderdtest .New (t , & coderdtest.Options {
120
- LifecycleTicker : tickCh ,
121
+ AutobuildTicker : tickCh ,
121
122
})
122
123
// Given: we have a user with a workspace
123
124
workspace = mustProvisionWorkspace (t , client )
@@ -155,7 +156,7 @@ func TestExecutorAutostartNotEnabled(t *testing.T) {
155
156
var (
156
157
tickCh = make (chan time.Time )
157
158
client = coderdtest .New (t , & coderdtest.Options {
158
- LifecycleTicker : tickCh ,
159
+ AutobuildTicker : tickCh ,
159
160
})
160
161
// Given: we have a user with a workspace
161
162
workspace = mustProvisionWorkspace (t , client )
@@ -188,7 +189,7 @@ func TestExecutorAutostopOK(t *testing.T) {
188
189
err error
189
190
tickCh = make (chan time.Time )
190
191
client = coderdtest .New (t , & coderdtest.Options {
191
- LifecycleTicker : tickCh ,
192
+ AutobuildTicker : tickCh ,
192
193
})
193
194
// Given: we have a user with a workspace
194
195
workspace = mustProvisionWorkspace (t , client )
@@ -228,7 +229,7 @@ func TestExecutorAutostopAlreadyStopped(t *testing.T) {
228
229
err error
229
230
tickCh = make (chan time.Time )
230
231
client = coderdtest .New (t , & coderdtest.Options {
231
- LifecycleTicker : tickCh ,
232
+ AutobuildTicker : tickCh ,
232
233
})
233
234
// Given: we have a user with a workspace
234
235
workspace = mustProvisionWorkspace (t , client )
@@ -266,7 +267,7 @@ func TestExecutorAutostopNotEnabled(t *testing.T) {
266
267
var (
267
268
tickCh = make (chan time.Time )
268
269
client = coderdtest .New (t , & coderdtest.Options {
269
- LifecycleTicker : tickCh ,
270
+ AutobuildTicker : tickCh ,
270
271
})
271
272
// Given: we have a user with a workspace
272
273
workspace = mustProvisionWorkspace (t , client )
@@ -299,7 +300,7 @@ func TestExecutorWorkspaceDeleted(t *testing.T) {
299
300
err error
300
301
tickCh = make (chan time.Time )
301
302
client = coderdtest .New (t , & coderdtest.Options {
302
- LifecycleTicker : tickCh ,
303
+ AutobuildTicker : tickCh ,
303
304
})
304
305
// Given: we have a user with a workspace
305
306
workspace = mustProvisionWorkspace (t , client )
@@ -339,7 +340,7 @@ func TestExecutorWorkspaceTooEarly(t *testing.T) {
339
340
err error
340
341
tickCh = make (chan time.Time )
341
342
client = coderdtest .New (t , & coderdtest.Options {
342
- LifecycleTicker : tickCh ,
343
+ AutobuildTicker : tickCh ,
343
344
})
344
345
// Given: we have a user with a workspace
345
346
workspace = mustProvisionWorkspace (t , client )
@@ -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
+ AutobuildTicker : tickCh ,
388
+ })
389
+ _ = coderdtest .New (t , & coderdtest.Options {
390
+ AutobuildTicker : 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