_TEMPLATES_DIR = _get_templates_dir()
-def _import_lua_template() -> Template:
+def template_from_str(template: str) -> Template:
ldr = FileSystemLoader(_TEMPLATES_DIR)
env = Environment(trim_blocks=True, lstrip_blocks=True, loader=ldr)
+ return env.from_string(template)
+
+
+def _import_lua_template() -> Template:
path = os.path.join(_TEMPLATES_DIR, "config.lua.j2")
with open(path, "r", encoding="UTF-8") as file:
template = file.read()
- return env.from_string(template)
+ return template_from_str(template)
_MAIN_TEMPLATE = _import_lua_template()
return obj.dns64
def render_lua(self) -> Text:
- return _MAIN_TEMPLATE.render(cfg=self)
+ lua = _MAIN_TEMPLATE.render(cfg=self)
+ print(lua)
+ return lua
{# Return server address or table of server addresses #}
{% macro tls_servers_table(servers) -%}
-{%- if servers is string -%}
-'{{ servers|string }}', {{ tls_server_auth(servers) }}
-{%- else-%}
{
{%- for item in servers -%}
{%- if item.address -%}
-{'{{ item.address|string }}', {{ tls_server_auth(item) }} },
+{'{{ item.address|string }}',{{ tls_server_auth(item) }}},
{%- else -%}
'{{ item|string }}',
{%- endif -%}
{%- endfor -%}
}
-{%- endif -%}
{%- endmacro %}
{% macro tls_server_auth(server) -%}
{%- for pin in server.pin_sha256 -%}
'{{ pin|string }}',
{%- endfor -%}
-},
+}
{%- endif -%}
{%- endif -%}
{%- endmacro %}
--- /dev/null
+from knot_resolver_manager.datamodel.config_schema import template_from_str
+from knot_resolver_manager.datamodel.forward_zone import ForwardServerSchema
+
+
+def test_string_table():
+ s = "any string"
+ t = [s, "other string"]
+ tmpl_str = """{% from 'macros/common_macros.lua.j2' import string_table %}
+{{ string_table(x) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=s) == f"'{s}'"
+ assert tmpl.render(x=t) == f"{{'{s}','{t[1]}',}}"
+
+
+def test_str2ip_table():
+ s = "2001:DB8::d0c"
+ t = [s, "192.0.2.1"]
+ tmpl_str = """{% from 'macros/common_macros.lua.j2' import str2ip_table %}
+{{ str2ip_table(x) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=s) == f"kres.str2ip('{s}')"
+ assert tmpl.render(x=t) == f"{{kres.str2ip('{s}'),kres.str2ip('{t[1]}'),}}"
+
+
+def test_qtype_table():
+ s = "AAAA"
+ t = [s, "TXT"]
+ tmpl_str = """{% from 'macros/common_macros.lua.j2' import qtype_table %}
+{{ qtype_table(x) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=s) == f"kres.type.{s}"
+ assert tmpl.render(x=t) == f"{{kres.type.{s},kres.type.{t[1]},}}"
+
+
+def test_servers_table():
+ s = "2001:DB8::d0c"
+ t = [s, "192.0.2.1"]
+ tmpl_str = """{% from 'macros/common_macros.lua.j2' import servers_table %}
+{{ servers_table(x) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=s) == f"'{s}'"
+ assert tmpl.render(x=t) == f"{{'{s}','{t[1]}',}}"
+ assert tmpl.render(x=[{"address": s}, {"address": t[1]}]) == f"{{'{s}','{t[1]}',}}"
+
+
+def test_tls_servers_table():
+ d = ForwardServerSchema(
+ {"address": "2001:DB8::d0c", "hostname": "res.example.com", "ca-file": "/etc/knot-resolver/tlsca.crt"}
+ )
+ t = [d, ForwardServerSchema({"address": "192.0.2.1", "pin-sha256": "YQ=="})]
+ tmpl_str = """{% from 'macros/common_macros.lua.j2' import tls_servers_table %}
+{{ tls_servers_table(x) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(x=[d.address, t[1].address]) == f"{{'{d.address}','{t[1].address}',}}"
+ assert (
+ tmpl.render(x=t)
+ == f"{{{{'{d.address}',hostname='{d.hostname}',ca_file='{d.ca_file}',}},{{'{t[1].address}',pin_sha256='{t[1].pin_sha256}',}},}}"
+ )