Esempio n. 1
0
 def test_ternary(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|ternary('yes', 'no') }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "kapitan == kapitan"}
         output = "yes"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 2
0
def jinja2_render_file(search_paths, name, ctx):
    """
    Render jinja2 file name with context ctx.
    search_paths is used to find the file name
    as there is a limitation with jsonnet's native_callback approach:
    one can't access the current directory being evaluated
    """
    ctx = json.loads(ctx)
    _full_path = ""

    for path in search_paths:
        _full_path = os.path.join(path, name)
        logger.debug("jinja2_render_file trying file %s", _full_path)
        if os.path.exists(_full_path):
            logger.debug("jinja2_render_file found file at %s", _full_path)
            try:
                return render_jinja2_file(_full_path,
                                          ctx,
                                          search_paths=search_paths)
            except Exception as e:
                raise CompileError(
                    "Jsonnet jinja2 failed to render {}: {}".format(
                        _full_path, e))

    raise IOError(
        "jinja2 failed to render, could not find file: {}".format(_full_path))
Esempio n. 3
0
 def test_regex_replace(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|regex_replace(pattern='world', replacement='kapitan') }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "hello world"}
         output = "hello kapitan"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 4
0
 def test_base64_decode(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|b64decode }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "dGhpcyBhbmQgdGhhdA=="}
         output = "this and that"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 5
0
 def test_regex_findall(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|regex_findall('world.*') }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "hello world"}
         output = "['world']"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 6
0
 def test_bool(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|bool }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "yes"}
         output = "True"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 7
0
 def test_to_datetime(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|to_datetime }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "2019-03-07 13:37:00"}
         output = "2019-03-07 13:37:00"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 8
0
 def test_regex_escape(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|regex_escape }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "+s[a-z].*"}
         output = "\\+s\\[a\\-z\\]\\.\\*"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 9
0
    def test_reveal_maybe_tag_no_reveal_flag(self):
        """
        creates ?{base64:some_value} and runs reveal_maybe jinja2 filters without --reveal flag
        """
        with tempfile.NamedTemporaryFile() as f:
            f.write("{{ my_ref_tag_var|reveal_maybe }}".encode("UTF-8"))
            f.seek(0)

            # new argparse namespace with --reveal and --refs-path values
            namespace = namedtuple("Namespace", [])
            namespace.reveal = False
            namespace.refs_path = tempfile.mkdtemp()

            # reveal_maybe uses cached, so inject namespace
            cached.args["compile"] = namespace
            cached.ref_controller_obj = RefController(
                cached.args["compile"].refs_path)
            cached.revealer_obj = Revealer(cached.ref_controller_obj)

            ref_tag = "?{base64:some_value}"
            ref_value = b"sitar_rock!"
            cached.ref_controller_obj[ref_tag] = Base64Ref(ref_value)
            context = {"my_ref_tag_var": ref_tag}
            self.assertEqual(render_jinja2_file(f.name, context),
                             "?{base64:some_value}")
Esempio n. 10
0
 def test_toml(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|toml }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": {"foo": ["this", "that"]}}
         output = 'foo = [ "this", "that",]\n'
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 11
0
 def test_sha256(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{text|sha256}}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "this and that"}
         digest = 'e863c1ac42619a2b429a08775a6acd89ff4c2c6b8dae12e3461a5fa63b2f92f5'
         self.assertEqual(render_jinja2_file(f.name, context), digest)
Esempio n. 12
0
 def test_yaml(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{text|yaml}}".encode("UTF-8"))
         f.seek(0)
         context = {"text": ["this", "that"]}
         yaml = '- this\n- that\n'
         self.assertEqual(render_jinja2_file(f.name, context), yaml)
Esempio n. 13
0
    def test_reveal_maybe_b64encode_tag(self):
        """
        creates ?{base64:some_value} and runs reveal_maybe|b64encode jinja2 filters
        """
        with tempfile.NamedTemporaryFile() as f:
            f.write(
                "{{ my_ref_tag_var|reveal_maybe|b64encode }}".encode("UTF-8"))
            f.seek(0)

            # new argparse namespace with --reveal and --refs-path values
            namespace = namedtuple('Namespace', [])
            namespace.reveal = True
            namespace.refs_path = tempfile.mkdtemp()

            # reveal_maybe uses cached, so inject namespace
            cached.args['compile'] = namespace
            cached.ref_controller_obj = RefController(
                cached.args['compile'].refs_path)
            cached.revealer_obj = Revealer(cached.ref_controller_obj)

            ref_tag = '?{base64:some_value}'
            ref_value = b'sitar_rock!'
            cached.ref_controller_obj[ref_tag] = Base64Ref(ref_value)
            context = {"my_ref_tag_var": ref_tag}
            ref_value_b64 = base64.b64encode(ref_value).decode()
            self.assertEqual(render_jinja2_file(f.name, context),
                             ref_value_b64)
