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

Function TestWorkspaceActivityBump

coderd/activitybump_test.go:23–256  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestWorkspaceActivityBump(t *testing.T) {
24 t.Parallel()
25
26 ctx := context.Background()
27
28 // deadline allows you to forcibly set a max_deadline on the build. This
29 // doesn't use template autostop requirements and instead edits the
30 // max_deadline on the build directly in the database.
31 setupActivityTest := func(t *testing.T, deadline ...time.Duration) (client *codersdk.Client, workspace codersdk.Workspace, assertBumped func(want bool)) {
32 t.Helper()
33 const ttl = time.Hour
34
35 db, pubsub := dbtestutil.NewDB(t)
36 client = coderdtest.New(t, &coderdtest.Options{
37 Database: db,
38 Pubsub: pubsub,
39 IncludeProvisionerDaemon: true,
40 // Agent stats trigger the activity bump, so we want to report
41 // very frequently in tests.
42 AgentStatsRefreshInterval: time.Millisecond * 100,
43 TemplateScheduleStore: schedule.MockTemplateScheduleStore{
44 GetFn: func(ctx context.Context, db database.Store, templateID uuid.UUID) (schedule.TemplateScheduleOptions, error) {
45 return schedule.TemplateScheduleOptions{
46 UserAutostopEnabled: true,
47 DefaultTTL: ttl,
48 // We set max_deadline manually below.
49 AutostopRequirement: schedule.TemplateAutostopRequirement{},
50 }, nil
51 },
52 },
53 })
54 user := coderdtest.CreateFirstUser(t, client)
55
56 ttlMillis := int64(ttl / time.Millisecond)
57 agentToken := uuid.NewString()
58 version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
59 Parse: echo.ParseComplete,
60 ProvisionPlan: echo.PlanComplete,
61 ProvisionGraph: echo.ProvisionGraphWithAgent(agentToken),
62 })
63 template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
64 coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
65 workspace = coderdtest.CreateWorkspace(t, client, template.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
66 cwr.TTLMillis = &ttlMillis
67 })
68 coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
69
70 var maxDeadline time.Time
71 // Update the max deadline.
72 if len(deadline) > 0 {
73 maxDeadline = dbtime.Now().Add(deadline[0])
74 }
75
76 err := db.UpdateWorkspaceBuildDeadlineByID(ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{
77 ID: workspace.LatestBuild.ID,
78 UpdatedAt: dbtime.Now(),
79 // Make the deadline really close so it needs to be bumped immediately.
80 Deadline: dbtime.Now().Add(time.Minute),

Callers

nothing calls this directly

Calls 15

NewDBFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateTemplateVersionFunction · 0.92
ProvisionGraphWithAgentFunction · 0.92
CreateTemplateFunction · 0.92
CreateWorkspaceFunction · 0.92
NowFunction · 0.92
NewFunction · 0.92
AwaitWorkspaceAgentsFunction · 0.92

Tested by

no test coverage detected