diff --git a/tests/ci/rally-gate/index.mako b/rally/ui/templates/ci/index.mako similarity index 100% rename from tests/ci/rally-gate/index.mako rename to rally/ui/templates/ci/index.mako diff --git a/tests/ci/rally-gate/index_verify.mako b/rally/ui/templates/ci/index_verify.mako similarity index 100% rename from tests/ci/rally-gate/index_verify.mako rename to rally/ui/templates/ci/index_verify.mako diff --git a/rally/ui/utils.py b/rally/ui/utils.py index 4858c77875..c60ff2865d 100644 --- a/rally/ui/utils.py +++ b/rally/ui/utils.py @@ -13,50 +13,24 @@ # License for the specific language governing permissions and limitations # under the License. -from __future__ import print_function import os.path -import re -import sys -HELP_MESSAGE = ( - "Usage:\n\t" - "utils.py render " - "[= = ...]\n\n\t" - "Where key-1,value-1 and key-2,value-2 are key pairs of template.") - - -def get_template(template_path): +def get_mako_template(template): import mako.lookup - - templates_dir = os.path.join(os.path.dirname(__file__), "templates") - - lookup_dirs = [ - templates_dir, - os.path.abspath(os.path.join(templates_dir, "..", "..", "..")) - ] - - lookup = mako.lookup.TemplateLookup(directories=lookup_dirs) - try: - return lookup.get_template(template_path) - except mako.exceptions.TopLevelLookupException as e: - raise ValueError(e) + dirs = os.path.join(os.path.dirname(__file__), "templates") + lookup = mako.lookup.TemplateLookup(directories=[dirs]) + return lookup.get_template(template) -def main(*args): - if (len(args) < 2 or args[0] != "render" - or not all(re.match("^[^=]+=[^=]+$", arg) for arg in args[2:])): - print(HELP_MESSAGE, file=sys.stderr) - return 1 - - try: - render_kwargs = dict([arg.split("=") for arg in args[2:]]) - print(get_template(args[1]).render(**render_kwargs)) - except ValueError as e: - print(str(e), file=sys.stderr) - return 1 - return 0 +def get_jinja_template(template): + import jinja2 + env = jinja2.Environment(loader=jinja2.PackageLoader("rally.ui", + "templates")) + return env.get_template(template) -if __name__ == "__main__": - sys.exit(main(*sys.argv[1:])) +def get_template(template): + if template.endswith(".mako"): + return get_mako_template(template) + return get_jinja_template(template) diff --git a/tests/ci/rally-gate.py b/tests/ci/rally-gate.py index d096222e61..858b23f79e 100755 --- a/tests/ci/rally-gate.py +++ b/tests/ci/rally-gate.py @@ -27,6 +27,8 @@ import tempfile from six.moves.urllib import parse +from rally.ui import utils + def use_keystone_v3(): """Alter deployment to use keystone v3.""" @@ -178,11 +180,9 @@ def main(): else: print("Ignoring file %s" % fname) print("Exit statuses: %r" % statuses) - - run(["python", rally_root + "/rally/ui/utils.py", "render", - "tests/ci/rally-gate/index.mako"], - gzip=False, stdout="rally-plot/extra/index.html") - + template = utils.get_template("ci/index.mako") + with open("rally-plot/extra/index.html", "w") as output: + output.write(template.render()) return any(statuses) diff --git a/tests/ci/rally-gate.sh b/tests/ci/rally-gate.sh index f75c31571e..271578b379 100755 --- a/tests/ci/rally-gate.sh +++ b/tests/ci/rally-gate.sh @@ -63,8 +63,7 @@ rally show keypairs rally -v --rally-debug task start --task $TASK $TASK_ARGS mkdir -p rally-plot/extra -python $BASE/new/rally/rally/ui/utils.py render\ - tests/ci/rally-gate/index.mako > rally-plot/extra/index.html +python $BASE/new/rally/tests/ci/render.py ci/index.mako > rally-plot/extra/index.html cp $TASK rally-plot/task.txt tar -czf rally-plot/plugins.tar.gz -C $RALLY_PLUGINS_DIR . rally task results | python -m json.tool > rally-plot/results.json diff --git a/tests/ci/rally-verify.sh b/tests/ci/rally-verify.sh index c97cfa69fb..25c1cac4d1 100755 --- a/tests/ci/rally-verify.sh +++ b/tests/ci/rally-verify.sh @@ -93,8 +93,8 @@ function main { gzip -9 ${OUTPUT_FILE} done - python $BASE/new/rally/rally/ui/utils.py render\ - tests/ci/rally-gate/index_verify.mako ${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html + python $BASE/new/rally/tests/ci/render.py ci/index_verify.mako \ + ${RESULTS[*]}> ${RESULTS_DIR}/extra/index.html if [[ ${RESULTS[*]} == *"fail"* ]] then diff --git a/tests/ci/render.py b/tests/ci/render.py new file mode 100644 index 0000000000..a08723f886 --- /dev/null +++ b/tests/ci/render.py @@ -0,0 +1,37 @@ +# Copyright 2015: Mirantis Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from __future__ import print_function +import re +import sys + +from rally.ui import utils + + +HELP_MESSAGE = ( + "Usage:\n\t" + "render.py ci/template.mako" + "[= = ...]\n\n\t" + "Where key-1,value-1 and key-2,value-2 are key pairs of template.") + + +if __name__ == "__main__": + args = sys.argv + if (len(args) < 1 or not all(re.match("^[^=]+=[^=]+$", + arg) for arg in args[2:])): + print(HELP_MESSAGE, file=sys.stderr) + sys.exit(1) + render_kwargs = dict([arg.split("=") for arg in args[2:]]) + print(utils.get_template(args[1]).render(**render_kwargs)) diff --git a/tests/unit/ui/test_utils.py b/tests/unit/ui/test_utils.py index 12c95a2ee6..dca7365c50 100644 --- a/tests/unit/ui/test_utils.py +++ b/tests/unit/ui/test_utils.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import mako import mock from rally.ui import utils @@ -22,29 +21,32 @@ from tests.unit import test class ModuleTestCase(test.TestCase): - def test_get_template(self): - self.assertIsInstance(utils.get_template("task/report.mako"), - mako.template.Template) + def test_get_mako_template(self): + try: + import mako + except ImportError: + self.skip("No mako module. Skipping test.") + template = utils.get_mako_template("ci/index.mako") + self.assertIsInstance(template, mako.template.Template) - def test_get_template_raises(self): - self.assertRaises(ValueError, utils.get_template, "absent_template") + def test_get_jinja_template(self): + try: + import jinja2 + except ImportError: + self.skip("Jinja not installed. Skipping test.") + self.assertRaises(jinja2.exceptions.TemplateNotFound, + utils.get_jinja_template, "nonexistent") - @mock.patch("rally.ui.utils.get_template") - def test_main(self, mock_get_template): - self.assertEqual(0, utils.main("render", "somepath", "a=1", "b=2")) + @mock.patch("rally.ui.utils.get_mako_template") + def test_get_template_mako(self, mock_get_mako_template): + mock_get_mako_template.return_value = "fake_template" + template = utils.get_mako_template("template.mako") + self.assertEqual("fake_template", template) + mock_get_mako_template.assert_called_once_with("template.mako") - mock_get_template.assert_called_once_with("somepath") - mock_get_template.return_value.render.assert_called_once_with( - a="1", b="2" - ) - - @mock.patch("rally.ui.utils.print", create=True) - @mock.patch("rally.ui.utils.sys.stderr") - def test_main_bad_input(self, mock_stderr, mock_print): - self.assertTrue(utils.HELP_MESSAGE.startswith("Usage:")) - for args in ([], ["not_a_render"], ["render"], - ["render", "expected_arg", "unexpected_arg"]): - self.assertEqual(1, utils.main(*args)) - mock_print.assert_called_once_with(utils.HELP_MESSAGE, - file=mock_stderr) - mock_print.reset_mock() + @mock.patch("rally.ui.utils.get_jinja_template") + def test_get_template_jinja(self, mock_get_jinja_template): + mock_get_jinja_template.return_value = "fake_template" + template = utils.get_jinja_template("template.html") + self.assertEqual("fake_template", template) + mock_get_jinja_template.assert_called_once_with("template.html")