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