add mk22-lab-dvr
diff --git a/tcp_tests/helpers/env_config.py b/tcp_tests/helpers/env_config.py
index 3ad9a36..84ed286 100644
--- a/tcp_tests/helpers/env_config.py
+++ b/tcp_tests/helpers/env_config.py
@@ -14,17 +14,20 @@
# TODO(slebedev): implement unit tests
+import collections
import copy
-import json
+import os
import re
-from devops.helpers import templates
+#from devops.helpers import templates
+from devops import error
+import json
import yaml
from tcp_tests.helpers import exceptions
+from tcp_tests.helpers import utils
from tcp_tests import logger
-
LOG = logger.logger
@@ -312,7 +315,67 @@
filename
)
)
- self.config = templates.yaml_template_load(filename)
+
+ #self.config = templates.yaml_template_load(filename)
+ self.config = yaml_template_load(filename)
else:
LOG.error("Template filename is not set, loading config " +
"from template aborted.")
+
+
+def yaml_template_load(config_file):
+ """Temporary moved from fuel_devops to use jinja2"""
+ dirname = os.path.dirname(config_file)
+
+ class TemplateLoader(yaml.Loader):
+ pass
+
+ def yaml_include(loader, node):
+ file_name = os.path.join(dirname, node.value)
+ if not os.path.isfile(file_name):
+ raise error.DevopsError(
+ "Cannot load the environment template {0} : include file {1} "
+ "doesn't exist.".format(dirname, file_name))
+ inputfile = utils.render_template(file_name)
+ return yaml.load(inputfile, TemplateLoader)
+
+ def yaml_get_env_variable(loader, node):
+ if not node.value.strip():
+ raise error.DevopsError(
+ "Environment variable is required after {tag} in "
+ "{filename}".format(tag=node.tag, filename=loader.name))
+ node_value = node.value.split(',', 1)
+ # Get the name of environment variable
+ env_variable = node_value[0].strip()
+
+ # Get the default value for environment variable if it exists in config
+ if len(node_value) > 1:
+ default_val = node_value[1].strip()
+ else:
+ default_val = None
+
+ value = os.environ.get(env_variable, default_val)
+ if value is None:
+ raise error.DevopsError(
+ "Environment variable {var} is not set from shell"
+ " environment! No default value provided in file "
+ "{filename}".format(var=env_variable, filename=loader.name))
+
+ return yaml.load(value, TemplateLoader)
+
+ def construct_mapping(loader, node):
+ loader.flatten_mapping(node)
+ return collections.OrderedDict(loader.construct_pairs(node))
+
+ if not os.path.isfile(config_file):
+ raise error.DevopsError(
+ "Cannot load the environment template {0} : file "
+ "doesn't exist.".format(config_file))
+
+ TemplateLoader.add_constructor("!include", yaml_include)
+ TemplateLoader.add_constructor("!os_env", yaml_get_env_variable)
+ TemplateLoader.add_constructor(
+ yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)
+
+ f = utils.render_template(config_file)
+ return yaml.load(f, TemplateLoader)
diff --git a/tcp_tests/helpers/utils.py b/tcp_tests/helpers/utils.py
index f913544..129c06a 100644
--- a/tcp_tests/helpers/utils.py
+++ b/tcp_tests/helpers/utils.py
@@ -19,6 +19,7 @@
import time
import traceback
+import jinja2
import paramiko
import yaml
from devops.helpers import helpers
@@ -317,6 +318,49 @@
self.write_content()
+def render_template(file_path):
+ required_env_vars = set()
+ optional_env_vars = dict()
+ def os_env(var_name, default=None):
+ var = os.environ.get(var_name, default)
+
+ if var is None:
+ raise Exception("Environment variable '{0}' is undefined!".format(var_name))
+
+ if default is None:
+ required_env_vars.add(var_name)
+ else:
+ optional_env_vars[var_name] = default
+
+ return var
+
+ options = {
+ 'os_env': os_env,
+ }
+ LOG.info("Reading template {0}".format(file_path))
+
+ path, filename = os.path.split(file_path)
+ environment = jinja2.Environment(
+ loader=jinja2.FileSystemLoader([path, os.path.dirname(path)], followlinks=True))
+ template = environment.get_template(filename).render(options)
+
+ if required_env_vars:
+ LOG.info("Required environment variables:")
+ for var in required_env_vars:
+ LOG.info(" {0}".format(var))
+ if optional_env_vars:
+ LOG.info("Optional environment variables:")
+ for var, default in sorted(optional_env_vars.iteritems()):
+ LOG.info(" {0} , default value = {1}".format(var, default))
+ return template
+
+
+def read_template(file_path):
+ """Read yaml as a jinja template"""
+ template = render_template(file_path)
+ return yaml.load(template)
+
+
def extract_name_from_mark(mark):
"""Simple function to extract name from pytest mark