@@ -2,7 +2,6 @@ package executor_test
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"os"
7
6
"strings"
8
7
"testing"
@@ -26,31 +25,25 @@ func TestExecutorAutostartOK(t *testing.T) {
26
25
t .Parallel ()
27
26
28
27
var (
29
- ctx = context .Background ()
30
- err error
28
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
31
29
tickCh = make (chan time.Time )
32
30
statsCh = make (chan executor.Stats )
33
31
client = coderdtest .New (t , & coderdtest.Options {
34
32
AutobuildTicker : tickCh ,
35
33
IncludeProvisionerD : true ,
36
34
AutobuildStats : statsCh ,
37
35
})
38
- // Given: we have a user with a workspace
39
- workspace = mustProvisionWorkspace (t , client )
36
+ // Given: we have a user with a workspace that has autostart enabled
37
+ workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
38
+ cwr .AutostartSchedule = ptr .Ref (sched .String ())
39
+ })
40
40
)
41
41
// Given: workspace is stopped
42
42
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
43
43
44
- // When: we enable workspace autostart
45
- sched , err := schedule .Weekly ("* * * * *" )
46
- require .NoError (t , err )
47
- require .NoError (t , client .UpdateWorkspaceAutostart (ctx , workspace .ID , codersdk.UpdateWorkspaceAutostartRequest {
48
- Schedule : ptr .Ref (sched .String ()),
49
- }))
50
-
51
- // When: the autobuild executor ticks
44
+ // When: the autobuild executor ticks after the scheduled time
52
45
go func () {
53
- tickCh <- time . Now (). UTC (). Add ( time . Minute )
46
+ tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
54
47
close (tickCh )
55
48
}()
56
49
@@ -66,6 +59,7 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
66
59
t .Parallel ()
67
60
68
61
var (
62
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
69
63
ctx = context .Background ()
70
64
err error
71
65
tickCh = make (chan time.Time )
@@ -75,8 +69,10 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
75
69
IncludeProvisionerD : true ,
76
70
AutobuildStats : statsCh ,
77
71
})
78
- // Given: we have a user with a workspace
79
- workspace = mustProvisionWorkspace (t , client )
72
+ // Given: we have a user with a workspace that has autostart enabled
73
+ workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
74
+ cwr .AutostartSchedule = ptr .Ref (sched .String ())
75
+ })
80
76
)
81
77
// Given: workspace is stopped
82
78
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
@@ -92,16 +88,9 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
92
88
ID : newVersion .ID ,
93
89
}))
94
90
95
- // When: we enable workspace autostart
96
- sched , err := schedule .Weekly ("* * * * *" )
97
- require .NoError (t , err )
98
- require .NoError (t , client .UpdateWorkspaceAutostart (ctx , workspace .ID , codersdk.UpdateWorkspaceAutostartRequest {
99
- Schedule : ptr .Ref (sched .String ()),
100
- }))
101
-
102
- // When: the autobuild executor ticks
91
+ // When: the autobuild executor ticks after the scheduled time
103
92
go func () {
104
- tickCh <- time . Now (). UTC (). Add ( time . Minute )
93
+ tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
105
94
close (tickCh )
106
95
}()
107
96
@@ -119,32 +108,26 @@ func TestExecutorAutostartAlreadyRunning(t *testing.T) {
119
108
t .Parallel ()
120
109
121
110
var (
122
- ctx = context .Background ()
123
- err error
111
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
124
112
tickCh = make (chan time.Time )
125
113
statsCh = make (chan executor.Stats )
126
114
client = coderdtest .New (t , & coderdtest.Options {
127
115
AutobuildTicker : tickCh ,
128
116
IncludeProvisionerD : true ,
129
117
AutobuildStats : statsCh ,
130
118
})
131
- // Given: we have a user with a workspace
132
- workspace = mustProvisionWorkspace (t , client )
119
+ // Given: we have a user with a workspace that has autostart enabled
120
+ workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
121
+ cwr .AutostartSchedule = ptr .Ref (sched .String ())
122
+ })
133
123
)
134
124
135
125
// Given: we ensure the workspace is running
136
126
require .Equal (t , codersdk .WorkspaceTransitionStart , workspace .LatestBuild .Transition )
137
127
138
- // When: we enable workspace autostart
139
- sched , err := schedule .Weekly ("* * * * *" )
140
- require .NoError (t , err )
141
- require .NoError (t , client .UpdateWorkspaceAutostart (ctx , workspace .ID , codersdk.UpdateWorkspaceAutostartRequest {
142
- Schedule : ptr .Ref (sched .String ()),
143
- }))
144
-
145
128
// When: the autobuild executor ticks
146
129
go func () {
147
- tickCh <- time . Now (). UTC (). Add ( time . Minute )
130
+ tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
148
131
close (tickCh )
149
132
}()
150
133
@@ -165,7 +148,7 @@ func TestExecutorAutostartNotEnabled(t *testing.T) {
165
148
IncludeProvisionerD : true ,
166
149
AutobuildStats : statsCh ,
167
150
})
168
- // Given: we have a user with a workspace
151
+ // Given: we have a user with a workspace that does not have autostart enabled
169
152
workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
170
153
cwr .AutostartSchedule = nil
171
154
})
@@ -177,9 +160,9 @@ func TestExecutorAutostartNotEnabled(t *testing.T) {
177
160
// Given: workspace is stopped
178
161
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
179
162
180
- // When: the autobuild executor ticks
163
+ // When: the autobuild executor ticks way into the future
181
164
go func () {
182
- tickCh <- time . Now (). UTC () .Add (time .Minute )
165
+ tickCh <- workspace . LatestBuild . CreatedAt .Add (24 * time .Hour )
183
166
close (tickCh )
184
167
}()
185
168
@@ -343,32 +326,26 @@ func TestExecutorWorkspaceDeleted(t *testing.T) {
343
326
t .Parallel ()
344
327
345
328
var (
346
- ctx = context .Background ()
347
- err error
329
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
348
330
tickCh = make (chan time.Time )
349
331
statsCh = make (chan executor.Stats )
350
332
client = coderdtest .New (t , & coderdtest.Options {
351
333
AutobuildTicker : tickCh ,
352
334
IncludeProvisionerD : true ,
353
335
AutobuildStats : statsCh ,
354
336
})
355
- // Given: we have a user with a workspace
356
- workspace = mustProvisionWorkspace (t , client )
337
+ // Given: we have a user with a workspace that has autostart enabled
338
+ workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
339
+ cwr .AutostartSchedule = ptr .Ref (sched .String ())
340
+ })
357
341
)
358
342
359
- // When: we enable workspace autostart
360
- sched , err := schedule .Weekly ("* * * * *" )
361
- require .NoError (t , err )
362
- require .NoError (t , client .UpdateWorkspaceAutostart (ctx , workspace .ID , codersdk.UpdateWorkspaceAutostartRequest {
363
- Schedule : ptr .Ref (sched .String ()),
364
- }))
365
-
366
343
// Given: workspace is deleted
367
344
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionDelete )
368
345
369
346
// When: the autobuild executor ticks
370
347
go func () {
371
- tickCh <- time . Now (). UTC (). Add ( time . Minute )
348
+ tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt )
372
349
close (tickCh )
373
350
}()
374
351
@@ -382,33 +359,25 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
382
359
t .Parallel ()
383
360
384
361
var (
385
- ctx = context .Background ()
386
- err error
362
+ sched = mustSchedule (t , "CRON_TZ=UTC 0 * * * *" )
387
363
tickCh = make (chan time.Time )
388
364
statsCh = make (chan executor.Stats )
389
365
client = coderdtest .New (t , & coderdtest.Options {
390
366
AutobuildTicker : tickCh ,
391
367
IncludeProvisionerD : true ,
392
368
AutobuildStats : statsCh ,
393
369
})
394
- futureTime = time .Now ().Add (time .Hour )
395
- futureTimeCron = fmt .Sprintf ("%d %d * * *" , futureTime .Minute (), futureTime .Hour ())
370
+ // futureTime = time.Now().Add(time.Hour)
371
+ // futureTimeCron = fmt.Sprintf("%d %d * * *", futureTime.Minute(), futureTime.Hour())
396
372
// Given: we have a user with a workspace configured to autostart some time in the future
397
373
workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
398
- cwr .AutostartSchedule = & futureTimeCron
374
+ cwr .AutostartSchedule = ptr . Ref ( sched . String ())
399
375
})
400
376
)
401
377
402
- // When: we enable workspace autostart with some time in the future
403
- sched , err := schedule .Weekly (futureTimeCron )
404
- require .NoError (t , err )
405
- require .NoError (t , client .UpdateWorkspaceAutostart (ctx , workspace .ID , codersdk.UpdateWorkspaceAutostartRequest {
406
- Schedule : ptr .Ref (sched .String ()),
407
- }))
408
-
409
- // When: the autobuild executor ticks
378
+ // When: the autobuild executor ticks before the next scheduled time
410
379
go func () {
411
- tickCh <- time . Now (). UTC ( )
380
+ tickCh <- sched . Next ( workspace . LatestBuild . CreatedAt ). Add ( - time . Minute )
412
381
close (tickCh )
413
382
}()
414
383
@@ -573,6 +542,13 @@ func mustWorkspace(t *testing.T, client *codersdk.Client, workspaceID uuid.UUID)
573
542
return ws
574
543
}
575
544
545
+ func mustSchedule (t * testing.T , s string ) * schedule.Schedule {
546
+ t .Helper ()
547
+ sched , err := schedule .Weekly (s )
548
+ require .NoError (t , err )
549
+ return sched
550
+ }
551
+
576
552
func TestMain (m * testing.M ) {
577
553
goleak .VerifyTestMain (m )
578
554
}
0 commit comments