(t *testing.T)
| 29 | const fullMetricName = "coderd_" + agentapi.BuildDurationMetricName |
| 30 | |
| 31 | func 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 { |
nothing calls this directly
no test coverage detected