mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
refactor: workspace autostop_schedule -> ttl (#1578)
Co-authored-by: G r e y <grey@coder.com>
This commit is contained in:
@ -551,69 +551,21 @@ func TestWorkspaceUpdateAutostart(t *testing.T) {
|
||||
|
||||
func TestWorkspaceUpdateAutostop(t *testing.T) {
|
||||
t.Parallel()
|
||||
var dublinLoc = mustLocation(t, "Europe/Dublin")
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
schedule string
|
||||
expectedError string
|
||||
at time.Time
|
||||
expectedNext time.Time
|
||||
expectedInterval time.Duration
|
||||
name string
|
||||
ttl *time.Duration
|
||||
expectedError string
|
||||
}{
|
||||
{
|
||||
name: "disable autostop",
|
||||
schedule: "",
|
||||
name: "disable ttl",
|
||||
ttl: nil,
|
||||
expectedError: "",
|
||||
},
|
||||
{
|
||||
name: "friday to monday",
|
||||
schedule: "CRON_TZ=Europe/Dublin 30 17 * * 1-5",
|
||||
expectedError: "",
|
||||
at: time.Date(2022, 5, 6, 17, 31, 0, 0, dublinLoc),
|
||||
expectedNext: time.Date(2022, 5, 9, 17, 30, 0, 0, dublinLoc),
|
||||
expectedInterval: 71*time.Hour + 59*time.Minute,
|
||||
},
|
||||
{
|
||||
name: "monday to tuesday",
|
||||
schedule: "CRON_TZ=Europe/Dublin 30 17 * * 1-5",
|
||||
expectedError: "",
|
||||
at: time.Date(2022, 5, 9, 17, 31, 0, 0, dublinLoc),
|
||||
expectedNext: time.Date(2022, 5, 10, 17, 30, 0, 0, dublinLoc),
|
||||
expectedInterval: 23*time.Hour + 59*time.Minute,
|
||||
},
|
||||
{
|
||||
// DST in Ireland began on Mar 27 in 2022 at 0100. Forward 1 hour.
|
||||
name: "DST start",
|
||||
schedule: "CRON_TZ=Europe/Dublin 30 17 * * *",
|
||||
expectedError: "",
|
||||
at: time.Date(2022, 3, 26, 17, 31, 0, 0, dublinLoc),
|
||||
expectedNext: time.Date(2022, 3, 27, 17, 30, 0, 0, dublinLoc),
|
||||
expectedInterval: 22*time.Hour + 59*time.Minute,
|
||||
},
|
||||
{
|
||||
// DST in Ireland ends on Oct 30 in 2022 at 0200. Back 1 hour.
|
||||
name: "DST end",
|
||||
schedule: "CRON_TZ=Europe/Dublin 30 17 * * *",
|
||||
expectedError: "",
|
||||
at: time.Date(2022, 10, 29, 17, 31, 0, 0, dublinLoc),
|
||||
expectedNext: time.Date(2022, 10, 30, 17, 30, 0, 0, dublinLoc),
|
||||
expectedInterval: 24*time.Hour + 59*time.Minute,
|
||||
},
|
||||
{
|
||||
name: "invalid location",
|
||||
schedule: "CRON_TZ=Imaginary/Place 30 17 * * 1-5",
|
||||
expectedError: "status code 500: invalid autostop 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 autostop schedule: validate weekly schedule: expected schedule to consist of 5 fields with an optional CRON_TZ=<timezone> prefix`,
|
||||
},
|
||||
{
|
||||
name: "only 3 values",
|
||||
schedule: "CRON_TZ=Europe/Dublin 30 9 *",
|
||||
expectedError: `status code 500: invalid autostop schedule: validate weekly schedule: expected schedule to consist of 5 fields with an optional CRON_TZ=<timezone> prefix`,
|
||||
name: "enable ttl",
|
||||
ttl: ptr(time.Hour),
|
||||
expectedError: "",
|
||||
},
|
||||
}
|
||||
|
||||
@ -633,10 +585,10 @@ func TestWorkspaceUpdateAutostop(t *testing.T) {
|
||||
)
|
||||
|
||||
// ensure test invariant: new workspaces have no autostop schedule.
|
||||
require.Empty(t, workspace.AutostopSchedule, "expected newly-minted workspace to have no autstop schedule")
|
||||
require.Nil(t, workspace.TTL, "expected newly-minted workspace to have no TTL")
|
||||
|
||||
err := client.UpdateWorkspaceAutostop(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostopRequest{
|
||||
Schedule: testCase.schedule,
|
||||
err := client.UpdateWorkspaceTTL(ctx, workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
|
||||
TTL: testCase.ttl,
|
||||
})
|
||||
|
||||
if testCase.expectedError != "" {
|
||||
@ -649,18 +601,7 @@ func TestWorkspaceUpdateAutostop(t *testing.T) {
|
||||
updated, err := client.Workspace(ctx, workspace.ID)
|
||||
require.NoError(t, err, "fetch updated workspace")
|
||||
|
||||
require.Equal(t, testCase.schedule, updated.AutostopSchedule, "expected autostop schedule to equal requested")
|
||||
|
||||
if testCase.schedule == "" {
|
||||
return
|
||||
}
|
||||
sched, err := schedule.Weekly(updated.AutostopSchedule)
|
||||
require.NoError(t, err, "parse returned schedule")
|
||||
|
||||
next := sched.Next(testCase.at)
|
||||
require.Equal(t, testCase.expectedNext, next, "unexpected next scheduled autostop time")
|
||||
interval := next.Sub(testCase.at)
|
||||
require.Equal(t, testCase.expectedInterval, interval, "unexpected interval")
|
||||
require.Equal(t, testCase.ttl, updated.TTL, "expected autostop ttl to equal requested")
|
||||
})
|
||||
}
|
||||
|
||||
@ -670,12 +611,12 @@ func TestWorkspaceUpdateAutostop(t *testing.T) {
|
||||
client = coderdtest.New(t, nil)
|
||||
_ = coderdtest.CreateFirstUser(t, client)
|
||||
wsid = uuid.New()
|
||||
req = codersdk.UpdateWorkspaceAutostopRequest{
|
||||
Schedule: "9 30 1-5",
|
||||
req = codersdk.UpdateWorkspaceTTLRequest{
|
||||
TTL: ptr(time.Hour),
|
||||
}
|
||||
)
|
||||
|
||||
err := client.UpdateWorkspaceAutostop(ctx, wsid, req)
|
||||
err := client.UpdateWorkspaceTTL(ctx, wsid, req)
|
||||
require.IsType(t, err, &codersdk.Error{}, "expected codersdk.Error")
|
||||
coderSDKErr, _ := err.(*codersdk.Error) //nolint:errorlint
|
||||
require.Equal(t, coderSDKErr.StatusCode(), 404, "expected status code 404")
|
||||
@ -683,15 +624,6 @@ func TestWorkspaceUpdateAutostop(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
return loc
|
||||
}
|
||||
|
||||
func TestWorkspaceWatcher(t *testing.T) {
|
||||
t.Parallel()
|
||||
client := coderdtest.New(t, nil)
|
||||
@ -715,3 +647,17 @@ func TestWorkspaceWatcher(t *testing.T) {
|
||||
cancel()
|
||||
require.EqualValues(t, codersdk.Workspace{}, <-wc)
|
||||
}
|
||||
|
||||
func mustLocation(t *testing.T, location string) *time.Location {
|
||||
t.Helper()
|
||||
loc, err := time.LoadLocation(location)
|
||||
if err != nil {
|
||||
t.Errorf("failed to load location %s: %s", location, err.Error())
|
||||
}
|
||||
|
||||
return loc
|
||||
}
|
||||
|
||||
func ptr[T any](x T) *T {
|
||||
return &x
|
||||
}
|
||||
|
Reference in New Issue
Block a user