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
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
refactor: make test table-driven
  • Loading branch information
johnstcn committed Apr 6, 2022
commit 1884766a6ddc5a3109cd9d228b07f9e7913b76ab
90 changes: 59 additions & 31 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,38 +190,66 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
// TODO(cian): mon -> tue
// TODO(cian): CST -> CDT
// TODO(cian): CDT -> CST
t.Parallel()
var (
ctx = context.Background()
client = coderdtest.New(t, nil)
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateProjectVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitProjectVersionJob(t, client, version.ID)
project = coderdtest.CreateProject(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
)

require.Empty(t, workspace.AutostartSchedule, "expected newly-minted workspace to have no autostart schedule")

schedSpec := "CRON_TZ=Europe/Dublin 30 9 1-5"
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: schedSpec,
})
require.NoError(t, err, "expected no error")

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

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

sched, err := schedule.Weekly(updated.AutostartSchedule)
require.NoError(t, err, "parse returned schedule")
var dublinLoc = mustLocation(t, "Europe/Dublin")

testCases := []struct {
name string
schedule string
expectedError string
at time.Time
expectedNext time.Time
}{
{
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),
},
}

for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
var (
ctx = context.Background()
client = coderdtest.New(t, nil)
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateProjectVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitProjectVersionJob(t, client, version.ID)
project = coderdtest.CreateProject(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
)

// ensure test invariant: new workspaces have no autostart schedule.
require.Empty(t, workspace.AutostartSchedule, "expected newly-minted workspace to have no autostart schedule")

err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: testCase.schedule,
})
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")

sched, err := schedule.Weekly(updated.AutostartSchedule)
require.NoError(t, err, "parse returned schedule")

next := sched.Next(testCase.at)
require.Equal(t, testCase.expectedNext, next, "unexpected next scheduled autostart time")
})
}
}

dublinLoc, err := time.LoadLocation("Europe/Dublin")
require.NoError(t, err, "failed to load timezone location")
func mustLocation(t *testing.T, location string) *time.Location {
loc, err := time.LoadLocation(location)
if err != nil {
t.Errorf("failed to load location %s: %s", location, err.Error())
}

timeAt := time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc)
expectedNext := time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc)
require.Equal(t, expectedNext, sched.Next(timeAt), "unexpected next scheduled autostart time")
return loc
}