|
| 1 | +package autobuild |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/coder/coder/v2/coderd/database" |
| 11 | + "github.com/coder/coder/v2/coderd/schedule" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_isEligibleForAutostart(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + // okXXX should be set to values that make 'isEligibleForAutostart' return true. |
| 18 | + |
| 19 | + // Intentionally chosen to be a non UTC time that changes the day of the week |
| 20 | + // when converted to UTC. |
| 21 | + localLocation, err := time.LoadLocation("America/Chicago") |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + // 5s after the autostart in UTC. |
| 27 | + okTick := time.Date(2021, 1, 1, 20, 0, 5, 0, localLocation).UTC() |
| 28 | + okWorkspace := database.Workspace{ |
| 29 | + DormantAt: sql.NullTime{Valid: false}, |
| 30 | + AutostartSchedule: sql.NullString{ |
| 31 | + Valid: true, |
| 32 | + // Every day at 8pm America/Chicago, which is 2am UTC the next day. |
| 33 | + String: "CRON_TZ=America/Chicago 0 20 * * *", |
| 34 | + }, |
| 35 | + } |
| 36 | + okBuild := database.WorkspaceBuild{ |
| 37 | + Transition: database.WorkspaceTransitionStop, |
| 38 | + // Put 24hr before the tick so it's eligible for autostart. |
| 39 | + CreatedAt: okTick.Add(time.Hour * -24), |
| 40 | + } |
| 41 | + okJob := database.ProvisionerJob{ |
| 42 | + JobStatus: database.ProvisionerJobStatusSucceeded, |
| 43 | + } |
| 44 | + okTemplateSchedule := schedule.TemplateScheduleOptions{ |
| 45 | + UserAutostartEnabled: true, |
| 46 | + AutostartRequirement: schedule.TemplateAutostartRequirement{ |
| 47 | + DaysOfWeek: 0b01111111, |
| 48 | + }, |
| 49 | + } |
| 50 | + var okWeekdayBit uint8 |
| 51 | + for i, weekday := range schedule.DaysOfWeek { |
| 52 | + // Find the local weekday |
| 53 | + if okTick.In(localLocation).Weekday() == weekday { |
| 54 | + okWeekdayBit = 1 << uint(i) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + testCases := []struct { |
| 59 | + Name string |
| 60 | + Workspace database.Workspace |
| 61 | + Build database.WorkspaceBuild |
| 62 | + Job database.ProvisionerJob |
| 63 | + TemplateSchedule schedule.TemplateScheduleOptions |
| 64 | + Tick time.Time |
| 65 | + |
| 66 | + ExpectedResponse bool |
| 67 | + }{ |
| 68 | + { |
| 69 | + Name: "Ok", |
| 70 | + Workspace: okWorkspace, |
| 71 | + Build: okBuild, |
| 72 | + Job: okJob, |
| 73 | + TemplateSchedule: okTemplateSchedule, |
| 74 | + Tick: okTick, |
| 75 | + ExpectedResponse: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + Name: "AutostartOnlyDayEnabled", |
| 79 | + Workspace: okWorkspace, |
| 80 | + Build: okBuild, |
| 81 | + Job: okJob, |
| 82 | + TemplateSchedule: schedule.TemplateScheduleOptions{ |
| 83 | + UserAutostartEnabled: true, |
| 84 | + AutostartRequirement: schedule.TemplateAutostartRequirement{ |
| 85 | + // Specific day of week is allowed |
| 86 | + DaysOfWeek: okWeekdayBit, |
| 87 | + }, |
| 88 | + }, |
| 89 | + Tick: okTick, |
| 90 | + ExpectedResponse: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + Name: "AutostartOnlyDayDisabled", |
| 94 | + Workspace: okWorkspace, |
| 95 | + Build: okBuild, |
| 96 | + Job: okJob, |
| 97 | + TemplateSchedule: schedule.TemplateScheduleOptions{ |
| 98 | + UserAutostartEnabled: true, |
| 99 | + AutostartRequirement: schedule.TemplateAutostartRequirement{ |
| 100 | + // Specific day of week is disallowed |
| 101 | + DaysOfWeek: 0b01111111 & (^okWeekdayBit), |
| 102 | + }, |
| 103 | + }, |
| 104 | + Tick: okTick, |
| 105 | + ExpectedResponse: false, |
| 106 | + }, |
| 107 | + { |
| 108 | + Name: "AutostartAllDaysDisabled", |
| 109 | + Workspace: okWorkspace, |
| 110 | + Build: okBuild, |
| 111 | + Job: okJob, |
| 112 | + TemplateSchedule: schedule.TemplateScheduleOptions{ |
| 113 | + UserAutostartEnabled: true, |
| 114 | + AutostartRequirement: schedule.TemplateAutostartRequirement{ |
| 115 | + // All days disabled |
| 116 | + DaysOfWeek: 0, |
| 117 | + }, |
| 118 | + }, |
| 119 | + Tick: okTick, |
| 120 | + ExpectedResponse: false, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, c := range testCases { |
| 125 | + c := c |
| 126 | + t.Run(c.Name, func(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + autostart := isEligibleForAutostart(c.Workspace, c.Build, c.Job, c.TemplateSchedule, c.Tick) |
| 130 | + require.Equal(t, c.ExpectedResponse, autostart, "autostart not expected") |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments