From: Aleš Mrázek Date: Thu, 27 Mar 2025 12:07:56 +0000 (+0100) Subject: datamodel: templates: fix undefined variables and attributes X-Git-Tag: v6.0.12~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fenvironments%2Fdocs-develop-loca-69s24w%2Fdeployments%2F6478;p=thirdparty%2Fknot-resolver.git datamodel: templates: fix undefined variables and attributes --- diff --git a/python/knot_resolver/datamodel/templates/macros/common_macros.lua.j2 b/python/knot_resolver/datamodel/templates/macros/common_macros.lua.j2 index 4c2ba11a8..a599bee0b 100644 --- a/python/knot_resolver/datamodel/templates/macros/common_macros.lua.j2 +++ b/python/knot_resolver/datamodel/templates/macros/common_macros.lua.j2 @@ -56,7 +56,7 @@ kres.type.{{ item|string }}, {%- else-%} { {%- for item in servers -%} -{%- if item.address -%} +{%- if item.address is defined and item.address -%} '{{ item.address|string }}', {%- else -%} '{{ item|string }}', @@ -70,7 +70,7 @@ kres.type.{{ item|string }}, {% macro tls_servers_table(servers) -%} { {%- for item in servers -%} -{%- if item.address -%} +{%- if item.address is defined and item.address -%} {'{{ item.address|string }}',{{ tls_server_auth(item) }}}, {%- else -%} '{{ item|string }}', diff --git a/python/knot_resolver/datamodel/templates/macros/forward_macros.lua.j2 b/python/knot_resolver/datamodel/templates/macros/forward_macros.lua.j2 index b7723fb07..1f3549b16 100644 --- a/python/knot_resolver/datamodel/templates/macros/forward_macros.lua.j2 +++ b/python/knot_resolver/datamodel/templates/macros/forward_macros.lua.j2 @@ -5,7 +5,7 @@ {%- endmacro %} {% macro forward_server(server) -%} -{%- if server.address -%} +{%- if server.address is defined and server.address-%} {%- for addr in server.address -%} {'{{ addr }}', {%- if server.transport == 'tls' -%} diff --git a/python/knot_resolver/datamodel/templates/macros/policy_macros.lua.j2 b/python/knot_resolver/datamodel/templates/macros/policy_macros.lua.j2 index 946833567..b89d0e3f2 100644 --- a/python/knot_resolver/datamodel/templates/macros/policy_macros.lua.j2 +++ b/python/knot_resolver/datamodel/templates/macros/policy_macros.lua.j2 @@ -56,7 +56,7 @@ policy.TAGS_ASSIGN({{ string_table(tags) }}) {%- endmacro %} {% macro policy_get_tagset(tags) -%} -{%- if tags -%} +{%- if tags is defined and tags-%} policy.get_tagset({{ string_table(tags) }}) {%- else -%} 0 diff --git a/python/knot_resolver/datamodel/templates/rate_limiting.lua.j2 b/python/knot_resolver/datamodel/templates/rate_limiting.lua.j2 index 63f921257..1f2a546d0 100644 --- a/python/knot_resolver/datamodel/templates/rate_limiting.lua.j2 +++ b/python/knot_resolver/datamodel/templates/rate_limiting.lua.j2 @@ -1,6 +1,6 @@ {% from 'macros/common_macros.lua.j2' import boolean %} -{% if cfg.rate_limiting.rate_limit -%} +{% if cfg.rate_limiting.rate_limit is defined and cfg.rate_limiting.rate_limit -%} assert(C.ratelimiting_init( '{{ cfg.rundir }}/ratelimiting', {{ cfg.rate_limiting.capacity }}, diff --git a/tests/manager/datamodel/templates/test_policy_macros.py b/tests/manager/datamodel/templates/test_policy_macros.py index 09aac85de..5fded1b4c 100644 --- a/tests/manager/datamodel/templates/test_policy_macros.py +++ b/tests/manager/datamodel/templates/test_policy_macros.py @@ -66,7 +66,7 @@ def test_policy_suffix_common(): {{ policy_suffix_common(action, suffix, common) }}""" tmpl = template_from_str(tmpl_str) - assert tmpl.render(action=action, suffix=suffix) == f"policy.suffix_common({action},{suffix})" + assert tmpl.render(action=action, suffix=suffix, common=None) == f"policy.suffix_common({action},{suffix})" assert ( tmpl.render(action=action, suffix=suffix, common=common) == f"policy.suffix_common({action},{suffix},{common})" ) @@ -89,7 +89,7 @@ def test_policy_rpz(): {{ policy_rpz(action, path, watch) }}""" tmpl = template_from_str(tmpl_str) - assert tmpl.render(action=action, path=path) == f"policy.rpz({action},'{path}',false)" + assert tmpl.render(action=action, path=path, watch=False) == f"policy.rpz({action},'{path}',false)" assert tmpl.render(action=action, path=path, watch=True) == f"policy.rpz({action},'{path}',true)"