@@ -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
+ }
0 commit comments