Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: fix agent tests on Windows 11 #17631

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 1 commit into from
May 16, 2025
Merged

Conversation

spikecurtis
Copy link
Contributor

@spikecurtis spikecurtis commented May 1, 2025

Fixes a couple agent tests so that they work correctly on Windows.

HOME is not a standard Windows environment variable, and we don't have any specific Code in Coder to set it on SSH, so I've removed the test case. Amazingly/bizarrely the Windows test runners set this variable, but this is not standard Windows behavior so we shouldn't be including it in our tests.

Also the command true is not valid on a default Windows install.

true: The term 'true' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I'm not really sure how the CI runners are allowing this test to pass, but again, it's not standard so we shouldn't be doing it.

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@spikecurtis spikecurtis force-pushed the spike/windows-agent-test-fixes branch from 33090d2 to 825b326 Compare May 1, 2025 10:16
@spikecurtis spikecurtis requested a review from mafredri May 1, 2025 10:20
@spikecurtis spikecurtis marked this pull request as ready for review May 1, 2025 10:20
{
key: "HOME",
want: u.HomeDir,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we set HOME here instead?

cmd.Env = append(cmd.Env, fmt.Sprintf("LOGNAME=%s", username))

This is what OpenSSH does as well: https://github.com/openssh/openssh-portable/blob/7cc8e150d51a4545b86d996692b541419b35d1a3/session.c#L991

Almost added it last time I was excavating here but I opted on the side of caution. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but then we'd be adding functionality AFAICT no one has asked for. I'd much prefer to reduce code than increase it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to push back on this. We're not adding functionality, we're fixing a bug. If you try out SSH server on Windows, the HOME environment variable will be set when you SSH in. As such we're just matching expectations and bringing our product in line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stirby punting to Product to make the call here. WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my understanding here that $HOME on windows is taboo. However, OpenSSH sets this, so we're either going to match the expectations of Windows administrators or OpenSSH users?

How does this fix a bug @mafredri? It sounds like adding functionality to satisfy this test.

It doesn't sound super compelling to keep this. I have no customer data suggesting one way or another.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would opt to cut it.

Copy link
Member

@mafredri mafredri May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stirby I can't say I'm deep enough in the know to know whether or not it's taboo, but if you're running SSH on Windows, you're doing so via OpenSSH server (Windows component), which means that HOME will be set. So whether or not the client is using OpenSSH or PuTTY or what have you, HOME will be set. I was suggesting we would match this behavior as I consider its absence a bug. The OpenSSH implementation has always been our guiding light for our own SSH implementation, but if you see no value in it then let's drop it.

Copy link
Member

@mafredri mafredri May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just add that this is a very simple fix IMO, basically a one-line change (+ one relocation). Not sure why this would be classified as a feature.

diff --git agent/agentssh/agentssh.go agent/agentssh/agentssh.go
index 293dd4db1..eeb6d406c 100644
--- agent/agentssh/agentssh.go
+++ agent/agentssh/agentssh.go
@@ -895,21 +895,23 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
 	cmd := s.Execer.PTYCommandContext(ctx, modifiedName, modifiedArgs...)
 	cmd.Dir = s.config.WorkingDirectory()
 
+	homedir, err := ei.HomeDir()
+	if err != nil {
+		return nil, xerrors.Errorf("get home dir: %w", err)
+	}
+
 	// If the metadata directory doesn't exist, we run the command
 	// in the users home directory.
 	_, err = os.Stat(cmd.Dir)
 	if cmd.Dir == "" || err != nil {
 		// Default to user home if a directory is not set.
-		homedir, err := ei.HomeDir()
-		if err != nil {
-			return nil, xerrors.Errorf("get home dir: %w", err)
-		}
 		cmd.Dir = homedir
 	}
 	cmd.Env = append(ei.Environ(), env...)
 	// Set login variables (see `man login`).
 	cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", username))
 	cmd.Env = append(cmd.Env, fmt.Sprintf("LOGNAME=%s", username))
+	cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", homedir))
 	cmd.Env = append(cmd.Env, fmt.Sprintf("SHELL=%s", shell))
 
 	// Set SSH connection environment variables (these are also set by OpenSSH

(TBH I'd like to see this change irrespective of Windows implementation, just so we're guaranteed to set the env correctly and not be influenced by the outside environment.)

But if we don't do this, I'd at least like to see this change reverted so that we keep the HOME test (it must be set correctly on Linux/macOS), add an exception for Windows and an accompanying reason why where diverging from the OpenSSH implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we don't do this, I'd at least like to see this change reverted so that we keep the HOME test (it must be set correctly on Linux/macOS), add an exception for Windows and an accompanying reason why where diverging from the OpenSSH implementation.

The Coder agent doesn't mess with the HOME environment variable on any platform. If it's set, then the logged in user will have it too, if not, then not. If we did what you are suggesting we'd be testing the environment and not the agent, which is not the point of these tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Coder agent doesn't mess with the HOME environment variable on any platform

Yes @spikecurtis, that's exactly the bug right there. We don't, but we should. Unless HOME is set, certain software may not work or behave correctly. So far we seem to have been lucky that no customer environment initializes without HOME or a HOME set to the wrong path, or perhaps customers have figured out how to fix it on their own. I do not want us to continue relying on luck and undefined behavior.

See man login.

@@ -1502,7 +1498,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "true",
Script: "echo foo",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

@spikecurtis spikecurtis requested review from mafredri and stirby May 12, 2025 05:29
{
key: "HOME",
want: u.HomeDir,
},
Copy link
Member

@mafredri mafredri May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just add that this is a very simple fix IMO, basically a one-line change (+ one relocation). Not sure why this would be classified as a feature.

diff --git agent/agentssh/agentssh.go agent/agentssh/agentssh.go
index 293dd4db1..eeb6d406c 100644
--- agent/agentssh/agentssh.go
+++ agent/agentssh/agentssh.go
@@ -895,21 +895,23 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
 	cmd := s.Execer.PTYCommandContext(ctx, modifiedName, modifiedArgs...)
 	cmd.Dir = s.config.WorkingDirectory()
 
+	homedir, err := ei.HomeDir()
+	if err != nil {
+		return nil, xerrors.Errorf("get home dir: %w", err)
+	}
+
 	// If the metadata directory doesn't exist, we run the command
 	// in the users home directory.
 	_, err = os.Stat(cmd.Dir)
 	if cmd.Dir == "" || err != nil {
 		// Default to user home if a directory is not set.
-		homedir, err := ei.HomeDir()
-		if err != nil {
-			return nil, xerrors.Errorf("get home dir: %w", err)
-		}
 		cmd.Dir = homedir
 	}
 	cmd.Env = append(ei.Environ(), env...)
 	// Set login variables (see `man login`).
 	cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", username))
 	cmd.Env = append(cmd.Env, fmt.Sprintf("LOGNAME=%s", username))
+	cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", homedir))
 	cmd.Env = append(cmd.Env, fmt.Sprintf("SHELL=%s", shell))
 
 	// Set SSH connection environment variables (these are also set by OpenSSH

(TBH I'd like to see this change irrespective of Windows implementation, just so we're guaranteed to set the env correctly and not be influenced by the outside environment.)

But if we don't do this, I'd at least like to see this change reverted so that we keep the HOME test (it must be set correctly on Linux/macOS), add an exception for Windows and an accompanying reason why where diverging from the OpenSSH implementation.

@spikecurtis spikecurtis merged commit 90e93a2 into main May 16, 2025
34 checks passed
@spikecurtis spikecurtis deleted the spike/windows-agent-test-fixes branch May 16, 2025 03:50
@github-actions github-actions bot locked and limited conversation to collaborators May 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants