-
Notifications
You must be signed in to change notification settings - Fork 894
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
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
72155f0
refactor(agent/agentssh): move envs to agent and add agentssh config …
mafredri ce6af82
fix comment
mafredri 2fe0a69
fix typo
mafredri 2858a0b
fix workdir default
mafredri be9fa5a
init with empty session token
mafredri 3564e68
simplify showmotd
mafredri 112a0d0
fix x11 test
mafredri 2cbb597
fix unhandled terminal output in test
mafredri dd66b45
another attempt to fix window stest
mafredri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -281,6 +281,68 @@ func TestAgent_SessionExec(t *testing.T) { | |
require.Equal(t, "test", strings.TrimSpace(string(output))) | ||
} | ||
|
||
//nolint:tparallel // Sub tests need to run sequentially. | ||
func TestAgent_Session_EnvironmentVariables(t *testing.T) { | ||
t.Parallel() | ||
|
||
manifest := agentsdk.Manifest{ | ||
EnvironmentVariables: map[string]string{ | ||
"MY_MANIFEST": "true", | ||
"MY_OVERRIDE": "false", | ||
"MY_SESSION_MANIFEST": "false", | ||
}, | ||
} | ||
banner := codersdk.ServiceBannerConfig{} | ||
session := setupSSHSession(t, manifest, banner, nil, func(_ *agenttest.Client, opts *agent.Options) { | ||
opts.EnvironmentVariables["MY_OVERRIDE"] = "true" | ||
}) | ||
|
||
err := session.Setenv("MY_SESSION_MANIFEST", "true") | ||
require.NoError(t, err) | ||
err = session.Setenv("MY_SESSION", "true") | ||
require.NoError(t, err) | ||
|
||
command := "sh" | ||
echoEnv := func(t *testing.T, w io.Writer, r io.Reader, env string) 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" | ||
} | ||
stdin, err := session.StdinPipe() | ||
require.NoError(t, err) | ||
defer stdin.Close() | ||
stdout, err := session.StdoutPipe() | ||
require.NoError(t, err) | ||
|
||
err = session.Start(command) | ||
require.NoError(t, err) | ||
|
||
//nolint:paralleltest // These tests need to run sequentially. | ||
for k, partialV := range map[string]string{ | ||
"CODER": "true", // From the agent. | ||
"MY_MANIFEST": "true", // From the manifest. | ||
"MY_OVERRIDE": "true", // From the agent environment variables option, overrides manifest. | ||
"MY_SESSION_MANIFEST": "false", // From the manifest, overrides session env. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review: This is not necessarily correct behavior (IMO), but I wanted to stay truthful to the existing implementation. |
||
"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) | ||
}) | ||
} | ||
} | ||
|
||
func TestAgent_GitSSH(t *testing.T) { | ||
t.Parallel() | ||
session := setupSSHSession(t, agentsdk.Manifest{}, codersdk.ServiceBannerConfig{}, nil) | ||
|
@@ -1991,15 +2053,17 @@ func setupSSHSession( | |
manifest agentsdk.Manifest, | ||
serviceBanner codersdk.ServiceBannerConfig, | ||
prepareFS func(fs afero.Fs), | ||
opts ...func(*agenttest.Client, *agent.Options), | ||
) *ssh.Session { | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
//nolint:dogsled | ||
conn, _, _, fs, _ := setupAgent(t, manifest, 0, func(c *agenttest.Client, _ *agent.Options) { | ||
opts = append(opts, func(c *agenttest.Client, o *agent.Options) { | ||
c.SetServiceBannerFunc(func() (codersdk.ServiceBannerConfig, error) { | ||
return serviceBanner, nil | ||
}) | ||
}) | ||
//nolint:dogsled | ||
conn, _, _, fs, _ := setupAgent(t, manifest, 0, opts...) | ||
if prepareFS != nil { | ||
prepareFS(fs) | ||
} | ||
|
@@ -2057,6 +2121,7 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati | |
Filesystem: fs, | ||
Logger: logger.Named("agent"), | ||
ReconnectingPTYTimeout: ptyTimeout, | ||
EnvironmentVariables: map[string]string{}, | ||
} | ||
|
||
for _, opt := range opts { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.