[MERGED] osmo-gsm-tester[master]: Unload suite local modules after suite exit to avoid collisions

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed May 9 09:48:31 UTC 2018


Pau Espin Pedrol has submitted this change and it was merged.

Change subject: Unload suite local modules after suite exit to avoid collisions
......................................................................


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
---
M src/osmo_gsm_tester/suite.py
M suites/dyn_ts_ipa/mo_mt_call_dyn_ipa.py
M suites/dyn_ts_osmo/mo_mt_call_dyn_osmo.py
M suites/voice/mo_mt_call_tchf.py
M suites/voice/mo_mt_call_tchh.py
5 files changed, 31 insertions(+), 0 deletions(-)

Approvals:
  Pau Espin Pedrol: Looks good to me, approved
  Jenkins Builder: Verified



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 @@
     resources_pool = None
     reserved_resources = None
     objects_to_clean_up = None
+    test_import_modules_to_clean_up = []
     _resource_requirements = None
     _config = None
     _processes = None
@@ -99,6 +100,27 @@
             obj = self.objects_to_clean_up.pop()
             try:
                 obj.cleanup()
+            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()
 
@@ -179,6 +201,7 @@
             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):

-- 
To view, visit https://gerrit.osmocom.org/8084
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I4efe815f85bc4ec2ca91aa9c2d3a369048f21571
Gerrit-PatchSet: 1
Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list