Files
runc/libcontainer/env_test.go
Rodrigo Campos 09501d96d2 libct: Override HOME if its set to the empty string
Before commit 06f1e0765 ("libct: speedup process.Env handling") we were
overriding HOME if it was set to "" too[1]. But now we only override it
if it wasn't set at all.

This patch restores the old behavior of overriding it if it was set to
an empty value.

Docker relies on this behaviour since ages[2].

[1]: 1c50804572/libcontainer/init_linux.go (L544-L549)
[2]: 843e51459f/integration-cli/docker_cli_run_test.go (L822-L843)

Signed-off-by: Rodrigo Campos <rodrigoca@microsoft.com>
2025-04-04 15:37:22 +02:00

65 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: []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)
}
}
}