2014-10-23 07:53:28 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
2015-02-12 06:45:07 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2014-10-23 07:53:28 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2014-10-23 07:53:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockCgroupManager struct {
|
|
|
|
pids []int
|
|
|
|
stats *cgroups.Stats
|
2015-02-12 06:45:07 +08:00
|
|
|
paths map[string]string
|
2014-10-23 07:53:28 +08:00
|
|
|
}
|
|
|
|
|
2014-12-06 09:02:49 +08:00
|
|
|
func (m *mockCgroupManager) GetPids() ([]int, error) {
|
2014-10-23 07:53:28 +08:00
|
|
|
return m.pids, nil
|
|
|
|
}
|
|
|
|
|
2014-12-06 09:02:49 +08:00
|
|
|
func (m *mockCgroupManager) GetStats() (*cgroups.Stats, error) {
|
2014-10-23 07:53:28 +08:00
|
|
|
return m.stats, nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *mockCgroupManager) Apply(pid int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (m *mockCgroupManager) Set(container *configs.Config) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:23:42 +08:00
|
|
|
func (m *mockCgroupManager) Destroy() error {
|
2015-01-13 05:54:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCgroupManager) GetPaths() map[string]string {
|
2015-02-12 06:45:07 +08:00
|
|
|
return m.paths
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-03 20:27:21 +08:00
|
|
|
func (m *mockCgroupManager) Freeze(state configs.FreezerState) error {
|
2015-01-13 19:52:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 06:45:07 +08:00
|
|
|
type mockProcess struct {
|
|
|
|
_pid int
|
|
|
|
started string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) terminate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) pid() int {
|
|
|
|
return m._pid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) startTime() (string, error) {
|
|
|
|
return m.started, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) wait() (*os.ProcessState, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockProcess) signal(_ os.Signal) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:53:28 +08:00
|
|
|
func TestGetContainerPids(t *testing.T) {
|
|
|
|
container := &linuxContainer{
|
|
|
|
id: "myid",
|
2014-12-17 17:12:23 +08:00
|
|
|
config: &configs.Config{},
|
2014-10-23 07:53:28 +08:00
|
|
|
cgroupManager: &mockCgroupManager{pids: []int{1, 2, 3}},
|
|
|
|
}
|
|
|
|
pids, err := container.Processes()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, expected := range []int{1, 2, 3} {
|
|
|
|
if pids[i] != expected {
|
|
|
|
t.Fatalf("expected pid %d but received %d", expected, pids[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerStats(t *testing.T) {
|
|
|
|
container := &linuxContainer{
|
|
|
|
id: "myid",
|
2014-12-17 17:12:23 +08:00
|
|
|
config: &configs.Config{},
|
2014-10-23 07:53:28 +08:00
|
|
|
cgroupManager: &mockCgroupManager{
|
|
|
|
pids: []int{1, 2, 3},
|
|
|
|
stats: &cgroups.Stats{
|
|
|
|
MemoryStats: cgroups.MemoryStats{
|
|
|
|
Usage: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
stats, err := container.Stats()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if stats.CgroupStats == nil {
|
|
|
|
t.Fatal("cgroup stats are nil")
|
|
|
|
}
|
|
|
|
if stats.CgroupStats.MemoryStats.Usage != 1024 {
|
|
|
|
t.Fatalf("expected memory usage 1024 but recevied %d", stats.CgroupStats.MemoryStats.Usage)
|
|
|
|
}
|
|
|
|
}
|
2015-02-12 06:45:07 +08:00
|
|
|
|
|
|
|
func TestGetContainerState(t *testing.T) {
|
|
|
|
var (
|
|
|
|
pid = os.Getpid()
|
|
|
|
expectedMemoryPath = "/sys/fs/cgroup/memory/myid"
|
|
|
|
expectedNetworkPath = "/networks/fd"
|
|
|
|
)
|
|
|
|
container := &linuxContainer{
|
|
|
|
id: "myid",
|
|
|
|
config: &configs.Config{
|
2015-03-06 02:16:35 +08:00
|
|
|
Namespaces: []configs.Namespace{
|
2015-02-12 06:45:07 +08:00
|
|
|
{Type: configs.NEWPID},
|
|
|
|
{Type: configs.NEWNS},
|
|
|
|
{Type: configs.NEWNET, Path: expectedNetworkPath},
|
|
|
|
{Type: configs.NEWUTS},
|
2015-04-08 05:16:29 +08:00
|
|
|
// emulate host for IPC
|
|
|
|
//{Type: configs.NEWIPC},
|
2015-02-12 06:45:07 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
initProcess: &mockProcess{
|
|
|
|
_pid: pid,
|
|
|
|
started: "010",
|
|
|
|
},
|
|
|
|
cgroupManager: &mockCgroupManager{
|
|
|
|
pids: []int{1, 2, 3},
|
|
|
|
stats: &cgroups.Stats{
|
|
|
|
MemoryStats: cgroups.MemoryStats{
|
|
|
|
Usage: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
paths: map[string]string{
|
|
|
|
"memory": expectedMemoryPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state, err := container.State()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if state.InitProcessPid != pid {
|
|
|
|
t.Fatalf("expected pid %d but received %d", pid, state.InitProcessPid)
|
|
|
|
}
|
|
|
|
if state.InitProcessStartTime != "010" {
|
|
|
|
t.Fatalf("expected process start time 010 but received %s", state.InitProcessStartTime)
|
|
|
|
}
|
|
|
|
paths := state.CgroupPaths
|
|
|
|
if paths == nil {
|
|
|
|
t.Fatal("cgroup paths should not be nil")
|
|
|
|
}
|
|
|
|
if memPath := paths["memory"]; memPath != expectedMemoryPath {
|
|
|
|
t.Fatalf("expected memory path %q but received %q", expectedMemoryPath, memPath)
|
|
|
|
}
|
|
|
|
for _, ns := range container.config.Namespaces {
|
2015-02-25 03:54:58 +08:00
|
|
|
path := state.NamespacePaths[ns.Type]
|
2015-02-12 06:45:07 +08:00
|
|
|
if path == "" {
|
|
|
|
t.Fatalf("expected non nil namespace path for %s", ns.Type)
|
|
|
|
}
|
|
|
|
if ns.Type == configs.NEWNET {
|
|
|
|
if path != expectedNetworkPath {
|
|
|
|
t.Fatalf("expected path %q but received %q", expectedNetworkPath, path)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file := ""
|
|
|
|
switch ns.Type {
|
|
|
|
case configs.NEWNET:
|
|
|
|
file = "net"
|
|
|
|
case configs.NEWNS:
|
|
|
|
file = "mnt"
|
|
|
|
case configs.NEWPID:
|
|
|
|
file = "pid"
|
|
|
|
case configs.NEWIPC:
|
|
|
|
file = "ipc"
|
|
|
|
case configs.NEWUSER:
|
|
|
|
file = "user"
|
|
|
|
case configs.NEWUTS:
|
|
|
|
file = "uts"
|
|
|
|
}
|
|
|
|
expected := fmt.Sprintf("/proc/%d/ns/%s", pid, file)
|
|
|
|
if expected != path {
|
|
|
|
t.Fatalf("expected path %q but received %q", expected, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|