@@ -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,28 @@ 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
+ err := cmd .Start ()
49
+ require .NoError (t , err )
50
+ go cmd .Wait ()
51
+ waitForSentinel (ctx , t , cmd , path )
52
+
53
+ env := procEnv (t , cmd .Process .Pid )
54
+ hasExecEnvs := slices .ContainsFunc (
55
+ env ,
56
+ func (e string ) bool {
57
+ return strings .HasPrefix (e , agentexec .EnvProcPrioMgmt ) ||
58
+ strings .HasPrefix (e , agentexec .EnvProcOOMScore ) ||
59
+ strings .HasPrefix (e , agentexec .EnvProcNiceScore )
60
+ })
61
+ require .False (t , hasExecEnvs , "expected environment variables to be filtered" )
62
+ })
63
+
40
64
t .Run ("Defaults" , func (t * testing.T ) {
41
65
ctx := testutil .Context (t , testutil .WaitMedium )
42
66
cmd , path := cmd (ctx , t , 0 , 0 )
@@ -176,6 +200,15 @@ func expectedOOMScore(t *testing.T) int {
176
200
return 998
177
201
}
178
202
203
+ // procEnv returns the environment variables for a given process.
204
+ func procEnv (t * testing.T , pid int ) []string {
205
+ t .Helper ()
206
+
207
+ env , err := os .ReadFile (fmt .Sprintf ("/proc/%d/environ" , pid ))
208
+ require .NoError (t , err )
209
+ return strings .Split (string (env ), "\x00 " )
210
+ }
211
+
179
212
func expectedNiceScore (t * testing.T ) int {
180
213
t .Helper ()
181
214
0 commit comments