Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5ec9bf

Browse files
committed
GREEN: fix even more tests
1 parent aa298a6 commit a5ec9bf

File tree

6 files changed

+44
-77
lines changed

6 files changed

+44
-77
lines changed

cli/autostart_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ func TestAutostart(t *testing.T) {
164164
t.Parallel()
165165

166166
var (
167-
ctx = context.Background()
168167
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
169168
user = coderdtest.CreateFirstUser(t, client)
170169
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
@@ -180,11 +179,6 @@ func TestAutostart(t *testing.T) {
180179
clitest.SetupConfig(t, client, root)
181180

182181
err := cmd.Execute()
183-
require.NoError(t, err, "unexpected error")
184-
185-
// Ensure nothing happened
186-
updated, err := client.Workspace(ctx, workspace.ID)
187-
require.NoError(t, err, "fetch updated workspace")
188-
require.Equal(t, *workspace.AutostartSchedule, *updated.AutostartSchedule, "expected previous autostart schedule")
182+
require.ErrorContains(t, err, "schedule: Minimum autostart interval 1m0s below template minimum 1h0m0s")
189183
})
190184
}

cli/create.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/coder/coder/cli/cliflag"
1212
"github.com/coder/coder/cli/cliui"
13-
"github.com/coder/coder/coderd/autobuild/schedule"
1413
"github.com/coder/coder/coderd/util/ptr"
1514
"github.com/coder/coder/codersdk"
1615
)
@@ -61,20 +60,6 @@ func create() *cobra.Command {
6160
}
6261
}
6362

64-
tz, err := time.LoadLocation(tzName)
65-
if err != nil {
66-
return xerrors.Errorf("Invalid workspace autostart timezone: %w", err)
67-
}
68-
schedSpec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", tz.String(), autostartMinute, autostartHour, autostartDow)
69-
_, err = schedule.Weekly(schedSpec)
70-
if err != nil {
71-
return xerrors.Errorf("invalid workspace autostart schedule: %w", err)
72-
}
73-
74-
if ttl == 0 {
75-
return xerrors.Errorf("TTL must be at least 1 minute")
76-
}
77-
7863
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
7964
if err == nil {
8065
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
@@ -129,6 +114,11 @@ func create() *cobra.Command {
129114
}
130115
}
131116

