mirror of
https://github.com/opencontainers/runc.git
synced 2025-10-05 23:46:57 +08:00
Pass os.Environ() as environment to process from init.
Replacement of #418 Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
@@ -360,10 +360,14 @@ func TestProcessEnv(t *testing.T) {
|
|||||||
defer container.Destroy()
|
defer container.Destroy()
|
||||||
|
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
pEnv := append(standardEnvironment, "FOO=BAR")
|
|
||||||
pconfig := libcontainer.Process{
|
pconfig := libcontainer.Process{
|
||||||
Args: []string{"sh", "-c", "env"},
|
Args: []string{"sh", "-c", "env"},
|
||||||
Env: pEnv,
|
Env: []string{
|
||||||
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||||
|
"HOSTNAME=integration",
|
||||||
|
"TERM=xterm",
|
||||||
|
"FOO=BAR",
|
||||||
|
},
|
||||||
Stdin: nil,
|
Stdin: nil,
|
||||||
Stdout: &stdout,
|
Stdout: &stdout,
|
||||||
}
|
}
|
||||||
@@ -386,7 +390,7 @@ func TestProcessEnv(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that HOME is set
|
// 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)
|
t.Fatal("Environment doesn't have HOME set: ", outputEnv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -245,3 +245,72 @@ func TestExecInTTY(t *testing.T) {
|
|||||||
t.Fatalf("unexpected running process, output %q", out)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
package libcontainer
|
package libcontainer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/docker/libcontainer/apparmor"
|
"github.com/docker/libcontainer/apparmor"
|
||||||
"github.com/docker/libcontainer/label"
|
"github.com/docker/libcontainer/label"
|
||||||
"github.com/docker/libcontainer/system"
|
"github.com/docker/libcontainer/system"
|
||||||
@@ -29,5 +31,5 @@ func (l *linuxSetnsInit) Init() error {
|
|||||||
return err
|
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,6 +3,7 @@
|
|||||||
package libcontainer
|
package libcontainer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/docker/libcontainer/apparmor"
|
"github.com/docker/libcontainer/apparmor"
|
||||||
@@ -89,5 +90,5 @@ func (l *linuxStandardInit) Init() error {
|
|||||||
if syscall.Getppid() == 1 {
|
if syscall.Getppid() == 1 {
|
||||||
return syscall.Kill(syscall.Getpid(), syscall.SIGKILL)
|
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())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user