mirror of
https://github.com/opencontainers/runc.git
synced 2025-10-05 15:37:02 +08:00
Add cgroup manage for mocking of package resources
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:

committed by
Victor Marmol

parent
926ab56ea8
commit
ccb83a1e83
41
cgroups.go
Normal file
41
cgroups.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package libcontainer
|
||||
|
||||
import (
|
||||
"github.com/docker/libcontainer/cgroups"
|
||||
"github.com/docker/libcontainer/cgroups/fs"
|
||||
"github.com/docker/libcontainer/cgroups/systemd"
|
||||
)
|
||||
|
||||
type CgroupManager interface {
|
||||
GetPids(*cgroups.Cgroup) ([]int, error)
|
||||
GetStats(*cgroups.Cgroup) (*cgroups.Stats, error)
|
||||
}
|
||||
|
||||
func newCgroupsManager() CgroupManager {
|
||||
if systemd.UseSystemd() {
|
||||
return &systemdCgroupManager{}
|
||||
}
|
||||
return &fsCgroupsManager{}
|
||||
}
|
||||
|
||||
type systemdCgroupManager struct {
|
||||
}
|
||||
|
||||
func (m *systemdCgroupManager) GetPids(config *cgroups.Cgroup) ([]int, error) {
|
||||
return systemd.GetPids(config)
|
||||
}
|
||||
|
||||
func (m *systemdCgroupManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) {
|
||||
return systemd.GetStats(config)
|
||||
}
|
||||
|
||||
type fsCgroupsManager struct {
|
||||
}
|
||||
|
||||
func (m *fsCgroupsManager) GetPids(config *cgroups.Cgroup) ([]int, error) {
|
||||
return fs.GetPids(config)
|
||||
}
|
||||
|
||||
func (m *fsCgroupsManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) {
|
||||
return fs.GetStats(config)
|
||||
}
|
@@ -12,7 +12,7 @@ type ContainerInfo interface {
|
||||
// errors:
|
||||
// ContainerDestroyed - Container no longer exists,
|
||||
// Systemerror - System error.
|
||||
RunState() (*RunState, error)
|
||||
RunState() (RunState, error)
|
||||
|
||||
// Returns the current config of the container.
|
||||
Config() *Config
|
||||
|
@@ -2,17 +2,14 @@
|
||||
|
||||
package libcontainer
|
||||
|
||||
import (
|
||||
"github.com/docker/libcontainer/cgroups/fs"
|
||||
"github.com/docker/libcontainer/cgroups/systemd"
|
||||
"github.com/docker/libcontainer/network"
|
||||
)
|
||||
import "github.com/docker/libcontainer/network"
|
||||
|
||||
type linuxContainer struct {
|
||||
id string
|
||||
root string
|
||||
config *Config
|
||||
state *State
|
||||
cgroupManager CgroupManager
|
||||
}
|
||||
|
||||
func (c *linuxContainer) ID() string {
|
||||
@@ -23,21 +20,12 @@ func (c *linuxContainer) Config() *Config {
|
||||
return c.config
|
||||
}
|
||||
|
||||
func (c *linuxContainer) RunState() (*RunState, error) {
|
||||
func (c *linuxContainer) RunState() (RunState, error) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (c *linuxContainer) Processes() ([]int, error) {
|
||||
var (
|
||||
err error
|
||||
pids []int
|
||||
)
|
||||
|
||||
if systemd.UseSystemd() {
|
||||
pids, err = systemd.GetPids(c.config.Cgroups)
|
||||
} else {
|
||||
pids, err = fs.GetPids(c.config.Cgroups)
|
||||
}
|
||||
pids, err := c.cgroupManager.GetPids(c.config.Cgroups)
|
||||
if err != nil {
|
||||
return nil, newGenericError(err, SystemError)
|
||||
}
|
||||
@@ -50,15 +38,9 @@ func (c *linuxContainer) Stats() (*ContainerStats, error) {
|
||||
stats = &ContainerStats{}
|
||||
)
|
||||
|
||||
if systemd.UseSystemd() {
|
||||
stats.CgroupStats, err = systemd.GetStats(c.config.Cgroups)
|
||||
} else {
|
||||
stats.CgroupStats, err = fs.GetStats(c.config.Cgroups)
|
||||
}
|
||||
if err != nil {
|
||||
if stats.CgroupStats, err = c.cgroupManager.GetStats(c.config.Cgroups); err != nil {
|
||||
return stats, newGenericError(err, SystemError)
|
||||
}
|
||||
|
||||
if stats.NetworkStats, err = network.GetStats(&c.state.NetworkState); err != nil {
|
||||
return stats, newGenericError(err, SystemError)
|
||||
}
|
||||
|
68
linux_container_test.go
Normal file
68
linux_container_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// +build linux
|
||||
|
||||
package libcontainer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/libcontainer/cgroups"
|
||||
)
|
||||
|
||||
type mockCgroupManager struct {
|
||||
pids []int
|
||||
stats *cgroups.Stats
|
||||
}
|
||||
|
||||
func (m *mockCgroupManager) GetPids(config *cgroups.Cgroup) ([]int, error) {
|
||||
return m.pids, nil
|
||||
}
|
||||
|
||||
func (m *mockCgroupManager) GetStats(config *cgroups.Cgroup) (*cgroups.Stats, error) {
|
||||
return m.stats, nil
|
||||
}
|
||||
|
||||
func TestGetContainerPids(t *testing.T) {
|
||||
container := &linuxContainer{
|
||||
id: "myid",
|
||||
config: &Config{},
|
||||
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",
|
||||
config: &Config{},
|
||||
cgroupManager: &mockCgroupManager{
|
||||
pids: []int{1, 2, 3},
|
||||
stats: &cgroups.Stats{
|
||||
MemoryStats: cgroups.MemoryStats{
|
||||
Usage: 1024,
|
||||
},
|
||||
},
|
||||
},
|
||||
state: &State{},
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
@@ -51,6 +51,7 @@ func (l *linuxFactory) Load(id string) (ContainerInfo, error) {
|
||||
root: containerRoot,
|
||||
config: config,
|
||||
state: state,
|
||||
cgroupManager: newCgroupsManager(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user