Skip to content

Commit 597056a

Browse files
test: improve tests
1 parent 340f410 commit 597056a

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

coderd/prebuilds/preset_snapshot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ func (ra *ReconciliationActions) IsNoop() bool {
8686
return ra.Create == 0 && len(ra.DeleteIDs) == 0 && ra.BackoffUntil.IsZero()
8787
}
8888

89-
// matchesCron checks if the given time matches the cron expression
89+
// MatchesCron checks if the given time matches the cron expression
9090
// Assumes the time is already in the correct timezone
91-
func matchesCron(cronExpression string, now time.Time) (bool, error) {
91+
func MatchesCron(cronExpression string, now time.Time) (bool, error) {
9292
sched, err := cron.Weekly(cronExpression)
9393
if err != nil {
9494
return false, xerrors.Errorf("failed to parse cron expression: %w", err)
@@ -111,7 +111,7 @@ func (p PresetSnapshot) calculateDesiredInstances(now time.Time) (int32, error)
111111

112112
// Check each schedule
113113
for _, schedule := range p.PrebuildSchedules {
114-
matches, err := matchesCron(schedule.CronExpression, now)
114+
matches, err := MatchesCron(schedule.CronExpression, now)
115115
if err != nil {
116116
return 0, xerrors.Errorf("failed to match cron expression: %w", err)
117117
}

coderd/prebuilds/preset_snapshot_test.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestNoPrebuilds(t *testing.T) {
8484
preset(true, 0, current),
8585
}
8686

87-
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, nil, nil, nil)
87+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, nil, nil, nil, nil)
8888
ps, err := snapshot.FilterByPreset(current.presetID)
8989
require.NoError(t, err)
9090

@@ -106,7 +106,7 @@ func TestNetNew(t *testing.T) {
106106
preset(true, 1, current),
107107
}
108108

109-
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, nil, nil, nil)
109+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, nil, nil, nil, nil)
110110
ps, err := snapshot.FilterByPreset(current.presetID)
111111
require.NoError(t, err)
112112

@@ -148,7 +148,7 @@ func TestOutdatedPrebuilds(t *testing.T) {
148148
var inProgress []database.CountInProgressPrebuildsRow
149149

150150
// WHEN: calculating the outdated preset's state.
151-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, nil, nil)
151+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil)
152152
ps, err := snapshot.FilterByPreset(outdated.presetID)
153153
require.NoError(t, err)
154154

@@ -214,7 +214,7 @@ func TestDeleteOutdatedPrebuilds(t *testing.T) {
214214
}
215215

216216
// WHEN: calculating the outdated preset's state.
217-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, nil, nil)
217+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil)
218218
ps, err := snapshot.FilterByPreset(outdated.presetID)
219219
require.NoError(t, err)
220220

@@ -459,7 +459,7 @@ func TestInProgressActions(t *testing.T) {
459459
}
460460

461461
// WHEN: calculating the current preset's state.
462-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, nil, nil)
462+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil)
463463
ps, err := snapshot.FilterByPreset(current.presetID)
464464
require.NoError(t, err)
465465

@@ -502,7 +502,7 @@ func TestExtraneous(t *testing.T) {
502502
var inProgress []database.CountInProgressPrebuildsRow
503503

504504
// WHEN: calculating the current preset's state.
505-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, nil, nil)
505+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil)
506506
ps, err := snapshot.FilterByPreset(current.presetID)
507507
require.NoError(t, err)
508508

@@ -683,7 +683,7 @@ func TestExpiredPrebuilds(t *testing.T) {
683683
}
684684

685685
// WHEN: calculating the current preset's state.
686-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, nil, nil, nil)
686+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, nil, nil, nil)
687687
ps, err := snapshot.FilterByPreset(current.presetID)
688688
require.NoError(t, err)
689689

@@ -719,7 +719,7 @@ func TestDeprecated(t *testing.T) {
719719
var inProgress []database.CountInProgressPrebuildsRow
720720

721721
// WHEN: calculating the current preset's state.
722-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, nil, nil)
722+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, nil, nil)
723723
ps, err := snapshot.FilterByPreset(current.presetID)
724724
require.NoError(t, err)
725725

@@ -772,7 +772,7 @@ func TestLatestBuildFailed(t *testing.T) {
772772
}
773773

774774
// WHEN: calculating the current preset's state.
775-
snapshot := prebuilds.NewGlobalSnapshot(presets, running, inProgress, backoffs, nil)
775+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, running, inProgress, backoffs, nil)
776776
psCurrent, err := snapshot.FilterByPreset(current.presetID)
777777
require.NoError(t, err)
778778

@@ -865,7 +865,7 @@ func TestMultiplePresetsPerTemplateVersion(t *testing.T) {
865865
},
866866
}
867867

868-
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, inProgress, nil, nil)
868+
snapshot := prebuilds.NewGlobalSnapshot(presets, nil, nil, inProgress, nil, nil)
869869

870870
// Nothing has to be created for preset 1.
871871
{
@@ -905,6 +905,49 @@ func TestMultiplePresetsPerTemplateVersion(t *testing.T) {
905905
}
906906
}
907907

908+
func TestMatchesCron(t *testing.T) {
909+
t.Parallel()
910+
testCases := []struct {
911+
name string
912+
spec string
913+
at time.Time
914+
expectedMatches bool
915+
}{
916+
// A comprehensive test suite for time range evaluation is implemented in TestIsWithinRange.
917+
// This test provides only basic coverage.
918+
{
919+
name: "Right before the start of the time range",
920+
spec: "* 9-18 * * 1-5",
921+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 8:58:59 UTC"),
922+
expectedMatches: false,
923+
},
924+
{
925+
name: "Start of the time range",
926+
spec: "* 9-18 * * 1-5",
927+
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 8:59:00 UTC"),
928+
expectedMatches: true,
929+
},
930+
}
931+
932+
for _, testCase := range testCases {
933+
testCase := testCase
934+
t.Run(testCase.name, func(t *testing.T) {
935+
t.Parallel()
936+
937+
matches, err := prebuilds.MatchesCron(testCase.spec, testCase.at)
938+
require.NoError(t, err)
939+
require.Equal(t, testCase.expectedMatches, matches)
940+
})
941+
}
942+
}
943+
944+
func mustParseTime(t *testing.T, layout, value string) time.Time {
945+
t.Helper()
946+
parsedTime, err := time.Parse(layout, value)
947+
require.NoError(t, err)
948+
return parsedTime
949+
}
950+
908951
func preset(active bool, instances int32, opts options, muts ...func(row database.GetTemplatePresetsWithPrebuildsRow) database.GetTemplatePresetsWithPrebuildsRow) database.GetTemplatePresetsWithPrebuildsRow {
909952
ttl := sql.NullInt32{}
910953
if opts.ttl > 0 {

0 commit comments

Comments
 (0)