Skip to content

Commit

Permalink
Pass os.Environ() as environment to process from init.
Browse files Browse the repository at this point in the history
Replacement of #418

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
  • Loading branch information
LK4D4 committed Mar 5, 2015
1 parent 88989e6 commit 9744d72
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
12 changes: 8 additions & 4 deletions integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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)
}
}
Expand Down
69 changes: 69 additions & 0 deletions integration/execin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion setns_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package libcontainer

import (
"os"

"github.com/docker/libcontainer/apparmor"
"github.com/docker/libcontainer/label"
"github.com/docker/libcontainer/system"
Expand All @@ -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())
}
3 changes: 2 additions & 1 deletion standard_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package libcontainer

import (
"os"
"syscall"

"github.com/docker/libcontainer/apparmor"
Expand Down Expand Up @@ -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())
}

0 comments on commit 9744d72

Please sign in to comment.