MCPcopy Index your code
hub / github.com/coder/coder / TestExecutorAutostartOK

Function TestExecutorAutostartOK

coderd/autobuild/lifecycle_executor_test.go:44–88  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

42}
43
44func TestExecutorAutostartOK(t *testing.T) {
45 t.Parallel()
46
47 var (
48 sched = mustSchedule(t, "CRON_TZ=UTC 0 * * * *")
49 tickCh = make(chan time.Time)
50 statsCh = make(chan autobuild.Stats)
51 client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{
52 AutobuildTicker: tickCh,
53 IncludeProvisionerDaemon: true,
54 AutobuildStats: statsCh,
55 })
56 // Given: we have a user with a workspace that has autostart enabled
57 workspace = mustProvisionWorkspace(t, client, func(cwr *codersdk.CreateWorkspaceRequest) {
58 cwr.AutostartSchedule = ptr.Ref(sched.String())
59 })
60 )
61 // Given: workspace is stopped
62 workspace = coderdtest.MustTransitionWorkspace(t, client, workspace.ID, codersdk.WorkspaceTransitionStart, codersdk.WorkspaceTransitionStop)
63 p, err := coderdtest.GetProvisionerForTags(db, time.Now(), workspace.OrganizationID, map[string]string{})
64 require.NoError(t, err)
65 // When: the autobuild executor ticks after the scheduled time
66 go func() {
67 tickTime := sched.Next(workspace.LatestBuild.CreatedAt)
68 coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime)
69 tickCh <- tickTime
70 close(tickCh)
71 }()
72
73 // Then: the workspace should eventually be started
74 stats := <-statsCh
75 assert.Len(t, stats.Errors, 0)
76 assert.Len(t, stats.Transitions, 1)
77 assert.Contains(t, stats.Transitions, workspace.ID)
78 assert.Equal(t, database.WorkspaceTransitionStart, stats.Transitions[workspace.ID])
79
80 workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
81 assert.Equal(t, codersdk.BuildReasonAutostart, workspace.LatestBuild.Reason)
82 // Assert some template props. If this is not set correctly, the test
83 // will fail.
84 ctx := testutil.Context(t, testutil.WaitShort)
85 template, err := client.Template(ctx, workspace.TemplateID)
86 require.NoError(t, err)
87 require.Equal(t, template.AutostartRequirement.DaysOfWeek, []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"})
88}
89
90func TestMultipleLifecycleExecutors(t *testing.T) {
91 t.Parallel()

Callers

nothing calls this directly

Calls 15

NewWithDatabaseFunction · 0.92
RefFunction · 0.92
MustTransitionWorkspaceFunction · 0.92
GetProvisionerForTagsFunction · 0.92
MustWorkspaceFunction · 0.92
ContextFunction · 0.92
mustProvisionWorkspaceFunction · 0.85
TemplateMethod · 0.80
mustScheduleFunction · 0.70
NextMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected