Skip to content

Commit fb9d8e3

Browse files
authored
feat(cli): warn user if setting autostart on workspace with template-level autostart (coder#20454)
Fixes: coder#15619
1 parent e60112e commit fb9d8e3

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

cli/schedule.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ func (r *RootCmd) scheduleStart() *serpent.Command {
176176
}
177177

178178
schedStr = ptr.Ref(sched.String())
179+
180+
// Check if the template has autostart requirements that may conflict
181+
// with the user's schedule.
182+
template, err := client.Template(inv.Context(), workspace.TemplateID)
183+
if err != nil {
184+
return xerrors.Errorf("get template: %w", err)
185+
}
186+
187+
if len(template.AutostartRequirement.DaysOfWeek) > 0 {
188+
_, _ = fmt.Fprintf(
189+
inv.Stderr,
190+
"Warning: your workspace template restricts autostart to the following days: %s.\n"+
191+
"Your workspace may only autostart on these days.\n",
192+
strings.Join(template.AutostartRequirement.DaysOfWeek, ", "),
193+
)
194+
}
179195
}
180196

181197
err = client.UpdateWorkspaceAutostart(inv.Context(), workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{

cli/schedule_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,67 @@ func TestScheduleOverride(t *testing.T) {
373373
})
374374
}
375375
}
376+
377+
//nolint:paralleltest // t.Setenv
378+
func TestScheduleStart_TemplateAutostartRequirement(t *testing.T) {
379+
t.Setenv("TZ", "UTC")
380+
loc, err := tz.TimezoneIANA()
381+
require.NoError(t, err)
382+
require.Equal(t, "UTC", loc.String())
383+
384+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
385+
user := coderdtest.CreateFirstUser(t, client)
386+
387+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
388+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
389+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
390+
391+
// Update template to have autostart requirement
392+
// Note: In AGPL, this will be ignored and all days will be allowed (enterprise feature).
393+
template, err = client.UpdateTemplateMeta(context.Background(), template.ID, codersdk.UpdateTemplateMeta{
394+
AutostartRequirement: &codersdk.TemplateAutostartRequirement{
395+
DaysOfWeek: []string{"monday", "wednesday", "friday"},
396+
},
397+
})
398+
require.NoError(t, err)
399+
400+
// Verify the template - in AGPL, AutostartRequirement will have all days (enterprise feature)
401+
template, err = client.Template(context.Background(), template.ID)
402+
require.NoError(t, err)
403+
require.NotEmpty(t, template.AutostartRequirement.DaysOfWeek, "template should have autostart requirement days")
404+
405+
workspace := coderdtest.CreateWorkspace(t, client, template.ID)
406+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
407+
408+
t.Run("ShowsWarning", func(t *testing.T) {
409+
// When: user sets autostart schedule
410+
inv, root := clitest.New(t,
411+
"schedule", "start", workspace.Name, "9:30AM", "Mon-Fri",
412+
)
413+
clitest.SetupConfig(t, client, root)
414+
pty := ptytest.New(t).Attach(inv)
415+
require.NoError(t, inv.Run())
416+
417+
// Then: warning should be shown
418+
// In AGPL, this will show all days (enterprise feature defaults to all days allowed)
419+
pty.ExpectMatch("Warning")
420+
pty.ExpectMatch("may only autostart")
421+
})
422+
423+
t.Run("NoWarningWhenManual", func(t *testing.T) {
424+
// When: user sets manual schedule
425+
inv, root := clitest.New(t,
426+
"schedule", "start", workspace.Name, "manual",
427+
)
428+
clitest.SetupConfig(t, client, root)
429+
430+
var stderrBuf bytes.Buffer
431+
inv.Stderr = &stderrBuf
432+
433+
require.NoError(t, inv.Run())
434+
435+
// Then: no warning should be shown on stderr
436+
stderrOutput := stderrBuf.String()
437+
require.NotContains(t, stderrOutput, "Warning")
438+
})
439+
}

0 commit comments

Comments
 (0)