From 9744d72c740dd8cdfbb8cb4c58fb235355e0a0b4 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Thu, 5 Mar 2015 14:33:13 -0800 Subject: [PATCH] Pass os.Environ() as environment to process from init. Replacement of #418 Signed-off-by: Alexander Morozov --- integration/exec_test.go | 12 ++++--- integration/execin_test.go | 69 ++++++++++++++++++++++++++++++++++++++ setns_init_linux.go | 4 ++- standard_init_linux.go | 3 +- 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/integration/exec_test.go b/integration/exec_test.go index d507789d..b68cb739 100644 --- a/integration/exec_test.go +++ b/integration/exec_test.go @@ -360,10 +360,14 @@ func TestProcessEnv(t *testing.T) { defer container.Destroy() var stdout bytes.Buffer - pEnv := append(standardEnvironment, "FOO=BAR") pconfig := libcontainer.Process{ - Args: []string{"sh", "-c", "env"}, - Env: pEnv, + Args: []string{"sh", "-c", "env"}, + Env: []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "HOSTNAME=integration", + "TERM=xterm", + "FOO=BAR", + }, Stdin: nil, Stdout: &stdout, } @@ -386,7 +390,7 @@ func TestProcessEnv(t *testing.T) { } // Make sure that HOME is set - if !strings.Contains(outputEnv, "HOME=") { + if !strings.Contains(outputEnv, "HOME=/root") { t.Fatal("Environment doesn't have HOME set: ", outputEnv) } } diff --git a/integration/execin_test.go b/integration/execin_test.go index 954c3af0..252e6e41 100644 --- a/integration/execin_test.go +++ b/integration/execin_test.go @@ -245,3 +245,72 @@ func TestExecInTTY(t *testing.T) { t.Fatalf("unexpected running process, output %q", out) } } + +func TestExecInEnvironment(t *testing.T) { + if testing.Short() { + return + } + rootfs, err := newRootfs() + if err != nil { + t.Fatal(err) + } + defer remove(rootfs) + config := newTemplateConfig(rootfs) + container, err := newContainer(config) + if err != nil { + t.Fatal(err) + } + defer container.Destroy() + + // Execute a first process in the container + stdinR, stdinW, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + process := &libcontainer.Process{ + Args: []string{"cat"}, + Env: standardEnvironment, + Stdin: stdinR, + } + err = container.Start(process) + stdinR.Close() + defer stdinW.Close() + if err != nil { + t.Fatal(err) + } + + buffers := newStdBuffers() + process2 := &libcontainer.Process{ + Args: []string{"env"}, + Env: []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "DEBUG=true", + "DEBUG=false", + "ENV=test", + }, + Stdin: buffers.Stdin, + Stdout: buffers.Stdout, + Stderr: buffers.Stderr, + } + err = container.Start(process2) + if err != nil { + t.Fatal(err) + } + if _, err := process2.Wait(); err != nil { + out := buffers.Stdout.String() + t.Fatal(err, out) + } + stdinW.Close() + if _, err := process.Wait(); err != nil { + t.Log(err) + } + 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) + } +} diff --git a/setns_init_linux.go b/setns_init_linux.go index 2d91475b..f77219d2 100644 --- a/setns_init_linux.go +++ b/setns_init_linux.go @@ -3,6 +3,8 @@ package libcontainer import ( + "os" + "github.com/docker/libcontainer/apparmor" "github.com/docker/libcontainer/label" "github.com/docker/libcontainer/system" @@ -29,5 +31,5 @@ func (l *linuxSetnsInit) Init() error { return err } } - return system.Execv(l.config.Args[0], l.config.Args[0:], l.config.Env) + return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ()) } diff --git a/standard_init_linux.go b/standard_init_linux.go index db721b8d..29619d3c 100644 --- a/standard_init_linux.go +++ b/standard_init_linux.go @@ -3,6 +3,7 @@ package libcontainer import ( + "os" "syscall" "github.com/docker/libcontainer/apparmor" @@ -89,5 +90,5 @@ func (l *linuxStandardInit) Init() error { if syscall.Getppid() == 1 { return syscall.Kill(syscall.Getpid(), syscall.SIGKILL) } - return system.Execv(l.config.Args[0], l.config.Args[0:], l.config.Env) + return system.Execv(l.config.Args[0], l.config.Args[0:], os.Environ()) }