Skip to content

Commit 57a24fa

Browse files
committed
fix: resolve race condition in TestWorkspaceAgent/GoogleCloud
The test was flaky due to a race condition where the agent CLI would start and attempt Google Cloud instance identity authentication before the agent was fully inserted into the database. This fix adds a synchronization point that waits for the agent to be properly inserted into the database before starting the agent CLI, eliminating the race condition. Fixes: coder/internal#778
1 parent 4ac6be6 commit 57a24fa

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

cli/agent_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/coder/coder/v2/coderd"
2121
"github.com/coder/coder/v2/coderd/coderdtest"
2222
"github.com/coder/coder/v2/coderd/database"
23+
"github.com/coder/coder/v2/coderd/database/dbauthz"
2324
"github.com/coder/coder/v2/coderd/database/dbfake"
2425
"github.com/coder/coder/v2/codersdk"
2526
"github.com/coder/coder/v2/codersdk/workspacesdk"
@@ -159,6 +160,15 @@ func TestWorkspaceAgent(t *testing.T) {
159160
return agents
160161
}).Do()
161162

163+
// Wait for the agent to be properly inserted into the database before starting the CLI.
164+
// This prevents a race condition where the agent tries to authenticate before
165+
// the database has been updated with the agent's instance ID.
166+
require.Eventually(t, func() bool {
167+
ctx := dbauthz.AsSystemRestricted(context.Background())
168+
_, err := db.GetWorkspaceAgentByInstanceID(ctx, instanceID)
169+
return err == nil
170+
}, testutil.WaitMedium, testutil.IntervalFast)
171+
162172
inv, cfg := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
163173
clitest.SetupConfig(t, member, cfg)
164174

0 commit comments

Comments
 (0)