From 71fc3faf6b074a0fdca1fa6a61c936178aec9235 Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Fri, 27 Feb 2015 03:56:08 -0500 Subject: [PATCH] setup setns process environment with os.Environ() instead of taking the raw config.Env, we use `os.Environ()` which works correctly because the environment has been setup properly with `newContainerInit` Signed-off-by: Daniel, Dao Quang Minh --- integration/execin_test.go | 68 ++++++++++++++++++++++++++++++++++++++ setns_init_linux.go | 4 ++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/integration/execin_test.go b/integration/execin_test.go index 3a1c53875..ace906faa 100644 --- a/integration/execin_test.go +++ b/integration/execin_test.go @@ -66,6 +66,74 @@ func TestExecIn(t *testing.T) { } } +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() + ps := &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(ps) + if err != nil { + t.Fatal(err) + } + if _, err := ps.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, "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) + } +} + func TestExecInRlimit(t *testing.T) { if testing.Short() { return diff --git a/setns_init_linux.go b/setns_init_linux.go index 2d91475bf..f77219d27 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()) }