@@ -10,6 +10,7 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"path/filepath"
13
+ "slices"
13
14
"strconv"
14
15
"strings"
15
16
"syscall"
@@ -20,6 +21,7 @@ import (
20
21
"golang.org/x/sys/unix"
21
22
"golang.org/x/xerrors"
22
23
24
+ "github.com/coder/coder/v2/agent/agentexec"
23
25
"github.com/coder/coder/v2/testutil"
24
26
)
25
27
@@ -37,6 +39,32 @@ func TestCLI(t *testing.T) {
37
39
requireNiceScore (t ,cmd .Process .Pid ,12 )
38
40
})
39
41
42
+ t .Run ("FiltersEnv" ,func (t * testing.T ) {
43
+ ctx := testutil .Context (t ,testutil .WaitMedium )
44
+ cmd ,path := cmd (ctx ,t ,123 ,12 )
45
+ cmd .Env = append (cmd .Env ,fmt .Sprintf ("%s=true" ,agentexec .EnvProcPrioMgmt ))
46
+ cmd .Env = append (cmd .Env ,fmt .Sprintf ("%s=123" ,agentexec .EnvProcOOMScore ))
47
+ cmd .Env = append (cmd .Env ,fmt .Sprintf ("%s=12" ,agentexec .EnvProcNiceScore ))
48
+ // Ensure unrelated environment variables are preserved.
49
+ cmd .Env = append (cmd .Env ,"CODER_TEST_ME_AGENTEXEC=true" )
50
+ err := cmd .Start ()
51
+ require .NoError (t ,err )
52
+ go cmd .Wait ()
53
+ waitForSentinel (ctx ,t ,cmd ,path )
54
+
55
+ env := procEnv (t ,cmd .Process .Pid )
56
+ hasExecEnvs := slices .ContainsFunc (
57
+ env ,
58
+ func (e string )bool {
59
+ return strings .HasPrefix (e ,agentexec .EnvProcPrioMgmt )||
60
+ strings .HasPrefix (e ,agentexec .EnvProcOOMScore )||
61
+ strings .HasPrefix (e ,agentexec .EnvProcNiceScore )
62
+ })
63
+ require .False (t ,hasExecEnvs ,"expected environment variables to be filtered" )
64
+ userEnv := slices .Contains (env ,"CODER_TEST_ME_AGENTEXEC=true" )
65
+ require .True (t ,userEnv ,"expected user environment variables to be preserved" )
66
+ })
67
+
40
68
t .Run ("Defaults" ,func (t * testing.T ) {
41
69
ctx := testutil .Context (t ,testutil .WaitMedium )
42
70
cmd ,path := cmd (ctx ,t ,0 ,0 )
@@ -176,6 +204,15 @@ func expectedOOMScore(t *testing.T) int {
176
204
return 998
177
205
}
178
206
207
+ // procEnv returns the environment variables for a given process.
208
+ func procEnv (t * testing.T ,pid int ) []string {
209
+ t .Helper ()
210
+
211
+ env ,err := os .ReadFile (fmt .Sprintf ("/proc/%d/environ" ,pid ))
212
+ require .NoError (t ,err )
213
+ return strings .Split (string (env ),"\x00 " )
214
+ }
215
+
179
216
func expectedNiceScore (t * testing.T )int {
180
217
t .Helper ()
181
218