Skip to content

coderd: autostart: codersdk, http api, database plumbing #879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: autostart: add more test cases
  • Loading branch information
johnstcn committed Apr 6, 2022
commit 9854f9b6ddf479ac7ecfa84a2bbdc7bda304b55d
25 changes: 18 additions & 7 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/moby/moby/pkg/namesgenerator"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/autostart/schedule"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
Expand Down Expand Up @@ -291,18 +292,28 @@ func (api *api) workspaceBuildByName(rw http.ResponseWriter, r *http.Request) {
}

func (api *api) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
var spec codersdk.UpdateWorkspaceAutostartRequest
if !httpapi.Read(rw, r, &spec) {
var req codersdk.UpdateWorkspaceAutostartRequest
if !httpapi.Read(rw, r, &req) {
return
}

var dbSched sql.NullString
if req.Schedule != "" {
validSched, err := schedule.Weekly(req.Schedule)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("invalid autostart schedule: %s", err),
})
return
}
dbSched.String = validSched.String()
dbSched.Valid = true
}

workspace := httpmw.WorkspaceParam(r)
err := api.Database.UpdateWorkspaceAutostart(r.Context(), database.UpdateWorkspaceAutostartParams{
ID: workspace.ID,
AutostartSchedule: sql.NullString{
String: spec.Schedule,
Valid: true,
},
ID: workspace.ID,
AutostartSchedule: dbSched,
})
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Expand Down
33 changes: 31 additions & 2 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ func TestWorkspaceBuildByName(t *testing.T) {
}

func TestWorkspaceUpdateAutostart(t *testing.T) {
// fri -> monday
// TODO(cian): mon -> tue
// TODO(cian): CST -> CDT
// TODO(cian): CDT -> CST

Expand All @@ -200,13 +198,35 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
at time.Time
expectedNext time.Time
}{
{
name: "disable autostart",
schedule: "",
expectedError: "",
},
{
name: "friday to monday",
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
expectedError: "",
at: time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc),
expectedNext: time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc),
},
{
name: "monday to tuesday",
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
expectedError: "",
at: time.Date(2022, 5, 9, 9, 31, 0, 0, dublinLoc),
expectedNext: time.Date(2022, 5, 10, 9, 30, 0, 0, dublinLoc),
},
{
name: "invalid location",
schedule: "CRON_TZ=Imaginary/Place 30 9 1-5",
expectedError: "status code 500: invalid autostart schedule: parse schedule: provided bad location Imaginary/Place: unknown time zone Imaginary/Place",
},
{
name: "invalid schedule",
schedule: "asdf asdf asdf ",
expectedError: `status code 500: invalid autostart schedule: parse schedule: failed to parse int from asdf: strconv.Atoi: parsing "asdf": invalid syntax`,
},
}

for _, testCase := range testCases {
Expand All @@ -229,13 +249,22 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: testCase.schedule,
})

if testCase.expectedError != "" {
require.EqualError(t, err, testCase.expectedError, "unexpected error when setting workspace autostart schedule")
return
}

require.NoError(t, err, "expected no error setting workspace autostart schedule")

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

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

if testCase.schedule == "" {
return
}
sched, err := schedule.Weekly(updated.AutostartSchedule)
require.NoError(t, err, "parse returned schedule")

Expand Down