Skip to content

chore: skip timing-sensistive AgentMetadata test in the standard suite #7237

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 7 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
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 windows?
  • Loading branch information
ammario committed Apr 21, 2023
commit 56a45cc1acdbfe84e14663afa15843e2df34dcf1
61 changes: 55 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"net/http"
"net/netip"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
Expand All @@ -35,6 +37,7 @@ import (

"cdr.dev/slog"
"github.com/coder/coder/agent/agentssh"
"github.com/coder/coder/agent/usershell"
"github.com/coder/coder/buildinfo"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/gitauth"
Expand Down Expand Up @@ -202,7 +205,36 @@ func (a *agent) runLoop(ctx context.Context) {
}
}

func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMetadataDescription) *codersdk.WorkspaceAgentMetadataResult {
func createMetadataCommand(ctx context.Context, script string) (*exec.Cmd, error) {
// This is largely copied from agentssh, but for some reason the command
// generated there always returns exit status 1 in Windows.
currentUser, err := user.Current()
if err != nil {
return nil, xerrors.Errorf("get current user: %w", err)
}
username := currentUser.Username

shell, err := usershell.Get(username)
if err != nil {
return nil, xerrors.Errorf("get user shell: %w", err)
}

var caller string
switch {
case filepath.Base(shell) == "pwsh.exe":
caller = "-Command"
case runtime.GOOS == "windows":
caller = "/c"
default:
caller = "-c"
}
// args := []string{caller, "Get-Process"}
args := []string{"-NoProfile", "-NonInteractive"}
_ = caller
return exec.CommandContext(ctx, "powershell", args...), nil
}

func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMetadataDescription) *codersdk.WorkspaceAgentMetadataResult {
var out bytes.Buffer
result := &codersdk.WorkspaceAgentMetadataResult{
// CollectedAt is set here for testing purposes and overrode by
Expand All @@ -213,18 +245,33 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
// if it is certain the clocks are in sync.
CollectedAt: time.Now(),
}
cmd, err := a.sshServer.CreateCommand(ctx, md.Script, nil)
cmd, err := createMetadataCommand(ctx, md.Script)
if err != nil {
result.Error = err.Error()
result.Error = fmt.Sprintf("create cmd: %+v", err)
return result
}

// execPath, err := exec.LookPath("cmd.exe")
// if err != nil {
// result.Error = fmt.Sprintf("look path: %+v", err)
// return result
// }

// cmd = exec.CommandContext(ctx, execPath, "/c", "echo hello")

cmd.Stdout = &out
cmd.Stderr = &out
cmd.Stdin = io.LimitReader(nil, 0)

// The error isn't mutually exclusive with useful output.
err = cmd.Run()
// We split up Start and Wait so that we can return a more useful error.
err = cmd.Start()
if err != nil {
result.Error = fmt.Sprintf("start cmd: %+v", err)
return result
}

// This error isn't mutually exclusive with useful output.
err = cmd.Wait()
const bufLimit = 10 << 10
if out.Len() > bufLimit {
err = errors.Join(
Expand All @@ -234,8 +281,10 @@ func (a *agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentM
out.Truncate(bufLimit)
}

fmt.Printf("ran %+v %+q: %v\n", cmd.Path, cmd.Args, err)

if err != nil {
result.Error = err.Error()
result.Error = fmt.Sprintf("run cmd: %+v", err)
}
result.Value = out.String()
return result
Expand Down
21 changes: 10 additions & 11 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,19 +976,20 @@ func TestAgent_StartupScript(t *testing.T) {
func TestAgent_Metadata(t *testing.T) {
t.Parallel()

echoHello := "echo hello"
if runtime.GOOS == "windows" {
echoHello = "echo 'hello'"
}

t.Run("Once", func(t *testing.T) {
t.Parallel()
script := "echo -n hello"
if runtime.GOOS == "windows" {
script = "powershell " + script
}
//nolint:dogsled
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Metadata: []codersdk.WorkspaceAgentMetadataDescription{
{
Key: "greeting",
Interval: 0,
Script: script,
Script: echoHello,
},
},
}, 0)
Expand All @@ -1012,17 +1013,13 @@ func TestAgent_Metadata(t *testing.T) {

t.Run("Many", func(t *testing.T) {
t.Parallel()
script := "echo -n hello"
if runtime.GOOS == "windows" {
script = "powershell " + script
}
//nolint:dogsled
_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Metadata: []codersdk.WorkspaceAgentMetadataDescription{
{
Key: "greeting",
Interval: 1,
Script: script,
Script: echoHello,
},
},
}, 0)
Expand All @@ -1034,7 +1031,9 @@ func TestAgent_Metadata(t *testing.T) {
}, testutil.WaitShort, testutil.IntervalMedium)

collectedAt1 := gotMd["greeting"].CollectedAt
assert.Equal(t, "hello", gotMd["greeting"].Value)
if !assert.Equal(t, "hello\n", gotMd["greeting"].Value) {
t.Logf("got: %+v", gotMd)
}

if !assert.Eventually(t, func() bool {
gotMd = client.getMetadata()
Expand Down