]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Move cgroup version enums to their own class
authorTom Hromatka <tom.hromatka@oracle.com>
Thu, 17 Dec 2020 19:02:06 +0000 (12:02 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 17 Dec 2020 21:53:27 +0000 (14:53 -0700)
In preparation for adding non-static functionality to
the Cgroup class, move the cgroup version enumerations
to their own class.  (Enums and __init__() can collide
in strange ways.)

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
ftests/001-cgget-basic_cgget_v1.py
ftests/002-cgdelete-recursive_delete.py
ftests/003-cgget-basic_cgget_v2.py
ftests/cgroup.py

index 87da4bc67725271774aae51195527eae48901e78..1c1016b1a4a859c2896ad392badf09d407d96252 100755 (executable)
@@ -20,7 +20,7 @@
 # along with this library; if not, see <http://www.gnu.org/licenses>.
 #
 
-from cgroup import Cgroup
+from cgroup import Cgroup, CgroupVersion
 import consts
 import ftests
 import os
@@ -36,8 +36,7 @@ def prereqs(config):
     result = consts.TEST_PASSED
     cause = None
 
-    # This test was written for a cgroup v1 cpu controller only
-    if Cgroup.version('cpu') != Cgroup.CGROUP_V1:
+    if CgroupVersion.get_version('cpu') != CgroupVersion.CGROUP_V1:
         result = consts.TEST_SKIPPED
         cause = "This test requires the cgroup v1 cpu controller"
 
index 755b87a6f626fdbe102acf19eb62d8f1de2c5c81..220a763a87a00d17117632978f6683539082cb55 100755 (executable)
@@ -20,7 +20,7 @@
 # along with this library; if not, see <http://www.gnu.org/licenses>.
 #
 
-from cgroup import Cgroup
+from cgroup import Cgroup, CgroupVersion
 import consts
 import ftests
 import os
@@ -41,8 +41,8 @@ def setup(config):
     Cgroup.create(config, CONTROLLER, os.path.join(PARENT, CHILD))
     Cgroup.create(config, CONTROLLER, os.path.join(PARENT, CHILD, GRANDCHILD))
 
-    version = Cgroup.version(CONTROLLER)
-    if version == Cgroup.CGROUP_V1:
+    version = CgroupVersion.get_version(CONTROLLER)
+    if version == CgroupVersion.CGROUP_V1:
         # cgdelete in a cgroup v1 controller should be able to move a process
         # from a child cgroup to its parent.
         #
index 9eb558dd99369dd7ee74a408ff28b25c968b6daa..54727fe59c409d4bfa4b397203180c88d7ecbdd3 100755 (executable)
@@ -20,7 +20,7 @@
 # along with this library; if not, see <http://www.gnu.org/licenses>.
 #
 
-from cgroup import Cgroup
+from cgroup import Cgroup, CgroupVersion
 import consts
 import ftests
 import os
@@ -36,7 +36,7 @@ def prereqs(config):
     result = consts.TEST_PASSED
     cause = None
 
-    if Cgroup.version('cpuset') != Cgroup.CGROUP_V2:
+    if CgroupVersion.get_version('cpuset') != CgroupVersion.CGROUP_V2:
         result = consts.TEST_SKIPPED
         cause = "This test requires the cgroup v2 cpuset controller"
 
index ec5c6821c9d187a7d796c25f6d38302805ebacad..146ced711a46e2532640642dea58626759268334 100644 (file)
@@ -24,11 +24,32 @@ from enum import Enum
 import os
 from run import Run
 
-class Cgroup(Enum):
+class CgroupVersion(Enum):
     CGROUP_UNK = 0
     CGROUP_V1 = 1
     CGROUP_V2 = 2
 
+    # given a controller name, get the cgroup version of the controller
+    @staticmethod
+    def get_version(controller):
+        with open('/proc/mounts', 'r') as mntf:
+            for line in mntf.readlines():
+                mnt_path = line.split()[1]
+
+                if line.split()[0] == 'cgroup':
+                    for option in line.split()[3].split(','):
+                        if option == controller:
+                            return CgroupVersion.CGROUP_V1
+                elif line.split()[0] == 'cgroup2':
+                    with open(os.path.join(mnt_path, 'cgroup.controllers'), 'r') as ctrlf:
+                        controllers = ctrlf.readline()
+                        for ctrl in controllers.split():
+                            if ctrl == controller:
+                                return CgroupVersion.CGROUP_V2
+
+        return CgroupVersion.CGROUP_UNK
+
+class Cgroup(object):
     @staticmethod
     def build_cmd_path(in_container, cmd):
         if in_container:
@@ -176,25 +197,6 @@ class Cgroup(Enum):
 
         return ret
 
-    @staticmethod
-    def version(controller):
-        with open('/proc/mounts', 'r') as mntf:
-            for line in mntf.readlines():
-                mnt_path = line.split()[1]
-
-                if line.split()[0] == 'cgroup':
-                    for option in line.split()[3].split(','):
-                        if option == controller:
-                            return Cgroup.CGROUP_V1
-                elif line.split()[0] == 'cgroup2':
-                    with open(os.path.join(mnt_path, 'cgroup.controllers'), 'r') as ctrlf:
-                        controllers = ctrlf.readline()
-                        for ctrl in controllers.split():
-                            if ctrl == controller:
-                                return Cgroup.CGROUP_V2
-
-        return Cgroup.CGROUP_UNK
-
     @staticmethod
     def classify(config, controller, cgname, pid_list, sticky=False,
                  cancel_sticky=False, in_container=True):