Skip to content

Commit 1884766

Browse files
committed
refactor: make test table-driven
1 parent f6895b7 commit 1884766

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

coderd/workspaces_test.go

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,38 +190,66 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
190190
// TODO(cian): mon -> tue
191191
// TODO(cian): CST -> CDT
192192
// TODO(cian): CDT -> CST
193-
t.Parallel()
194-
var (
195-
ctx = context.Background()
196-
client = coderdtest.New(t, nil)
197-
_ = coderdtest.NewProvisionerDaemon(t, client)
198-
user = coderdtest.CreateFirstUser(t, client)
199-
version = coderdtest.CreateProjectVersion(t, client, user.OrganizationID, nil)
200-
_ = coderdtest.AwaitProjectVersionJob(t, client, version.ID)
201-
project = coderdtest.CreateProject(t, client, user.OrganizationID, version.ID)
202-
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
203-
)
204-
205-
require.Empty(t, workspace.AutostartSchedule, "expected newly-minted workspace to have no autostart schedule")
206-
207-
schedSpec := "CRON_TZ=Europe/Dublin 30 9 1-5"
208-
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
209-
Schedule: schedSpec,
210-
})
211-
require.NoError(t, err, "expected no error")
212-
213-
updated, err := client.Workspace(ctx, workspace.ID)
214-
require.NoError(t, err, "fetch updated workspace")
215193

216-
require.Equal(t, schedSpec, updated.AutostartSchedule, "expected autostart schedule to equal")
217-
218-
sched, err := schedule.Weekly(updated.AutostartSchedule)
219-
require.NoError(t, err, "parse returned schedule")
194+
var dublinLoc = mustLocation(t, "Europe/Dublin")
195+
196+
testCases := []struct {
197+
name string
198+
schedule string
199+
expectedError string
200+
at time.Time
201+
expectedNext time.Time
202+
}{
203+
{
204+
name: "friday to monday",
205+
schedule: "CRON_TZ=Europe/Dublin 30 9 1-5",
206+
expectedError: "",
207+
at: time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc),
208+
expectedNext: time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc),
209+
},
210+
}
211+
212+
for _, testCase := range testCases {
213+
testCase := testCase
214+
t.Run(testCase.name, func(t *testing.T) {
215+
var (
216+
ctx = context.Background()
217+
client = coderdtest.New(t, nil)
218+
_ = coderdtest.NewProvisionerDaemon(t, client)
219+
user = coderdtest.CreateFirstUser(t, client)
220+
version = coderdtest.CreateProjectVersion(t, client, user.OrganizationID, nil)
221+
_ = coderdtest.AwaitProjectVersionJob(t, client, version.ID)
222+
project = coderdtest.CreateProject(t, client, user.OrganizationID, version.ID)
223+
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
224+
)
225+
226+
// ensure test invariant: new workspaces have no autostart schedule.
227+
require.Empty(t, workspace.AutostartSchedule, "expected newly-minted workspace to have no autostart schedule")
228+
229+
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
230+
Schedule: testCase.schedule,
231+
})
232+
require.NoError(t, err, "expected no error setting workspace autostart schedule")
233+
234+
updated, err := client.Workspace(ctx, workspace.ID)
235+
require.NoError(t, err, "fetch updated workspace")
236+
237+
require.Equal(t, testCase.schedule, updated.AutostartSchedule, "expected autostart schedule to equal requested")
238+
239+
sched, err := schedule.Weekly(updated.AutostartSchedule)
240+
require.NoError(t, err, "parse returned schedule")
241+
242+
next := sched.Next(testCase.at)
243+
require.Equal(t, testCase.expectedNext, next, "unexpected next scheduled autostart time")
244+
})
245+
}
246+
}
220247

221-
dublinLoc, err := time.LoadLocation("Europe/Dublin")
222-
require.NoError(t, err, "failed to load timezone location")
248+
func mustLocation(t *testing.T, location string) *time.Location {
249+
loc, err := time.LoadLocation(location)
250+
if err != nil {
251+
t.Errorf("failed to load location %s: %s", location, err.Error())
252+
}
223253

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

0 commit comments

Comments
 (0)