--- /dev/null
+import pytest
+
+from knot_resolver_manager.datamodel.cache_schema import PredictionSchema, PrefillSchema
+from knot_resolver_manager.datamodel.config_schema import template_from_str
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ [
+ PrefillSchema(
+ {
+ "origin": ".",
+ "url": "https://www.internic.net/domain/root.zone",
+ }
+ )
+ ],
+ """prefill.config({
+ ['.'] = {
+ url = 'https://www.internic.net/domain/root.zone',
+ interval = 86400,
+ }
+})""",
+ ),
+ (
+ [
+ PrefillSchema(
+ {
+ "origin": ".",
+ "url": "https://www.internic.net/domain/root.zone",
+ "refresh-interval": "12h",
+ "ca-file": "/etc/pki/tls/certs/ca-bundle.crt",
+ }
+ )
+ ],
+ """prefill.config({
+ ['.'] = {
+ url = 'https://www.internic.net/domain/root.zone',
+ interval = 43200,
+ ca_file = '/etc/pki/tls/certs/ca-bundle.crt',
+ }
+})""",
+ ),
+ ],
+)
+def test_prefill_config(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/cache_macros.lua.j2' import prefill_config %}" "{{ prefill_config(config) }}"
+ )
+ assert tmpl.render(config=val, negation=False).replace(" ", "") == lua.replace(" ", "")
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ PredictionSchema(),
+ """predict.config({
+ window = 15,
+ period = 24,
+})""",
+ ),
+ (
+ PredictionSchema(
+ {
+ "window": "60m",
+ }
+ ),
+ """predict.config({
+ window = 60,
+ period = 24,
+})""",
+ ),
+ (
+ PredictionSchema({"window": "60m", "period": 48}),
+ """predict.config({
+ window = 60,
+ period = 48,
+})""",
+ ),
+ ],
+)
+def test_predict_config(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/cache_macros.lua.j2' import predict_config %}" "{{ predict_config(config) }}"
+ )
+ assert tmpl.render(config=val, negation=False) == lua
+import pytest
+
from knot_resolver_manager.datamodel.config_schema import template_from_str
from knot_resolver_manager.datamodel.forward_schema import ForwardServerSchema
-def test_boolean():
- tmpl_str = """{% from 'macros/common_macros.lua.j2' import boolean %}
-{{ boolean(x) }}"""
-
- tmpl = template_from_str(tmpl_str)
- assert tmpl.render(x=True) == "true"
- assert tmpl.render(x=False) == "false"
-
-
-def test_boolean_neg():
- tmpl_str = """{% from 'macros/common_macros.lua.j2' import boolean %}
-{{ boolean(x,true) }}"""
-
- tmpl = template_from_str(tmpl_str)
- assert tmpl.render(x=True) == "false"
- assert tmpl.render(x=False) == "true"
-
+@pytest.mark.parametrize(
+ "val",
+ ["string", 55, 5.5, True, None],
+)
+def test_quotes(val):
+ tmpl = template_from_str("{% from 'macros/common_macros.lua.j2' import quotes %}{{ quotes(string) }}")
+ assert tmpl.render(string=val) == f"'{val}'"
-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]}',}}"
+@pytest.mark.parametrize(
+ "val,lua",
+ [(True, "true"), (False, "false"), (1, "true"), (0, "false")],
+)
+def test_boolean(val, lua):
+ tmpl = template_from_str("{% from 'macros/common_macros.lua.j2' import boolean %}{{ boolean(bool, negation) }}")
+ assert tmpl.render(bool=val, negation=False) == lua
+ assert tmpl.render(bool=not bool(val), negation=True) == lua
-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) }}"""
+# MODULES
- 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]},}}"
+@pytest.mark.parametrize(
+ "val",
+ ["module_name_to_load"],
+)
+def test_modules_load(val):
+ tmpl = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import modules_load %}{{ modules_load(module_name) }}"
+ )
+ assert tmpl.render(module_name=val) == f"modules.load('{val}')"
-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]}',}}"
+@pytest.mark.parametrize(
+ "val",
+ ["module_name_to_unload"],
+)
+def test_modules_unload(val):
+ tmpl = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import modules_unload %}{{ modules_unload(module_name) }}"
+ )
+ assert tmpl.render(module_name=val) == f"modules.unload('{val}')"
-def test_tls_servers_table():
- d = ForwardServerSchema(
- # the ca-file is a dummy, because it's existence is checked
- {"address": ["2001:DB8::d0c"], "hostname": "res.example.com", "ca-file": "/etc/passwd"}
+@pytest.mark.parametrize("val,name", [(True, "module_name_to_load"), (False, "module_name_to_unload")])
+def test_module_loader(val, name):
+ tmpl = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import module_loader %}{{ module_loader(bool, module_name) }}"
+ )
+ lua = tmpl.render(bool=val, module_name=name)
+ if val is True:
+ assert lua == f"modules.load('{name}')"
+ elif val is False:
+ assert lua == f"modules.load('{name}')\nmodules.unload('{name}')"
+
+
+# TABLES
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ ("string", "'string'"),
+ (["s1", "s2", "s3"], "{'s1','s2','s3',}"),
+ ],
+)
+def test_table_or_string(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import table_or_string %}{{ table_or_string(val) }}"
)
- t = [
- d,
- ForwardServerSchema(
- {
- "address": "192.0.2.1",
- "pin-sha256": "OTJmODU3ZDMyOWMwOWNlNTU4Y2M0YWNjMjI5NWE2NWJlMzY4MzRmMzY3NGU3NDAwNTI1YjMxZTMxYTgzMzQwMQ==",
- }
- ),
- ]
- 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}',}}}},}}"
+ assert tmpl.render(val=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ ("2001:DB8::d0c", "kres.str2ip('2001:DB8::d0c')"),
+ (["2001:DB8::d0c", "192.0.2.1"], "{kres.str2ip('2001:DB8::d0c'),kres.str2ip('192.0.2.1'),}"),
+ ],
+)
+def test_table_or_str2ip(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/common_macros.lua.j2' import table_or_str2ip %}{{ table_or_str2ip(val) }}"
)
+ assert tmpl.render(val=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ ("AAAA", "kres.type.AAAA"),
+ (["AAAA", "TXT"], "{kres.type.AAAA,kres.type.TXT,}"),
+ ],
+)
+def test_table_or_qtype(val, lua):
+ tmpl = template_from_str("{% from 'macros/common_macros.lua.j2' import table_or_qtype %}{{ table_or_qtype(val) }}")
+ assert tmpl.render(val=val) == lua
--- /dev/null
+import pytest
+
+from knot_resolver_manager.datamodel.config_schema import template_from_str
+from knot_resolver_manager.datamodel.dns64_schema import Dns64Schema
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ Dns64Schema({"prefix": "64:ff9b::/96"}),
+ """dns64.config({
+ prefix = '64:ff9b::',
+})""",
+ ),
+ (
+ Dns64Schema({"prefix": "64:ff9b::/96", "rev-ttl": "5s", "exclude-subnets": ["2001:db8:888::/48"]}),
+ """dns64.config({
+ prefix = '64:ff9b::',
+ rev_ttl = 5,
+ exclude_subnets = {'2001:db8:888::/48',},
+})""",
+ ),
+ (
+ Dns64Schema({"prefix": "64:ff9b::/96", "rev-ttl": "1m", "exclude-subnets": ["2001:db8:888::/48", "::/0"]}),
+ """dns64.config({
+ prefix = '64:ff9b::',
+ rev_ttl = 60,
+ exclude_subnets = {'2001:db8:888::/48','::/0',},
+})""",
+ ),
+ ],
+)
+def test_dns64_config(val, lua):
+ tmpl = template_from_str("{% from 'macros/dns64_macros.lua.j2' import dns64_config %}{{ dns64_config(config) }}")
+ assert tmpl.render(config=val) == lua
--- /dev/null
+import pytest
+
+from knot_resolver_manager.datamodel.config_schema import template_from_str
+from knot_resolver_manager.datamodel.dnssec_schema import TrustAnchorFileSchema
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ [". 3600 IN DS 19036 8 2 49AAC11...", ". 3600 IN DS 19036 8 2 49AAC11..."],
+ """trust_anchors.add('. 3600 IN DS 19036 8 2 49AAC11...')
+trust_anchors.add('. 3600 IN DS 19036 8 2 49AAC11...')\n""",
+ )
+ ],
+)
+def test_trust_anchors(val, lua):
+ tmpl = template_from_str("{% from 'macros/dnssec_macros.lua.j2' import trust_anchors %}" "{{ trust_anchors(tas) }}")
+ assert tmpl.render(tas=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ ["bad.boy", "example.com"],
+ """trust_anchors.set_insecure({
+ 'bad.boy',
+ 'example.com',
+})""",
+ )
+ ],
+)
+def test_negative_trust_anchors(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/dnssec_macros.lua.j2' import negative_trust_anchors %}" "{{ negative_trust_anchors(ntas) }}"
+ )
+ assert tmpl.render(ntas=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ [
+ TrustAnchorFileSchema({"file": "/path/to/tafile", "read-only": True}),
+ TrustAnchorFileSchema({"file": "/path/to/another/tafile"}),
+ ],
+ """trust_anchors.add_file('/path/to/tafile', readonly = true)
+trust_anchors.add_file('/path/to/another/tafile', readonly = false)\n""",
+ )
+ ],
+)
+def test_trust_anchors_files(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/dnssec_macros.lua.j2' import trust_anchors_files %}" "{{ trust_anchors_files(tafs) }}"
+ )
+ assert tmpl.render(tafs=val) == lua
--- /dev/null
+import pytest
+
+from knot_resolver_manager.datamodel.config_schema import template_from_str
+from knot_resolver_manager.datamodel.dnssec_schema import TrustAnchorFileSchema
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ [". 3600 IN DS 19036 8 2 49AAC11...", ". 3600 IN DS 19036 8 2 49AAC11..."],
+ """trust_anchors.add('. 3600 IN DS 19036 8 2 49AAC11...')
+trust_anchors.add('. 3600 IN DS 19036 8 2 49AAC11...')\n""",
+ )
+ ],
+)
+def test_trust_anchors(val, lua):
+ tmpl = template_from_str("{% from 'macros/dnssec_macros.lua.j2' import trust_anchors %}" "{{ trust_anchors(tas) }}")
+ assert tmpl.render(tas=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ ["bad.boy", "example.com"],
+ """trust_anchors.set_insecure({
+ 'bad.boy',
+ 'example.com',
+})""",
+ )
+ ],
+)
+def test_negative_trust_anchors(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/dnssec_macros.lua.j2' import negative_trust_anchors %}" "{{ negative_trust_anchors(ntas) }}"
+ )
+ assert tmpl.render(ntas=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ [
+ TrustAnchorFileSchema({"file": "/path/to/tafile", "read-only": True}),
+ TrustAnchorFileSchema({"file": "/path/to/another/tafile"}),
+ ],
+ """trust_anchors.add_file('/path/to/tafile', readonly = true)
+trust_anchors.add_file('/path/to/another/tafile', readonly = false)\n""",
+ )
+ ],
+)
+def test_trust_anchors_files(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/dnssec_macros.lua.j2' import trust_anchors_files %}" "{{ trust_anchors_files(tafs) }}"
+ )
+ assert tmpl.render(tafs=val) == lua
+import pytest
+
from knot_resolver_manager.datamodel.config_schema import template_from_str
-from knot_resolver_manager.datamodel.forward_schema import ForwardSchema
-from knot_resolver_manager.datamodel.types import IPAddressOptionalPort
+from knot_resolver_manager.datamodel.forward_schema import ForwardOptionsSchema, ForwardSchema, ForwardServerSchema
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ ForwardOptionsSchema(),
+ """{
+ dnssec = true,
+ auth = false
+}""",
+ ),
+ (
+ ForwardOptionsSchema({"dnssec": False, "authoritative": True}),
+ """{
+ dnssec = false,
+ auth = true
+}""",
+ ),
+ ],
+)
+def test_forward_options(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/forward_macros.lua.j2' import forward_options %}" "{{ forward_options(options) }}"
+ )
+ assert tmpl.render(options=val) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ (
+ "2001:148f:fffe::1",
+ "tls",
+ "odvr.nic.cz",
+ "/path/to/ca-file.crt",
+ ),
+ """{
+ '2001:148f:fffe::1',
+ tls = true,
+ hostname = 'odvr.nic.cz',
+ ca_file = '/path/to/ca-file.crt',
+}""",
+ ),
+ (
+ (
+ "2001:148f:fffe::1",
+ None,
+ None,
+ None,
+ ),
+ """{
+ '2001:148f:fffe::1',
+ tls = false,
+}""",
+ ),
+ ],
+)
+def test_forward_server_config(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/forward_macros.lua.j2' import forward_server_config %}"
+ "{{ forward_server_config(address, transport, hostname, pin_sha256, ca_file) }}"
+ )
+ assert tmpl.render(address=val[0], transport=val[1], hostname=val[2], pin_sha256=None, ca_file=val[3]) == lua
+
+
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ ("185.43.135.1", "{ '185.43.135.1' },"),
+ (
+ ForwardServerSchema(
+ {
+ "address": "2001:148f:fffe::1",
+ "transport": "tls",
+ "hostname": "odvr.nic.cz",
+ "ca-file": "/path/to/ca-file.crt",
+ }
+ ),
+ """{
+ '2001:148f:fffe::1',
+ tls = true,
+ hostname = 'odvr.nic.cz',
+ ca_file = '/path/to/ca-file.crt',
+},
+""",
+ ),
+ (
+ ForwardServerSchema(
+ {
+ "address": ["2001:148f:fffe::1", "185.43.135.1"],
+ "transport": "tls",
+ "hostname": "odvr.nic.cz",
+ "ca-file": "/path/to/ca-file.crt",
+ }
+ ),
+ """{
+ '2001:148f:fffe::1',
+ tls = true,
+ hostname = 'odvr.nic.cz',
+ ca_file = '/path/to/ca-file.crt',
+},
+{
+ '185.43.135.1',
+ tls = true,
+ hostname = 'odvr.nic.cz',
+ ca_file = '/path/to/ca-file.crt',
+},
+""",
+ ),
+ ],
+)
+def test_forward_server(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/forward_macros.lua.j2' import forward_server %}" "{{ forward_server(server) }}"
+ )
+ assert tmpl.render(server=val) == lua
def test_policy_rule_forward_add():
- tmpl_str = """{% from 'macros/forward_macros.lua.j2' import policy_rule_forward_add %}
-{{ policy_rule_forward_add(rule.subtree[0],rule.options,rule.servers) }}"""
-
- rule = ForwardSchema(
- {
- "subtree": ".",
- "servers": [{"address": ["2001:148f:fffe::1", "185.43.135.1"], "hostname": "odvr.nic.cz"}],
- "options": {
- "authoritative": False,
- "dnssec": True,
- },
- }
+ tmpl = template_from_str(
+ "{% from 'macros/forward_macros.lua.j2' import policy_rule_forward_add %}"
+ "{{ policy_rule_forward_add(subtree, options, servers) }}"
)
- result = "policy.rule_forward_add('.',{dnssec=true,auth=false},{{'2001:148f:fffe::1',tls=false,hostname='odvr.nic.cz',},{'185.43.135.1',tls=false,hostname='odvr.nic.cz',},})"
- tmpl = template_from_str(tmpl_str)
- assert tmpl.render(rule=rule) == result
+ lua = """policy.rule_forward_add(
+ 'subtree',
+ options,
+ servers
+)"""
+
+ assert tmpl.render(subtree="subtree", options="options", servers="servers") == lua
+
+
+# @pytest.mark.parametrize(
+# "val,lua",
+# [
+# (
+# ForwardSchema(
+# {
+# "subtree": ".",
+# "servers": [
+# {
+# "address": ["2001:148f:fffe::1", "185.43.135.1"],
+# "transport": "tls",
+# "hostname": "odvr.nic.cz",
+# }
+# ],
+# "options": {
+# "authoritative": False,
+# "dnssec": True,
+# },
+# }
+# ),
+# """policy.rule_forward_add(
+# '.',
+# {
+# dnssec = true,
+# auth = false,
+# },
+# {
+# {
+# '2001:148f:fffe::1',
+# tls = true,
+# hostname = 'odvr.nic.cz',
+# },
+# {
+# '185.43.135.1',
+# tls = true,
+# hostname = 'odvr.nic.cz',
+# },
+# },
+# )""",
+# ),
+# (
+# ForwardSchema(
+# {
+# "subtree": ".",
+# "servers": ["2001:148f:fffe::1", "185.43.135.1"],
+# "options": {
+# "authoritative": False,
+# "dnssec": True,
+# },
+# }
+# ),
+# """policy.rule_forward_add(
+# '.',
+# {
+# dnssec = true,
+# auth = false,
+# },
+# {
+# '2001:148f:fffe::1',
+# '185.43.135.1',
+# },
+# )""",
+# ),
+# ],
+# )
+# def test_policy_rule_forward_add(val, lua):
+# tmpl_str = """{% from 'macros/forward_macros.lua.j2' import forward %}
+# {{ forward(fwd) }}"""
- rule.servers = [IPAddressOptionalPort("2001:148f:fffe::1"), IPAddressOptionalPort("185.43.135.1")]
- result = "policy.rule_forward_add('.',{dnssec=true,auth=false},{{'2001:148f:fffe::1'},{'185.43.135.1'},})"
- assert tmpl.render(rule=rule) == result
+# tmpl = template_from_str(tmpl_str)
+# assert tmpl.render(fwd=val) == lua
+import pytest
+
from knot_resolver_manager.datamodel.config_schema import template_from_str
from knot_resolver_manager.datamodel.network_schema import ListenSchema
-def test_network_listen():
- tmpl_str = """{% from 'macros/network_macros.lua.j2' import network_listen %}
-{{ network_listen(listen) }}"""
- tmpl = template_from_str(tmpl_str)
-
- soc = ListenSchema({"unix-socket": "/tmp/kresd-socket", "kind": "dot"})
- assert tmpl.render(listen=soc) == "net.listen('/tmp/kresd-socket',nil,{kind='tls',freebind=false})\n"
- soc_list = ListenSchema({"unix-socket": [soc.unix_socket.to_std()[0], "/tmp/kresd-socket2"], "kind": "dot"})
- assert (
- tmpl.render(listen=soc_list)
- == "net.listen('/tmp/kresd-socket',nil,{kind='tls',freebind=false})\n"
- + "net.listen('/tmp/kresd-socket2',nil,{kind='tls',freebind=false})\n"
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ ("2001:DB8::d0c", "'2001:DB8::d0c'"),
+ (["2001:DB8::d0c", "192.0.2.1"], "{'2001:DB8::d0c','192.0.2.1',}"),
+ ],
+)
+def test_table_or_server(val, lua):
+ tmpl = template_from_str(
+ "{% from 'macros/network_macros.lua.j2' import table_or_server %}" "{{ table_or_server(val) }}"
)
+ assert tmpl.render(val=val) == lua
- ip = ListenSchema({"interface": "::1@55", "freebind": True})
- assert tmpl.render(listen=ip) == "net.listen('::1',55,{kind='dns',freebind=true})\n"
- ip_list = ListenSchema({"interface": [ip.interface.to_std()[0], "127.0.0.1@5353"]})
- assert (
- tmpl.render(listen=ip_list)
- == "net.listen('::1',55,{kind='dns',freebind=false})\n"
- + "net.listen('127.0.0.1',5353,{kind='dns',freebind=false})\n"
- )
- intrfc = ListenSchema({"interface": "eth0", "kind": "doh2"})
- assert tmpl.render(listen=intrfc) == "net.listen(net.eth0,443,{kind='doh2',freebind=false})\n"
- intrfc_list = ListenSchema({"interface": [intrfc.interface.to_std()[0], "lo"], "port": 5555, "kind": "doh2"})
- assert (
- tmpl.render(listen=intrfc_list)
- == "net.listen(net.eth0,5555,{kind='doh2',freebind=false})\n"
- + "net.listen(net.lo,5555,{kind='doh2',freebind=false})\n"
- )
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (
+ ListenSchema({"unix-socket": "/tmp/kresd-socket", "kind": "dot"}),
+ """net.listen('/tmp/kresd-socket', nil, {
+ kind = 'tls',
+ freebind = false,
+})
+""",
+ ),
+ (
+ ListenSchema({"unix-socket": ["/tmp/kresd-socket", "/tmp/kresd-socket2"], "kind": "dot"}),
+ """net.listen('/tmp/kresd-socket', nil, {
+ kind = 'tls',
+ freebind = false,
+})
+net.listen('/tmp/kresd-socket2', nil, {
+ kind = 'tls',
+ freebind = false,
+})
+""",
+ ),
+ (
+ ListenSchema({"interface": "::1@55", "freebind": True}),
+ """net.listen('::1', 55, {
+ kind = 'dns',
+ freebind = true,
+})
+""",
+ ),
+ (
+ ListenSchema({"interface": ["::1@55", "127.0.0.1@5353"]}),
+ """net.listen('::1', 55, {
+ kind = 'dns',
+ freebind = false,
+})
+net.listen('127.0.0.1', 5353, {
+ kind = 'dns',
+ freebind = false,
+})
+""",
+ ),
+ (
+ ListenSchema({"interface": "eth0", "kind": "doh2"}),
+ """net.listen(net.eth0, 443, {
+ kind = 'doh2',
+ freebind = false,
+})
+""",
+ ),
+ (
+ ListenSchema({"interface": ["eth0", "lo"], "port": 5555, "kind": "doh2"}),
+ """net.listen(net.eth0, 5555, {
+ kind = 'doh2',
+ freebind = false,
+})
+net.listen(net.lo, 5555, {
+ kind = 'doh2',
+ freebind = false,
+})
+""",
+ ),
+ ],
+)
+def test_network_listen(val, lua):
+ tmpl_str = """{% from 'macros/network_macros.lua.j2' import network_listen %}
+{{ network_listen(listen) }}"""
+
+ tmpl = template_from_str(tmpl_str)
+ assert tmpl.render(listen=val) == lua
-from typing import Any
+from typing import Any, List
import pytest
from knot_resolver_manager.datamodel.view_schema import ViewOptionsSchema, ViewSchema
-def test_view_flags():
- tmpl_str = """{% from 'macros/view_macros.lua.j2' import view_flags %}
-{{ view_flags(options) }}"""
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (None, "0"),
+ (
+ ["udp53", "tcp53", "dot", "doh", "doq"],
+ "0 + 2^C.KR_PROTO_UDP53 + 2^C.KR_PROTO_TCP53 + 2^C.KR_PROTO_DOT + 2^C.KR_PROTO_DOH + 2^C.KR_PROTO_DOQ",
+ ),
+ ],
+)
+def test_view_protocols(val, lua):
+ tmpl_str = """{% from 'macros/view_macros.lua.j2' import view_protocols %}
+{{ view_protocols(protocols) }}"""
tmpl = template_from_str(tmpl_str)
- options = ViewOptionsSchema({"dns64": False, "minimize": False})
- assert tmpl.render(options=options) == '"NO_MINIMIZE","DNS64_DISABLE",'
- assert tmpl.render(options=ViewOptionsSchema()) == ""
+ assert tmpl.render(protocols=val) == lua
-def test_view_answer():
+@pytest.mark.parametrize(
+ "val,lua",
+ [
+ (ViewOptionsSchema({"dns64": False, "minimize": False}), '"NO_MINIMIZE",\n"DNS64_DISABLE",\n'),
+ (ViewOptionsSchema({"minimize": False}), '"NO_MINIMIZE",\n'),
+ (ViewOptionsSchema({"dns64": False}), '"DNS64_DISABLE",\n'),
+ (ViewOptionsSchema(), ""),
+ ],
+)
+def test_view_options(val, lua):
tmpl_str = """{% from 'macros/view_macros.lua.j2' import view_options_flags %}
{{ view_options_flags(options) }}"""
tmpl = template_from_str(tmpl_str)
- options = ViewOptionsSchema({"dns64": False, "minimize": False})
- assert tmpl.render(options=options) == "policy.FLAGS({'NO_MINIMIZE','DNS64_DISABLE',})"
- assert tmpl.render(options=ViewOptionsSchema()) == "policy.FLAGS({})"
+ assert tmpl.render(options=val) == lua
@pytest.mark.parametrize(
- "val,res",
+ "val,lua",
[
("allow", "policy.TAGS_ASSIGN({})"),
("refused", "'policy.REFUSE'"),
("noanswer", "'policy.NO_ANSWER'"),
],
)
-def test_view_answer(val: Any, res: Any):
+def test_view_answer(val, lua):
tmpl_str = """{% from 'macros/view_macros.lua.j2' import view_answer %}
{{ view_answer(view.answer) }}"""
tmpl = template_from_str(tmpl_str)
- view = ViewSchema({"subnets": ["10.0.0.0/8"], "answer": val})
- assert tmpl.render(view=view) == res
+ assert tmpl.render(view=ViewSchema({"subnets": ["10.0.0.0/8"], "answer": val})) == lua