[asterisk-commits] sgriepentrog: branch sgriepentrog/testsuite-valgrind-nitesh r4352 - in /aster...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Nov 20 10:36:07 CST 2013


Author: sgriepentrog
Date: Wed Nov 20 10:36:05 2013
New Revision: 4352

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=4352
Log:
current version from nitesh

Modified:
    asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestCase.py
    asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestConfig.py
    asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/asterisk.py
    asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/runtests.py

Modified: asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestCase.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestCase.py?view=diff&rev=4352&r1=4351&r2=4352
==============================================================================
--- asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestCase.py (original)
+++ asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestCase.py Wed Nov 20 10:36:05 2013
@@ -181,10 +181,18 @@
         by individual tests, however.
 
         """
+
+        valgrind = False
+        if None != self.test_config.valgrind:
+            valgrind = self.test_config.valgrind
+        elif os.getenv("VALGRIND") == "true":
+            valgrind = True
         for c in range(count):
             logger.info("Creating Asterisk instance %d" % (c + 1))
             host = "127.0.0.%d" % (c + 1)
             self.ast.append(Asterisk(base=self.base, host=host))
+            self.ast[c].valgrind = valgrind
+
             """ If a base configuration for this Asterisk instance has been
             provided, install it first"""
             if base_configs_path:
@@ -615,4 +623,3 @@
             self.create_ami_factory(count=self.asterisk_instances)
         if self.connect_agi:
             self.create_fastagi_factory(count=self.asterisk_instances)
-

Modified: asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestConfig.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestConfig.py?view=diff&rev=4352&r1=4351&r2=4352
==============================================================================
--- asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestConfig.py (original)
+++ asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/TestConfig.py Wed Nov 20 10:36:05 2013
@@ -252,6 +252,7 @@
         self.feature_check = {}
         self.test_configuration = "(none)"
         self.condition_definitions = []
+        self.valgrind = None  # Indicates whether to run asterisk under valgrind
         self.global_test_config = global_test_config
         self.__parse_config()
 
@@ -339,6 +340,15 @@
                 self.can_run = False
                 print "ERROR: '%s' is not a valid Asterisk version" % \
                     properties["forced-version"]
+        if "valgrind" in properties:
+            try:
+                if properties["valgrind"].upper().strip() == "YES":
+                    self.valgrind = True
+                if properties["valgrind"].upper().strip() == "NO":
+                    self.valgrind = False
+            except:
+                print "ERROR: '%s' is not a valid value for valgrind" %\
+                        properties["valgrind"]
 
     def __parse_config(self):
         test_config = "%s/test-config.yaml" % self.test_name

Modified: asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/asterisk.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/asterisk.py?view=diff&rev=4352&r1=4351&r2=4352
==============================================================================
--- asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/asterisk.py (original)
+++ asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/lib/python/asterisk/asterisk.py Wed Nov 20 10:36:05 2013
@@ -206,6 +206,10 @@
         self.directories = {}
         self.ast_version = AsteriskVersion()
         self.base = Asterisk.test_suite_root
+        self.valgrind = False
+        self.valgrind_supp_filename = ""
+        self.valgrind_log_filename = ""
+        self.valgrind_xml_filename = ""
         if base is not None:
             self.base = "%s/%s" % (self.base, base)
         if self.localtest_root:
@@ -261,6 +265,10 @@
         """
 
         def __start_asterisk_callback():
