Skip to content

Commit 9854f9b

Browse files
committed
fix: autostart: add more test cases
1 parent 1884766 commit 9854f9b

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

coderd/workspaces.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/moby/moby/pkg/namesgenerator"
1414
"golang.org/x/xerrors"
1515

16+
"github.com/coder/coder/coderd/autostart/schedule"
1617
"github.com/coder/coder/coderd/database"
1718
"github.com/coder/coder/coderd/httpapi"
1819
"github.com/coder/coder/coderd/httpmw"
@@ -291,18 +292,28 @@ func (api *api) workspaceBuildByName(rw http.ResponseWriter, r *http.Request) {
291292
}
292293

293294
func (api *api) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
294-
var spec codersdk.UpdateWorkspaceAutostartRequest
295-
if !httpapi.Read(rw, r, &spec) {
295+
var req codersdk.UpdateWorkspaceAutostartRequest
296+
if !httpapi.Read(rw, r, &req) {
296297
return
297298
}
298299

300+
var dbSched sql.NullString
301+
if req.Schedule != "" {
302+
validSched, err := schedule.Weekly(req.Schedule)
303+
if err != nil {
304+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
305+
Message: fmt.Sprintf("invalid autostart schedule: %s", err),
306+
})
307+
return
308+
}
309+
dbSched.String = validSched.String()
310+
dbSched.Valid = true
311+
}
312+
299313
workspace := httpmw.WorkspaceParam(r)
300314
err := api.Database.UpdateWorkspaceAutostart(r.Context(), database.UpdateWorkspaceAutostartParams{
301-
ID: workspace.ID,
302-
AutostartSchedule: sql.NullString{
303-
String: spec.Schedule,
304-
Valid: true,
305-
},
315+
ID: workspace.ID,
316+
AutostartSchedule: dbSched,
306317
})
307318
if err != nil {
308319
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{

coderd/workspaces_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ func TestWorkspaceBuildByName(t *testing.T) {
186186
}
187187

188188
func TestWorkspaceUpdateAutostart(t *testing.T) {
189-
// fri -> monday
190-
// TODO(cian): mon -> tue
191189
// TODO(cian): CST -> CDT
192190
// TODO(cian): CDT -> CST
193191

@@ -200,13 +198,35 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
200198
at time.Time
201199
expectedNext time.Time
202200
}{
201+
{
202+
name: "disable autostart",
203+
schedule: "",
204+
expectedError: "",
205+
},
203206
{
204207
name: "friday to monday",
205208
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
206209
expectedError: "",
207210
at: time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc),
208211
expectedNext: time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc),
209212
},
213+
{
214+
name: "monday to tuesday",
215+
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
216+
expectedError: "",
217+
at: time.Date(2022, 5, 9, 9, 31, 0, 0, dublinLoc),
218+
expectedNext: time.Date(2022, 5, 10, 9, 30, 0, 0, dublinLoc),
219+
},
220+
{
221+
name: "invalid location",
222+
schedule: "CRON_TZ=Imaginary/Place 30 9 1-5",
223+
expectedError: "status code 500: invalid autostart schedule: parse schedule: provided bad location Imaginary/Place: unknown time zone Imaginary/Place",
224+
},
225+
{
226+
name: "invalid schedule",
227+
schedule: "asdf asdf asdf ",
228+
expectedError: `status code 500: invalid autostart schedule: parse schedule: failed to parse int from asdf: strconv.Atoi: parsing "asdf": invalid syntax`,
229+
},
210230
}
211231

212232
for _, testCase := range testCases {
@@ -229,13 +249,22 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
229249
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
230250
Schedule: testCase.schedule,
231251
})
252+
253+
if testCase.expectedError != "" {
254+
require.EqualError(t, err, testCase.expectedError, "unexpected error when setting workspace autostart schedule")
255+
return
256+
}
257+
232258
require.NoError(t, err, "expected no error setting workspace autostart schedule")
233259

234260
updated, err := client.Workspace(ctx, workspace.ID)
235261
require.NoError(t, err, "fetch updated workspace")
236262

237263
require.Equal(t, testCase.schedule, updated.AutostartSchedule, "expected autostart schedule to equal requested")
238264

265+
if testCase.schedule == "" {
266+
return
267+
}
239268
sched, err := schedule.Weekly(updated.AutostartSchedule)
240269
require.NoError(t, err, "parse returned schedule")
241270

0 commit comments

Comments
 (0)