fix: schedule autobuild directly on TestExecutorAutostopTemplateDisabled (#10453)

Fixes flake seen here: https://github.com/coder/coder/actions/runs/6716682414/job/18253279654

The test used a cron schedule to compute autobuild ticks, with ticks every hour on the hour.  The default TTL was set to an hour.  Usually, the next tick is less than one hour in the future, unless the test runs at :00 past the hour, which it did in my flake'd
run.  But, given that this is an autostop test, the cron schedule is irrelevant (such schedules are used for auto_start_).  So, I've removed it from the test and compute the build ticks directly.

Also, the test originally had the workspace TTL set to longer than the default template TTL, and then tested that no build happened when the tick was prior to both. This seems odd to me, as we want to demonstrate the the executor disregards the workspace TTL.
So, I changed the test to set the workspace TTL shorter, and then send in a tick between the two, verify that we don't autostop, then a tick after the template TTL and verify that we do.
This commit is contained in:
Spike Curtis
2023-11-01 15:16:20 +04:00
committed by GitHub
parent 94eb9b8db1
commit 95ce697e3a

View File

@ -743,7 +743,6 @@ func TestExecutorAutostopTemplateDisabled(t *testing.T) {
// Given: we have a workspace built from a template that disallows user autostop
var (
sched = mustSchedule(t, "CRON_TZ=UTC 0 * * * *")
tickCh = make(chan time.Time)
statsCh = make(chan autobuild.Stats)
@ -761,9 +760,9 @@ func TestExecutorAutostopTemplateDisabled(t *testing.T) {
},
},
})
// Given: we have a user with a workspace configured to autostart some time in the future
// Given: we have a user with a workspace configured to autostop 30 minutes in the future
workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) {
cwr.TTLMillis = ptr.Ref(8 * time.Hour.Milliseconds())
cwr.TTLMillis = ptr.Ref(30 * time.Minute.Milliseconds())
})
)
@ -771,16 +770,28 @@ func TestExecutorAutostopTemplateDisabled(t *testing.T) {
// Then: the deadline should be set to the template default TTL
assert.WithinDuration(t, workspace.LatestBuild.CreatedAt.Add(time.Hour), workspace.LatestBuild.Deadline.Time, time.Minute)
// When: the autobuild executor ticks before the next scheduled time
// When: the autobuild executor ticks after the workspace setting, but before the template setting:
go func() {
tickCh <- sched.Next(workspace.LatestBuild.CreatedAt).Add(time.Minute)
close(tickCh)
tickCh <- workspace.LatestBuild.CreatedAt.Add(45 * time.Minute)
}()
// Then: nothing should happen
stats := <-statsCh
assert.NoError(t, stats.Error)
assert.Len(t, stats.Transitions, 0)
// When: the autobuild executor ticks after the template setting:
go func() {
tickCh <- workspace.LatestBuild.CreatedAt.Add(61 * time.Minute)
close(tickCh)
}()
// Then: the workspace should be stopped
stats = <-statsCh
assert.NoError(t, stats.Error)
assert.Len(t, stats.Transitions, 1)
assert.Contains(t, stats.Transitions, workspace.ID)
assert.Equal(t, database.WorkspaceTransitionStop, stats.Transitions[workspace.ID])
}
// Test that an AGPL AccessControlStore properly disables