From 3e4988faef136607bf8b2ab5b267b6e15652ebc2 Mon Sep 17 00:00:00 2001 From: Jan Vcelak Date: Thu, 1 Oct 2009 16:52:44 +0200 Subject: [PATCH] monitor plugins test rewritten and widen to support tuning plugins as well --- tests/monitorpluginstest.py | 81 -------------- tests/pluginstest.py | 216 ++++++++++++++++++++++++++++++++++++ tests/tuned-test.py | 6 +- 3 files changed, 219 insertions(+), 84 deletions(-) delete mode 100644 tests/monitorpluginstest.py create mode 100644 tests/pluginstest.py diff --git a/tests/monitorpluginstest.py b/tests/monitorpluginstest.py deleted file mode 100644 index fe5d38e..0000000 --- a/tests/monitorpluginstest.py +++ /dev/null @@ -1,81 +0,0 @@ -# -# Copyright (C) 2008, 2009 Red Hat, Inc. -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# 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. -# - -import sys, os, ConfigParser - -class MonitorPluginsTest: - def __init__(self, tunedir): - - self.tunedir = tunedir - - # fake configuration - self.config = ConfigParser.RawConfigParser() - #self.config.add_section("main") - #self.config.set("main", "interval", 10) - - def run(self): - plugins = map( - lambda f: f[:-3], # remove .py - filter( # only modules - lambda f: f[0] != "." and f[-3:] == ".py" and f != "__init__.py", - os.listdir(self.tunedir + "/monitorplugins") - )) - - if len(plugins) == 0: - print "No monitor plugins found." - return False - - print "Found monitor plugins:" - for p in plugins: - print "* %s" % (p) - self._checkPlugin(p) - - def _checkPlugin(self, plugin): - - # module loading - print " - import" - try: - exec "from monitorplugins.%s import _plugin" % (plugin) - except Exception as e: - print " failed: %s %s" % (e, type(e)) - return False - - - # module initialization - print " - init()" - try: - _plugin.init(self.config) - except Exception as e: - print " failed: %s %s" % (e, type(e)) - return False - - # module get test data - print " - getLoad()" - try: - if _plugin.getLoad() == None: - raise Exception("plugin returned None as a result") - except Exception as e: - print " failed: %s %s" % (e, type(e)) - - # module cleanup - print " - cleanup()" - try: - _plugin.cleanup() - except Exception as e: - print " failed: %s %s" % (e, type(e)) - diff --git a/tests/pluginstest.py b/tests/pluginstest.py new file mode 100644 index 0000000..73fd26b --- /dev/null +++ b/tests/pluginstest.py @@ -0,0 +1,216 @@ +# +# Copyright (C) 2008, 2009 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# 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. +# + +import os, ConfigParser + +class PluginsTester: + + def __init__(self, tunedir): + + self.tunedir = tunedir + self.mp_dir = "monitorplugins" + self.tp_dir = "tuningplugins" + + self.config = ConfigParser.RawConfigParser() + + def run(self): + + self.__checkPlugins() + + def __getPluginsFromDir(self, dir): + + files = os.listdir(self.tunedir + "/" + dir); + plugins = filter(lambda f: f[0] != "." and f[-3:] == ".py" and f != "__init__.py", files) + plugins = map(lambda f: f[:-3], plugins) + plugins.sort() + + return plugins + + ### reporting tests progress + + def __reportTestBegin(self, info): + print " * %s" % (info) + + def __reportTestStep(self, info): + self.__testStep = info + + def __reportTestResult(self, exception = None): + if exception == None: + print " - %s: success" % (self.__testStep) + else: + print " - %s: failed (%s, %s)" % (self.__testStep, exception, type (exception)) + def __reportTestSkip(self, info): + print " - %s: skipped, %s" % (self.__testStep, info) + + ### testing + + def __checkPlugins(self): + + monitorplugins = self.__getPluginsFromDir(self.mp_dir) + tuningplugins = self.__getPluginsFromDir(self.tp_dir) + + # check plugins availability + + print "checking plugins availablity" + + self.__checkSiblingPlugins(monitorplugins, tuningplugins) + + print + + # monitor plugins test + + print "monitor plugins test" + if len(monitorplugins) == 0: + print " - no plugins found" + + monitor_results = {} + + for mp in monitorplugins: + load = self.__testMonitorPlugin(mp) + monitor_results[mp] = load + + print + + # tuning plugins test + + print "tunning plugins test" + if len(tuningplugins) == 0: + print " - no plugins found" + + for tp in tuningplugins: + try: + load = monitor_results[tp] + except: + load = None + self.__testTuningPlugin(tp, load) + + def __checkSiblingPlugins(self, monitorplugins, tuningplugins): + + ok = True + + for mp in monitorplugins: + if tuningplugins.count(mp) != 1: + ok = False + print " - monitor plugin '%s' misses tuning plugin" % mp + + for tp in tuningplugins: + if monitorplugins.count(tp) != 1: + ok = False + print " - tuning plugin '%s' misses monitor plugin" % tp + + if ok: + print " - monitor and tuning plugins match" + + def __testMonitorPlugin(self, name): + + self.__reportTestBegin("monitor plugin: %s" % (name)) + + # initialization + + self.__reportTestStep("initialization") + try: + exec "from %s.%s import _plugin" % (self.mp_dir, name) + except Exception as e: + self.__reportTestResult(e) + return None + self.__reportTestResult() + + # init() + + self.__reportTestStep("call init()") + try: + _plugin.init(self.config) + except Exception as e: + self.__reportTestResult(e) + return None + self.__reportTestResult() + + # getLoad() + + self.__reportTestStep("call getLoad()") + try: + load = _plugin.getLoad() + if load == None: + raise Exception("Plugin returned None as a result.") + except Exception as e: + self.__reportTestResult(e) + return None + self.__reportTestResult() + + # cleanup() + + self.__reportTestStep("call cleanup()") + try: + _plugin.cleanup() + except Exception as e: + self.__reportTestResult(e) + return None + self.__reportTestResult() + + return load + + def __testTuningPlugin(self, name, load): + + self.__reportTestBegin("tuning plugin: %s" % (name)) + + # initialization + + self.__reportTestStep("initialization") + try: + exec "from %s.%s import _plugin" % (self.tp_dir, name) + except Exception as e: + self.__reportTestResult(e) + return False + self.__reportTestResult() + + # init() + + self.__reportTestStep("call init()") + try: + _plugin.init(self.config) + except Exception as e: + self.__reportTestResult(e) + return False + self.__reportTestResult() + + # setTuning() + + self.__reportTestStep("call setTuning()") + + if load == None: + self.__reportTestSkip("no data from monitor plugin available") + else: + try: + _plugin.setTuning(load) + except Exception as e: + self.__reportTestResult(e) + return False + self.__reportTestResult() + + # cleanup() + + self.__reportTestStep("call cleanup()") + try: + _plugin.cleanup() + except Exception as e: + self.__reportTestResult(e) + return False + self.__reportTestResult() + + return True + diff --git a/tests/tuned-test.py b/tests/tuned-test.py index 469ad0d..4e65d7c 100755 --- a/tests/tuned-test.py +++ b/tests/tuned-test.py @@ -18,11 +18,11 @@ # import sys -import monitorpluginstest +import pluginstest # importing modules - parent directory tunedir = sys.path[0] + "/.." sys.path.insert(1, tunedir); -mpt = monitorpluginstest.MonitorPluginsTest(tunedir) -mpt.run() +pt = pluginstest.PluginsTester(tunedir) +pt.run()