Skip to content

refactor(agent/agentssh): move envs to agent and add agentssh config struct #12204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix unhandled terminal output in test
  • Loading branch information
mafredri committed Feb 19, 2024
commit 2cbb5971129ec1f79f206ff014ae1b0f31aed087
40 changes: 33 additions & 7 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,14 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
require.NoError(t, err)

command := "sh"
echoEnv := func(t *testing.T, w io.Writer, r io.Reader, env string) string {
echoEnv := func(t *testing.T, w io.Writer, env string) {
if runtime.GOOS == "windows" {
_, err := fmt.Fprintf(w, "echo %%%s%%\r\n", env)
require.NoError(t, err)
} else {
_, err := fmt.Fprintf(w, "echo $%s\n", env)
require.NoError(t, err)
}
scanner := bufio.NewScanner(r)
require.True(t, scanner.Scan())
t.Logf("%s=%s", env, scanner.Text())
return scanner.Text()
}
if runtime.GOOS == "windows" {
command = "cmd.exe"
Expand All @@ -328,6 +324,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
err = session.Start(command)
require.NoError(t, err)

ctx := testutil.Context(t, testutil.WaitLong)

s := bufio.NewScanner(stdout)
out := make(chan string)
testutil.Go(t, func() {
for s.Scan() {
select {
case out <- s.Text():
case <-ctx.Done():
return
}
}
})

// Until we have gotten the first result, the shell may spit out some data.
first := true
//nolint:paralleltest // These tests need to run sequentially.
for k, partialV := range map[string]string{
"CODER": "true", // From the agent.
Expand All @@ -337,8 +349,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
"MY_SESSION": "true", // From the session.
} {
t.Run(k, func(t *testing.T) {
out := echoEnv(t, stdin, stdout, k)
require.Contains(t, strings.TrimSpace(out), partialV)
echoEnv(t, stdin, k)
if first {
for {
s := testutil.RequireRecvCtx(ctx, t, out)
t.Logf("%s=%s", k, s)
s = strings.TrimSpace(s)
if strings.Contains(s, partialV) {
first = false
return
}
}
}
s := testutil.RequireRecvCtx(ctx, t, out)
t.Logf("%s=%s", k, s)
s = strings.TrimSpace(s)
require.Contains(t, s, partialV)
})
}
}
Expand Down