These tests are dependent on the state of the coder server. Running them in parallel is prone to racy behavior. nolint:tparallel,paralleltest
(t *testing.T)
| 167 | // Running them in parallel is prone to racy behavior. |
| 168 | // nolint:tparallel,paralleltest |
| 169 | func TestTools(t *testing.T) { |
| 170 | // Given: a running coderd instance using SSH test setup pattern |
| 171 | setupCtx := testutil.Context(t, testutil.WaitShort) |
| 172 | client, store := coderdtest.NewWithDatabase(t, nil) |
| 173 | owner := coderdtest.CreateFirstUser(t, client) |
| 174 | // Given: a member user with which to test the tools. |
| 175 | memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 176 | // Given: a workspace with an agent. |
| 177 | // nolint:gocritic // This is in a test package and does not end up in the build |
| 178 | r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ |
| 179 | OrganizationID: owner.OrganizationID, |
| 180 | OwnerID: member.ID, |
| 181 | }).WithAgent(func(agents []*proto.Agent) []*proto.Agent { |
| 182 | agents[0].Apps = []*proto.App{ |
| 183 | { |
| 184 | Slug: "some-agent-app", |
| 185 | }, |
| 186 | } |
| 187 | return agents |
| 188 | }).Do() |
| 189 | preset := dbgen.Preset(t, store, database.InsertPresetParams{ |
| 190 | TemplateVersionID: r.TemplateVersion.ID, |
| 191 | Name: testutil.GetRandomNameHyphenated(t), |
| 192 | CreatedAt: r.TemplateVersion.CreatedAt, |
| 193 | Description: "Preset for agent tool tests.", |
| 194 | }) |
| 195 | |
| 196 | // Given: a client configured with the agent token. |
| 197 | agentClient := agentsdk.New(client.URL, agentsdk.WithFixedToken(r.AgentToken)) |
| 198 | // Get the agent ID from the API. Overriding it in dbfake doesn't work. |
| 199 | ws, err := client.Workspace(setupCtx, r.Workspace.ID) |
| 200 | require.NoError(t, err) |
| 201 | require.NotEmpty(t, ws.LatestBuild.Resources) |
| 202 | require.NotEmpty(t, ws.LatestBuild.Resources[0].Agents) |
| 203 | agentID := ws.LatestBuild.Resources[0].Agents[0].ID |
| 204 | |
| 205 | // Given: the workspace agent has written logs. |
| 206 | agentClient.PatchLogs(setupCtx, agentsdk.PatchLogs{ |
| 207 | Logs: []agentsdk.Log{ |
| 208 | { |
| 209 | CreatedAt: time.Now(), |
| 210 | Level: codersdk.LogLevelInfo, |
| 211 | Output: "test log message", |
| 212 | }, |
| 213 | }, |
| 214 | }) |
| 215 | |
| 216 | t.Run("ReportTask", func(t *testing.T) { |
| 217 | ctx := testutil.Context(t, testutil.WaitShort) |
| 218 | tb, err := toolsdk.NewDeps(memberClient, toolsdk.WithTaskReporter(func(args toolsdk.ReportTaskArgs) error { |
| 219 | return agentClient.PatchAppStatus(ctx, agentsdk.PatchAppStatus{ |
| 220 | AppSlug: "some-agent-app", |
| 221 | Message: args.Summary, |
| 222 | URI: args.Link, |
| 223 | State: codersdk.WorkspaceAppStatusState(args.State), |
| 224 | }) |
| 225 | })) |
| 226 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected