+109
-17
lines changedFilter options
+109
-17
lines changed Original file line number Diff line number Diff line change
@@ -925,36 +925,33 @@ func getWorkspaceAndAgent(ctx context.Context, inv *serpent.Invocation, client *
925
925
func getWorkspaceAgent(workspace codersdk.Workspace, agentName string) (workspaceAgent codersdk.WorkspaceAgent, err error) {
926
926
resources := workspace.LatestBuild.Resources
927
927
928
-
agents := make([]codersdk.WorkspaceAgent, 0)
928
+
var (
929
+
availableNames []string
930
+
agents []codersdk.WorkspaceAgent
931
+
)
929
932
for _, resource := range resources {
930
-
agents = append(agents, resource.Agents...)
933
+
for _, agent := range resource.Agents {
934
+
availableNames = append(availableNames, agent.Name)
935
+
agents = append(agents, agent)
936
+
}
931
937
}
932
938
if len(agents) == 0 {
933
939
return codersdk.WorkspaceAgent{}, xerrors.Errorf("workspace %q has no agents", workspace.Name)
934
940
}
941
+
slices.Sort(availableNames)
935
942
if agentName != "" {
936
943
for _, otherAgent := range agents {
937
944
if otherAgent.Name != agentName {
938
945
continue
939
946
}
940
-
workspaceAgent = otherAgent
941
-
break
942
-
}
943
-
if workspaceAgent.ID == uuid.Nil {
944
-
return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q", agentName)
947
+
return otherAgent, nil
945
948
}
949
+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("agent not found by name %q, available agents: %v", agentName, availableNames)
946
950
}
947
-
if workspaceAgent.ID == uuid.Nil {
948
-
if len(agents) > 1 {
949
-
workspaceAgent, err = cryptorand.Element(agents)
950
-
if err != nil {
951
-
return codersdk.WorkspaceAgent{}, err
952
-
}
953
-
} else {
954
-
workspaceAgent = agents[0]
955
-
}
951
+
if len(agents) == 1 {
952
+
return agents[0], nil
956
953
}
957
-
return workspaceAgent, nil
954
+
return codersdk.WorkspaceAgent{}, xerrors.Errorf("multiple agents found, please specify the agent name, available agents: %v", availableNames)
958
955
}
959
956
960
957
// Attempt to poll workspace autostop. We write a per-workspace lockfile to
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
11
11
"time"
12
12
13
13
gliderssh "github.com/gliderlabs/ssh"
14
+
"github.com/google/uuid"
14
15
"github.com/stretchr/testify/assert"
15
16
"github.com/stretchr/testify/require"
16
17
"golang.org/x/crypto/ssh"
@@ -346,3 +347,97 @@ func newAsyncCloser(ctx context.Context, t *testing.T) *asyncCloser {
346
347
started: make(chan struct{}),
347
348
}
348
349
}
350
+
351
+
func Test_getWorkspaceAgent(t *testing.T) {
352
+
t.Parallel()
353
+
354
+
createWorkspaceWithAgents := func(agents []codersdk.WorkspaceAgent) codersdk.Workspace {
355
+
return codersdk.Workspace{
356
+
Name: "test-workspace",
357
+
LatestBuild: codersdk.WorkspaceBuild{
358
+
Resources: []codersdk.WorkspaceResource{
359
+
{
360
+
Agents: agents,
361
+
},
362
+
},
363
+
},
364
+
}
365
+
}
366
+
367
+
createAgent := func(name string) codersdk.WorkspaceAgent {
368
+
return codersdk.WorkspaceAgent{
369
+
ID: uuid.New(),
370
+
Name: name,
371
+
}
372
+
}
373
+
374
+
t.Run("SingleAgent_NoNameSpecified", func(t *testing.T) {
375
+
t.Parallel()
376
+
agent := createAgent("main")
377
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent})
378
+
379
+
result, err := getWorkspaceAgent(workspace, "")
380
+
require.NoError(t, err)
381
+
assert.Equal(t, agent.ID, result.ID)
382
+
assert.Equal(t, "main", result.Name)
383
+
})
384
+
385
+
t.Run("MultipleAgents_NoNameSpecified", func(t *testing.T) {
386
+
t.Parallel()
387
+
agent1 := createAgent("main1")
388
+
agent2 := createAgent("main2")
389
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
390
+
391
+
_, err := getWorkspaceAgent(workspace, "")
392
+
require.Error(t, err)
393
+
assert.Contains(t, err.Error(), "multiple agents found")
394
+
assert.Contains(t, err.Error(), "available agents: [main1 main2]")
395
+
})
396
+
397
+
t.Run("AgentNameSpecified_Found", func(t *testing.T) {
398
+
t.Parallel()
399
+
agent1 := createAgent("main1")
400
+
agent2 := createAgent("main2")
401
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
402
+
403
+
result, err := getWorkspaceAgent(workspace, "main1")
404
+
require.NoError(t, err)
405
+
assert.Equal(t, agent1.ID, result.ID)
406
+
assert.Equal(t, "main1", result.Name)
407
+
})
408
+
409
+
t.Run("AgentNameSpecified_NotFound", func(t *testing.T) {
410
+
t.Parallel()
411
+
agent1 := createAgent("main1")
412
+
agent2 := createAgent("main2")
413
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent1, agent2})
414
+
415
+
_, err := getWorkspaceAgent(workspace, "nonexistent")
416
+
require.Error(t, err)
417
+
assert.Contains(t, err.Error(), `agent not found by name "nonexistent"`)
418
+
assert.Contains(t, err.Error(), "available agents: [main1 main2]")
419
+
})
420
+
421
+
t.Run("NoAgents", func(t *testing.T) {
422
+
t.Parallel()
423
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{})
424
+
425
+
_, err := getWorkspaceAgent(workspace, "")
426
+
require.Error(t, err)
427
+
assert.Contains(t, err.Error(), `workspace "test-workspace" has no agents`)
428
+
})
429
+
430
+
t.Run("AvailableAgentNames_SortedCorrectly", func(t *testing.T) {
431
+
t.Parallel()
432
+
// Define agents in non-alphabetical order.
433
+
agent2 := createAgent("zod")
434
+
agent1 := createAgent("clark")
435
+
agent3 := createAgent("krypton")
436
+
workspace := createWorkspaceWithAgents([]codersdk.WorkspaceAgent{agent2, agent1, agent3})
437
+
438
+
_, err := getWorkspaceAgent(workspace, "nonexistent")
439
+
require.Error(t, err)
440
+
// Available agents should be sorted alphabetically.
441
+
assert.Contains(t, err.Error(), "available agents: [clark krypton zod]")
442
+
})
443
+
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4