@@ -1347,26 +1347,54 @@ func TestAPI(t *testing.T) {
13471347}
13481348return errTestTermination
13491349})
1350- <- terminated
1350+ testutil . RequireReceive ( ctx , t , terminated )
13511351
13521352t .Log ("Waiting for agent reinjection..." )
13531353
13541354// 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.
13581355gomock .InOrder (
13591356mCCLI .EXPECT ().DetectArchitecture (gomock .Any (),"test-container-id" ).Return (runtime .GOARCH ,nil ),
13601357mCCLI .EXPECT ().ExecAs (gomock .Any (),"test-container-id" ,"root" ,"mkdir" ,"-p" ,"/.coder-agent" ).Return (nil ,nil ),
13611358mCCLI .EXPECT ().Copy (gomock .Any (),"test-container-id" ,coderBin ,"/.coder-agent/coder" ).Return (nil ),
13621359mCCLI .EXPECT ().ExecAs (gomock .Any (),"test-container-id" ,"root" ,"chmod" ,"0755" ,"/.coder-agent" ,"/.coder-agent/coder" ).Return (nil ,nil ),
13631360)
13641361
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+ }
13701398
13711399// Verify that the agent was reused.
13721400require .Len (t ,fakeSAC .created ,1 )
@@ -1387,19 +1415,6 @@ func TestAPI(t *testing.T) {
13871415mCCLI .EXPECT ().ExecAs (gomock .Any (),"new-test-container-id" ,"root" ,"chmod" ,"0755" ,"/.coder-agent" ,"/.coder-agent/coder" ).Return (nil ,nil ),
13881416)
13891417
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-
14031418fakeDCCLI .readConfig .MergedConfiguration .Customizations .Coder = []agentcontainers.CoderCustomization {
14041419{
14051420DisplayApps :map [codersdk.DisplayApp ]bool {
@@ -1412,6 +1427,10 @@ func TestAPI(t *testing.T) {
14121427},
14131428}
14141429
1430+ // Terminate the running agent.
1431+ close (continueTerminate )
1432+ testutil .RequireReceive (ctx ,t ,terminated )
1433+
14151434// Simulate the agent deletion (this happens because the
14161435// devcontainer configuration changed).
14171436testutil .RequireSend (ctx ,t ,fakeSAC .deleteErrC ,nil )