2014-10-17 08:00:59 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2014-10-29 06:00:28 +08:00
|
|
|
"os"
|
2014-10-17 08:00:59 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-12-17 08:34:46 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer"
|
2014-10-17 08:00:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecPS(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
buffers, exitCode, err := runContainer(config, "", "ps")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(buffers.Stdout.String(), "\n")
|
|
|
|
if len(lines) < 2 {
|
|
|
|
t.Fatalf("more than one process running for output %q", buffers.Stdout.String())
|
|
|
|
}
|
|
|
|
expected := `1 root ps`
|
|
|
|
actual := strings.Trim(lines[1], "\n ")
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("expected output %q but received %q", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 06:00:28 +08:00
|
|
|
|
2014-10-29 06:08:04 +08:00
|
|
|
func TestIPCPrivate(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
l, err := os.Readlink("/proc/1/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(buffers.Stdout.String(), "\n"); actual == l {
|
2015-01-08 06:19:36 +08:00
|
|
|
t.Fatalf("ipc link should be private to the container but equals host %q %q", actual, l)
|
2014-10-29 06:08:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 06:00:28 +08:00
|
|
|
func TestIPCHost(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
l, err := os.Readlink("/proc/1/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
2014-12-17 08:34:46 +08:00
|
|
|
config.Namespaces.Remove(libcontainer.NEWIPC)
|
2014-10-29 06:00:28 +08:00
|
|
|
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(buffers.Stdout.String(), "\n"); actual != l {
|
|
|
|
t.Fatalf("ipc link not equal to host link %q %q", actual, l)
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 06:08:04 +08:00
|
|
|
|
|
|
|
func TestIPCJoinPath(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
l, err := os.Readlink("/proc/1/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
2014-12-17 08:34:46 +08:00
|
|
|
config.Namespaces.Add(libcontainer.NEWIPC, "/proc/1/ns/ipc")
|
2014-10-29 06:08:04 +08:00
|
|
|
|
|
|
|
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/ipc")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(buffers.Stdout.String(), "\n"); actual != l {
|
|
|
|
t.Fatalf("ipc link not equal to host link %q %q", actual, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPCBadPath(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
2014-12-17 08:34:46 +08:00
|
|
|
config.Namespaces.Add(libcontainer.NEWIPC, "/proc/1/ns/ipcc")
|
2014-10-29 06:08:04 +08:00
|
|
|
|
|
|
|
_, _, err = runContainer(config, "", "true")
|
|
|
|
if err == nil {
|
2015-01-08 06:19:36 +08:00
|
|
|
t.Fatal("container succeeded with bad ipc path")
|
2014-10-29 06:08:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 02:16:53 +08:00
|
|
|
|
|
|
|
func TestRlimit(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
out, _, err := runContainer(config, "", "/bin/sh", "-c", "ulimit -n")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if limit := strings.TrimSpace(out.Stdout.String()); limit != "1024" {
|
|
|
|
t.Fatalf("expected rlimit to be 1024, got %s", limit)
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 06:19:36 +08:00
|
|
|
|
|
|
|
func TestPIDNSPrivate(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rootfs, err := newRootFs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
l, err := os.Readlink("/proc/1/ns/pid")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
buffers, exitCode, err := runContainer(config, "", "readlink", "/proc/self/ns/pid")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("exit code not 0. code %d stderr %q", exitCode, buffers.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual := strings.Trim(buffers.Stdout.String(), "\n"); actual == l {
|
|
|
|
t.Fatalf("pid link should be private to the container but equals host %q %q", actual, l)
|
|
|
|
}
|
|
|
|
}
|