117+
schedSpec := buildSchedule(autostartMinute, autostartHour, autostartDow, tzName)
118+
if ttl < time.Minute {
119+
return xerrors.Errorf("TTL must be at least 1 minute")
120+
}
121+
132122
templateVersion, err := client.TemplateVersion(cmd.Context(), template.ActiveVersionID)
133123
if err != nil {
134124
return err
@@ -226,7 +216,7 @@ func create() *cobra.Command {
226216
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.CreateWorkspaceRequest{
227217
TemplateID: template.ID,
228218
Name: workspaceName,
229-
AutostartSchedule: &schedSpec,
219+
AutostartSchedule: schedSpec,
230220
TTLMillis: ptr.Ref(ttl.Milliseconds()),
231221
ParameterValues: parameters,
232222
})
@@ -262,7 +252,16 @@ func create() *cobra.Command {
262252
cliflag.StringVarP(cmd.Flags(), &autostartMinute, "autostart-minute", "", "CODER_WORKSPACE_AUTOSTART_MINUTE", "0", "Specify the minute(s) at which the workspace should autostart (e.g. 0).")
263253
cliflag.StringVarP(cmd.Flags(), &autostartHour, "autostart-hour", "", "CODER_WORKSPACE_AUTOSTART_HOUR", "9", "Specify the hour(s) at which the workspace should autostart (e.g. 9).")
264254
cliflag.StringVarP(cmd.Flags(), &autostartDow, "autostart-day-of-week", "", "CODER_WORKSPACE_AUTOSTART_DOW", "MON-FRI", "Specify the days(s) on which the workspace should autostart (e.g. MON,TUE,WED,THU,FRI)")
265-
cliflag.StringVarP(cmd.Flags(), &tzName, "tz", "", "TZ", "", "Specify your timezone location for workspace autostart (e.g. US/Central).")
255+
cliflag.StringVarP(cmd.Flags(), &tzName, "tz", "", "TZ", "UTC", "Specify your timezone location for workspace autostart (e.g. US/Central).")
266256
cliflag.DurationVarP(cmd.Flags(), &ttl, "ttl", "", "CODER_WORKSPACE_TTL", 8*time.Hour, "Specify a time-to-live (TTL) for the workspace (e.g. 8h).")
267257
return cmd
268258
}
259+
260+
func buildSchedule(minute, hour, dow, tzName string) *string {
261+
if minute == "" || hour == "" || dow == "" {
262+
return nil
263+
}
264+
265+
schedSpec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", tzName, minute, hour, dow)
266+
return &schedSpec
267+
}

cli/create_test.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
func TestCreate(t *testing.T) {
2525
t.Parallel()
26-
t.Fatal("CIAN FIX THIS")
2726
t.Run("Create", func(t *testing.T) {
2827
t.Parallel()
2928
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
@@ -66,7 +65,6 @@ func TestCreate(t *testing.T) {
6665

6766
t.Run("AboveTemplateMaxTTL", func(t *testing.T) {
6867
t.Parallel()
69-
t.Fatal("CIAN FIX THIS")
7068
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
7169
user := coderdtest.CreateFirstUser(t, client)
7270
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
@@ -78,24 +76,20 @@ func TestCreate(t *testing.T) {
7876
"create",
7977
"my-workspace",
8078
"--template", template.Name,
81-
"--ttl", "24h",
79+
"--ttl", "12h1m",
80+
"-y", // don't bother with waiting
8281
}
8382
cmd, root := clitest.New(t, args...)
8483
clitest.SetupConfig(t, client, root)
85-
errCh := make(chan error)
8684
pty := ptytest.New(t)
8785
cmd.SetIn(pty.Input())
8886
cmd.SetOut(pty.Output())
89-
go func() {
90-
defer close(errCh)
91-
errCh <- cmd.Execute()
92-
}()
93-
require.EqualError(t, <-errCh, "TODO what is the error")
87+
err := cmd.Execute()
88+
assert.ErrorContains(t, err, "ttl_ms: ttl must be below template maximum 12h0m0s")
9489
})
9590

9691
t.Run("BelowTemplateMinAutostartInterval", func(t *testing.T) {
9792
t.Parallel()
98-
t.Fatal("CIAN FIX THIS")
9993
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
10094
user := coderdtest.CreateFirstUser(t, client)
10195
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
@@ -109,18 +103,15 @@ func TestCreate(t *testing.T) {
109103
"--template", template.Name,
110104
"--autostart-minute", "*", // Every minute
111105
"--autostart-hour", "*", // Every hour
106+
"-y", // don't bother with waiting
112107
}
113108
cmd, root := clitest.New(t, args...)
114109
clitest.SetupConfig(t, client, root)
115-
errCh := make(chan error)
116110
pty := ptytest.New(t)
117111
cmd.SetIn(pty.Input())
118112
cmd.SetOut(pty.Output())
119-
go func() {
120-
defer close(errCh)
121-
errCh <- cmd.Execute()
122-
}()
123-
require.EqualError(t, <-errCh, "TODO what is the error")
113+
err := cmd.Execute()
114+
assert.ErrorContains(t, err, "Minimum autostart interval 1m0s below template minimum 1h0m0s")
124115
})
125116

126117
t.Run("CreateErrInvalidTz", func(t *testing.T) {
@@ -135,24 +126,19 @@ func TestCreate(t *testing.T) {
135126
"my-workspace",
136127
"--template", template.Name,
137128
"--tz", "invalid",
129+
"-y",
138130
}
139131
cmd, root := clitest.New(t, args...)
140132
clitest.SetupConfig(t, client, root)
141-
doneChan := make(chan struct{})
142133
pty := ptytest.New(t)
143134
cmd.SetIn(pty.Input())
144135
cmd.SetOut(pty.Output())
145-
go func() {
146-
defer close(doneChan)
147-
err := cmd.Execute()
148-
assert.EqualError(t, err, "Invalid workspace autostart timezone: unknown time zone invalid")
149-
}()
150-
<-doneChan
136+
err := cmd.Execute()
137+
assert.ErrorContains(t, err, "schedule: parse schedule: provided bad location invalid: unknown time zone invalid")
151138
})
152139

153140
t.Run("CreateErrInvalidTTL", func(t *testing.T) {
154141
t.Parallel()
155-
t.Fatal("CIAN FIX THIS")
156142
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true})
157143
user := coderdtest.CreateFirstUser(t, client)
158144
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
@@ -163,19 +149,15 @@ func TestCreate(t *testing.T) {
163149
"my-workspace",
164150
"--template", template.Name,
165151
"--ttl", "0s",
152+
"-y",
166153
}
167154
cmd, root := clitest.New(t, args...)
168155
clitest.SetupConfig(t, client, root)
169-
doneChan := make(chan struct{})
170156
pty := ptytest.New(t)
171157
cmd.SetIn(pty.Input())
172158
cmd.SetOut(pty.Output())
173-
go func() {
174-
defer close(doneChan)
175-
err := cmd.Execute()
176-
assert.EqualError(t, err, "TTL must be at least 1 minute")
177-
}()
178-
<-doneChan
159+
err := cmd.Execute()
160+
assert.EqualError(t, err, "TTL must be at least 1 minute")
179161
})
180162

181163
t.Run("CreateFromListWithSkip", func(t *testing.T) {

cli/ttl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ func TestTTL(t *testing.T) {
193193
cmd.SetOut(stdoutBuf)
194194

195195
err := cmd.Execute()
196-
require.EqualError(t, err, "TODO what is the error")
196+
require.ErrorContains(t, err, "ttl_ms: ttl must be below template maximum 8h0m0s")
197197

198198
// Ensure ttl not updated
199199
updated, err := client.Workspace(ctx, workspace.ID)
200200
require.NoError(t, err, "fetch updated workspace")
201201
require.NotNil(t, updated.TTLMillis)
202-
require.Equal(t, (8 * time.Hour).Milliseconds, *updated.TTLMillis)
202+
require.Equal(t, (8 * time.Hour).Milliseconds(), *updated.TTLMillis)
203203
})
204204
}

coderd/workspaces.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
342342
return
343343
}
344344

345-
dbTTL, err := validWorkspaceTTLMillis(createWorkspace.TTLMillis)
345+
dbTTL, err := validWorkspaceTTLMillis(createWorkspace.TTLMillis, time.Duration(template.MaxTtl))
346346
if err != nil {
347347
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
348348
Message: "Invalid Workspace TTL",
@@ -560,20 +560,6 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
560560
return
561561
}
562562

563-
dbTTL, err := validWorkspaceTTLMillis(req.TTLMillis)
564-
if err != nil {
565-
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
566-
Message: "validate workspace ttl",
567-
Errors: []httpapi.Error{
568-
{
569-
Field: "ttl_ms",
570-
Detail: err.Error(),
571-
},
572-
},
573-
})
574-
return
575-
}
576-
577563
template, err := api.Database.GetTemplateByID(r.Context(), workspace.TemplateID)
578564
if err != nil {
579565
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
@@ -582,16 +568,18 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
582568
return
583569
}
584570

