diff options
author | David Sommerseth <davids@redhat.com> | 2010-05-14 19:03:50 +0200 |
---|---|---|
committer | David Sommerseth <davids@redhat.com> | 2010-05-14 19:03:50 +0200 |
commit | 09c18db0baebb77c02de066638ad2ed27edb15cc (patch) | |
tree | 7aeb83d54da507f267694fa0162256a576515b14 | |
parent | 1a9c5a4c7372c5dead09254b11c844debcc2a26c (diff) | |
download | rteval-09c18db0baebb77c02de066638ad2ed27edb15cc.tar.gz rteval-09c18db0baebb77c02de066638ad2ed27edb15cc.tar.xz rteval-09c18db0baebb77c02de066638ad2ed27edb15cc.zip |
Added unit-tests
Added a generic unit-test framework and rewritten the self-test parts in
some rteval modules where this was appropriate.
The unit-test contains a list of modules to load and test. It will run a
function named unit_test() in the imported modules. It is expected that
the unit_test() function will return 0 on success, otherwise it is logged
as an error.
-rw-r--r-- | rteval/cputopology.py | 28 | ||||
-rw-r--r-- | rteval/dmi.py | 39 | ||||
-rw-r--r-- | rteval/rtevalConfig.py | 16 | ||||
-rw-r--r-- | rteval/xmlout.py | 126 | ||||
-rwxr-xr-x | unit-tests/unittest.py | 130 |
5 files changed, 269 insertions, 70 deletions
diff --git a/rteval/cputopology.py b/rteval/cputopology.py index 61ef982..4b696f7 100644 --- a/rteval/cputopology.py +++ b/rteval/cputopology.py @@ -99,12 +99,24 @@ class CPUtopology: def getCPUsockets(self): return self.__cpu_sockets -if __name__ == '__main__': - cputop = CPUtopology() - n = cputop.parse() - - x = libxml2.newDoc('1.0') - x.setRootElement(n) - x.saveFormatFileEnc('-','UTF-8',1) +def unit_test(rootdir): + try: + cputop = CPUtopology() + n = cputop.parse() + + print " ---- XML Result ---- " + x = libxml2.newDoc('1.0') + x.setRootElement(n) + x.saveFormatFileEnc('-','UTF-8',1) + + print " ---- getCPUcores() / getCPUscokets() ---- " + print "CPU cores: %i (online: %i) - CPU sockets: %i" % (cputop.getCPUcores(False), + cputop.getCPUcores(True), + cputop.getCPUsockets()) + return 0 + except Exception, e: + print "** EXCEPTION %s", str(e) + return 1 - # print "CPU cores: %i (online: %i) - CPU sockets: %i" % (cputop.getCPUcores(False), cputop.getCPUcores(True), cputop.getCPUsockets()) +if __name__ == '__main__': + unit_test() diff --git a/rteval/dmi.py b/rteval/dmi.py index 3b08a2c..4f4835c 100644 --- a/rteval/dmi.py +++ b/rteval/dmi.py @@ -83,12 +83,35 @@ class DMIinfo(object): xml.AppendXMLnodes(node) -if __name__ == '__main__': +def unit_test(rootdir): from pprint import pprint - - d = DMIinfo('.') - x = xmlout.XMLOut('dmi_test', "0.0") - x.NewReport() - d.genxml(x) - x.close() - x.Write('-') + + class unittest_ConfigDummy(object): + def __init__(self, rootdir): + self.config = {'installdir': '%s/rteval'} + self.__update_vars() + + def __update_vars(self): + for k in self.config.keys(): + self.__dict__[k] = self.config[k] + + try: + ProcessWarnings() + if os.getuid() != 0: + print "** ERROR ** Must be root to run this unit_test()" + return 1 + + cfg = unittest_ConfigDummy(rootdir) + d = DMIinfo(cfg) + x = xmlout.XMLOut('dmi_test', "0.0") + x.NewReport() + d.genxml(x) + x.close() + x.Write('-') + return 0 + except Exception, e: + print "** EXCEPTION: %s" % str(e) + return 1 + +if __name__ == '__main__': + sys.exit(unit_test('.')) diff --git a/rteval/rtevalConfig.py b/rteval/rtevalConfig.py index 5fad260..bb77da2 100644 --- a/rteval/rtevalConfig.py +++ b/rteval/rtevalConfig.py @@ -185,3 +185,19 @@ class rtevalConfig(rtevalCfgSection): return rtevalCfgSection(self.__config_data[section]) except KeyError, err: raise KeyError("The section '%s' does not exist in the config file" % section) + + +def unit_test(rootdir): + try: + cfg = rtevalConfig() + cfg.Load(rootdir + '/rteval/rteval.conf') + print cfg + return 0 + except Exception, e: + print "** EXCEPTION %s", str(e) + return 1 + + +if __name__ == '__main__': + import sys + sys.exit(unit_test('..')) diff --git a/rteval/xmlout.py b/rteval/xmlout.py index b4fe371..648131b 100644 --- a/rteval/xmlout.py +++ b/rteval/xmlout.py @@ -257,58 +257,76 @@ class XMLOut(object): return self.currtag.addChild(nodes) +def unit_test(rootdir): + try: + x = XMLOut('rteval', 'UNIT-TEST', None, 'UTF-8') + x.NewReport() + x.openblock('run_info', {'days': 0, 'hours': 0, 'minutes': 32, 'seconds': 18}) + x.taggedvalue('time', '11:22:33') + x.taggedvalue('date', '2000-11-22') + x.closeblock() + x.openblock('uname') + x.taggedvalue('node', u'testing - \xe6\xf8') + x.taggedvalue('kernel', 'my_test_kernel', {'is_RT': 0}) + x.taggedvalue('arch', 'mips') + x.closeblock() + x.openblock('hardware') + x.taggedvalue('cpu_cores', 2) + x.taggedvalue('memory_size', 1024*1024*2) + x.closeblock() + x.openblock('loads', {'load_average': 3.29}) + x.taggedvalue('command_line','./load/loader --extreme --ultimate --threads 4096', + {'name': 'heavyloader'}) + x.taggedvalue('command_line','dd if=/dev/zero of=/dev/null', {'name': 'lightloader'}) + x.closeblock() + x.close() + print "------------- XML OUTPUT ----------------------------" + x.Write("-") + print "------------- XSLT PARSED OUTPUT --------------------" + x.Write("-", "rteval_text.xsl") + print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + x.Write("/tmp/xmlout-test.xml") + del x + + print "------------- LOAD XML FROM FILE -----------------------------" + x = XMLOut('rteval','UNIT-TEST', None, 'UTF-8') + x.LoadReport("/tmp/xmlout-test.xml", True) + print "------------- LOADED XML DATA --------------------------------" + x.Write("-") + print "------------- XSLT PARSED OUTPUT FROM LOADED XML--------------" + x.Write("-", "rteval_text.xsl") + x.close() + + ## Test new data parser ... it eats most data types + print "------------- TESTING XMLOut::ParseData() --------------" + x.NewReport() + x.ParseData("ParseTest", "test string", {"type": "simple_string"}) + x.ParseData("ParseTest", 1234, {"type": "integer"}) + x.ParseData("ParseTest", 39.3904, {"type": "float"}) + x.ParseData("ParseTest", (11,22,33,44,55), {"type": "tuples"}) + x.ParseData("ParseTest", (99,88,77), {"type": "tuples", "comment": "Changed default tuple tag name"}, + "int_values") + test = {"var1": "value 1", + "var2": { "varA1": 1, + "pi": 3.1415926, + "varA3": (1, + 2, + {"test1": "val1"}, + (4.1,4.2,4.3), + 5), + "varA4": {'another_level': True, + 'another_value': "blabla"} + }, + "utf8 data": u'æøå', + u"løpe": True} + x.ParseData("ParseTest", test, {"type": "dict"}, prefix="test ") + x.close() + x.Write("-") + return 0 + except Exception, e: + print "** EXCEPTION %s", str(e) + return 1 + if __name__ == '__main__': - x = XMLOut('rteval', '0.6', None, 'UTF-8') - x.NewReport() - x.openblock('run_info', {'days': 0, 'hours': 0, 'minutes': 32, 'seconds': 18}) - x.taggedvalue('time', '11:22:33') - x.taggedvalue('date', '2000-11-22') - x.closeblock() - x.openblock('uname') - x.taggedvalue('node', u'testing - \xe6\xf8') - x.taggedvalue('kernel', 'my_test_kernel', {'is_RT': 0}) - x.taggedvalue('arch', 'mips') - x.closeblock() - x.openblock('hardware') - x.taggedvalue('cpu_cores', 2) - x.taggedvalue('memory_size', 1024*1024*2) - x.closeblock() - x.openblock('loads', {'load_average': 3.29}) - x.taggedvalue('command_line','./load/loader --extreme --ultimate --threads 4096', {'name': 'heavyloader'}) - x.taggedvalue('command_line','dd if=/dev/zero of=/dev/null', {'name': 'lightloader'}) - x.closeblock() - x.close() - print "------------- XML OUTPUT ----------------------------" - x.Write("-") - print "------------- XSLT PARSED OUTPUT --------------------" - x.Write("-", "rteval_text.xsl") - print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" - x.LoadReport("latency.xml", True) - x.Write("-") - x.Write("-", "rteval_text.xsl") - x.close() - - ## Test new data parser ... it eats most data types - x.NewReport() - x.ParseData("ParseTest", "test string", {"type": "simple_string"}) - x.ParseData("ParseTest", 1234, {"type": "integer"}) - x.ParseData("ParseTest", 39.3904, {"type": "float"}) - x.ParseData("ParseTest", (11,22,33,44,55), {"type": "tuples"}) - x.ParseData("ParseTest", (99,88,77), {"type": "tuples", "comment": "Changed default tuple tag name"}, - "int_values") - test = {"var1": "value 1", - "var2": { "varA1": 1, - "pi": 3.1415926, - "varA3": (1, - 2, - {"test1": "val1"}, - (4.1,4.2,4.3), - 5), - "varA4": {'another_level': True, - 'another_value': "blabla"} - }, - "utf8 data": u'æøå', - u"løpe": True} - x.ParseData("ParseTest", test, {"type": "dict"}, prefix="test ") - x.close() - x.Write("-") + sys.exit(unit_test('..')) + diff --git a/unit-tests/unittest.py b/unit-tests/unittest.py new file mode 100755 index 0000000..f450cbc --- /dev/null +++ b/unit-tests/unittest.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# Copyright (c) 2010 Red Hat, Inc. All rights reserved. This copyrighted material +# is made available to anyone wishing to use, modify, copy, or +# redistribute it subject to the terms and conditions of the GNU General +# Public License v.2. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# Author: David Sommerseth <davids@redhat.com> +# + +import os, sys + +class UnitTest(object): + "Unified unit test class" + + def __init__(self, srcrootdir): + "UnitTest constructor. srcrootdir argument must point at the source root directory" + self.imported_mods = [] + self.mod_impcount = 0 + self.mod_impfail = 0 + self.mod_testpass = 0 + self.mod_testfail = 0 + self.mod_testmiss = 0 + self.rootdir = srcrootdir + sys.path.insert(0, self.rootdir) + + + def LoadModules(self, modules): + """Loads all the defined modules. The modules argument takes a tuple list + consisting of ('subdir','module name')""" + + for (directory, mod) in modules: + # Check if the directory is in the "include" path + try: + sys.path.index('%s/%s' % (self.rootdir,directory)) + except ValueError: + # Not found, insert it + sys.path.insert(0, '%s/%s' % (self.rootdir, directory)) + + try: + impmod = __import__(mod) + print "** Imported %s/%s" % (directory, mod) + self.imported_mods.append({'name': '%s/%s' %(directory, mod), + 'module': impmod}) + self.mod_impcount += 1 + except ImportError, e: + print "!! ** ERROR ** Failed to import %s/%s (Exception: %s)" % (directory, mod, str(e)) + self.mod_impfail += 1 + + return True + + + def RunTests(self): + "Runs the unit_test() function in all successfully imported modules" + + for m in self.imported_mods: + try: + # Check if the unit_test() function exists and is callable before trying + # to run the unit test + if callable(m['module'].unit_test): + print + print 78 * '-' + print "** Running unit test for: %s" % m['name'] + print 78 * '.' + res = m['module'].unit_test(self.rootdir) + print 78 * '.' + if res == 0: + print "** Result of %s: PASS" % m['name'] + self.mod_testpass += 1 + else: + print "** Result of %s: FAILED (return code: %s)" % (m['name'], str(res)) + self.mod_testfail += 1 + print 78 * '=' + else: + self.mod_testmiss += 1 + print "!!! ** ERROR ** Could not run %s::unit_test()" % m['name'] + except AttributeError: + self.mod_testmiss += 1 + print "!!! ** ERROR ** No %s::unit_test() method found" % m['name'] + + + def PrintTestSummary(self): + "Prints a result summary of all the tests" + print + print " --------------------" + print " ** TEST SUMMARY ** " + print " --------------------" + print + print " - Modules:" + print " Declared for test: %i" % (self.mod_impcount + self.mod_impfail) + print " Successfully imported: %i" % self.mod_impcount + print " Failed import: %i" % self.mod_impfail + print + print " - Tests:" + print " Tests scheduled: %i" % (self.mod_testpass + self.mod_testfail + self.mod_testmiss) + print " Sucessfully tests: %i" % self.mod_testpass + print " Failed tests: %i" % self.mod_testfail + print " Missing unit_test() %i" % self.mod_testmiss + print + + +if __name__ == '__main__': + + # Retrieve the root directory if the source dir + # - use the first occurence of the 'v7' subdir as the root dirq + srcrootdir_ar = os.getcwd().split('/') + rootdir = '/'.join(srcrootdir_ar[0:srcrootdir_ar.index('rteval')+1]) + print "** Source root dir: %s" % rootdir + + # Prepare the unit tester + tests = UnitTest(rootdir) + + # Load defined modules ('subdir','import name') + tests.LoadModules(( + ('rteval','cputopology'), + ('rteval','dmi'), + ('rteval','rtevalConfig'), + ('rteval','xmlout') + )) + # Run all tests + tests.RunTests() + tests.PrintTestSummary() + |