-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add agent exec pkg #15577
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
feat: add agent exec pkg #15577
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
90281bd
feat: add agentexec pkg
sreya 538c6c3
remove CLI references
sreya 00c9cd7
add basic test
sreya 196c8a9
fixup some test stuff
sreya 97e68f4
idk
sreya 712b328
more tests
sreya 5f633e1
lint
sreya 05702c3
skip nonlinux
sreya 0132fb3
build files
sreya ae30643
test file only for linux
sreya 986e18e
use flag parsing instead
sreya b158919
flag parsing
sreya 7235bfb
add comment for the use of flags
sreya 521956f
update cmdtest for other oses
sreya 471ea8d
stray deletion
sreya 5076cf0
pr changes
sreya b8725f2
overlooked changes
sreya 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
add basic test
- Loading branch information
commit 00c9cd71721842df30f4c52015b54dd11b80336f
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package agentexec_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sys/unix" | ||
|
||
"github.com/coder/coder/v2/testutil" | ||
) | ||
|
||
func TestCLI(t *testing.T) { | ||
t.Parallel() | ||
t.Run("OK", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
cmd, dir := cmd(t, ctx) | ||
cmd.Env = append(cmd.Env, "CODER_PROC_NICE_SCORE=10") | ||
cmd.Env = append(cmd.Env, "CODER_PROC_OOM_SCORE=123") | ||
err := cmd.Start() | ||
require.NoError(t, err) | ||
|
||
waitForSentinel(t, ctx, cmd, dir) | ||
requireOOMScore(t, cmd.Process.Pid, 123) | ||
requireNiceScore(t, cmd.Process.Pid, 10) | ||
}) | ||
} | ||
|
||
func requireNiceScore(t *testing.T, pid int, score int) { | ||
t.Helper() | ||
|
||
nice, err := unix.Getpriority(0, pid) | ||
Check failure on line 41 in agent/agentexec/cli_test.go
|
||
require.NoError(t, err) | ||
require.Equal(t, score, nice) | ||
} | ||
|
||
func requireOOMScore(t *testing.T, pid int, expected int) { | ||
t.Helper() | ||
|
||
actual, err := os.ReadFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid)) | ||
require.NoError(t, err) | ||
score := strings.TrimSpace(string(actual)) | ||
require.Equal(t, strconv.Itoa(expected), score) | ||
} | ||
|
||
func waitForSentinel(t *testing.T, ctx context.Context, cmd *exec.Cmd, dir string) { | ||
t.Helper() | ||
|
||
require.Eventually(t, func() bool { | ||
// Check if the process is still running. | ||
err := cmd.Process.Signal(syscall.Signal(0)) | ||
require.NoError(t, err) | ||
|
||
_, err = os.Stat(dir) | ||
return err == nil && ctx.Err() == nil | ||
}, testutil.WaitLong, testutil.IntervalFast) | ||
} | ||
|
||
func cmd(t *testing.T, ctx context.Context, args ...string) (*exec.Cmd, string) { | ||
dir := "" | ||
cmd := exec.Command(TestBin, args...) | ||
if len(args) == 0 { | ||
dir = t.TempDir() | ||
//nolint:gosec | ||
cmd = exec.CommandContext(ctx, TestBin, "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", dir)) | ||
} | ||
var buf bytes.Buffer | ||
cmd.Stdout = &buf | ||
cmd.Stderr = &buf | ||
t.Cleanup(func() { | ||
// Print output of a command if the test fails. | ||
if t.Failed() { | ||
t.Logf("cmd %q output: %s", cmd.Args, buf.String()) | ||
} | ||
|
||
if cmd.Process != nil { | ||
_ = cmd.Process.Kill() | ||
} | ||
}) | ||
return cmd, dir | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package agentexec_test | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
) | ||
|
||
const TestBin = "/tmp/agent-test" | ||
sreya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func TestMain(m *testing.M) { | ||
buildBinary() | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func buildBinary() { | ||
out, err := exec.Command("go", "build", "-o", TestBin, "./cmdtest").CombinedOutput() | ||
mustf(err, "build binary: %s", out) | ||
} | ||
|
||
func mustf(err error, msg string, args ...any) { | ||
if err != nil { | ||
panic(fmt.Sprintf(msg, args...)) | ||
} | ||
} |
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.