From: Tom Hromatka Date: Thu, 17 Dec 2020 19:02:06 +0000 (-0700) Subject: ftests: Move cgroup version enums to their own class X-Git-Tag: v2.0.3~11^2^2~29^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45bf8f44a17e920467c5601d33f574ab183df9ca;p=thirdparty%2Flibcgroup.git ftests: Move cgroup version enums to their own class 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 --- diff --git a/ftests/001-cgget-basic_cgget_v1.py b/ftests/001-cgget-basic_cgget_v1.py index 87da4bc6..1c1016b1 100755 --- a/ftests/001-cgget-basic_cgget_v1.py +++ b/ftests/001-cgget-basic_cgget_v1.py @@ -20,7 +20,7 @@ # along with this library; if not, see . # -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" diff --git a/ftests/002-cgdelete-recursive_delete.py b/ftests/002-cgdelete-recursive_delete.py index 755b87a6..220a763a 100755 --- a/ftests/002-cgdelete-recursive_delete.py +++ b/ftests/002-cgdelete-recursive_delete.py @@ -20,7 +20,7 @@ # along with this library; if not, see . # -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. # diff --git a/ftests/003-cgget-basic_cgget_v2.py b/ftests/003-cgget-basic_cgget_v2.py index 9eb558dd..54727fe5 100755 --- a/ftests/003-cgget-basic_cgget_v2.py +++ b/ftests/003-cgget-basic_cgget_v2.py @@ -20,7 +20,7 @@ # along with this library; if not, see . # -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" diff --git a/ftests/cgroup.py b/ftests/cgroup.py index ec5c6821..146ced71 100644 --- a/ftests/cgroup.py +++ b/ftests/cgroup.py @@ -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):