pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18662 )


Change subject: testenv: Support test overlaying a directory to look for 
templates
......................................................................

testenv: Support test overlaying a directory to look for templates

This way tests which require a very specific config file can override
specific template files used by object classes.

Change-Id: I65d1b1e826d2d430ee83810d998b98d0ccaa07cd
---
M selftest/suite_test/suite_test.py
A selftest/suite_test/suitedirB/suiteC/suite.conf
A selftest/suite_test/suitedirB/suiteC/test_template_overlay.py
M src/osmo_gsm_tester/core/template.py
M src/osmo_gsm_tester/obj/stp_osmo.py
M src/osmo_gsm_tester/testenv.py
6 files changed, 70 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester 
refs/changes/62/18662/1

diff --git a/selftest/suite_test/suite_test.py 
b/selftest/suite_test/suite_test.py
index 4b32439..260b9c4 100755
--- a/selftest/suite_test/suite_test.py
+++ b/selftest/suite_test/suite_test.py
@@ -105,6 +105,13 @@
 results = s.run_tests('test_suite_params.py')
 print(report.suite_to_text(s))

+print('- test with template overlay')
+trial = FakeTrial()
+s_def = suite.load('suiteC')
+s = suite.SuiteRun(trial, 'suiteC', s_def)
+results = s.run_tests('test_template_overlay.py')
+print(report.suite_to_text(s))
+
 print('\n- graceful exit.')
 #deleting generated tmp trial dir:
 shutil.rmtree(example_trial_dir, ignore_errors=True)
diff --git a/selftest/suite_test/suitedirB/suiteC/suite.conf 
b/selftest/suite_test/suitedirB/suiteC/suite.conf
new file mode 100644
index 0000000..44be3fc
--- /dev/null
+++ b/selftest/suite_test/suitedirB/suiteC/suite.conf
@@ -0,0 +1,6 @@
+resources:
+  ip_address:
+  - addr: 10.42.42.2 # stp
+
+defaults:
+  timeout: 60s
diff --git a/selftest/suite_test/suitedirB/suiteC/test_template_overlay.py 
b/selftest/suite_test/suitedirB/suiteC/test_template_overlay.py
new file mode 100644
index 0000000..2dd9378
--- /dev/null
+++ b/selftest/suite_test/suitedirB/suiteC/test_template_overlay.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+from osmo_gsm_tester.testenv import *
+
+import os
+import sys
+
+print('- Testing: expect to fail on invalid templates overlay dir')
+try:
+    #stp.configure()
+    tenv.set_overlay_template_dir(os.path.join(os.path.dirname(__file__), 
'nonexistent-templatedir'))
+    sys.stderr.write('Error: setting non-existing templates dir should raise 
RuntimeError\n')
+    assert(False)
+except RuntimeError:
+    print('sucess: setting non-existing templates dir raised RuntimeError\n')
+    pass
+
+mytemplatedir = os.path.join(os.path.dirname(__file__), 'mytemplatedir')
+tenv.set_overlay_template_dir(mytemplatedir)
+
+stp = tenv.stp()
+print('- Testing: original template')
+stp.configure()
+
+print('- Testing:overlay template')
+mytemplatefile = os.path.join(mytemplatedir, 'osmo-stp.cfg.tmpl')
+try:
+    with open(mytemplatefile, 'w') as f:
+        r = """! Overlay Config file genreated by test
+line vty
+ no login
+ bind ${stp.ip_address.addr}
+        """
+        f.write(r)
+
+    # After creating the new template, it won\'t be used until
+    # set_overlay_template_dir() is called again because the templates are
+    # somehow cached by mako.
+    print('- After creating the new template, still old template is used' )
+    stp.configure()
+    print('- New template is used after re-generating cache with 
set_overlay_template_dir:')
+    tenv.set_overlay_template_dir(mytemplatedir)
+    stp.configure()
+finally:
+    os.remove(mytemplatefile)
diff --git a/src/osmo_gsm_tester/core/template.py 
b/src/osmo_gsm_tester/core/template.py
index 82985ef..8178c13 100644
--- a/src/osmo_gsm_tester/core/template.py
+++ b/src/osmo_gsm_tester/core/template.py
@@ -31,6 +31,8 @@
     return os.path.join(os.path.dirname(os.path.dirname(__file__)), 
'templates')

 def set_templates_dir(*templates_dirs):
+    '''Set a lit of directories to look for templates. It must be called
+       everytime a template file is updated.'''
     global _lookup
     global _logger
     if not templates_dirs:
diff --git a/src/osmo_gsm_tester/obj/stp_osmo.py 
b/src/osmo_gsm_tester/obj/stp_osmo.py
index 1382016..83a2f75 100644
--- a/src/osmo_gsm_tester/obj/stp_osmo.py
+++ b/src/osmo_gsm_tester/obj/stp_osmo.py
@@ -35,7 +35,6 @@

     def start(self):
         self.log('Starting osmo-stp')
-        self.run_dir = 
util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
         self.configure()

         inst = 
util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-stp')))
@@ -61,6 +60,7 @@
         self.process.launch()

     def configure(self):
+        self.run_dir = 
util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
         self.config_file = self.run_dir.new_file('osmo-stp.cfg')
         self.dbg(config_file=self.config_file)

diff --git a/src/osmo_gsm_tester/testenv.py b/src/osmo_gsm_tester/testenv.py
index f3ac02e..7ca854b 100644
--- a/src/osmo_gsm_tester/testenv.py
+++ b/src/osmo_gsm_tester/testenv.py
@@ -24,6 +24,7 @@
 import sys

 from .core import process
+from .core import template
 from .core import log as log_module
 from .core import process as process_module
 from .core import resource
@@ -139,6 +140,7 @@
         self.suite_run.reserved_resources.put_all()
         MainLoop.unregister_poll_func(self.poll)
         self.test_import_modules_cleanup()
+        self.set_overlay_template_dir(None)

     def config_suite_specific(self):
         return self.suite_run.config_suite_specific()
@@ -146,6 +148,14 @@
     def config_test_specific(self):
         return 
self.suite_run.config_suite_specific().get(self._test.module_name(), {})

+    def set_overlay_template_dir(self, template_dir=None):
+        '''Overlay a directory on top of default one when looking for
+           directories. It must be called everytime a template file is 
updated.'''
+        if template_dir is None:
+            template.set_templates_dir(template.default_templates_dir())
+        else:
+            template.set_templates_dir(template_dir, 
template.default_templates_dir())
+
     def prompt(self, *msgs, **msg_details):
         'ask for user interaction. Do not use in tests that should run 
automatically!'
         if msg_details:

--
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/18662
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: I65d1b1e826d2d430ee83810d998b98d0ccaa07cd
Gerrit-Change-Number: 18662
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pes...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to