Skip to content

Commit c9f8ee0

Browse files
committed
libct/int: improve TestExecInEnvironment
This is a slight refactor of TestExecInEnvironment, making it more strict wrt checking the exec output. 1. Explain why DEBUG is added twice to the env. 2. Reuse the execEnv for the check. 3. Make the check more strict -- instead of looking for substrings, check line by line. 4. Add a check for extra environment variables. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 8dfbf10 commit c9f8ee0

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

libcontainer/integration/execin_test.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"slices"
89
"strconv"
910
"strings"
1011
"testing"
@@ -345,16 +346,20 @@ func TestExecInEnvironment(t *testing.T) {
345346
defer stdinW.Close() //nolint: errcheck
346347
ok(t, err)
347348

349+
execEnv := []string{
350+
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
351+
// The below line is added to check the deduplication feature:
352+
// if a variable with the same name appears more than once,
353+
// only the last value should be added to the environment.
354+
"DEBUG=true",
355+
"DEBUG=false",
356+
"ENV=test",
357+
}
348358
buffers := newStdBuffers()
349359
process2 := &libcontainer.Process{
350-
Cwd: "/",
351-
Args: []string{"env"},
352-
Env: []string{
353-
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
354-
"DEBUG=true",
355-
"DEBUG=false",
356-
"ENV=test",
357-
},
360+
Cwd: "/",
361+
Args: []string{"/bin/env"},
362+
Env: execEnv,
358363
Stdin: buffers.Stdin,
359364
Stdout: buffers.Stdout,
360365
Stderr: buffers.Stderr,
@@ -366,14 +371,24 @@ func TestExecInEnvironment(t *testing.T) {
366371
_ = stdinW.Close()
367372
waitProcess(process, t)
368373

369-
out := buffers.Stdout.String()
370-
// check execin's process environment
371-
if !strings.Contains(out, "DEBUG=false") ||
372-
!strings.Contains(out, "ENV=test") ||
373-
!strings.Contains(out, "HOME=/root") ||
374-
!strings.Contains(out, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") ||
375-
strings.Contains(out, "DEBUG=true") {
376-
t.Fatalf("unexpected running process, output %q", out)
374+
// Check exec process environment.
375+
t.Logf("exec output:\n%s", buffers.Stdout.String())
376+
out := strings.Fields(buffers.Stdout.String())
377+
// If not present in the Process.Env, runc should add $HOME,
378+
// which is deduced by parsing container's /etc/passwd.
379+
// We use a known image in the test, so we know its value.
380+
for _, e := range append(execEnv, "HOME=/root") {
381+
if e == "DEBUG=true" { // This one should be dedup'ed out.
382+
continue
383+
}
384+
if !slices.Contains(out, e) {
385+
t.Errorf("Expected env %s not found in exec output", e)
386+
}
387+
}
388+
// Check that there are no extra variables. We have 1 env ("DEBUG=true")
389+
// removed and 1 env ("HOME=root") added, resulting in the same number.
390+
if got, want := len(out), len(execEnv); want != got {
391+
t.Errorf("Mismatched number of variables: want %d, got %d", want, got)
377392
}
378393
}
379394

0 commit comments

Comments
 (0)