Skip to content

Commit

Permalink
libct/int: improve TestExecInEnvironment
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kolyshkin authored and lifubang committed Dec 25, 2024
1 parent 14cc41c commit 15595de
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions libcontainer/integration/execin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -345,16 +346,20 @@ func TestExecInEnvironment(t *testing.T) {
defer stdinW.Close() //nolint: errcheck
ok(t, err)

execEnv := []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
// The below line is added to check the deduplication feature:
// if a variable with the same name appears more than once,
// only the last value should be added to the environment.
"DEBUG=true",
"DEBUG=false",
"ENV=test",
}
buffers := newStdBuffers()
process2 := &libcontainer.Process{
Cwd: "/",
Args: []string{"env"},
Env: []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"DEBUG=true",
"DEBUG=false",
"ENV=test",
},
Cwd: "/",
Args: []string{"/bin/env"},
Env: execEnv,
Stdin: buffers.Stdin,
Stdout: buffers.Stdout,
Stderr: buffers.Stderr,
Expand All @@ -366,14 +371,24 @@ func TestExecInEnvironment(t *testing.T) {
_ = stdinW.Close()
waitProcess(process, t)

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

Expand Down

0 comments on commit 15595de

Please sign in to comment.