Esempio n. 14
0
 def test_fileglob(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|fileglob }}".encode("UTF-8"))
         f.seek(0)
         context = {"text": "./tests/*jinja2.py"}
         output = "['./tests/test_jinja2.py']"
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 15
0
 def test_shuffle(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|shuffle }}".encode("UTF-8"))
         f.seek(0)
         array = [1, 2, 3, 4, 5]
         context = {"text": array}
         self.assertNotEqual(render_jinja2_file(f.name, context), array)
Esempio n. 16
0
 def body(self):
     self.root.metadata.name = self.kwargs.name
     self.root.data["tunnel.sh"] = render_jinja2_file(
         "components/incognito/tunnel.sh.j2", {
             "rpc_port": self.kwargs.rpc_port,
             "node_port": self.kwargs.node_port,
             "public_ip": self.kwargs.public_ip
         })
Esempio n. 17
0
 def test_strftime(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ text|strftime }}".encode("UTF-8"))
         f.seek(0)
         format = "%a, %d %b %Y %H:%M"
         context = {"text": format}
         output = time.strftime(format)
         self.assertEqual(render_jinja2_file(f.name, context), output)
Esempio n. 18
0
 def test_inventory_context(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{inventory.parameters.cluster.name}}".encode("UTF-8"))
         cluster_name = "minikube"
         target_name = "minikube-es"
         inv = inventory(["examples/kubernetes"], target_name)
         context = {"inventory": inv}
         f.seek(0)
         self.assertEqual(render_jinja2_file(f.name, context), cluster_name)
Esempio n. 19
0
 def test_inventory_global_context(self):
     with tempfile.NamedTemporaryFile() as f:
         target_name = "minikube-es"
         f.write("{{inventory_global[\"%s\"].parameters.cluster.name}}".encode("UTF-8") % target_name.encode("UTF-8"))
         cluster_name = "minikube"
         inv_global = inventory("examples/kubernetes", None)
         context = {"inventory_global": inv_global}
         f.seek(0)
         self.assertEqual(render_jinja2_file(f.name, context), cluster_name)
Esempio n. 20
0
 def test_custom_filter_jinja2(self):
     with tempfile.NamedTemporaryFile() as f:
         f.write("{{ inventory.parameters.cluster.name | custom_jinja2_filter }}".encode("UTF-8"))
         cluster_name = "minikube"
         target_name = "minikube-es"
         inv = inventory(["examples/kubernetes"], target_name)
         context = {"inventory": inv}
         f.seek(0)
         actual_output = render_jinja2_file(
             f.name, context, "./examples/kubernetes/lib/custom_jinja2_filter.py"
         )
         expected_output = base64_encode(cluster_name)
         self.assertEqual(actual_output, expected_output)
Esempio n. 21
0
def jinja2_render_file(search_path, name, ctx):
    """
    Render jinja2 file name with context ctx.
    search_path is used to find the file name
    as there is a limitation with jsonnet's native_callback approach:
    one can't access the current directory being evaluated
    """
    ctx = json.loads(ctx)
    _full_path = os.path.join(search_path, name)
    logger.debug("jinja2_render_file trying file %s", _full_path)
    if os.path.exists(_full_path):
        logger.debug("jinja2_render_file found file at %s", _full_path)
        return render_jinja2_file(_full_path, ctx)
    # default IOError if we reach here
    raise IOError("Could not find file %s" % name)
Esempio n. 22
0
def jinja2_render_file(search_path, name, ctx):
    """
    Render jinja2 file name with context ctx.
    search_path is used to find the file name
    as there is a limitation with jsonnet's native_callback approach:
    one can't access the current directory being evaluated
    """
    ctx = json.loads(ctx)
    _full_path = os.path.join(search_path, name)
    logger.debug("jinja2_render_file trying file %s", _full_path)
    try:
        if os.path.exists(_full_path):
            logger.debug("jinja2_render_file found file at %s", _full_path)
            return render_jinja2_file(_full_path, ctx)
        else:
            raise IOError("Could not find file %s" % name)
    except Exception as e:
        logger.error("Jsonnet jinja2 failed to render %s: %s", _full_path, str(e))
        raise CompileError(e)
Esempio n. 23
0
    def test_reveal_maybe_no_tag(self):
        """
        runs reveal_maybe jinja2 filter on data without ref tags
        """
        with tempfile.NamedTemporaryFile() as f:
            f.write("{{ my_var|reveal_maybe }}".encode("UTF-8"))
            f.seek(0)

            # new argparse namespace with --reveal and --refs-path values
            namespace = namedtuple("Namespace", [])
            namespace.reveal = True
            namespace.refs_path = tempfile.mkdtemp()

            # reveal_maybe uses cached, so inject namespace
            cached.args["compile"] = namespace
            cached.ref_controller_obj = RefController(cached.args["compile"].refs_path)
            cached.revealer_obj = Revealer(cached.ref_controller_obj)

            var_value = "heavy_rock!"
            context = {"my_var": var_value}
            self.assertEqual(render_jinja2_file(f.name, context), var_value)
def j2(filename, ctx):
    return render_jinja2_file(filename, ctx, search_paths=search_paths)