pespin has submitted this change. ( 
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.ok
M selftest/suite_test/suite_test.ok.ign
M selftest/suite_test/suite_test.py
A selftest/suite_test/suitedirB/suiteC/mytemplatedir/.gitignore
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
9 files changed, 181 insertions(+), 1 deletion(-)

Approvals:
  Jenkins Builder: Verified
  pespin: Looks good to me, approved



diff --git a/selftest/suite_test/suite_test.ok 
b/selftest/suite_test/suite_test.ok
index fee07d0..a7c6422 100644
--- a/selftest/suite_test/suite_test.ok
+++ b/selftest/suite_test/suite_test.ok
@@ -619,5 +619,109 @@
     skip: test_fail.py
     skip: test_fail_raise.py
     pass: test_suite_params.py (N.N sec)
+- test with template overlay
+cnf suiteC: DBG: reading suite.conf  [suite.py:[LINENR]]
+
+---------------------------------------------------------------------
+trial suiteC
+---------------------------------------------------------------------
+tst suiteC: reserving resources in 
[PATH]/selftest/suite_test/test_work/state_dir ...  [suite.py:[LINENR]]
+tst suiteC: DBG: {combining='resources'}  [suite.py:[LINENR]]
+tst {combining_scenarios='resources'}: DBG: 
{definition_conf={ip_address=[{'addr': '10.42.42.2'}]}}  
[suiteC↪{combining_scenarios='resources'}]  [suite.py:[LINENR]]
+tst suiteC: DBG: {combining='modifiers'}  [suite.py:[LINENR]]
+tst {combining_scenarios='modifiers'}: DBG: {definition_conf={}}  
[suiteC↪{combining_scenarios='modifiers'}]  [suite.py:[LINENR]]
+tst suiteC: Reserving 1 x ip_address (candidates: 3)  [resource.py:[LINENR]]
+tst suiteC: DBG: Picked - _hash: fd103b22c7cf2480d609150e06f4bbd92ac78d8c
+  addr: 10.42.42.2
+  [resource.py:[LINENR]]
+
+----------------------------------------------
+trial suiteC test_template_overlay.py
+----------------------------------------------
+tst test_template_overlay.py:[LINENR]: - Testing: expect to fail on invalid 
templates overlay dir  [suiteC↪test_template_overlay.py:[LINENR]]  
[test_template_overlay.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]: sucess: setting non-existing templates 
dir raised RuntimeError  [suiteC↪test_template_overlay.py:[LINENR]]  
[test_template_overlay.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]:   
[suiteC↪test_template_overlay.py:[LINENR]]  [test_template_overlay.py:[LINENR]]
+--- ReservedResources: DBG: requesting use of ip_address {specifics={}}  
[resource.py:[LINENR]]
+tst suiteC: Using 1 x ip_address (candidates: 1)  [resource.py:[LINENR]]
+tst suiteC: DBG: Picked - _hash: fd103b22c7cf2480d609150e06f4bbd92ac78d8c
+  _reserved_by: suiteC-[ID_NUM]-[ID_NUM]
+  addr: 10.42.42.2
+  [resource.py:[LINENR]]
+--- ReservedResources: DBG: {available=1}  [resource.py:[LINENR]]
+--- ReservedResources: DBG: 
{using={_hash='fd103b22c7cf2480d609150e06f4bbd92ac78d8c', 
_reserved_by='suiteC-[ID_NUM]-[ID_NUM]', addr='10.42.42.2'}}  
[resource.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]: - Testing: original template  
[suiteC↪test_template_overlay.py:[LINENR]]  [test_template_overlay.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: 
{config_file='test_trial_tmp/test_run_6/suiteC/test_template_overlay.py/osmo-stp_10.42.42.2/osmo-stp.cfg'}
  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+tst suiteC: DBG: {combining='config'}  [suite.py:[LINENR]]
+tst {combining_scenarios='config'}: DBG: {definition_conf={}}  
[suiteC↪{combining_scenarios='config'}]  [suite.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: STP CONFIG:
+{'stp': {'ip_address': {'_hash': 'fd103b22c7cf2480d609150e06f4bbd92ac78d8c',
+                        '_reserved_by': 'suiteC-[ID_NUM]-[ID_NUM]',
+                        '_used': True,
+                        'addr': '10.42.42.2'}}}  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+cnf Templates: DBG: rendering osmo-stp.cfg.tmpl  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪Templates]  
[template.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: ! Configuration rendered by osmo-gsm-tester
+log stderr
+ logging filter all 1
+ logging color 1
+ logging print category 1
+ logging print extended-timestamp 1
+ logging level set-all debug
+line vty
+ no login
+ bind 10.42.42.2
+!ctrl
+! bind 10.42.42.2
+cs7 instance 0
+ xua rkm routing-key-allocation dynamic-permitted
+ listen m3ua 2905
+  accept-asp-connections dynamic-permitted
+  local-ip 10.42.42.2
+  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]: - Testing:overlay template  
[suiteC↪test_template_overlay.py:[LINENR]]  [test_template_overlay.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]: - After creating the new template, 
still old template is used  [suiteC↪test_template_overlay.py:[LINENR]]  
[test_template_overlay.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: 
{config_file='test_trial_tmp/test_run_6/suiteC/test_template_overlay.py/osmo-stp_10.42.42_2.2/osmo-stp.cfg'}
  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: STP CONFIG:
+{'stp': {'ip_address': {'_hash': 'fd103b22c7cf2480d609150e06f4bbd92ac78d8c',
+                        '_reserved_by': 'suiteC-[ID_NUM]-[ID_NUM]',
+                        '_used': True,
+                        'addr': '10.42.42.2'}}}  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+cnf Templates: DBG: rendering osmo-stp.cfg.tmpl  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪Templates]  
[template.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: ! Configuration rendered by osmo-gsm-tester
+log stderr
+ logging filter all 1
+ logging color 1
+ logging print category 1
+ logging print extended-timestamp 1
+ logging level set-all debug
+line vty
+ no login
+ bind 10.42.42.2
+!ctrl
+! bind 10.42.42.2
+cs7 instance 0
+ xua rkm routing-key-allocation dynamic-permitted
+ listen m3ua 2905
+  accept-asp-connections dynamic-permitted
+  local-ip 10.42.42.2
+  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+tst test_template_overlay.py:[LINENR]: - New template is used after 
re-generating cache with set_overlay_template_dir:  
[suiteC↪test_template_overlay.py:[LINENR]]  [test_template_overlay.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: 
{config_file='test_trial_tmp/test_run_6/suiteC/test_template_overlay.py/osmo-stp_10.42.42_3.2/osmo-stp.cfg'}
  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: STP CONFIG:
+{'stp': {'ip_address': {'_hash': 'fd103b22c7cf2480d609150e06f4bbd92ac78d8c',
+                        '_reserved_by': 'suiteC-[ID_NUM]-[ID_NUM]',
+                        '_used': True,
+                        'addr': '10.42.42.2'}}}  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+cnf Templates: DBG: rendering osmo-stp.cfg.tmpl  
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪Templates]  
[template.py:[LINENR]]
+run osmo-stp_10.42.42.2: DBG: ! Overlay Config file genreated by test
+line vty
+ no login
+ bind 10.42.42.2
+          
[suiteC↪test_template_overlay.py:[LINENR]↪test_template_overlay.py↪osmo-stp_10.42.42.2]
  [stp_osmo.py:[LINENR]]
+tst test_template_overlay.py:[LINENR] Test passed (N.N sec)  
[suiteC↪test_template_overlay.py]  [test.py:[LINENR]]
+---------------------------------------------------------------------
+trial suiteC PASS
+---------------------------------------------------------------------
+PASS: suiteC (pass: 1)
+    pass: test_template_overlay.py (N.N sec)

 - graceful exit.
diff --git a/selftest/suite_test/suite_test.ok.ign 
b/selftest/suite_test/suite_test.ok.ign
index 9bd168f..460da92 100644
--- a/selftest/suite_test/suite_test.ok.ign
+++ b/selftest/suite_test/suite_test.ok.ign
@@ -3,3 +3,4 @@
 \([0-9.]+ sec\)        (N.N sec)
 {combining_scenarios='resources', scenario='foo'}:.*   
{combining_scenarios='resources', scenario='foo'}: [RESOURCE_DICT]
 test_suite-[0-9]*-[0-9]*       test_suite-[ID_NUM]-[ID_NUM]
+suiteC-[0-9]*-[0-9]*   suiteC-[ID_NUM]-[ID_NUM]
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/mytemplatedir/.gitignore 
b/selftest/suite_test/suitedirB/suiteC/mytemplatedir/.gitignore
new file mode 100644
index 0000000..1e2640b
--- /dev/null
+++ b/selftest/suite_test/suitedirB/suiteC/mytemplatedir/.gitignore
@@ -0,0 +1,6 @@
+# For 'test_template_overlay.py', it's fine having this directory completely
+# empy. However, we need a file in this directory so it can be stored in git.
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore
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: 3
Gerrit-Owner: pespin <pes...@sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin <pes...@sysmocom.de>
Gerrit-MessageType: merged

Reply via email to