585-
if dbTTL.Valid && dbTTL.Int64 > template.MaxTtl {
571+
dbTTL, err := validWorkspaceTTLMillis(req.TTLMillis, time.Duration(template.MaxTtl))
572+
if err != nil {
586573
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
587-
Message: "Constrained by template",
574+
Message: "Invalid workspace TTL",
588575
Errors: []httpapi.Error{
589576
{
590577
Field: "ttl_ms",
591-
Detail: fmt.Sprintf("requested value is %s but template max is %s", time.Duration(dbTTL.Int64), time.Duration(template.MaxTtl)),
578+
Detail: err.Error(),
592579
},
593580
},
594581
})
582+
return
595583
}
596584

597585
err = api.Database.UpdateWorkspaceTTL(r.Context(), database.UpdateWorkspaceTTLParams{
@@ -883,7 +871,7 @@ func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {
883871
return &millis
884872
}
885873

886-
func validWorkspaceTTLMillis(millis *int64) (sql.NullInt64, error) {
874+
func validWorkspaceTTLMillis(millis *int64, max time.Duration) (sql.NullInt64, error) {
887875
if ptr.NilOrZero(millis) {
888876
return sql.NullInt64{}, nil
889877
}
@@ -898,6 +886,10 @@ func validWorkspaceTTLMillis(millis *int64) (sql.NullInt64, error) {
898886
return sql.NullInt64{}, xerrors.New("ttl must be less than 7 days")
899887
}
900888

889+
if truncated > max {
890+
return sql.NullInt64{}, xerrors.Errorf("ttl must be below template maximum %s", max.String())
891+
}
892+
901893
return sql.NullInt64{
902894
Valid: true,
903895
Int64: int64(truncated),

coderd/workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func TestWorkspaceUpdateTTL(t *testing.T) {
647647
{
648648
name: "above template maximum ttl",
649649
ttlMillis: ptr.Ref((12 * time.Hour).Milliseconds()),
650-
expectedError: "requested value is 12h0m0s but template max is 8h0m0s",
650+
expectedError: "ttl_ms: ttl must be below template maximum 8h0m0s",
651651
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) { ctr.MaxTTLMillis = ptr.Ref((8 * time.Hour).Milliseconds()) },
652652
},
653653
}

0 commit comments

Comments
 (0)