2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
/*
|
|
|
|
Utility for testing cgroup operations.
|
|
|
|
|
|
|
|
Creates a mock of the cgroup filesystem for the duration of the test.
|
|
|
|
*/
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-03-09 09:32:02 +08:00
|
|
|
"path/filepath"
|
2014-05-15 06:21:44 +08:00
|
|
|
"testing"
|
2015-02-27 11:00:17 +08:00
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type cgroupTestUtil struct {
|
2015-11-05 18:41:08 +08:00
|
|
|
// cgroup data to use in tests.
|
|
|
|
CgroupData *cgroupData
|
2014-05-15 06:21:44 +08:00
|
|
|
|
|
|
|
// Path to the mock cgroup directory.
|
|
|
|
CgroupPath string
|
|
|
|
|
|
|
|
// Temporary directory to store mock cgroup filesystem.
|
|
|
|
tempDir string
|
|
|
|
t *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new test util for the specified subsystem
|
|
|
|
func NewCgroupTestUtil(subsystem string, t *testing.T) *cgroupTestUtil {
|
2015-11-05 18:41:08 +08:00
|
|
|
d := &cgroupData{
|
2015-11-05 18:52:14 +08:00
|
|
|
config: &configs.Cgroup{},
|
2015-02-27 11:00:17 +08:00
|
|
|
}
|
2015-12-15 08:26:29 +08:00
|
|
|
d.config.Resources = &configs.Resources{}
|
2015-03-09 09:32:02 +08:00
|
|
|
tempDir, err := ioutil.TempDir("", "cgroup_test")
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
d.root = tempDir
|
2015-03-09 09:32:02 +08:00
|
|
|
testCgroupPath := filepath.Join(d.root, subsystem)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the full mock cgroup path exists.
|
|
|
|
err = os.MkdirAll(testCgroupPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return &cgroupTestUtil{CgroupData: d, CgroupPath: testCgroupPath, tempDir: tempDir, t: t}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cgroupTestUtil) cleanup() {
|
|
|
|
os.RemoveAll(c.tempDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the specified contents on the mock of the specified cgroup files.
|
|
|
|
func (c *cgroupTestUtil) writeFileContents(fileContents map[string]string) {
|
|
|
|
for file, contents := range fileContents {
|
|
|
|
err := writeFile(c.CgroupPath, file, contents)
|
|
|
|
if err != nil {
|
|
|
|
c.t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|