+            if self.valgrind:
+                print "Running Asterisk under valgrind"
+            else:
+                print "Asterisk not running under valgrind"
             self.processProtocol = AsteriskProtocol(self.host, self.__stop_deferred)
             self.process = reactor.spawnProcess(self.processProtocol,
                                                 self.cmd[0],
@@ -280,7 +288,6 @@
         def __wait_fully_booted_error(cli_command):
             """ Errback for CLI command waitfullybooted """
             if time.time() - self.__start_asterisk_time > 5:
-                logger.error("Asterisk core waitfullybooted for %s failed" % self.host)
                 self.__start_deferred.errback(Exception("Command core waitfullybooted failed"))
             else:
                 logger.debug("Asterisk core waitfullybooted failed, attempting again...")
@@ -288,12 +295,26 @@
 
         self.install_configs(os.getcwd() + "/configs")
         self.__setup_configs()
-
         self.cmd = [
             self.ast_binary,
             "-f", "-g", "-q", "-m", "-n",
             "-C", "%s" % os.path.join(self.astetcdir, "asterisk.conf")
         ]
+
+        if self.valgrind:
+            self.valgrind_log_filename = os.path.join(self.base, "valgrind_logs.txt")
+            self.valgrind_xml_filename = os.path.join(self.base, "valgrind_xml.xml")
+            self.valgrind_supp_filename = os.path.join(self.base, "etc/asterisk/valgrind.supp")
+            if os.path.exists(self.valgrind_supp_filename):
+                logger.info("Using the valgrind suppression file %s" % (self.valgrind_supp_filename))
+                self.cmd = ["valgrind", "--log-file=%s" % (self.valgrind_log_filename), "--xml=yes",
+                            "--xml-file=%s" % (self.valgrind_xml_filename), "--suppressions=%s" % (
+                            self.valgrind_supp_filename), "--leak-check=full"] + self.cmd
+            else:
+                logger.info("Valgrind Suppression file does not exist %s" % (os.path.dirname(self.valgrind_supp_filename)))
+                self.cmd = ["valgrind", "--log-file=%s" % (self.valgrind_log_filename), "--xml=yes",
+                            "--xml-file=%s" % (self.valgrind_xml_filename), "--leak-check=full"] + self.cmd
+	print "Asterisk binary command [%s]" % (self.cmd) 
 
         # Make the start/stop deferreds - this method will return
         # the start deferred, and pass the stop deferred to the AsteriskProtocol

Modified: asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/runtests.py
URL: http://svnview.digium.com/svn/testsuite/asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/runtests.py?view=diff&rev=4352&r1=4351&r2=4352
==============================================================================
--- asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/runtests.py (original)
+++ asterisk/team/sgriepentrog/testsuite-valgrind-nitesh/runtests.py Wed Nov 20 10:36:05 2013
@@ -18,6 +18,7 @@
 import socket
 import shutil
 import xml.dom
+import xml.dom.minidom
 import random
 
 sys.path.append("lib/python")
@@ -38,6 +39,7 @@
         self.time = 0.0
         self.test_name = test_name
         self.ast_version = ast_version
+        self.existing_ast_instances = 0
         self.options = options
         self.test_config = TestConfig(test_name, global_config)
         self.failure_message = ""
@@ -60,6 +62,14 @@
             print msg
             self.stdout += msg + "\n"
             cmd.append(str(self.ast_version).rstrip())
+            #Before running the test case, check if there exists
+            #any asterisk directories for this test case
+            ast_directories = "%s/%s" % (Asterisk.test_suite_root, self.test_name.lstrip("tests/"))
+            while True:
+                if os.path.isdir("%s/ast%d" % (ast_directories, self.existing_ast_instances+1)):
+                   self.existing_ast_instances += 1
+                else:
+                   break
             p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
             self.pid = p.pid
@@ -71,16 +81,17 @@
                 pass
             p.wait()
 
-            self.__parse_run_output(self.stdout)
-
             self.passed = (p.returncode == 0 and self.test_config.expectPass) or (p.returncode and not self.test_config.expectPass)
             core_dumps = self.__check_for_core()
             if (len(core_dumps)):
                 print "Core dumps detected; failing test"
                 self.passed = False
                 self.__archive_core_dumps(core_dumps)
+            self.__parse_valgrind_xml()
+            self.__parse_run_output(self.stdout)
             if not self.passed:
                 self.__archive_ast_logs()
+                self.__archive_ast_valgrind_logs()
                 self.__archive_pcap_dump()
             print 'Test %s %s\n' % (cmd, 'passed' if self.passed else 'failed')
 
@@ -128,7 +139,7 @@
 
     def __archive_ast_logs(self):
         ast_directories = "%s/%s" % (Asterisk.test_suite_root, self.test_name.lstrip("tests/"))
-        i = 1
+        i = self.existing_ast_instances + 1
         while True:
             if os.path.isdir("%s/ast%d" % (ast_directories, i)):
                 ast_dir = "%s/ast%d/var/log/asterisk" % (ast_directories, i)
@@ -137,9 +148,9 @@
                 if not os.path.exists(dest_dir):
                     try:
                         hardlink_or_copy(ast_dir + "/messages.txt",
-                            dest_dir + "/messages.txt")
+                                         dest_dir + "/messages.txt")
                         hardlink_or_copy(ast_dir + "/full.txt",
-                            dest_dir + "/full.txt")
+                                         dest_dir + "/full.txt")
                         if os.path.exists(ast_dir + "/mmlog"):
                             hardlink_or_copy(ast_dir + "/mmlog",
                                 dest_dir + "/mmlog")
@@ -147,6 +158,84 @@
                         print "Exception occurred while archiving logs from %s to %s: %s" % (
                             ast_dir, dest_dir, e
                         )
