Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db25e84

Browse files
committed
Add unit test to test blocking autostart with DaysOfWeek
1 parent 78257b8 commit db25e84

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

coderd/schedule/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var DaysOfWeek = []time.Weekday{
3737

3838
type TemplateAutostartRequirement struct {
3939
// DaysOfWeek is a bitmap of which days of the week the workspace is allowed
40-
// to be restarted. If fully zero, the workspace is not allowed to be restarted.
40+
// to be auto started. If fully zero, the workspace is not allowed to be auto started.
4141
//
4242
// First bit is Monday, ..., seventh bit is Sunday, eighth bit is unused.
4343
DaysOfWeek uint8

enterprise/coderd/templates_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ func TestTemplates(t *testing.T) {
159159
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
160160
require.Equal(t, []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}, template.AutostartRequirement.DaysOfWeek)
161161

162-
// ctx := testutil.Context(t, testutil.WaitLong)
163-
ctx := context.Background()
162+
ctx := testutil.Context(t, testutil.WaitLong)
164163
updated, err := client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
165164
Name: template.Name,
166165
DisplayName: template.DisplayName,

enterprise/coderd/workspaces_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,65 @@ func TestWorkspaceAutobuild(t *testing.T) {
736736
})
737737
}
738738

739+
// Blocked by autostart requirements
740+
func TestExecutorAutostartBlocked(t *testing.T) {
741+
t.Parallel()
742+
743+
now := time.Now()
744+
var allowed []string
745+
for _, day := range agplschedule.DaysOfWeek {
746+
// Skip the day the workspace was created on and if the next day is within 2
747+
// hours, skip that too. The cron scheduler will start the workspace every hour,
748+
// so it can span into the next day.
749+
if day != now.UTC().Weekday() &&
750+
day != now.UTC().Add(time.Hour*2).Weekday() {
751+
allowed = append(allowed, day.String())
752+
}
753+
}
754+
755+
var (
756+
sched = must(cron.Weekly("CRON_TZ=UTC 0 * * * *"))
757+
tickCh = make(chan time.Time)
758+
statsCh = make(chan autobuild.Stats)
759+
client, owner = coderdenttest.New(t, &coderdenttest.Options{
760+
Options: &coderdtest.Options{
761+
AutobuildTicker: tickCh,
762+
IncludeProvisionerDaemon: true,
763+
AutobuildStats: statsCh,
764+
TemplateScheduleStore: schedule.NewEnterpriseTemplateScheduleStore(agplUserQuietHoursScheduleStore()),
765+
},
766+
LicenseOptions: &coderdenttest.LicenseOptions{
767+
Features: license.Features{codersdk.FeatureAdvancedTemplateScheduling: 1},
768+
},
769+
})
770+
version = coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil)
771+
template = coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID, func(request *codersdk.CreateTemplateRequest) {
772+
request.AutostartRequirement = &codersdk.TemplateAutostartRequirement{
773+
DaysOfWeek: allowed,
774+
}
775+
})
776+
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
777+
workspace = coderdtest.CreateWorkspace(t, client, owner.OrganizationID, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
778+
cwr.AutostartSchedule = ptr.Ref(sched.String())
779+
})
780+
_ = coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
781+
)
782+
783+
// Given: workspace is stopped
784+
workspace = coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
785+
786+
// When: the autobuild executor ticks way into the future
787+
go func() {
788+
tickCh <- workspace.LatestBuild.CreatedAt.Add(24 * time.Hour)
789+
close(tickCh)
790+
}()
791+
792+
// Then: the workspace should not be started.
793+
stats := <-statsCh
794+
require.NoError(t, stats.Error)
795+
require.Len(t, stats.Transitions, 0)
796+
}
797+
739798
func TestWorkspacesFiltering(t *testing.T) {
740799
t.Parallel()
741800

@@ -911,3 +970,10 @@ func TestWorkspaceLock(t *testing.T) {
911970
require.True(t, workspace.LastUsedAt.After(lastUsedAt))
912971
})
913972
}
973+
974+
func must[T any](value T, err error) T {
975+
if err != nil {
976+
panic(err)
977+
}
978+
return value
979+
}

0 commit comments

Comments
 (0)