@@ -190,14 +190,14 @@ func TestExecutorAutostopOK(t *testing.T) {
190
190
})
191
191
// Given: we have a user with a workspace
192
192
workspace = mustProvisionWorkspace (t , client )
193
- ttl = * workspace .TTL
194
193
)
195
194
// Given: workspace is running
196
195
require .Equal (t , codersdk .WorkspaceTransitionStart , workspace .LatestBuild .Transition )
196
+ require .NotZero (t , workspace .LatestBuild .Deadline )
197
197
198
- // When: the autobuild executor ticks *after* the TTL :
198
+ // When: the autobuild executor ticks *after* the deadline :
199
199
go func () {
200
- tickCh <- time . Now (). UTC () .Add (ttl + time .Minute )
200
+ tickCh <- workspace . LatestBuild . Deadline .Add (time .Minute )
201
201
close (tickCh )
202
202
}()
203
203
@@ -209,6 +209,55 @@ func TestExecutorAutostopOK(t *testing.T) {
209
209
require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
210
210
}
211
211
212
+ func TestExecutorAutostopExtend (t * testing.T ) {
213
+ t .Parallel ()
214
+
215
+ var (
216
+ ctx = context .Background ()
217
+ tickCh = make (chan time.Time )
218
+ client = coderdtest .New (t , & coderdtest.Options {
219
+ AutobuildTicker : tickCh ,
220
+ IncludeProvisionerD : true ,
221
+ })
222
+ // Given: we have a user with a workspace
223
+ workspace = mustProvisionWorkspace (t , client )
224
+ originalDeadline = workspace .LatestBuild .Deadline
225
+ )
226
+ // Given: workspace is running
227
+ require .Equal (t , codersdk .WorkspaceTransitionStart , workspace .LatestBuild .Transition )
228
+ require .NotZero (t , originalDeadline )
229
+
230
+ // Given: we extend the workspace deadline
231
+ err := client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
232
+ Deadline : originalDeadline .Add (30 * time .Minute ),
233
+ })
234
+ require .NoError (t , err , "extend workspace deadline" )
235
+
236
+ // When: the autobuild executor ticks *after* the original deadline:
237
+ go func () {
238
+ tickCh <- originalDeadline .Add (time .Minute )
239
+ }()
240
+
241
+ // Then: nothing should happen
242
+ <- time .After (5 * time .Second )
243
+ ws := mustWorkspace (t , client , workspace .ID )
244
+ require .Equal (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected no further workspace builds to occur" )
245
+ require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
246
+
247
+ // When: the autobuild executor ticks after the *new* deadline:
248
+ go func () {
249
+ tickCh <- ws .LatestBuild .Deadline .Add (time .Minute )
250
+ close (tickCh )
251
+ }()
252
+
253
+ // Then: the workspace should be stopped
254
+ <- time .After (5 * time .Second )
255
+ ws = mustWorkspace (t , client , workspace .ID )
256
+ require .NotEqual (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected a workspace build to occur" )
257
+ require .Equal (t , codersdk .ProvisionerJobSucceeded , ws .LatestBuild .Job .Status , "expected provisioner job to have succeeded" )
258
+ require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
259
+ }
260
+
212
261
func TestExecutorAutostopAlreadyStopped (t * testing.T ) {
213
262
t .Parallel ()
214
263
@@ -222,15 +271,14 @@ func TestExecutorAutostopAlreadyStopped(t *testing.T) {
222
271
workspace = mustProvisionWorkspace (t , client , func (cwr * codersdk.CreateWorkspaceRequest ) {
223
272
cwr .AutostartSchedule = nil
224
273
})
225
- ttl = * workspace .TTL
226
274
)
227
275
228
276
// Given: workspace is stopped
229
277
workspace = mustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
230
278
231
279
// When: the autobuild executor ticks past the TTL
232
280
go func () {
233
- tickCh <- time . Now (). UTC () .Add (ttl + time .Minute )
281
+ tickCh <- workspace . LatestBuild . Deadline .Add (time .Minute )
234
282
close (tickCh )
235
283
}()
236
284
@@ -264,7 +312,7 @@ func TestExecutorAutostopNotEnabled(t *testing.T) {
264
312
265
313
// When: the autobuild executor ticks past the TTL
266
314
go func () {
267
- tickCh <- time . Now (). UTC () .Add (time .Minute )
315
+ tickCh <- workspace . LatestBuild . Deadline .Add (time .Minute )
268
316
close (tickCh )
269
317
}()
270
318
@@ -352,7 +400,7 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
352
400
require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
353
401
}
354
402
355
- func TestExecutorWorkspaceTTLTooEarly (t * testing.T ) {
403
+ func TestExecutorWorkspaceAutostopBeforeDeadline (t * testing.T ) {
356
404
t .Parallel ()
357
405
358
406
var (
@@ -367,7 +415,7 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
367
415
368
416
// When: the autobuild executor ticks before the TTL
369
417
go func () {
370
- tickCh <- time . Now (). UTC ( )
418
+ tickCh <- workspace . LatestBuild . Deadline . Add ( - 1 * time . Minute )
371
419
close (tickCh )
372
420
}()
373
421
@@ -378,6 +426,38 @@ func TestExecutorWorkspaceTTLTooEarly(t *testing.T) {
378
426
require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
379
427
}
380
428
429
+ func TestExecutorWorkspaceAutostopNoWaitChangedMyMind (t * testing.T ) {
430
+ t .Parallel ()
431
+
432
+ var (
433
+ ctx = context .Background ()
434
+ tickCh = make (chan time.Time )
435
+ client = coderdtest .New (t , & coderdtest.Options {
436
+ AutobuildTicker : tickCh ,
437
+ IncludeProvisionerD : true ,
438
+ })
439
+ // Given: we have a user with a workspace
440
+ workspace = mustProvisionWorkspace (t , client )
441
+ )
442
+
443
+ // Given: the user changes their mind and decides their workspace should not auto-stop
444
+ err := client .UpdateWorkspaceTTL (ctx , workspace .ID , codersdk.UpdateWorkspaceTTLRequest {TTL : nil })
445
+ require .NoError (t , err )
446
+
447
+ // When: the autobuild executor ticks after the deadline
448
+ go func () {
449
+ tickCh <- workspace .LatestBuild .Deadline .Add (time .Minute )
450
+ close (tickCh )
451
+ }()
452
+
453
+ // Then: the workspace should still stop - sorry!
454
+ <- time .After (5 * time .Second )
455
+ ws := mustWorkspace (t , client , workspace .ID )
456
+ require .NotEqual (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected a workspace build to occur" )
457
+ require .Equal (t , codersdk .ProvisionerJobSucceeded , ws .LatestBuild .Job .Status , "expected provisioner job to have succeeded" )
458
+ require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
459
+ }
460
+
381
461
func TestExecutorAutostartMultipleOK (t * testing.T ) {
382
462
if os .Getenv ("DB" ) == "" {
383
463
t .Skip (`This test only really works when using a "real" database, similar to a HA setup` )
0 commit comments