Content-Length: 382757 | pFad | http://github.com/coder/coder/commit/c97167262c603b71b56b45a21a0fb3917c0d0a4e

D5 fix: resolve race condition in TestWorkspaceAgent instance identity t… · coder/coder@c971672 · GitHub
Skip to content

Commit c971672

Browse files
committed
fix: resolve race condition in TestWorkspaceAgent instance identity tests
The TestWorkspaceAgent/GoogleCloud test was flaky because the agent could start and attempt authentication before the workspace build resources (including the agent with its instance ID) were fully inserted into the database. This fix ensures that workspace resources are queryable before starting the agent, preventing the 404 error when the agent tries to authenticate with its instance ID. The same fix has been applied to all instance identity tests (Azure, AWS, and Google Cloud) to prevent similar race conditions. Fixes coder/internal#778 Co-authored-by: sreya <4856196+sreya@users.noreply.github.com>
1 parent aedc019 commit c971672

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

cli/agent_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,20 @@ func TestWorkspaceAgent(t *testing.T) {
8080
}).Do()
8181

8282
inv, _ := clitest.New(t, "agent", "--auth", "azure-instance-identity", "--agent-url", client.URL.String())
83+
ctx := inv.Context()
8384
inv = inv.WithContext(
8485
//nolint:revive,staticcheck
85-
context.WithValue(inv.Context(), "azure-client", metadataClient),
86+
context.WithValue(ctx, "azure-client", metadataClient),
8687
)
8788

88-
ctx := inv.Context()
89+
// Ensure the workspace build resources are queryable before starting the agent.
90+
// This prevents a race condition where the agent tries to authenticate before
91+
// the provisioner has inserted the agent records.
92+
ws, err := client.Workspace(ctx, r.Workspace.ID)
93+
require.NoError(t, err)
94+
require.NotEmpty(t, ws.LatestBuild.Resources, "workspace resources should be available")
95+
require.NotEmpty(t, ws.LatestBuild.Resources[0].Agents, "workspace agents should be available")
96+
8997
clitest.Start(t, inv)
9098
coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).
9199
MatchResources(matchAgentWithVersion).Wait()
@@ -119,13 +127,21 @@ func TestWorkspaceAgent(t *testing.T) {
119127
}).Do()
120128

121129
inv, _ := clitest.New(t, "agent", "--auth", "aws-instance-identity", "--agent-url", client.URL.String())
130+
ctx := inv.Context()
122131
inv = inv.WithContext(
123132
//nolint:revive,staticcheck
124-
context.WithValue(inv.Context(), "aws-client", metadataClient),
133+
context.WithValue(ctx, "aws-client", metadataClient),
125134
)
126135

136+
// Ensure the workspace build resources are queryable before starting the agent.
137+
// This prevents a race condition where the agent tries to authenticate before
138+
// the provisioner has inserted the agent records.
139+
ws, err := client.Workspace(ctx, r.Workspace.ID)
140+
require.NoError(t, err)
141+
require.NotEmpty(t, ws.LatestBuild.Resources, "workspace resources should be available")
142+
require.NotEmpty(t, ws.LatestBuild.Resources[0].Agents, "workspace agents should be available")
143+
127144
clitest.Start(t, inv)
128-
ctx := inv.Context()
129145
coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).
130146
MatchResources(matchAgentWithVersion).
131147
Wait()
@@ -162,14 +178,21 @@ func TestWorkspaceAgent(t *testing.T) {
162178
inv, cfg := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
163179
clitest.SetupConfig(t, member, cfg)
164180

181+
// Ensure the workspace build resources are queryable before starting the agent.
182+
// This prevents a race condition where the agent tries to authenticate before
183+
// the provisioner has inserted the agent records.
184+
ctx := inv.Context()
185+
ws, err := client.Workspace(ctx, r.Workspace.ID)
186+
require.NoError(t, err)
187+
require.NotEmpty(t, ws.LatestBuild.Resources, "workspace resources should be available")
188+
require.NotEmpty(t, ws.LatestBuild.Resources[0].Agents, "workspace agents should be available")
189+
165190
clitest.Start(t,
166191
inv.WithContext(
167192
//nolint:revive,staticcheck
168-
context.WithValue(inv.Context(), "gcp-client", metadataClient),
193+
context.WithValue(ctx, "gcp-client", metadataClient),
169194
),
170195
)
171-
172-
ctx := inv.Context()
173196
coderdtest.NewWorkspaceAgentWaiter(t, client, r.Workspace.ID).
174197
MatchResources(matchAgentWithVersion).
175198
Wait()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/coder/coder/commit/c97167262c603b71b56b45a21a0fb3917c0d0a4e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy