Skip to content

Commit a1ee4d4

Browse files
authored
fix: test: TestSSH_RemoteForward wait for startup script (#10211)
1 parent e829cbf commit a1ee4d4

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

cli/ssh_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ import (
4343
"github.com/coder/coder/v2/testutil"
4444
)
4545

46+
const (
47+
startupScriptPattern = "i-am-ready"
48+
)
49+
4650
func setupWorkspaceForAgent(t *testing.T, mutate func([]*proto.Agent) []*proto.Agent) (*codersdk.Client, codersdk.Workspace, string) {
4751
t.Helper()
4852
if mutate == nil {
@@ -68,6 +72,12 @@ func setupWorkspaceForAgent(t *testing.T, mutate func([]*proto.Agent) []*proto.A
6872
Auth: &proto.Agent_Token{
6973
Token: agentToken,
7074
},
75+
Scripts: []*proto.Script{
76+
{
77+
Script: fmt.Sprintf("echo '%s'", startupScriptPattern),
78+
RunOnStart: true,
79+
},
80+
},
7181
}}),
7282
}},
7383
},
@@ -393,12 +403,6 @@ func TestSSH(t *testing.T) {
393403

394404
client, workspace, agentToken := setupWorkspaceForAgent(t, nil)
395405

396-
_ = agenttest.New(t, client.URL, agentToken)
397-
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
398-
399-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
400-
defer cancel()
401-
402406
inv, root := clitest.New(t,
403407
"ssh",
404408
workspace.Name,
@@ -408,14 +412,23 @@ func TestSSH(t *testing.T) {
408412
clitest.SetupConfig(t, client, root)
409413
pty := ptytest.New(t).Attach(inv)
410414
inv.Stderr = pty.Output()
415+
416+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
417+
defer cancel()
418+
411419
cmdDone := tGo(t, func() {
412420
err := inv.WithContext(ctx).Run()
413421
assert.NoError(t, err, "ssh command failed")
414422
})
415423

416-
// Wait for the prompt or any output really to indicate the command has
417-
// started and accepting input on stdin.
418-
_ = pty.Peek(ctx, 1)
424+
// Agent is still starting
425+
pty.ExpectMatch("Waiting")
426+
427+
_ = agenttest.New(t, client.URL, agentToken)
428+
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
429+
430+
// Startup script has just finished
431+
pty.ExpectMatch(startupScriptPattern)
419432

420433
// Download the test page
421434
pty.WriteLine("curl localhost:8222")

0 commit comments

Comments
 (0)