Files
runc/libcontainer/env_test.go
Kir Kolyshkin 321073efde runc exec -p: fix adding HOME to nil env
Before commit 7dc24868, when process.env was nil, prepareEnv
returned a flag telling HOME is not set, and it was added.

Commit 7dc24868 moved the functionality of adding HOME into
prepareEnv but did not properly handle nil case. As a result,
runc exec -p with process.json having no env set resulted in
an exec with no HOME set.

Fix this, and add unit and integration tests.

Fixes: 7dc24868 ("libct: switch to numeric UID/GID/groups")
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2026-04-29 23:15:18 -07:00

69 lines
1.2 KiB
Go

package libcontainer
import (
"os/user"
"slices"
"strconv"
"testing"
)
func TestPrepareEnv(t *testing.T) {
u, err := user.Current()
if err != nil {
t.Fatal(err)
}
home := "HOME=" + u.HomeDir
uid, err := strconv.Atoi(u.Uid)
if err != nil {
t.Fatal(err)
}
tests := []struct {
env, wantEnv []string
}{
{
env: nil,
wantEnv: []string{home},
},
{
env: []string{},
wantEnv: []string{home},
},
{
env: []string{"HOME=/whoo", "FOO=bar"},
wantEnv: []string{"HOME=/whoo", "FOO=bar"},
},
{
env: []string{"A=a", "A=b", "A=c"},
wantEnv: []string{"A=c", home},
},
{
env: []string{"TERM=vt100", "HOME=/home/one", "HOME=/home/two", "TERM=xterm", "HOME=/home/three", "FOO=bar"},
wantEnv: []string{"TERM=xterm", "HOME=/home/three", "FOO=bar"},
},
{
env: []string{"HOME=", "HOME=/foo"},
wantEnv: []string{"HOME=/foo"},
},
{
env: []string{"HOME="},
wantEnv: []string{home},
},
{
env: []string{"HOME=/foo", "HOME="},
wantEnv: []string{home},
},
}
for _, tc := range tests {
env, err := prepareEnv(tc.env, uid)
if err != nil {
t.Error(err)
continue
}
if !slices.Equal(env, tc.wantEnv) {
t.Errorf("want %v, got %v", tc.wantEnv, env)
}
}
}