]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
ftests: Add a cgroup controller class
authorTom Hromatka <tom.hromatka@oracle.com>
Thu, 17 Dec 2020 19:47:48 +0000 (12:47 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Thu, 17 Dec 2020 21:53:31 +0000 (14:53 -0700)
Add a controller class that mirrors libcgroup's
struct cgroup_controller.  The controller class
contains a dictionary that represents the array
of struct control_values.  To simplify the usability
within the functional tests, the __str__() and
__eq__() methods have been overridden.

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
ftests/controller.py [new file with mode: 0644]

diff --git a/ftests/controller.py b/ftests/controller.py
new file mode 100644 (file)
index 0000000..917efa9
--- /dev/null
@@ -0,0 +1,69 @@
+#
+# Cgroup class for the libcgroup functional tests
+#
+# Copyright (c) 2020 Oracle and/or its affiliates.
+# Author: Tom Hromatka <tom.hromatka@oracle.com>
+#
+
+#
+# This library is free software; you can redistribute it and/or modify it
+# under the terms of version 2.1 of the GNU Lesser General Public License as
+# published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+# for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this library; if not, see <http://www.gnu.org/licenses>.
+#
+
+from log import Log
+
+class Controller(object):
+    # The controller class closely mirrors libcgroup's struct cgroup_controller
+    def __init__(self, name):
+        self.name = name
+        # self.settings maps to
+        # struct control_value *values[CG_NV_MAX];
+        self.settings = dict()
+
+    def __str__(self):
+        out_str = "Controller {}\n".format(self.name)
+
+        for setting_key in self.settings:
+            out_str += "    {} = {}\n".format(setting_key, self.settings[setting_key])
+
+        return out_str
+
+    def __eq__(self, other):
+        if not isinstance(other, Controller):
+            return False
+
+        if not self.name == other.name:
+            return False
+
+        if not self.settings == other.settings:
+            self_keys = set(self.settings.keys())
+            other_keys = set(other.settings.keys())
+
+            added = other_keys - self_keys
+            if added is not None:
+                for key in added:
+                    Log.log_critical("Other contains {} = {}".format(key, other.settings[key]))
+
+            removed = self_keys - other_keys
+            if removed is not None:
+                for key in removed:
+                    Log.log_critical("Self contains {} = {}".format(key, self.settings[key]))
+
+            common = self_keys.intersection(other_keys)
+            for key in common:
+                if self.settings[key] != other.settings[key]:
+                    Log.log_critical("self{} = {} while other{} = {}".format(
+                        key, self.settings[key], key, other.settings[key]))
+
+            return False
+
+        return True