2014-10-17 08:00:59 +08:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2014-12-25 23:43:05 +08:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2014-10-29 06:00:28 +08:00
|
|
|
"os"
|
2014-10-17 08:00:59 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2014-11-25 06:39:32 +08:00
|
|
|
|
2014-12-25 23:43:05 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-10-17 08:00:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecPS(t *testing.T) {
|
2015-02-04 19:21:03 +08:00
|
|
|
testExecPS(t, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUsernsExecPS(t *testing.T) {
|
|
|
|
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) {
|
|
|
|
t.Skip("userns is unsupported")
|
|
|
|
}
|
|
|
|
testExecPS(t, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testExecPS(t *testing.T, userns bool) {
|
2014-10-17 08:00:59 +08:00
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-10-17 08:00:59 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
config := newTemplateConfig(rootfs)
|
2015-02-04 19:21:03 +08:00
|
|
|
if userns {
|
|
|
|
config.UidMappings = []configs.IDMap{{0, 0, 1000}}
|
|
|
|
config.GidMappings = []configs.IDMap{{0, 0, 1000}}
|
|
|
|
config.Namespaces = append(config.Namespaces, configs.Namespace{Type: configs.NEWUSER})
|
|
|
|
}
|
|
|
|
|
2014-10-17 08:00:59 +08:00
|
|
|
buffers, exitCode, err := runContainer(config, "", "ps")
|
|
|
|
if err != nil {
|
2015-02-07 13:12:27 +08:00
|
|
|
t.Fatalf("%s: %s", buffers, err)
|
2014-10-17 08:00:59 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-10-29 06:08:04 +08:00
|
|
|
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-27 20:54:19 +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
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-10-29 06:00:28 +08:00
|
|
|
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)
|
2015-01-27 20:54:19 +08:00
|
|
|
config.Namespaces.Remove(configs.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
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-10-29 06:08:04 +08:00
|
|
|
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)
|
2015-01-27 20:54:19 +08:00
|
|
|
config.Namespaces.Add(configs.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
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-10-29 06:08:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
2015-01-27 20:54:19 +08:00
|
|
|
config.Namespaces.Add(configs.NEWIPC, "/proc/1/ns/ipcc")
|
2014-10-29 06:08:04 +08:00
|
|
|
|
|
|
|
_, _, err = runContainer(config, "", "true")
|
|
|
|
if err == nil {
|
2015-01-27 20:54:19 +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
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-11-27 02:16:53 +08:00
|
|
|
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)
|
|
|
|
}
|
2015-03-04 17:10:53 +08:00
|
|
|
if limit := strings.TrimSpace(out.Stdout.String()); limit != "1025" {
|
|
|
|
t.Fatalf("expected rlimit to be 1025, got %s", limit)
|
2014-11-27 02:16:53 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-25 06:39:32 +08:00
|
|
|
|
2014-12-25 23:43:05 +08:00
|
|
|
func newTestRoot() (string, error) {
|
|
|
|
dir, err := ioutil.TempDir("", "libcontainer")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
func waitProcess(p *libcontainer.Process, t *testing.T) {
|
2015-02-03 18:53:31 +08:00
|
|
|
status, err := p.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !status.Success() {
|
|
|
|
t.Fatal(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-25 23:43:05 +08:00
|
|
|
func TestEnter(t *testing.T) {
|
2015-01-19 22:12:00 +08:00
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
2014-12-25 23:43:05 +08:00
|
|
|
root, err := newTestRoot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2014-12-25 23:43:05 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
|
2015-02-26 01:11:39 +08:00
|
|
|
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
|
2014-12-25 23:43:05 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := factory.Create("test", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout, stdout2 bytes.Buffer
|
|
|
|
|
2015-02-03 18:53:31 +08:00
|
|
|
pconfig := libcontainer.Process{
|
2014-12-25 23:43:05 +08:00
|
|
|
Args: []string{"sh", "-c", "cat && readlink /proc/self/ns/pid"},
|
2015-02-07 11:16:11 +08:00
|
|
|
Env: standardEnvironment,
|
2014-12-25 23:43:05 +08:00
|
|
|
Stdin: stdinR,
|
|
|
|
Stdout: &stdout,
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(&pconfig)
|
2014-12-25 23:43:05 +08:00
|
|
|
stdinR.Close()
|
|
|
|
defer stdinW.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
pid, err := pconfig.Pid()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-12-25 23:43:05 +08:00
|
|
|
|
2015-03-04 09:30:36 +08:00
|
|
|
// Execute another process in the container
|
2015-01-21 17:29:53 +08:00
|
|
|
stdinR2, stdinW2, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
pconfig2 := libcontainer.Process{
|
|
|
|
Env: standardEnvironment,
|
|
|
|
}
|
|
|
|
pconfig2.Args = []string{"sh", "-c", "cat && readlink /proc/self/ns/pid"}
|
|
|
|
pconfig2.Stdin = stdinR2
|
|
|
|
pconfig2.Stdout = &stdout2
|
2014-12-25 23:43:05 +08:00
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(&pconfig2)
|
2015-01-21 17:29:53 +08:00
|
|
|
stdinR2.Close()
|
|
|
|
defer stdinW2.Close()
|
2014-12-25 23:43:05 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
pid2, err := pconfig2.Pid()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-21 17:29:53 +08:00
|
|
|
processes, err := container.Processes()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
for i := range processes {
|
|
|
|
if processes[i] == pid || processes[i] == pid2 {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n != 2 {
|
|
|
|
t.Fatal("unexpected number of processes", processes, pid, pid2)
|
|
|
|
}
|
|
|
|
|
2015-01-21 23:41:30 +08:00
|
|
|
// Wait processes
|
2015-01-21 17:29:53 +08:00
|
|
|
stdinW2.Close()
|
2015-02-23 17:26:43 +08:00
|
|
|
waitProcess(&pconfig2, t)
|
2014-12-25 23:43:05 +08:00
|
|
|
|
|
|
|
stdinW.Close()
|
2015-02-23 17:26:43 +08:00
|
|
|
waitProcess(&pconfig, t)
|
2014-12-25 23:43:05 +08:00
|
|
|
|
|
|
|
// Check that both processes live in the same pidns
|
|
|
|
pidns := string(stdout.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pidns2 := string(stdout2.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pidns != pidns2 {
|
|
|
|
t.Fatal("The second process isn't in the required pid namespace", pidns, pidns2)
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 22:12:00 +08:00
|
|
|
|
2015-03-04 09:30:36 +08:00
|
|
|
func TestProcessEnv(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
root, err := newTestRoot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
rootfs, err := newRootfs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
|
|
|
|
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := factory.Create("test", config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer container.Destroy()
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
pconfig := libcontainer.Process{
|
2015-03-06 06:33:13 +08:00
|
|
|
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",
|
|
|
|
},
|
2015-03-04 09:30:36 +08:00
|
|
|
Stdin: nil,
|
|
|
|
Stdout: &stdout,
|
|
|
|
}
|
|
|
|
err = container.Start(&pconfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for process
|
|
|
|
waitProcess(&pconfig, t)
|
|
|
|
|
|
|
|
outputEnv := string(stdout.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the environment has the key/value pair we added
|
|
|
|
if !strings.Contains(outputEnv, "FOO=BAR") {
|
|
|
|
t.Fatal("Environment doesn't have the expected FOO=BAR key/value pair: ", outputEnv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that HOME is set
|
2015-03-06 06:33:13 +08:00
|
|
|
if !strings.Contains(outputEnv, "HOME=/root") {
|
2015-03-04 09:30:36 +08:00
|
|
|
t.Fatal("Environment doesn't have HOME set: ", outputEnv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:12:00 +08:00
|
|
|
func TestFreeze(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
root, err := newTestRoot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
2015-02-04 09:44:58 +08:00
|
|
|
rootfs, err := newRootfs()
|
2015-01-19 22:12:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer remove(rootfs)
|
|
|
|
|
|
|
|
config := newTemplateConfig(rootfs)
|
|
|
|
|
2015-02-26 01:11:39 +08:00
|
|
|
factory, err := libcontainer.New(root, libcontainer.Cgroupfs)
|
2015-01-19 22:12:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := factory.Create("test", config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer container.Destroy()
|
|
|
|
|
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-03 18:53:31 +08:00
|
|
|
pconfig := libcontainer.Process{
|
2015-01-19 22:12:00 +08:00
|
|
|
Args: []string{"cat"},
|
2015-02-07 11:16:11 +08:00
|
|
|
Env: standardEnvironment,
|
2015-01-19 22:12:00 +08:00
|
|
|
Stdin: stdinR,
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
err = container.Start(&pconfig)
|
2015-01-19 22:12:00 +08:00
|
|
|
stdinR.Close()
|
|
|
|
defer stdinW.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
pid, err := pconfig.Pid()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-01-19 22:12:00 +08:00
|
|
|
process, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Pause(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-03 18:53:31 +08:00
|
|
|
state, err := container.Status()
|
2015-01-19 22:12:00 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := container.Resume(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
if state != libcontainer.Paused {
|
2015-01-21 20:24:18 +08:00
|
|
|
t.Fatal("Unexpected state: ", state)
|
|
|
|
}
|
2015-01-19 22:12:00 +08:00
|
|
|
|
|
|
|
stdinW.Close()
|
|
|
|
s, err := process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !s.Success() {
|
|
|
|
t.Fatal(s.String())
|
|
|
|
}
|
|
|
|
}
|