Skip to content

Commit 9a98fbf

Browse files
committed
ensure tz remains the same
1 parent dd3fa2a commit 9a98fbf

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

cli/schedule_test.go

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/coder/coder/v2/coderd/database"
1818
"github.com/coder/coder/v2/coderd/database/dbfake"
1919
"github.com/coder/coder/v2/coderd/schedule/cron"
20+
"github.com/coder/coder/v2/coderd/util/tz"
2021
"github.com/coder/coder/v2/codersdk"
2122
"github.com/coder/coder/v2/pty/ptytest"
2223
"github.com/coder/coder/v2/testutil"
@@ -30,6 +31,8 @@ import (
3031
// It returns the owner and member clients, the database, and the workspaces.
3132
// The workspaces are returned in the same order as they are created.
3233
func setupTestSchedule(t *testing.T, sched *cron.Schedule) (ownerClient, memberClient *codersdk.Client, db database.Store, ws []codersdk.Workspace) {
34+
t.Helper()
35+
3336
ownerClient, db = coderdtest.NewWithDatabase(t, nil)
3437
owner := coderdtest.CreateFirstUser(t, ownerClient)
3538
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
@@ -75,18 +78,20 @@ func setupTestSchedule(t *testing.T, sched *cron.Schedule) (ownerClient, memberC
7578
return ownerClient, memberClient, db, ws
7679
}
7780

81+
//nolint:paralleltest // t.Setenv
7882
func TestScheduleShow(t *testing.T) {
79-
t.Parallel()
80-
8183
// Given
84+
// Set timezone to Asia/Kolkata to surface any timezone-related bugs.
85+
t.Setenv("TZ", "Asia/Kolkata")
86+
loc, err := tz.TimezoneIANA()
87+
require.NoError(t, err)
88+
require.Equal(t, "Asia/Kolkata", loc.String())
8289
sched, err := cron.Weekly("CRON_TZ=Europe/Dublin 30 7 * * Mon-Fri")
8390
require.NoError(t, err, "invalid schedule")
8491
ownerClient, memberClient, _, ws := setupTestSchedule(t, sched)
8592
now := time.Now()
8693

8794
t.Run("OwnerNoArgs", func(t *testing.T) {
88-
t.Parallel()
89-
9095
// When: owner specifies no args
9196
inv, root := clitest.New(t, "schedule", "show")
9297
//nolint:gocritic // Testing that owner user sees all
@@ -98,18 +103,16 @@ func TestScheduleShow(t *testing.T) {
98103
// 1st workspace: a-owner-ws1 has both autostart and autostop enabled.
99104
pty.ExpectMatch(ws[0].OwnerName + "/" + ws[0].Name)
100105
pty.ExpectMatch(sched.Humanize())
101-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
106+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
102107
pty.ExpectMatch("8h")
103-
pty.ExpectMatch(ws[0].LatestBuild.Deadline.Time.Format(time.RFC3339))
108+
pty.ExpectMatch(ws[0].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
104109
// 2nd workspace: b-owner-ws2 has only autostart enabled.
105110
pty.ExpectMatch(ws[1].OwnerName + "/" + ws[1].Name)
106111
pty.ExpectMatch(sched.Humanize())
107-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
112+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
108113
})
109114

110115
t.Run("OwnerAll", func(t *testing.T) {
111-
t.Parallel()
112-
113116
// When: owner lists all workspaces
114117
inv, root := clitest.New(t, "schedule", "show", "--all")
115118
//nolint:gocritic // Testing that owner user sees all
@@ -121,24 +124,22 @@ func TestScheduleShow(t *testing.T) {
121124
// 1st workspace: a-owner-ws1 has both autostart and autostop enabled.
122125
pty.ExpectMatch(ws[0].OwnerName + "/" + ws[0].Name)
123126
pty.ExpectMatch(sched.Humanize())
124-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
127+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
125128
pty.ExpectMatch("8h")
126-
pty.ExpectMatch(ws[0].LatestBuild.Deadline.Time.Format(time.RFC3339))
129+
pty.ExpectMatch(ws[0].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
127130
// 2nd workspace: b-owner-ws2 has only autostart enabled.
128131
pty.ExpectMatch(ws[1].OwnerName + "/" + ws[1].Name)
129132
pty.ExpectMatch(sched.Humanize())
130-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
133+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
131134
// 3rd workspace: c-member-ws3 has only autostop enabled.
132135
pty.ExpectMatch(ws[2].OwnerName + "/" + ws[2].Name)
133136
pty.ExpectMatch("8h")
134-
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339))
137+
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
135138
// 4th workspace: d-member-ws4 has neither autostart nor autostop enabled.
136139
pty.ExpectMatch(ws[3].OwnerName + "/" + ws[3].Name)
137140
})
138141

139142
t.Run("OwnerSearchByName", func(t *testing.T) {
140-
t.Parallel()
141-
142143
// When: owner specifies a search query
143144
inv, root := clitest.New(t, "schedule", "show", "--search", "name:"+ws[1].Name)
144145
//nolint:gocritic // Testing that owner user sees all
@@ -150,12 +151,10 @@ func TestScheduleShow(t *testing.T) {
150151
// 2nd workspace: b-owner-ws2 has only autostart enabled.
151152
pty.ExpectMatch(ws[1].OwnerName + "/" + ws[1].Name)
152153
pty.ExpectMatch(sched.Humanize())
153-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
154+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
154155
})
155156

156157
t.Run("OwnerOneArg", func(t *testing.T) {
157-
t.Parallel()
158-
159158
// When: owner asks for a specific workspace by name
160159
inv, root := clitest.New(t, "schedule", "show", ws[2].OwnerName+"/"+ws[2].Name)
161160
//nolint:gocritic // Testing that owner user sees all
@@ -167,12 +166,10 @@ func TestScheduleShow(t *testing.T) {
167166
// 3rd workspace: c-member-ws3 has only autostop enabled.
168167
pty.ExpectMatch(ws[2].OwnerName + "/" + ws[2].Name)
169168
pty.ExpectMatch("8h")
170-
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339))
169+
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
171170
})
172171

173172
t.Run("MemberNoArgs", func(t *testing.T) {
174-
t.Parallel()
175-
176173
// When: a member specifies no args
177174
inv, root := clitest.New(t, "schedule", "show")
178175
clitest.SetupConfig(t, memberClient, root)
@@ -183,14 +180,12 @@ func TestScheduleShow(t *testing.T) {
183180
// 1st workspace: c-member-ws3 has only autostop enabled.
184181
pty.ExpectMatch(ws[2].OwnerName + "/" + ws[2].Name)
185182
pty.ExpectMatch("8h")
186-
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339))
183+
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
187184
// 2nd workspace: d-member-ws4 has neither autostart nor autostop enabled.
188185
pty.ExpectMatch(ws[3].OwnerName + "/" + ws[3].Name)
189186
})
190187

191188
t.Run("MemberAll", func(t *testing.T) {
192-
t.Parallel()
193-
194189
// When: a member lists all workspaces
195190
inv, root := clitest.New(t, "schedule", "show", "--all")
196191
clitest.SetupConfig(t, memberClient, root)
@@ -206,14 +201,12 @@ func TestScheduleShow(t *testing.T) {
206201
// 1st workspace: c-member-ws3 has only autostop enabled.
207202
pty.ExpectMatch(ws[2].OwnerName + "/" + ws[2].Name)
208203
pty.ExpectMatch("8h")
209-
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339))
204+
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
210205
// 2nd workspace: d-member-ws4 has neither autostart nor autostop enabled.
211206
pty.ExpectMatch(ws[3].OwnerName + "/" + ws[3].Name)
212207
})
213208

214209
t.Run("JSON", func(t *testing.T) {
215-
t.Parallel()
216-
217210
// When: owner lists all workspaces in JSON format
218211
inv, root := clitest.New(t, "schedule", "show", "--all", "--output", "json")
219212
var buf bytes.Buffer
@@ -239,21 +232,21 @@ func TestScheduleShow(t *testing.T) {
239232
// 1st workspace: a-owner-ws1 has both autostart and autostop enabled.
240233
assert.Equal(t, ws[0].OwnerName+"/"+ws[0].Name, parsed[0]["workspace"])
241234
assert.Equal(t, sched.Humanize(), parsed[0]["starts_at"])
242-
assert.Equal(t, sched.Next(now).Format(time.RFC3339), parsed[0]["starts_next"])
235+
assert.Equal(t, sched.Next(now).In(loc).Format(time.RFC3339), parsed[0]["starts_next"])
243236
assert.Equal(t, "8h", parsed[0]["stops_after"])
244-
assert.Equal(t, ws[0].LatestBuild.Deadline.Time.Format(time.RFC3339), parsed[0]["stops_next"])
237+
assert.Equal(t, ws[0].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339), parsed[0]["stops_next"])
245238
// 2nd workspace: b-owner-ws2 has only autostart enabled.
246239
assert.Equal(t, ws[1].OwnerName+"/"+ws[1].Name, parsed[1]["workspace"])
247240
assert.Equal(t, sched.Humanize(), parsed[1]["starts_at"])
248-
assert.Equal(t, sched.Next(now).Format(time.RFC3339), parsed[1]["starts_next"])
241+
assert.Equal(t, sched.Next(now).In(loc).Format(time.RFC3339), parsed[1]["starts_next"])
249242
assert.Empty(t, parsed[1]["stops_after"])
250243
assert.Empty(t, parsed[1]["stops_next"])
251244
// 3rd workspace: c-member-ws3 has only autostop enabled.
252245
assert.Equal(t, ws[2].OwnerName+"/"+ws[2].Name, parsed[2]["workspace"])
253246
assert.Empty(t, parsed[2]["starts_at"])
254247
assert.Empty(t, parsed[2]["starts_next"])
255248
assert.Equal(t, "8h", parsed[2]["stops_after"])
256-
assert.Equal(t, ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339), parsed[2]["stops_next"])
249+
assert.Equal(t, ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339), parsed[2]["stops_next"])
257250
// 4th workspace: d-member-ws4 has neither autostart nor autostop enabled.
258251
assert.Equal(t, ws[3].OwnerName+"/"+ws[3].Name, parsed[3]["workspace"])
259252
assert.Empty(t, parsed[3]["starts_at"])
@@ -262,18 +255,20 @@ func TestScheduleShow(t *testing.T) {
262255
})
263256
}
264257

258+
//nolint:paralleltest // t.Setenv
265259
func TestScheduleModify(t *testing.T) {
266-
t.Parallel()
267-
268260
// Given
261+
// Set timezone to Asia/Kolkata to surface any timezone-related bugs.
262+
t.Setenv("TZ", "Asia/Kolkata")
263+
loc, err := tz.TimezoneIANA()
264+
require.NoError(t, err)
265+
require.Equal(t, "Asia/Kolkata", loc.String())
269266
sched, err := cron.Weekly("CRON_TZ=Europe/Dublin 30 7 * * Mon-Fri")
270267
require.NoError(t, err, "invalid schedule")
271268
ownerClient, _, _, ws := setupTestSchedule(t, sched)
272269
now := time.Now()
273270

274271
t.Run("SetStart", func(t *testing.T) {
275-
t.Parallel()
276-
277272
// When: we set the start schedule
278273
inv, root := clitest.New(t,
279274
"schedule", "start", ws[3].OwnerName+"/"+ws[3].Name, "7:30AM", "Mon-Fri", "Europe/Dublin",
@@ -286,12 +281,10 @@ func TestScheduleModify(t *testing.T) {
286281
// Then: the updated schedule should be shown
287282
pty.ExpectMatch(ws[3].OwnerName + "/" + ws[3].Name)
288283
pty.ExpectMatch(sched.Humanize())
289-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
284+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
290285
})
291286

292287
t.Run("SetStop", func(t *testing.T) {
293-
t.Parallel()
294-
295288
// When: we set the stop schedule
296289
inv, root := clitest.New(t,
297290
"schedule", "stop", ws[2].OwnerName+"/"+ws[2].Name, "8h30m",
@@ -304,12 +297,10 @@ func TestScheduleModify(t *testing.T) {
304297
// Then: the updated schedule should be shown
305298
pty.ExpectMatch(ws[2].OwnerName + "/" + ws[2].Name)
306299
pty.ExpectMatch("8h30m")
307-
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.Format(time.RFC3339))
300+
pty.ExpectMatch(ws[2].LatestBuild.Deadline.Time.In(loc).Format(time.RFC3339))
308301
})
309302

310303
t.Run("UnsetStart", func(t *testing.T) {
311-
t.Parallel()
312-
313304
// When: we unset the start schedule
314305
inv, root := clitest.New(t,
315306
"schedule", "start", ws[1].OwnerName+"/"+ws[1].Name, "manual",
@@ -324,8 +315,6 @@ func TestScheduleModify(t *testing.T) {
324315
})
325316

326317
t.Run("UnsetStop", func(t *testing.T) {
327-
t.Parallel()
328-
329318
// When: we unset the stop schedule
330319
inv, root := clitest.New(t,
331320
"schedule", "stop", ws[0].OwnerName+"/"+ws[0].Name, "manual",
@@ -340,10 +329,14 @@ func TestScheduleModify(t *testing.T) {
340329
})
341330
}
342331

332+
//nolint:paralleltest // t.Setenv
343333
func TestScheduleOverride(t *testing.T) {
344-
t.Parallel()
345-
346334
// Given
335+
// Set timezone to Asia/Kolkata to surface any timezone-related bugs.
336+
t.Setenv("TZ", "Asia/Kolkata")
337+
loc, err := tz.TimezoneIANA()
338+
require.NoError(t, err)
339+
require.Equal(t, "Asia/Kolkata", loc.String())
347340
sched, err := cron.Weekly("CRON_TZ=Europe/Dublin 30 7 * * Mon-Fri")
348341
require.NoError(t, err, "invalid schedule")
349342
ownerClient, _, _, ws := setupTestSchedule(t, sched)
@@ -363,7 +356,7 @@ func TestScheduleOverride(t *testing.T) {
363356
// Then: the updated schedule should be shown
364357
pty.ExpectMatch(ws[0].OwnerName + "/" + ws[0].Name)
365358
pty.ExpectMatch(sched.Humanize())
366-
pty.ExpectMatch(sched.Next(now).Format(time.RFC3339))
359+
pty.ExpectMatch(sched.Next(now).In(loc).Format(time.RFC3339))
367360
pty.ExpectMatch("8h")
368361
pty.ExpectMatch(expectedDeadline)
369362
}

0 commit comments

Comments
 (0)