2014-12-10 16:39:00 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecIn(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-12-10 16:39:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
config := newTemplateConfig(rootfs)
|
2015-02-17 05:09:00 +08:00
|
|
|
container, err := newContainer(config)
|
2014-12-10 16:39:00 +08:00
|
|
|
if err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatal(err)
|
2014-12-10 16:39:00 +08:00
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
defer container.Destroy()
|
2015-02-23 17:26:43 +08:00
|
|
|
|
|
|
|
// Execute a first process in the container
|
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
process := &libcontainer.Process{
|
2015-02-23 17:26:43 +08:00
|
|
|
Args: []string{"cat"},
|
|
|
|
Env: standardEnvironment,
|
|
|
|
Stdin: stdinR,
|
2015-02-17 05:09:00 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(process)
|
|
|
|
stdinR.Close()
|
|
|
|
defer stdinW.Close()
|
2015-02-17 05:09:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
|
|
|
|
buffers := newStdBuffers()
|
|
|
|
ps := &libcontainer.Process{
|
2015-02-17 05:09:00 +08:00
|
|
|
Args: []string{"ps"},
|
|
|
|
Env: standardEnvironment,
|
|
|
|
Stdin: buffers.Stdin,
|
|
|
|
Stdout: buffers.Stdout,
|
|
|
|
Stderr: buffers.Stderr,
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(ps)
|
2015-02-17 05:09:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := ps.Wait(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
stdinW.Close()
|
|
|
|
if _, err := process.Wait(); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Log(err)
|
2014-12-10 16:39:00 +08:00
|
|
|
}
|
|
|
|
out := buffers.Stdout.String()
|
2015-02-23 17:26:43 +08:00
|
|
|
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
|
2014-12-10 16:39:00 +08:00
|
|
|
t.Fatalf("unexpected running process, output %q", out)
|
|
|
|
}
|
|
|
|
}
|
2014-12-10 17:13:31 +08:00
|
|
|
|
|
|
|
func TestExecInRlimit(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-12-10 17:13:31 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
config := newTemplateConfig(rootfs)
|
2015-02-17 05:09:00 +08:00
|
|
|
container, err := newContainer(config)
|
2014-12-10 17:13:31 +08:00
|
|
|
if err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Fatal(err)
|
2014-12-10 17:13:31 +08:00
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
defer container.Destroy()
|
2015-02-23 17:26:43 +08:00
|
|
|
|
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-17 05:09:00 +08:00
|
|
|
process := &libcontainer.Process{
|
2015-02-23 17:26:43 +08:00
|
|
|
Args: []string{"cat"},
|
|
|
|
Env: standardEnvironment,
|
|
|
|
Stdin: stdinR,
|
2015-02-17 05:09:00 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(process)
|
|
|
|
stdinR.Close()
|
|
|
|
defer stdinW.Close()
|
2015-02-17 05:09:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
|
|
|
|
buffers := newStdBuffers()
|
|
|
|
ps := &libcontainer.Process{
|
2015-02-17 05:09:00 +08:00
|
|
|
Args: []string{"/bin/sh", "-c", "ulimit -n"},
|
|
|
|
Env: standardEnvironment,
|
|
|
|
Stdin: buffers.Stdin,
|
|
|
|
Stdout: buffers.Stdout,
|
|
|
|
Stderr: buffers.Stderr,
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(ps)
|
2015-02-17 05:09:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := ps.Wait(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
stdinW.Close()
|
|
|
|
if _, err := process.Wait(); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
t.Log(err)
|
2014-12-10 17:13:31 +08:00
|
|
|
}
|
|
|
|
out := buffers.Stdout.String()
|
|
|
|
if limit := strings.TrimSpace(out); limit != "1024" {
|
|
|
|
t.Fatalf("expected rlimit to be 1024, got %s", limit)
|
|
|
|
}
|
|
|
|
}
|