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
This commit is contained in:
Pau Espin 2020-06-04 18:44:42 +02:00
parent 4ddcdaf62c
commit 166dc10769
9 changed files with 181 additions and 1 deletions

View File

@ -619,5 +619,109 @@ PASS: test_suite (pass: 1, skip: 6)
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.

View File

@ -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]

View File

@ -105,6 +105,13 @@ print(repr(s.reserved_resources))
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)

View File

@ -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

View File

@ -0,0 +1,6 @@
resources:
ip_address:
- addr: 10.42.42.2 # stp
defaults:
timeout: 60s

View File

@ -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)

View File

@ -31,6 +31,8 @@ def default_templates_dir():
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:

View File

@ -35,7 +35,6 @@ class OsmoStp(log.Origin):
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 @@ class OsmoStp(log.Origin):
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)

View File

@ -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 @@ class TestEnv(log_module.Origin):
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 @@ class TestEnv(log_module.Origin):
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: