[PATCH 3/4] client.tests.cgroup: {set,get}_property functions unification; bugfixes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 * override "check" parameter to True / False / String (different value)
 * add checkprop parameter to change the property which is used to check the
   value.
 * use more sane "set_property_h" function name instead of "set_prop"
 * change raise error.TestFail for error.TestError
 * get_property: return [""] when no input
 * code cleanup

Signed-off-by: Lukas Doktor <ldoktor@xxxxxxxxxx>
---
 client/tests/cgroup/cgroup.py        |   10 ++--
 client/tests/cgroup/cgroup_common.py |  102 ++++++++++++++++------------------
 2 files changed, 53 insertions(+), 59 deletions(-)

diff --git a/client/tests/cgroup/cgroup.py b/client/tests/cgroup/cgroup.py
index 8626c52..a290d76 100644
--- a/client/tests/cgroup/cgroup.py
+++ b/client/tests/cgroup/cgroup.py
@@ -176,7 +176,7 @@ class cgroup(test.test):
         logging.debug("test_memory: Memfill mem only limit")
         ps = item.test("memfill %d %s" % (mem, outf.name))
         item.set_cgroup(ps.pid, pwd)
-        item.set_prop("memory.limit_in_bytes", ("%dM" % (mem/2)), pwd)
+        item.set_property_h("memory.limit_in_bytes", ("%dM" % (mem/2)), pwd)
         ps.stdin.write('\n')
         i = 0
         while ps.poll() == None:
@@ -222,7 +222,7 @@ class cgroup(test.test):
             logging.debug("test_memory: Memfill mem + swap limit")
             ps = item.test("memfill %d %s" % (mem, outf.name))
             item.set_cgroup(ps.pid, pwd)
-            item.set_prop("memory.memsw.limit_in_bytes", "%dM"%(mem/2), pwd)
+            item.set_property_h("memory.memsw.limit_in_bytes", "%dM"%(mem/2), pwd)
             ps.stdin.write('\n')
             i = 0
             while ps.poll() == None:
@@ -352,15 +352,15 @@ class cgroup(test.test):
 
         try:
             # Available cpus: cpuset.cpus = "0-$CPUS\n"
-            no_cpus = int(item.get_prop("cpuset.cpus").split('-')[1]) + 1
+            no_cpus = int(item.get_property("cpuset.cpus")[0].split('-')[1]) + 1
         except:
             raise error.TestFail("Failed to get no_cpus or no_cpus = 1")
 
         pwd = item.mk_cgroup()
         try:
-            tmp = item.get_prop("cpuset.cpus")
+            tmp = item.get_property("cpuset.cpus")[0]
             item.set_property("cpuset.cpus", tmp, pwd)
-            tmp = item.get_prop("cpuset.mems")
+            tmp = item.get_property("cpuset.mems")[0]
             item.set_property("cpuset.mems", tmp, pwd)
         except:
             cleanup(True)
diff --git a/client/tests/cgroup/cgroup_common.py b/client/tests/cgroup/cgroup_common.py
index 7a514c6..a7229ce 100755
--- a/client/tests/cgroup/cgroup_common.py
+++ b/client/tests/cgroup/cgroup_common.py
@@ -60,7 +60,7 @@ class Cgroup(object):
         """
         self.root = modules.get_pwd(self.module)
         if not self.root:
-            raise error.TestFail("cg.initialize(): Module %s not found"
+            raise error.TestError("cg.initialize(): Module %s not found"
                                                                 % self.module)
 
 
@@ -76,7 +76,7 @@ class Cgroup(object):
             else:
                 pwd = mkdtemp(prefix='cgroup-', dir=self.root) + '/'
         except Exception, inst:
-            raise error.TestFail("cg.mk_cgroup(): %s" % inst)
+            raise error.TestError("cg.mk_cgroup(): %s" % inst)
         self.cgroups.append(pwd)
         return pwd
 
@@ -94,7 +94,7 @@ class Cgroup(object):
             logging.warn("cg.rm_cgroup(): Removed cgroup which wasn't created"
                          "using this Cgroup")
         except Exception, inst:
-            raise error.TestFail("cg.rm_cgroup(): %s" % inst)
+            raise error.TestError("cg.rm_cgroup(): %s" % inst)
 
 
     def test(self, cmd):
@@ -143,9 +143,9 @@ class Cgroup(object):
         try:
             open(pwd+'/tasks', 'w').write(str(pid))
         except Exception, inst:
-            raise error.TestFail("cg.set_cgroup(): %s" % inst)
+            raise error.TestError("cg.set_cgroup(): %s" % inst)
         if self.is_cgroup(pid, pwd):
-            raise error.TestFail("cg.set_cgroup(): Setting %d pid into %s "
+            raise error.TestError("cg.set_cgroup(): Setting %d pid into %s "
                                  "cgroup failed" % (pid, pwd))
 
     def set_root_cgroup(self, pid):
@@ -157,20 +157,6 @@ class Cgroup(object):
         return self.set_cgroup(pid, self.root)
 
 
-    def get_prop(self, prop, pwd=None):
-        """
-        Gets one line of the property value
-        @param prop: property name (file)
-        @param pwd: cgroup directory
-        @return: String value or None when FAILED
-        """
-        tmp = self.get_property(prop, pwd)
-        if tmp:
-            if tmp[0][-1] == '\n':
-                tmp[0] = tmp[0][:-1]
-            return tmp[0]
-
-
     def get_property(self, prop, pwd=None):
         """
         Gets the property value
