diff options
author | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-05-09 11:24:23 +0200 |
---|---|---|
committer | Pau Espin Pedrol <pespin@sysmocom.de> | 2018-05-09 11:24:25 +0200 |
commit | 155a355e01d63669912ad0cfe2a1969b486373fb (patch) | |
tree | 37f64ba24801d6a533d474c86797714ee811242f | |
parent | 415f34d3e9916a9af0ef6a69186ef7cce980c32c (diff) |
Unload suite local modules after suite exit to avoid collisions
Since sys.path is modified idynamically to load modules from "lib" subdir of each suite, from python env point of view all those modules share a namespace. As a result, there can be name collisions.
If a name collision appears (eg test1 loads "testlib.py" and test2
afterwards also loads its own "testlib.py"), then python interpreter
thinks the testlib.py module is already loaded, so test2 ends up using
"testlib.py" from test1.
The way to solve this is to make suite local
modules to live only through the scope of the suite, and unload the
modules once the suite is finished.
Change-Id: I4efe815f85bc4ec2ca91aa9c2d3a369048f21571
-rw-r--r-- | src/osmo_gsm_tester/suite.py | 23 | ||||
-rwxr-xr-x | suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py | 2 | ||||
-rwxr-xr-x | suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py | 2 | ||||
-rwxr-xr-x | suites/voice/mo_mt_call_tchf.py | 2 | ||||
-rwxr-xr-x | suites/voice/mo_mt_call_tchh.py | 2 |
5 files changed, 31 insertions, 0 deletions
diff --git a/src/osmo_gsm_tester/suite.py b/src/osmo_gsm_tester/suite.py index 44aabbd..76cd248 100644 --- a/src/osmo_gsm_tester/suite.py +++ b/src/osmo_gsm_tester/suite.py @@ -70,6 +70,7 @@ class SuiteRun(log.Origin): resources_pool = None reserved_resources = None objects_to_clean_up = None + test_import_modules_to_clean_up = [] _resource_requirements = None _config = None _processes = None @@ -102,6 +103,27 @@ class SuiteRun(log.Origin): except Exception: log.log_exn() + def test_import_modules_register_for_cleanup(self, mod): + ''' + Tests are required to call this API for any module loaded from its own + lib subdir, because they are loaded in the global namespace. Otherwise + later tests importing modules with the same name will re-use an already + loaded module. + ''' + if mod not in self.test_import_modules_to_clean_up: + self.dbg('registering module %r for cleanup' % mod) + self.test_import_modules_to_clean_up.append(mod) + + def test_import_modules_cleanup(self): + while self.test_import_modules_to_clean_up: + mod = self.test_import_modules_to_clean_up.pop() + try: + self.dbg('Cleaning up module %r' % mod) + del sys.modules[mod.__name__] + del mod + except Exception: + log.log_exn() + def mark_start(self): self.start_timestamp = time.time() self.duration = 0 @@ -179,6 +201,7 @@ class SuiteRun(log.Origin): self.objects_cleanup() self.free_resources() MainLoop.unregister_poll_func(self.poll) + self.test_import_modules_cleanup() util.import_path_remove(suite_libdir) self.duration = time.time() - self.start_timestamp diff --git a/suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py b/suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py index f1771b9..82e357c 100755 --- a/suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py +++ b/suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 from osmo_gsm_tester.testenv import * +import testlib +suite.test_import_modules_register_for_cleanup(testlib) from testlib import call_test_setup_run def my_bts_setup(bts): diff --git a/suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py b/suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py index c7fe0c3..1424da8 100755 --- a/suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py +++ b/suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 from osmo_gsm_tester.testenv import * +import testlib +suite.test_import_modules_register_for_cleanup(testlib) from testlib import call_test_setup_run def my_bts_setup(bts): diff --git a/suites/voice/mo_mt_call_tchf.py b/suites/voice/mo_mt_call_tchf.py index a640a97..af55dfd 100755 --- a/suites/voice/mo_mt_call_tchf.py +++ b/suites/voice/mo_mt_call_tchf.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 from osmo_gsm_tester.testenv import * +import testlib +suite.test_import_modules_register_for_cleanup(testlib) from testlib import call_test_setup_run def my_bts_setup(bts): diff --git a/suites/voice/mo_mt_call_tchh.py b/suites/voice/mo_mt_call_tchh.py index e6f6b98..43d0760 100755 --- a/suites/voice/mo_mt_call_tchh.py +++ b/suites/voice/mo_mt_call_tchh.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 from osmo_gsm_tester.testenv import * +import testlib +suite.test_import_modules_register_for_cleanup(testlib) from testlib import call_test_setup_run def my_bts_setup(bts): |