From 7a42b7bb8189a4815953913c0c58dccc06f83cba Mon Sep 17 00:00:00 2001 From: Alina Quereilhac Date: Thu, 31 Mar 2011 14:53:25 +0200 Subject: [PATCH] decorator added to test_util for detecting usable ns3 library --- test/lib/test_util.py | 41 ++++++++++++++++++++++++++++++++++-- test/testbeds/ns3/execute.py | 4 ++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/test/lib/test_util.py b/test/lib/test_util.py index 25fc0cc1..1f321c4f 100644 --- a/test/lib/test_util.py +++ b/test/lib/test_util.py @@ -1,8 +1,8 @@ #!/usr/bin/env python -# vim:ts=4:sw=4:et:ai:sts=4 -import sys import nepi.util.environ +import imp +import sys # Unittest from Python 2.6 doesn't have these decorators def _bannerwrap(f, text): @@ -19,6 +19,43 @@ def skipUnless(cond, text): def skipIf(cond, text): return (lambda f: _bannerwrap(f, text)) if cond else lambda f: f +def ns3_bindings_path(): + if "NEPI_NS3BINDINGS" in os.environ: + return os.environ["NEPI_NS3BINDINGS"] + return None + +def ns3_library_path(): + if "NEPI_NS3LIBRARY" in os.environ: + return os.environ["NEPI_NS3LIBRARY"] + return None + +def autoconfig_ns3_backend(conf): + if ns3_bindings_path(): + conf.set_attribute_value("Ns3Bindings", ns3_bindings_path()) + if ns3_library_path(): + conf.set_attribute_value("Ns3Library", ns3_library_path()) + +def ns3_usable(): + if ns3_library_path(): + try: + ctypes.CDLL(ns3_library_path(), ctypes.RTLD_GLOBAL) + except: + return False + if ns3_bindings_path(): + sys.path.insert(0, ns3_bindings_path()) + + try: + found = imp.find_module('ns3') + module = imp.load_module('ns3', *found) + except ImportError: + return False + finally: + if ns3_bindings_path(): + del sys.path[0] + + return True + + def find_bin(name, extra_path = None): search = [] if "PATH" in os.environ: diff --git a/test/testbeds/ns3/execute.py b/test/testbeds/ns3/execute.py index 5e2f01f5..40587555 100755 --- a/test/testbeds/ns3/execute.py +++ b/test/testbeds/ns3/execute.py @@ -14,6 +14,8 @@ class Ns3ExecuteTestCase(unittest.TestCase): def setUp(self): self.root_dir = tempfile.mkdtemp() + @test_util.skipUnless(test_util.ns3_usable(), + "Test requires working ns-3 bindings") def test_run_ping_if(self): testbed_version = "3_9_RC3" instance = ns3.TestbedInstance(testbed_version) @@ -77,6 +79,8 @@ class Ns3ExecuteTestCase(unittest.TestCase): instance.stop() instance.shutdown() + @test_util.skipUnless(test_util.ns3_usable(), + "Test requires working ns-3 bindings") def test_run_ping_routing(self): testbed_version = "3_9_RC3" instance = ns3.TestbedInstance(testbed_version) -- 2.43.0