+            else:
+                break
+            i += 1
+
+    def __archive_ast_valgrind_logs(self):
+        print "Archiving the valgrind logs"
+        ast_directories = "%s/%s" % (Asterisk.test_suite_root,
+                                     self.test_name.lstrip("tests/"))
+        i = self.existing_ast_instances + 1
+        while True:
+            if os.path.isdir("%s/ast%d" % (ast_directories, i)):
+                src_dir = "%s/ast%d/" % (ast_directories, i)
+                dest_dir = "./logs/%s/ast%d/" % (self.test_name.lstrip(
+                                                 "tests/"), i)
+                if os.path.exists(dest_dir):
+                    try:
+                        if os.path.exists(src_dir+"/valgrind_logs.xml"):
+                            hardlink_or_copy(src_dir + "/valgrind_logs.txt", dest_dir +
+                                             "/valgrind_logs.txt")
+                        if os.path.exists(src_dir+"/valgrind_xml.xml"):
+                            hardlink_or_copy(src_dir + "/valgrind_xml.xml", dest_dir +
+                                             "/valgrind_xml.xml")
+                    except Exception, e:
+                        print "Exception occurred valgrind logs from %s to %s: %s" % (
+                            src_dir, dest_dir, e
+                        )
+            else:
+                break
+            i += 1
+
+    def __parse_valgrind_xml(self):
+        #Parse valgrind XML and calculate number of bytes definately lost
+        ast_directories = "%s/%s" % (Asterisk.test_suite_root, self.test_name.lstrip("tests/"))
+        i = self.existing_ast_instances+1
+        while True:
+            if os.path.isdir("%s/ast%d" % (ast_directories, i)):
+                ast_dir = "%s/ast%d/" % (ast_directories, i)
+                if os.path.exists(ast_dir+"/valgrind_xml.xml"):
+                    print "Found the valgrind XML"
+                    count_bytes_leaked = 0
+                    count_errors_found = 0
+                    try:
+                        xmldoc = xml.dom.minidom.parse(ast_dir+"/valgrind_xml.xml")
+                    except Exception, e:
+                        print "Exception occurred while parsing the valgrind xml [%s], will retry by adding the tag [</valgrindoutput>]" % (e)
+                        with open(ast_dir+"/valgrind_xml.xml", "a") as xmlfile:
+                            xmlfile.write("</valgrindoutput>")
+                        try:
+                            xmldoc = xml.dom.minidom.parse(ast_dir+"/valgrind_xml.xml")
+                        except Exception, e:
+                            print "Exception occurred while parsing the valgrind xml, skipping the valgrind validation %s" % (e)
+                            i += 1
+                            continue 
+                    try:
+                        error_list = xmldoc.getElementsByTagName('error')
+                        for error in error_list:
+                            count_errors_found += 1
+                            error_kind = error.getElementsByTagName('kind')
+                            if len(error_kind) == 1:
+                                kind = error_kind[0]
+                                if str(kind.childNodes[0].nodeValue) == "Leak_DefinitelyLost":
+                                    leaked_bytes = error.getElementsByTagName('leakedbytes')
+                                    if len(leaked_bytes) == 1:
+                                        dom_element = leaked_bytes[0]
+                                        count_bytes_leaked += int(dom_element.childNodes[0].nodeValue)
+                    
+                    except Exception, e:
+                        print "Exception checking elements valgrind XML: %s" % (e)
+                    if count_bytes_leaked > 0:
+                        msg = "Leak detected, leaked bytes %s" % (count_bytes_leaked)
+                        self.stdout += msg + "\n"
+                        print msg
+                        self.passed = False
+                    if count_errors_found > 0:
+                        msg = "Valgrind detected errors, error count %s" % (count_errors_found)
+                        self.stdout += msg + "\n"
+                        print msg
+                        self.passed = False
             else:
                 break
             i += 1
@@ -404,6 +493,9 @@
     parser.add_option("-L", "--list-tags", action="store_true",
             dest="list_tags", default=False,
             help="List available tags")
+    parser.add_option("--valgrind", action="store_true",
+            dest="valgrind", default=False,
+            help="Run Asterisk under valgrind")
     (options, args) = parser.parse_args(argv)
 
     ast_version = AsteriskVersion(options.version)
@@ -423,6 +515,8 @@
         test_suite.list_tags()
         return 0
 
+    if options.valgrind:
+        os.environ["VALGRIND"] = "true"
     print "Running tests for Asterisk %s ...\n" % str(ast_version)
 
     test_suite.run()
@@ -473,6 +567,5 @@
         # Try a copy instead
         shutil.copyfile(source, destination)
 
-
 if __name__ == "__main__":
     sys.exit(main() or 0)




More information about the asterisk-commits mailing list