(t *testing.T)
| 467 | } |
| 468 | |
| 469 | func TestExecutorAutostopExtend(t *testing.T) { |
| 470 | t.Parallel() |
| 471 | |
| 472 | var ( |
| 473 | ctx = context.Background() |
| 474 | tickCh = make(chan time.Time) |
| 475 | statsCh = make(chan autobuild.Stats) |
| 476 | client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{ |
| 477 | AutobuildTicker: tickCh, |
| 478 | IncludeProvisionerDaemon: true, |
| 479 | AutobuildStats: statsCh, |
| 480 | }) |
| 481 | // Given: we have a user with a workspace |
| 482 | workspace = mustProvisionWorkspace(t, client) |
| 483 | originalDeadline = workspace.LatestBuild.Deadline |
| 484 | ) |
| 485 | // Given: workspace is running |
| 486 | require.Equal(t, codersdk.WorkspaceTransitionStart, workspace.LatestBuild.Transition) |
| 487 | require.NotZero(t, originalDeadline) |
| 488 | |
| 489 | // Given: we extend the workspace deadline |
| 490 | newDeadline := originalDeadline.Time.Add(30 * time.Minute) |
| 491 | err := client.PutExtendWorkspace(ctx, workspace.ID, codersdk.PutExtendWorkspaceRequest{ |
| 492 | Deadline: newDeadline, |
| 493 | }) |
| 494 | require.NoError(t, err, "extend workspace deadline") |
| 495 | |
| 496 | p, err := coderdtest.GetProvisionerForTags(db, time.Now(), workspace.OrganizationID, nil) |
| 497 | require.NoError(t, err) |
| 498 | |
| 499 | // When: the autobuild executor ticks *after* the original deadline: |
| 500 | go func() { |
| 501 | tickTime := originalDeadline.Time.Add(time.Minute) |
| 502 | coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime) |
| 503 | tickCh <- tickTime |
| 504 | }() |
| 505 | |
| 506 | // Then: nothing should happen and the workspace should stay running |
| 507 | stats := <-statsCh |
| 508 | assert.Len(t, stats.Errors, 0) |
| 509 | assert.Len(t, stats.Transitions, 0) |
| 510 | |
| 511 | // When: the autobuild executor ticks after the *new* deadline: |
| 512 | go func() { |
| 513 | tickTime := newDeadline.Add(time.Minute) |
| 514 | coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime) |
| 515 | tickCh <- tickTime |
| 516 | close(tickCh) |
| 517 | }() |
| 518 | |
| 519 | // Then: the workspace should be stopped |
| 520 | stats = <-statsCh |
| 521 | assert.Len(t, stats.Errors, 0) |
| 522 | assert.Len(t, stats.Transitions, 1) |
| 523 | assert.Contains(t, stats.Transitions, workspace.ID) |
| 524 | assert.Equal(t, database.WorkspaceTransitionStop, stats.Transitions[workspace.ID]) |
| 525 | } |
| 526 |
nothing calls this directly
no test coverage detected