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

Function TestUpdateLifecycle

coderd/agentapi/lifecycle_test.go:31–643  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29const fullMetricName = "coderd_" + agentapi.BuildDurationMetricName
30
31func TestUpdateLifecycle(t *testing.T) {
32 t.Parallel()
33
34 someTime, err := time.Parse(time.RFC3339, "2023-01-01T00:00:00Z")
35 require.NoError(t, err)
36 someTime = dbtime.Time(someTime)
37 now := dbtime.Now()
38
39 // Fixed times for build duration metric assertions.
40 // The expected duration is exactly 90 seconds.
41 buildCreatedAt := dbtime.Time(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC))
42 agentReadyAt := dbtime.Time(time.Date(2025, 1, 1, 0, 1, 30, 0, time.UTC))
43 expectedDuration := agentReadyAt.Sub(buildCreatedAt).Seconds() // 90.0
44
45 var (
46 workspaceID = uuid.New()
47 agentCreated = database.WorkspaceAgent{
48 ID: uuid.New(),
49 LifecycleState: database.WorkspaceAgentLifecycleStateCreated,
50 StartedAt: sql.NullTime{Valid: false},
51 ReadyAt: sql.NullTime{Valid: false},
52 }
53 agentStarting = database.WorkspaceAgent{
54 ID: uuid.New(),
55 LifecycleState: database.WorkspaceAgentLifecycleStateStarting,
56 StartedAt: sql.NullTime{Valid: true, Time: someTime},
57 ReadyAt: sql.NullTime{Valid: false},
58 }
59 )
60
61 t.Run("OKStarting", func(t *testing.T) {
62 t.Parallel()
63
64 lifecycle := &agentproto.Lifecycle{
65 State: agentproto.Lifecycle_STARTING,
66 ChangedAt: timestamppb.New(now),
67 }
68
69 dbM := dbmock.NewMockStore(gomock.NewController(t))
70 dbM.EXPECT().UpdateWorkspaceAgentLifecycleStateByID(gomock.Any(), database.UpdateWorkspaceAgentLifecycleStateByIDParams{
71 ID: agentCreated.ID,
72 LifecycleState: database.WorkspaceAgentLifecycleStateStarting,
73 StartedAt: sql.NullTime{
74 Time: now,
75 Valid: true,
76 },
77 ReadyAt: sql.NullTime{Valid: false},
78 }).Return(nil)
79
80 publishCalled := false
81 api := &agentapi.LifecycleAPI{
82 AgentFn: func(ctx context.Context) (database.WorkspaceAgent, error) {
83 return agentCreated, nil
84 },
85 WorkspaceID: workspaceID,
86 Database: dbM,
87 Log: testutil.Logger(t),
88 PublishWorkspaceUpdateFn: func(ctx context.Context, _ uuid.UUID, kind wspubsub.WorkspaceEventKind) error {

Callers

nothing calls this directly

Calls 15

EXPECTMethod · 0.95
UpdateLifecycleMethod · 0.95
TimeFunction · 0.92
NowFunction · 0.92
NewMockStoreFunction · 0.92
LoggerFunction · 0.92
NewLifecycleMetricsFunction · 0.92
HistogramValueFunction · 0.92
MetricValueFunction · 0.92
LogMethod · 0.80
DurationMethod · 0.80

Tested by

no test coverage detected