@@ -2,6 +2,7 @@ package coderd_test
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
"encoding/json"
6
7
"fmt"
7
8
"maps"
@@ -1508,39 +1509,52 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
1508
1509
t .Parallel ()
1509
1510
client ,db := coderdtest .NewWithDatabase (t ,nil )
1510
1511
user := coderdtest .CreateFirstUser (t ,client )
1511
- apps := []* proto.App {
1512
- {
1513
- Slug :"code-server" ,
1514
- Command :"some-command" ,
1515
- Url :"http://localhost:3000" ,
1516
- Icon :"/code.svg" ,
1517
- },
1518
- {
1519
- Slug :"code-server-2" ,
1520
- DisplayName :"code-server-2" ,
1521
- Command :"some-command" ,
1522
- Url :"http://localhost:3000" ,
1523
- Icon :"/code.svg" ,
1524
- Healthcheck :& proto.Healthcheck {
1525
- Url :"http://localhost:3000" ,
1526
- Interval :5 ,
1527
- Threshold :6 ,
1528
- },
1529
- },
1530
- }
1512
+
1513
+ // When using WithAgent() here and setting some *proto.App instances on the agent, Do() ends up trying to insert duplicate records.
1531
1514
r := dbfake .WorkspaceBuild (t ,db , database.WorkspaceTable {
1532
1515
OrganizationID :user .OrganizationID ,
1533
1516
OwnerID :user .UserID ,
1534
- }).WithAgent (func (agents []* proto.Agent ) []* proto.Agent {
1535
- agents [0 ].Apps = apps
1536
- return agents
1537
1517
}).Do ()
1538
1518
1519
+ res := dbgen .WorkspaceResource (t ,db , database.WorkspaceResource {JobID :r .Build .JobID })
1520
+ agent := dbgen .WorkspaceAgent (t ,db , database.WorkspaceAgent {ResourceID :res .ID })
1521
+
1522
+ // It's simpler to call db.InsertWorkspaceApp directly than dbgen.WorkspaceApp because it's more terse and direct;
1523
+ // the latter sets a bunch of defaults which make this test hard.
1524
+ _ ,err := db .InsertWorkspaceApp (dbauthz .AsSystemRestricted (t .Context ()), database.InsertWorkspaceAppParams {
1525
+ ID :uuid .New (),
1526
+ Slug :"code-server" ,
1527
+ AgentID :agent .ID ,
1528
+ Icon :"/code.svg" ,
1529
+ Command : sql.NullString {String :"some-command" ,Valid :true },
1530
+ Url : sql.NullString {String :"http://localhost:3000" ,Valid :true },
1531
+ SharingLevel :database .AppSharingLevelOwner ,
1532
+ Health :database .WorkspaceAppHealthDisabled ,
1533
+ OpenIn :database .WorkspaceAppOpenInWindow ,
1534
+ })
1535
+ require .NoError (t ,err )
1536
+ _ ,err = db .InsertWorkspaceApp (dbauthz .AsSystemRestricted (t .Context ()), database.InsertWorkspaceAppParams {
1537
+ ID :uuid .New (),
1538
+ Slug :"code-server-2" ,
1539
+ DisplayName :"code-server-2" ,
1540
+ AgentID :agent .ID ,
1541
+ Icon :"/code.svg" ,
1542
+ Command : sql.NullString {String :"some-command" ,Valid :true },
1543
+ Url : sql.NullString {String :"http://localhost:3000" ,Valid :true },
1544
+ HealthcheckInterval :5 ,
1545
+ HealthcheckUrl :"http://localhost:3000" ,
1546
+ HealthcheckThreshold :6 ,
1547
+ Health :database .WorkspaceAppHealthInitializing ,
1548
+ SharingLevel :database .AppSharingLevelOwner ,
1549
+ OpenIn :database .WorkspaceAppOpenInWindow ,
1550
+ })
1551
+ require .NoError (t ,err )
1552
+
1539
1553
ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
1540
1554
defer cancel ()
1541
1555
1542
1556
agentClient := agentsdk .New (client .URL )
1543
- agentClient .SetSessionToken (r . AgentToken )
1557
+ agentClient .SetSessionToken (agent . AuthToken . String () )
1544
1558
conn ,err := agentClient .ConnectRPC (ctx )
1545
1559
require .NoError (t ,err )
1546
1560
defer func () {