Skip to content

Commit 196c8a9

Browse files
committed
fixup some test stuff
1 parent 00c9cd7 commit 196c8a9

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

agent/agentexec/cli.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func CLI(args []string, environ []string) error {
2424
return xerrors.Errorf("agent-exec is only supported on Linux")
2525
}
2626

27+
if len(args) < 2 {
28+
return xerrors.Errorf("malformed command %q", args)
29+
}
30+
31+
args = args[2:]
32+
2733
pid := os.Getpid()
2834

2935
oomScore, ok := envVal(environ, EnvProcOOMScore)

agent/agentexec/cli_test.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9+
"path/filepath"
910
"strconv"
1011
"strings"
1112
"syscall"
1213
"testing"
14+
"time"
1315

1416
"github.com/stretchr/testify/require"
1517
"golang.org/x/sys/unix"
@@ -23,13 +25,14 @@ func TestCLI(t *testing.T) {
2325
t.Parallel()
2426

2527
ctx := testutil.Context(t, testutil.WaitMedium)
26-
cmd, dir := cmd(t, ctx)
28+
cmd, path := cmd(ctx, t)
2729
cmd.Env = append(cmd.Env, "CODER_PROC_NICE_SCORE=10")
2830
cmd.Env = append(cmd.Env, "CODER_PROC_OOM_SCORE=123")
2931
err := cmd.Start()
3032
require.NoError(t, err)
33+
go cmd.Wait()
3134

32-
waitForSentinel(t, ctx, cmd, dir)
35+
waitForSentinel(t, ctx, cmd, path)
3336
requireOOMScore(t, cmd.Process.Pid, 123)
3437
requireNiceScore(t, cmd.Process.Pid, 10)
3538
})
@@ -52,27 +55,40 @@ func requireOOMScore(t *testing.T, pid int, expected int) {
5255
require.Equal(t, strconv.Itoa(expected), score)
5356
}
5457

55-
func waitForSentinel(t *testing.T, ctx context.Context, cmd *exec.Cmd, dir string) {
58+
func waitForSentinel(t *testing.T, ctx context.Context, cmd *exec.Cmd, path string) {
5659
t.Helper()
5760

58-
require.Eventually(t, func() bool {
59-
// Check if the process is still running.
61+
ticker := time.NewTicker(testutil.IntervalFast)
62+
defer ticker.Stop()
63+
64+
// RequireEventually doesn't work well with require.NoError or similar require functions.
65+
for {
6066
err := cmd.Process.Signal(syscall.Signal(0))
6167
require.NoError(t, err)
6268

63-
_, err = os.Stat(dir)
64-
return err == nil && ctx.Err() == nil
65-
}, testutil.WaitLong, testutil.IntervalFast)
69+
_, err = os.Stat(path)
70+
if err == nil {
71+
return
72+
}
73+
74+
select {
75+
case <-ticker.C:
76+
case <-ctx.Done():
77+
return
78+
}
79+
}
6680
}
6781

68-
func cmd(t *testing.T, ctx context.Context, args ...string) (*exec.Cmd, string) {
69-
dir := ""
82+
func cmd(ctx context.Context, t *testing.T, args ...string) (*exec.Cmd, string) {
83+
file := ""
7084
cmd := exec.Command(TestBin, args...)
7185
if len(args) == 0 {
72-
dir = t.TempDir()
86+
dir := t.TempDir()
87+
file = filepath.Join(dir, uniqueFile(t))
7388
//nolint:gosec
74-
cmd = exec.CommandContext(ctx, TestBin, "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", dir))
89+
cmd = exec.CommandContext(ctx, TestBin, "agent-exec", "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", file))
7590
}
91+
cmd.Env = os.Environ()
7692
var buf bytes.Buffer
7793
cmd.Stdout = &buf
7894
cmd.Stderr = &buf
@@ -86,5 +102,9 @@ func cmd(t *testing.T, ctx context.Context, args ...string) (*exec.Cmd, string)
86102
_ = cmd.Process.Kill()
87103
}
88104
})
89-
return cmd, dir
105+
return cmd, file
106+
}
107+
108+
func uniqueFile(t *testing.T) string {
109+
return fmt.Sprintf("%s-%d", strings.ReplaceAll(t.Name(), "/", "_"), time.Now().UnixNano())
90110
}

0 commit comments

Comments
 (0)