| 1 |
- name: test the origin filter # CAUTION: moving this task requires updating the assertion line numbers |
| 2 |
vars: # vars placed before assert to allow adding assertions without changing the line number of existing vars |
| 3 |
some_var: Hello |
| 4 |
assert: |
| 5 |
that: |
| 6 |
- "'some_string' | ansible._protomatter.origin == None" |
| 7 |
- some_var | ansible._protomatter.origin == role_path ~ '/tasks/main.yml:3:15' |
| 8 |
- missing_var | ansible._protomatter.origin is undefined |
| 9 |
|
| 10 |
- name: register a task result which generates a deprecation warning for use by later tests |
| 11 |
debug: |
| 12 |
var: lookup('emit_deprecation_warning') |
| 13 |
vars: |
| 14 |
ansible_deprecation_warnings: true |
| 15 |
register: |
| 16 |
deprecation_warning: _task.result |
| 17 |
not_unmasked: deprecation_warning.deprecations[0].deprecator | type_debug |
| 18 |
unmasked_scalar: (deprecation_warning.deprecations[0] | ansible._protomatter.unmask("PluginInfo")).deprecator | type_debug |
| 19 |
unmasked_list: (deprecation_warning.deprecations[0] | ansible._protomatter.unmask(["PluginInfo"])).deprecator | type_debug |
| 20 |
|
| 21 |
- name: test the unmask filter |
| 22 |
assert: |
| 23 |
that: |
| 24 |
- not_unmasked == 'dict' |
| 25 |
- unmasked_scalar == "PluginInfo" |
| 26 |
- unmasked_list == "PluginInfo" |
| 27 |
- 1 | ansible._protomatter.unmask("PluginInfo") == 1 |
| 28 |
- missing_var | ansible._protomatter.unmask("PluginInfo") is undefined |
| 29 |
|
| 30 |
- name: create variables to test the apply_trust filter |
| 31 |
set_fact: |
| 32 |
untrusted_string: "{{ untrusted }}" |
| 33 |
formerly_untrusted_string: "{{ untrusted | ansible._protomatter.apply_trust }}" |
| 34 |
still_untrusted_number: "{{ a_number | ansible._protomatter.apply_trust }}" |
| 35 |
vars: |
| 36 |
untrusted: !unsafe "{{ 1 }}" |
| 37 |
a_number: 1 |
| 38 |
|
| 39 |
- name: test the apply_trust filter |
| 40 |
assert: |
| 41 |
that: |
| 42 |
- untrusted_string == "{" + "{ 1 }}" |
| 43 |
- formerly_untrusted_string == 1 |
| 44 |
- still_untrusted_number | ansible._protomatter.tag_names == ['Origin'] # does not have TrustedAsTemplate |
| 45 |
- missing_var | ansible._protomatter.apply_trust is undefined |
| 46 |
|
| 47 |
# DTFIX-FUTURE: protomatter should be available from unit tests, either always or via a fixture opt-in |
| 48 |
|
| 49 |
- name: test the dump_object filter |
| 50 |
assert: |
| 51 |
that: |
| 52 |
- "'some_string' | ansible._protomatter.dump_object == 'some_string'" |
| 53 |
- some_var | ansible._protomatter.dump_object == some_var |
| 54 |
- missing_var | ansible._protomatter.dump_object is undefined |
| 55 |
- lookup('synthetic_plugin_info') | type_debug == 'PluginInfo' |
| 56 |
- lookup('synthetic_plugin_info') | ansible._protomatter.dump_object | type_debug == 'dict' |
| 57 |
- lookup('synthetic_plugin_info') | ansible._protomatter.dump_object == expected_plugin_info |
| 58 |
- (syntax_error | ansible._protomatter.dump_object).exception.message is contains 'Syntax error in template' |
| 59 |
vars: |
| 60 |
some_var: Hello |
| 61 |
expected_plugin_info: |
| 62 |
resolved_name: ns.col.module |
| 63 |
type: module |
| 64 |
syntax_error: '{{ bogus syntax oops DSYFF*&H#$*F#$@F' |
| 65 |
|
| 66 |
- name: test the python_literal_eval filter |
| 67 |
assert: |
| 68 |
that: |
| 69 |
- "'[1, 2]' | ansible._protomatter.python_literal_eval == [1, 2]" |
| 70 |
- "'x[1, 2]' | ansible._protomatter.python_literal_eval | ansible._protomatter.true_type == 'CapturedExceptionMarker'" |
| 71 |
- "'x[1, 2]' | ansible._protomatter.python_literal_eval(ignore_errors=True) == 'x[1, 2]'" |
| 72 |
- missing_var | ansible._protomatter.python_literal_eval is undefined |
| 73 |
|
| 74 |
- name: test non-string input failure to python_literal_eval filter |
| 75 |
assert: |
| 76 |
that: 123 | ansible._protomatter.python_literal_eval |
| 77 |
ignore_errors: true |
| 78 |
register: nonstr_python_literal_eval |
| 79 |
|
| 80 |
- assert: |
| 81 |
that: |
| 82 |
- nonstr_python_literal_eval is failed |
| 83 |
- nonstr_python_literal_eval.msg is contains "must be a string" |
| 84 |
|
| 85 |
- name: test the tag_names filter |
| 86 |
assert: |
| 87 |
that: |
| 88 |
- "'some_string' | ansible._protomatter.tag_names == []" |
| 89 |
- some_var | ansible._protomatter.tag_names == ['Origin', 'TrustedAsTemplate'] |
| 90 |
- missing_var | ansible._protomatter.tag_names is undefined |
| 91 |
vars: |
| 92 |
some_var: Hello |
| 93 |
|
| 94 |
- name: test the true_type filter |
| 95 |
assert: |
| 96 |
that: |
| 97 |
- "'some_string' | ansible._protomatter.true_type == 'str'" |
| 98 |
- some_var | ansible._protomatter.true_type == "_AnsibleTaggedStr" |
| 99 |
- missing_var | ansible._protomatter.true_type == "UndefinedMarker" |
| 100 |
vars: |
| 101 |
some_var: Hello |
| 102 |
|
| 103 |
- name: unmask with an invalid type for type_names |
| 104 |
debug: |
| 105 |
msg: "{{ [] | ansible._protomatter.unmask(invalid_type) }}" |
| 106 |
register: unamsk_type_names_type_error |
| 107 |
ignore_errors: yes |
| 108 |
vars: |
| 109 |
invalid_type: 1 |
| 110 |
|
| 111 |
- name: unmask an invalid type |
| 112 |
debug: |
| 113 |
msg: "{{ [] | ansible._protomatter.unmask(bogus_type) }}" |
| 114 |
register: unmask_bogus_type |
| 115 |
ignore_errors: yes |
| 116 |
vars: |
| 117 |
bogus_type: BogusType |
| 118 |
|
| 119 |
- name: verify the unmask errors |
| 120 |
assert: |
| 121 |
that: |
| 122 |
- unamsk_type_names_type_error is failed |
| 123 |
- unamsk_type_names_type_error.msg is contains "The 'type_names' argument must be of type 'str' or 'list', not 'int'." |
| 124 |
- unmask_bogus_type is failed |
| 125 |
- unmask_bogus_type.msg is search "Unknown type name\(s\). BogusType" |
| 126 |
|
| 127 |
- transform_factory: |
| 128 |
vars: |
| 129 |
ansible_deprecation_warnings: true |
| 130 |
register: |
| 131 |
transformed: _task.result |
| 132 |
json_fallback_to_str: (transformed | ansible._protomatter.unmask(['WarningSummary','DeprecationSummary'])) | to_json(profile='fallback_to_str') |
| 133 |
warning_summary: (transformed.warnings | ansible._protomatter.unmask(['WarningSummary']))[0] | type_debug |
| 134 |
warning_summary_event: (transformed.warnings | ansible._protomatter.unmask(['WarningSummary']))[0].event | type_debug |
| 135 |
deprecation_summary_event: (transformed.deprecations | ansible._protomatter.unmask(['DeprecationSummary']))[0].event | type_debug |
| 136 |
# unmask the wrong type, ensure that the default transform still occurs |
| 137 |
wrong_type_unmasked: (transformed.warnings | ansible._protomatter.unmask('EncryptedString'))[0] | type_debug |
| 138 |
# unmask at a higher level, validate that it propagates to child lazies |
| 139 |
warning_summary_propagated_event: (transformed | ansible._protomatter.unmask(['WarningSummary'])).warnings[0].event | type_debug |
| 140 |
|
| 141 |
- debug: |
| 142 |
var: json_fallback_to_str |
| 143 |
|
| 144 |
- name: validate transformed and unmasked values |
| 145 |
assert: |
| 146 |
that: |
| 147 |
- json_fallback_to_str is contains "WarningSummary(event=Event(msg='a warning'," |
| 148 |
- transformed.warnings | length > 0 |
| 149 |
- transformed.warnings[0] | type_debug == 'str' |
| 150 |
- warning_summary == 'WarningSummary' |
| 151 |
- warning_summary_event == 'Event' |
| 152 |
- transformed.deprecations | length > 0 |
| 153 |
- transformed.deprecations[0] | type_debug == 'dict' |
| 154 |
- deprecation_summary_event == 'Event' |
| 155 |
- wrong_type_unmasked == 'str' |
| 156 |
- warning_summary_propagated_event == 'Event' |