[Asterisk-code-review] runtests.py: Handle pjproject-bundled pjsua python bindings (testsuite[master])

Anonymous Coward asteriskteam at digium.com
Wed Mar 16 18:21:26 CDT 2016


Anonymous Coward #1000019 has submitted this change and it was merged.

Change subject: runtests.py:  Handle pjproject-bundled pjsua python bindings
......................................................................


runtests.py:  Handle pjproject-bundled pjsua python bindings

When run with bundled pjproject, the testsuite needs a way to find the python
pjsua bindings installed in astdatadir/third-party/project. Since astdatadir
could be a system installed location like /var/lib/asterisk or in
./astroot/var/lib/asterisk or someplace entirely different, we need to grab
the asterisk.conf file in $AST_TEST_ROOT/etc/asterisk and retrieve it's
astdatadir variable. This is the same logic that the asterisk.Asterisk module
uses. So now pjproject_lib = $AST_TEST_ROOT/<astdatadir>/third-party/pjproject.

Now that we have the path, we need to get it into sys.path.  For runtest.py
itself, we can just sys.path.insert(0, pjproject_lib).  This puts it at the
front of the search order so it'll override any system-installed pjproject. The
tests run in a separate process though so adding to sys.path in runtests.py
doesn't do them any good.  So additionally, we also now update the PYTHONPATH
environment variable with 'lib/python' and pjproject_lib so the test processes
inherit it.

BTW, resetting PYTHONPATH eliminates the need to explicitly add lib/python to
sys.path in any custom python used by the tests.  A future patch can remove
them and any future needs for adding module search paths can be handled
right in runtests.py.

Change-Id: Ib0464c8507da7b030b887abfeef89e5bd2c03cf2
---
M lib/python/asterisk/test_suite_utils.py
M runtests.py
2 files changed, 62 insertions(+), 0 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Anonymous Coward #1000019: Verified
  Joshua Colp: Looks good to me, approved



diff --git a/lib/python/asterisk/test_suite_utils.py b/lib/python/asterisk/test_suite_utils.py
index d32d7d7..d8ef541 100644
--- a/lib/python/asterisk/test_suite_utils.py
+++ b/lib/python/asterisk/test_suite_utils.py
@@ -19,6 +19,7 @@
 from os import remove
 from shutil import move
 from tempfile import mkstemp
+from config import ConfigFile
 
 LOGGER = logging.getLogger(__name__)
 
@@ -170,3 +171,36 @@
             return addr
 
     return None
+
+def get_asterisk_conf():
+    localtest_root = os.getenv("AST_TEST_ROOT")
+    if localtest_root:
+        # The default etc directory for Asterisk
+        default_etc_directory = os.path.join(localtest_root, "etc/asterisk")
+    else:
+        # The default etc directory for Asterisk
+        default_etc_directory = "/etc/asterisk"
+
+    # Find the system installed asterisk.conf
+    ast_confs = [
+        os.path.join(default_etc_directory, "asterisk.conf"),
+        "/usr/local/etc/asterisk/asterisk.conf",
+    ]
+    _ast_conf = None
+    for config in ast_confs:
+        if os.path.exists(config):
+            _ast_conf = ConfigFile(config)
+            break
+    if _ast_conf is None:
+        msg = "Unable to locate asterisk.conf in any known location"
+        LOGGER.error(msg)
+        raise Exception(msg)
+
+    # Get the Asterisk directories from the Asterisk config file
+    _ast_conf.directories = {};
+    for cat in _ast_conf.categories:
+        if cat.name == "directories":
+            for (var, val) in cat.options:
+                _ast_conf.directories[var] = val
+
+    return _ast_conf
diff --git a/runtests.py b/runtests.py
index 9e0feb5..5b0b651 100755
--- a/runtests.py
+++ b/runtests.py
@@ -39,15 +39,42 @@
 if not os.path.isdir("logs"):
     os.mkdir("logs")
 
+# The current sys.path is only used by runtests.py
 sys.path.append("lib/python")
+# The tests themselves are run in a separate process
+# so we're going to accumulate additional paths in
+# new_PYTHONPATH to pass to the new process.
+# Since we're going to replace the current PYTHONPATH
+# environment variable, we need to save it to our
+# new_PYTHONPATH.
+new_PYTHONPATH=[]
+if os.getenv("PYTHONPATH"):
+    new_PYTHONPATH.append(os.getenv("PYTHONPATH"))
+new_PYTHONPATH.append("lib/python")
 
 from asterisk.version import AsteriskVersion
 from asterisk.asterisk import Asterisk
 from asterisk.test_config import TestConfig
 from mailer import send_email
+from asterisk import test_suite_utils
 
 TESTS_CONFIG = "tests.yaml"
 TEST_RESULTS = "asterisk-test-suite-report.xml"
+
+# If using embedded pjproject, we need to add the
+# astdatadir/third-party/pjproject directory to sys.path
+# so pjsua can be found.
+ast_config = test_suite_utils.get_asterisk_conf()
+astdatadir = ast_config.directories["astdatadir"] or '/var/lib/asterisk'
+if astdatadir[0] == os.path.sep:
+     astdatadir = astdatadir[1:]
+
+pjproject_lib = os.path.join(os.getenv("AST_TEST_ROOT") or os.path.sep, astdatadir, "third-party/pjproject")
+if os.path.exists(pjproject_lib):
+    # runtests.py needs pjproject_lib for the dependency checks.
+    sys.path.append(pjproject_lib)
+    # And of course, the tests need it.
+    new_PYTHONPATH.append(pjproject_lib)
 
 # If True, abandon the current running TestRun. Used by SIGTERM.
 abandon_test = False
@@ -86,6 +113,7 @@
         self.did_run = True
         start_time = time.time()
         os.environ['TESTSUITE_ACTIVE_TEST'] = self.test_name
+        os.environ['PYTHONPATH'] = os.pathsep.join(new_PYTHONPATH)
         cmd = [
             "%s/run-test" % self.test_name,
         ]

-- 
To view, visit https://gerrit.asterisk.org/2355
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib0464c8507da7b030b887abfeef89e5bd2c03cf2
Gerrit-PatchSet: 3
Gerrit-Project: testsuite
Gerrit-Branch: master
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>



More information about the asterisk-code-review mailing list