@@ -181,45 +167,50 @@ class Cgroup(object):
         if pwd == None:
             pwd = self.root
         try:
-            ret = open(pwd+prop, 'r').readlines()
+            # Remove tailing '\n' from each line
+            ret = [_[:-1] for _ in open(pwd+prop, 'r').readlines()]
+            if ret:
+                return ret
+            else:
+                return [""]
         except Exception, inst:
-            raise error.TestFail("cg.get_property(): %s" % inst)
-        else:
-            return ret
+            raise error.TestError("cg.get_property(): %s" % inst)
 
 
-    def set_prop(self, prop, value, pwd=None, check=True):
+    def set_property_h(self, prop, value, pwd=None, check=True, checkprop=None):
         """
         Sets the one-line property value concerning the K,M,G postfix
         @param prop: property name (file)
         @param value: desired value
         @param pwd: cgroup directory
-        @param check: check the value after setup
+        @param check: check the value after setup / override checking value
+        @param checkprop: override prop when checking the value
         """
         _value = value
         try:
             value = str(value)
-            if value[-1] == '\n':
-                value = value[:-1]
-            if value[-1] == 'K':
-                value = int(value[:-1]) * 1024
-            elif value[-1] == 'M':
-                value = int(value[:-1]) * 1048576
-            elif value[-1] == 'G':
-                value = int(value[:-1]) * 1073741824
+            human = {'B': 1,
+                     'K': 1024,
+                     'M': 1048576,
+                     'G': 1073741824,
+                     'T': 1099511627776
+                    }
+            if human.has_key(value[-1]):
+                value = int(value[:-1]) * human[value[-1]]
         except:
             logging.warn("cg.set_prop() fallback into cg.set_property.")
             value = _value
-        self.set_property(prop, value, pwd, check)
+        self.set_property(prop, value, pwd, check, checkprop)
 
 
-    def set_property(self, prop, value, pwd=None, check=True):
+    def set_property(self, prop, value, pwd=None, check=True, checkprop=None):
         """
         Sets the property value
         @param prop: property name (file)
         @param value: desired value
         @param pwd: cgroup directory
-        @param check: check the value after setup
+        @param check: check the value after setup / override checking value
+        @param checkprop: override prop when checking the value
         """
         value = str(value)
         if pwd == None:
@@ -227,14 +218,17 @@ class Cgroup(object):
         try:
             open(pwd+prop, 'w').write(value)
         except Exception, inst:
-            raise error.TestFail("cg.set_property(): %s" % inst)
-        if check:
-            # Get the first line - '\n'
-            _value = self.get_property(prop, pwd)[0][:-1]
-            if value != _value:
-                raise error.TestFail("cg.set_property(): Setting failed: "
-                                     "desired = %s, real value = %s"
-                                     % (value, _value))
+            raise error.TestError("cg.set_property(): %s" % inst)
+        if check != False:
+            if check == True:
+                check = value
+            if checkprop == None:
+                checkprop = prop
+            _values = self.get_property(checkprop, pwd)
+            if check not in _values:
+                raise error.TestError("cg.set_property(): Setting failed: "
+                                      "desired = %s, real values = %s"
+                                      % (repr(check), repr(_values)))
 
 
     def smoke_test(self):
@@ -246,14 +240,14 @@ class Cgroup(object):
 
         ps = self.test("smoke")
         if ps == None:
-            raise error.TestFail("cg.smoke_test: Couldn't create process")
+            raise error.TestError("cg.smoke_test: Couldn't create process")
 
         if (ps.poll() != None):
-            raise error.TestFail("cg.smoke_test: Process died unexpectidly")
+            raise error.TestError("cg.smoke_test: Process died unexpectidly")
 
         # New process should be a root member
         if self.is_root_cgroup(ps.pid):
-            raise error.TestFail("cg.smoke_test: Process is not a root member")
+            raise error.TestError("cg.smoke_test: Process is not a root member")
 
         # Change the cgroup
         self.set_cgroup(ps.pid, pwd)
@@ -261,10 +255,10 @@ class Cgroup(object):
         # Try to remove used cgroup
         try:
             self.rm_cgroup(pwd)
-        except error.TestFail:
+        except error.TestError:
             pass
         else:
-            raise error.TestFail("cg.smoke_test: Unexpected successful deletion"
+            raise error.TestError("cg.smoke_test: Unexpected successful deletion"
                                  " of the used cgroup")
 
         # Return the process into the root cgroup
@@ -277,7 +271,7 @@ class Cgroup(object):
         ps.stdin.write('\n')
         time.sleep(2)
         if (ps.poll() == None):
-            raise error.TestFail("cg.smoke_test: Process is not finished")
+            raise error.TestError("cg.smoke_test: Process is not finished")
 
 
 class CgroupModules(object):
@@ -316,13 +310,13 @@ class CgroupModules(object):
         """
         logging.debug("Desired cgroup modules: %s", _modules)
         mounts = []
-        fp = open('/proc/mounts', 'r')
-        line = fp.readline().split()
+        proc_mounts = open('/proc/mounts', 'r')
+        line = proc_mounts.readline().split()
         while line:
             if line[2] == 'cgroup':
                 mounts.append(line)
-            line = fp.readline().split()
-        fp.close()
+            line = proc_mounts.readline().split()
+        proc_mounts.close()
 
         for module in _modules:
             # Is it already mounted?
-- 
1.7.6.2

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux