@@ -1347,26 +1347,54 @@ func TestAPI(t *testing.T) {
1347
1347
}
1348
1348
return errTestTermination
1349
1349
})
1350
- <- terminated
1350
+ testutil . RequireReceive ( ctx , t , terminated )
1351
1351
1352
1352
t .Log ("Waiting for agent reinjection..." )
1353
1353
1354
1354
// Expect the agent to be reinjected.
1355
- mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
1356
- Containers : []codersdk.WorkspaceAgentContainer {testContainer },
1357
- }, nil ).Times (1 ) // 1 update.
1358
1355
gomock .InOrder (
1359
1356
mCCLI .EXPECT ().DetectArchitecture (gomock .Any (), "test-container-id" ).Return (runtime .GOARCH , nil ),
1360
1357
mCCLI .EXPECT ().ExecAs (gomock .Any (), "test-container-id" , "root" , "mkdir" , "-p" , "/.coder-agent" ).Return (nil , nil ),
1361
1358
mCCLI .EXPECT ().Copy (gomock .Any (), "test-container-id" , coderBin , "/.coder-agent/coder" ).Return (nil ),
1362
1359
mCCLI .EXPECT ().ExecAs (gomock .Any (), "test-container-id" , "root" , "chmod" , "0755" , "/.coder-agent" , "/.coder-agent/coder" ).Return (nil , nil ),
1363
1360
)
1364
1361
1365
- // Agent reinjection will succeed and we will not re-create the
1366
- // agent, nor re-probe pwd.
1367
- err = api .RefreshContainers (ctx )
1368
- require .NoError (t , err , "refresh containers should not fail" )
1369
- t .Logf ("Agents created: %d, deleted: %d" , len (fakeSAC .created ), len (fakeSAC .deleted ))
1362
+ // Verify that the agent has started.
1363
+ agentStarted := make (chan struct {})
1364
+ continueTerminate := make (chan struct {})
1365
+ terminated = make (chan struct {})
1366
+ testutil .RequireSend (ctx , t , fakeDCCLI .execErrC , func (_ string , args ... string ) error {
1367
+ defer close (terminated )
1368
+ if len (args ) > 0 {
1369
+ assert .Equal (t , "agent" , args [0 ])
1370
+ } else {
1371
+ assert .Fail (t , `want "agent" command argument` )
1372
+ }
1373
+ close (agentStarted )
1374
+ <- continueTerminate
1375
+ return errTestTermination
1376
+ })
1377
+
1378
+ WaitStartLoop:
1379
+ for {
1380
+ // Agent reinjection will succeed and we will not re-create the
1381
+ // agent, nor re-probe pwd.
1382
+ mCCLI .EXPECT ().List (gomock .Any ()).Return (codersdk.WorkspaceAgentListContainersResponse {
1383
+ Containers : []codersdk.WorkspaceAgentContainer {testContainer },
1384
+ }, nil ).Times (1 ) // 1 update.
1385
+ err = api .RefreshContainers (ctx )
1386
+ require .NoError (t , err , "refresh containers should not fail" )
1387
+
1388
+ t .Logf ("Agents created: %d, deleted: %d" , len (fakeSAC .created ), len (fakeSAC .deleted ))
1389
+
1390
+ select {
1391
+ case <- agentStarted :
1392
+ break WaitStartLoop
1393
+ case <- ctx .Done ():
1394
+ t .Fatal ("timeout waiting for agent to start" )
1395
+ default :
1396
+ }
1397
+ }
1370
1398
1371
1399
// Verify that the agent was reused.
1372
1400
require .Len (t , fakeSAC .created , 1 )
@@ -1387,19 +1415,6 @@ func TestAPI(t *testing.T) {
1387
1415
mCCLI .EXPECT ().ExecAs (gomock .Any (), "new-test-container-id" , "root" , "chmod" , "0755" , "/.coder-agent" , "/.coder-agent/coder" ).Return (nil , nil ),
1388
1416
)
1389
1417
1390
- // Terminate the agent and verify it can be reinjected.
1391
- terminated = make (chan struct {})
1392
- testutil .RequireSend (ctx , t , fakeDCCLI .execErrC , func (_ string , args ... string ) error {
1393
- defer close (terminated )
1394
- if len (args ) > 0 {
1395
- assert .Equal (t , "agent" , args [0 ])
1396
- } else {
1397
- assert .Fail (t , `want "agent" command argument` )
1398
- }
1399
- return errTestTermination
1400
- })
1401
- <- terminated
1402
-
1403
1418
fakeDCCLI .readConfig .MergedConfiguration .Customizations .Coder = []agentcontainers.CoderCustomization {
1404
1419
{
1405
1420
DisplayApps : map [codersdk.DisplayApp ]bool {
@@ -1412,6 +1427,10 @@ func TestAPI(t *testing.T) {
1412
1427
},
1413
1428
}
1414
1429
1430
+ // Terminate the running agent.
1431
+ close (continueTerminate )
1432
+ testutil .RequireReceive (ctx , t , terminated )
1433
+
1415
1434
// Simulate the agent deletion (this happens because the
1416
1435
// devcontainer configuration changed).
1417
1436
testutil .RequireSend (ctx , t , fakeSAC .deleteErrC , nil )
0 commit comments