Commit b47c319b authored by Yuto KAWAMURA(kawamuray)'s avatar Yuto KAWAMURA(kawamuray) Committed by Hrvoje Ribicic

Add unit tests for cgroup management functions of hv_lxc

This patch adds a few unit tests for cgroup management functions of
hv_lxc.
Signed-off-by: default avatarYuto KAWAMURA(kawamuray) <kawamuray.dadada@gmail.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent e9ba3aac
......@@ -218,6 +218,18 @@ DIRS = \
test/data/bdev-rbd \
test/data/ovfdata \
test/data/ovfdata/other \
test/data/cgroup_root \
test/data/cgroup_root/memory \
test/data/cgroup_root/memory/lxc \
test/data/cgroup_root/memory/lxc/instance1 \
test/data/cgroup_root/cpuset \
test/data/cgroup_root/cpuset/some_group \
test/data/cgroup_root/cpuset/some_group/lxc \
test/data/cgroup_root/cpuset/some_group/lxc/instance1 \
test/data/cgroup_root/devices \
test/data/cgroup_root/devices/some_group \
test/data/cgroup_root/devices/some_group/lxc \
test/data/cgroup_root/devices/some_group/lxc/instance1 \
test/py \
test/py/cmdlib \
test/py/cmdlib/testsupport \
......@@ -1525,6 +1537,9 @@ TEST_FILES = \
test/data/bdev-rbd/output_invalid.txt \
test/data/cert1.pem \
test/data/cert2.pem \
test/data/cgroup_root/memory/lxc/instance1/memory.limit_in_bytes \
test/data/cgroup_root/cpuset/some_group/lxc/instance1/cpuset.cpus \
test/data/cgroup_root/devices/some_group/lxc/instance1/devices.list \
test/data/cluster_config_2.7.json \
test/data/cluster_config_2.8.json \
test/data/cluster_config_2.9.json \
......@@ -1575,7 +1590,8 @@ TEST_FILES = \
test/data/ovfdata/wrong_manifest.ovf \
test/data/ovfdata/wrong_ova.ova \
test/data/ovfdata/wrong_xml.ovf \
test/data/proc_diskstats.txt \
test/data/proc_cgroup.txt \
test/data/proc_diskstats.txt \
test/data/proc_drbd8.txt \
test/data/proc_drbd80-emptyline.txt \
test/data/proc_drbd80-emptyversion.txt \
......
3:devices:/some_group
2:memory:/
1:cpuset:/some_group
......@@ -33,6 +33,7 @@ from ganeti.hypervisor import hv_lxc
from ganeti.hypervisor.hv_lxc import LXCHypervisor
import mock
import os
import shutil
import tempfile
import testutils
......@@ -101,5 +102,84 @@ class TestLXCHypervisorGetInstanceInfo(unittest.TestCase):
self.assertIsNone(self.hv.GetInstanceInfo("inst1"))
class TestCgroupMount(unittest.TestCase):
@patch_object(utils, "GetMounts")
@patch_object(LXCHypervisor, "_MountCgroupSubsystem")
def testGetOrPrepareCgroupSubsysMountPoint(self, mntcgsub_mock, getmnt_mock):
getmnt_mock.return_value = [
("/dev/foo", "/foo", "foo", "cpuset"),
("cpuset", "/sys/fs/cgroup/cpuset", "cgroup", "rw,relatime,cpuset"),
("devices", "/sys/fs/cgroup/devices", "cgroup", "rw,devices,relatime"),
("cpumem", "/sys/fs/cgroup/cpumem", "cgroup", "cpu,memory,rw,relatime"),
]
mntcgsub_mock.return_value = "/foo"
hv = LXCHypervisor()
self.assertEqual(hv._GetOrPrepareCgroupSubsysMountPoint("cpuset"),
"/sys/fs/cgroup/cpuset")
self.assertEqual(hv._GetOrPrepareCgroupSubsysMountPoint("devices"),
"/sys/fs/cgroup/devices")
self.assertEqual(hv._GetOrPrepareCgroupSubsysMountPoint("cpu"),
"/sys/fs/cgroup/cpumem")
self.assertEqual(hv._GetOrPrepareCgroupSubsysMountPoint("memory"),
"/sys/fs/cgroup/cpumem")
self.assertEqual(hv._GetOrPrepareCgroupSubsysMountPoint("freezer"),
"/foo")
mntcgsub_mock.assert_called_with("freezer")
class TestCgroupReadData(unittest.TestCase):
cgroot = os.path.abspath(testutils.TestDataFilename("cgroup_root"))
def setUp(self):
self.hv = LXCHypervisor()
@patch_object(LXCHypervisor, "_CGROUP_ROOT_DIR", cgroot)
def testGetCgroupMountPoint(self):
self.assertEqual(self.hv._GetCgroupMountPoint(), self.cgroot)
@patch_object(LXCHypervisor, "_PROC_CGROUP_FILE",
testutils.TestDataFilename("proc_cgroup.txt"))
def testGetCurrentCgroupSubsysGroups(self):
expected_groups = {
"memory": "", # root
"cpuset": "some_group",
"devices": "some_group",
}
self.assertEqual(self.hv._GetCurrentCgroupSubsysGroups(), expected_groups)
@patch_object(LXCHypervisor, "_GetOrPrepareCgroupSubsysMountPoint")
@patch_object(LXCHypervisor, "_GetCurrentCgroupSubsysGroups")
def testGetCgroupInstanceSubsysDir(self, getcgg_mock, getmp_mock):
getmp_mock.return_value = "/cg"
getcgg_mock.return_value = {"cpuset": "grp"}
self.assertEqual(self.hv._GetCgroupInstanceSubsysDir("instance1", "memory"),
"/cg/lxc/instance1")
self.assertEqual(self.hv._GetCgroupInstanceSubsysDir("instance1", "cpuset"),
"/cg/grp/lxc/instance1")
@patch_object(LXCHypervisor, "_GetCgroupInstanceSubsysDir")
def testGetCgroupInstanceValue(self, getdir_mock):
getdir_mock.return_value = utils.PathJoin(self.cgroot, "memory", "lxc",
"instance1")
self.assertEqual(self.hv._GetCgroupInstanceValue("instance1", "memory",
"memory.limit_in_bytes"),
"128")
getdir_mock.return_value = utils.PathJoin(self.cgroot, "cpuset",
"some_group", "lxc", "instance1")
self.assertEqual(self.hv._GetCgroupInstanceValue("instance1", "cpuset",
"cpuset.cpus"),
"0-1")
@patch_object(LXCHypervisor, "_GetCgroupInstanceValue")
def testGetCgroupCpuList(self, getval_mock):
getval_mock.return_value = "0-1"
self.assertEqual(self.hv._GetCgroupCpuList("instance1"), [0, 1])
@patch_object(LXCHypervisor, "_GetCgroupInstanceValue")
def testGetCgroupMemoryLimit(self, getval_mock):
getval_mock.return_value = "128"
self.assertEqual(self.hv._GetCgroupMemoryLimit("instance1"), 128)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment