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

Function TestWorkspaceAgent_LifecycleState

coderd/workspaceagents_test.go:2212–2285  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2210}
2211
2212func TestWorkspaceAgent_LifecycleState(t *testing.T) {
2213 t.Parallel()
2214
2215 t.Run("Set", func(t *testing.T) {
2216 t.Parallel()
2217 ctx := testutil.Context(t, testutil.WaitLong)
2218
2219 client, db := coderdtest.NewWithDatabase(t, nil)
2220 user := coderdtest.CreateFirstUser(t, client)
2221 r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
2222 OrganizationID: user.OrganizationID,
2223 OwnerID: user.UserID,
2224 }).WithAgent().Do()
2225 workspace, err := client.Workspace(context.Background(), r.Workspace.ID)
2226 require.NoError(t, err)
2227 for _, res := range workspace.LatestBuild.Resources {
2228 for _, a := range res.Agents {
2229 require.Equal(t, codersdk.WorkspaceAgentLifecycleCreated, a.LifecycleState)
2230 }
2231 }
2232
2233 ac := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken))
2234 conn, err := ac.ConnectRPC(ctx)
2235 require.NoError(t, err)
2236 defer func() {
2237 cErr := conn.Close()
2238 require.NoError(t, cErr)
2239 }()
2240 agentAPI := agentproto.NewDRPCAgentClient(conn)
2241
2242 tests := []struct {
2243 state codersdk.WorkspaceAgentLifecycle
2244 wantErr bool
2245 }{
2246 {codersdk.WorkspaceAgentLifecycleCreated, false},
2247 {codersdk.WorkspaceAgentLifecycleStarting, false},
2248 {codersdk.WorkspaceAgentLifecycleStartTimeout, false},
2249 {codersdk.WorkspaceAgentLifecycleStartError, false},
2250 {codersdk.WorkspaceAgentLifecycleReady, false},
2251 {codersdk.WorkspaceAgentLifecycleShuttingDown, false},
2252 {codersdk.WorkspaceAgentLifecycleShutdownTimeout, false},
2253 {codersdk.WorkspaceAgentLifecycleShutdownError, false},
2254 {codersdk.WorkspaceAgentLifecycleOff, false},
2255 {codersdk.WorkspaceAgentLifecycle("nonexistent_state"), true},
2256 {codersdk.WorkspaceAgentLifecycle(""), true},
2257 }
2258 //nolint:paralleltest // No race between setting the state and getting the workspace.
2259 for _, tt := range tests {
2260 t.Run(string(tt.state), func(t *testing.T) {
2261 state, err := agentsdk.ProtoFromLifecycleState(tt.state)
2262 if tt.wantErr {
2263 require.Error(t, err)
2264 return
2265 }
2266 _, err = agentAPI.UpdateLifecycle(ctx, &agentproto.UpdateLifecycleRequest{
2267 Lifecycle: &agentproto.Lifecycle{
2268 State: state,
2269 ChangedAt: timestamppb.Now(),

Callers

nothing calls this directly

Calls 15

UpdateLifecycleMethod · 0.95
ContextFunction · 0.92
NewWithDatabaseFunction · 0.92
CreateFirstUserFunction · 0.92
WorkspaceBuildFunction · 0.92
NewFunction · 0.92
WithFixedTokenFunction · 0.92
WorkspaceAgentLifecycleTypeAlias · 0.92
ProtoFromLifecycleStateFunction · 0.92
WithAgentMethod · 0.80
WorkspaceMethod · 0.80
ConnectRPCMethod · 0.80

Tested by

no test coverage detected