|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test that Ansible variables produce proper boolean types, not strings. |
| 4 | +This prevents issues with Ansible 12.0.0's strict type checking. |
| 5 | +""" |
| 6 | + |
| 7 | +import jinja2 |
| 8 | + |
| 9 | + |
| 10 | +def render_template(template_str, variables=None): |
| 11 | + """Render a Jinja2 template with given variables.""" |
| 12 | + env = jinja2.Environment() |
| 13 | + template = env.from_string(template_str) |
| 14 | + return template.render(variables or {}) |
| 15 | + |
| 16 | + |
| 17 | +class TestBooleanVariables: |
| 18 | + """Test that critical variables produce actual booleans.""" |
| 19 | + |
| 20 | + def test_ipv6_support_produces_boolean(self): |
| 21 | + """Ensure ipv6_support produces boolean, not string 'true'/'false'.""" |
| 22 | + # Test with gateway defined (should be boolean True) |
| 23 | + template = "{{ ansible_default_ipv6['gateway'] is defined }}" |
| 24 | + vars_with_gateway = {'ansible_default_ipv6': {'gateway': 'fe80::1'}} |
| 25 | + result = render_template(template, vars_with_gateway) |
| 26 | + assert result == "True" # Jinja2 renders boolean True as string "True" |
| 27 | + |
| 28 | + # Test without gateway (should be boolean False) |
| 29 | + vars_no_gateway = {'ansible_default_ipv6': {}} |
| 30 | + result = render_template(template, vars_no_gateway) |
| 31 | + assert result == "False" # Jinja2 renders boolean False as string "False" |
| 32 | + |
| 33 | + # The key is that we're NOT producing string literals "true" or "false" |
| 34 | + bad_template = "{% if ansible_default_ipv6['gateway'] is defined %}true{% else %}false{% endif %}" |
| 35 | + result_bad = render_template(bad_template, vars_no_gateway) |
| 36 | + assert result_bad == "false" # This is a string literal, not a boolean |
| 37 | + |
| 38 | + # Verify our fix doesn't produce string literals |
| 39 | + assert result != "false" # Our fix produces "False" (from boolean), not "false" (string literal) |
| 40 | + |
| 41 | + def test_algo_variables_boolean_fallbacks(self): |
| 42 | + """Ensure algo_* variables produce booleans in their fallback cases.""" |
| 43 | + # Test the fixed template (produces boolean) |
| 44 | + good_template = "{% if var is defined %}{{ var | bool }}{%- else %}{{ false }}{% endif %}" |
| 45 | + result_good = render_template(good_template, {}) |
| 46 | + assert result_good == "False" # Boolean False renders as "False" |
| 47 | + |
| 48 | + # Test the old broken template (produces string) |
| 49 | + bad_template = "{% if var is defined %}{{ var | bool }}{%- else %}false{% endif %}" |
| 50 | + result_bad = render_template(bad_template, {}) |
| 51 | + assert result_bad == "false" # String literal "false" |
| 52 | + |
| 53 | + # Verify they're different |
| 54 | + assert result_good != result_bad |
| 55 | + assert result_good == "False" and result_bad == "false" |
| 56 | + |
| 57 | + def test_boolean_filter_on_strings(self): |
| 58 | + """Test that the bool filter correctly converts string values.""" |
| 59 | + # Since we can't test Ansible's bool filter directly in Jinja2, |
| 60 | + # we test the pattern we're using in our templates |
| 61 | + |
| 62 | + # Test that our templates don't use raw string "true"/"false" |
| 63 | + # which would fail in Ansible 12 |
| 64 | + bad_pattern = "{%- else %}false{% endif %}" |
| 65 | + good_pattern = "{%- else %}{{ false }}{% endif %}" |
| 66 | + |
| 67 | + # The bad pattern produces a string literal |
| 68 | + result_bad = render_template("{% if var is defined %}something" + bad_pattern, {}) |
| 69 | + assert "false" in result_bad # String literal |
| 70 | + |
| 71 | + # The good pattern produces a boolean value |
| 72 | + result_good = render_template("{% if var is defined %}something" + good_pattern, {}) |
| 73 | + assert "False" in result_good # Boolean False rendered as "False" |
| 74 | + |
| 75 | + def test_ansible_12_conditional_compatibility(self): |
| 76 | + """ |
| 77 | + Test that our fixes work with Ansible 12's strict type checking. |
| 78 | + This simulates what Ansible 12 will do with our variables. |
| 79 | + """ |
| 80 | + # Our fixed template - produces actual boolean |
| 81 | + fixed_ipv6 = "{{ ansible_default_ipv6['gateway'] is defined }}" |
| 82 | + fixed_algo = "{% if var is defined %}{{ var | bool }}{%- else %}{{ false }}{% endif %}" |
| 83 | + |
| 84 | + # Simulate the boolean value in a conditional context |
| 85 | + # In Ansible 12, this would fail if it's a string "true"/"false" |
| 86 | + vars_with_gateway = {'ansible_default_ipv6': {'gateway': 'fe80::1'}} |
| 87 | + ipv6_result = render_template(fixed_ipv6, vars_with_gateway) |
| 88 | + |
| 89 | + # The result should be "True" (boolean rendered), not "true" (string literal) |
| 90 | + assert ipv6_result == "True" |
| 91 | + assert ipv6_result != "true" |
| 92 | + |
| 93 | + # Test algo variable fallback |
| 94 | + algo_result = render_template(fixed_algo, {}) |
| 95 | + assert algo_result == "False" |
| 96 | + assert algo_result != "false" |
| 97 | + |
| 98 | + def test_regression_no_string_booleans(self): |
| 99 | + """ |
| 100 | + Regression test: ensure we never produce string literals 'true' or 'false'. |
| 101 | + This is what breaks Ansible 12.0.0. |
| 102 | + """ |
| 103 | + # These patterns should NOT appear in our fixed code |
| 104 | + bad_patterns = [ |
| 105 | + "{}true{}", |
| 106 | + "{}false{}", |
| 107 | + "{%- else %}true{% endif %}", |
| 108 | + "{%- else %}false{% endif %}", |
| 109 | + ] |
| 110 | + |
| 111 | + # Test that our fixed templates don't produce string boolean literals |
| 112 | + fixed_template = "{{ ansible_default_ipv6['gateway'] is defined }}" |
| 113 | + for _pattern in bad_patterns: |
| 114 | + assert "true" not in fixed_template.replace(" ", "") |
| 115 | + assert "false" not in fixed_template.replace(" ", "") |
| 116 | + |
| 117 | + # Test algo variable fix |
| 118 | + fixed_algo = "{% if var is defined %}{{ var | bool }}{%- else %}{{ false }}{% endif %}" |
| 119 | + assert "{}false{}" not in fixed_algo.replace(" ", "") |
| 120 | + assert "{{ false }}" in fixed_algo |
| 121 | + |
0 commit comments