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