script inventory plugin correct error message (#85765)

type was reflecting 'converted' type, not 'pre conversion' of the data
now message points at specific data keys
add deprecation tests
pull/85871/head
Brian Coca 3 months ago committed by GitHub
parent 43bb87107d
commit c87dc6ed7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,2 @@
bugfixes:
- script inventory plugin will now show correct 'incorrect' type when doing implicit conversions on groups.

@ -256,9 +256,10 @@ class InventoryModule(BaseInventoryPlugin):
group = self.inventory.add_group(group)
if not isinstance(data, dict):
original_type = native_type_name(data)
data = {'hosts': data}
display.deprecated(
msg=f"Group {group!r} was converted to {native_type_name(dict)!r} from {native_type_name(data)!r}.",
msg=f"Group {group!r} was converted to {native_type_name(dict)!r} from {original_type!r}.",
version='2.23',
obj=origin,
)

@ -0,0 +1,13 @@
#!/bin/sh
echo '{
"good_group": {
"vars": {
"test1": "value1",
"test2": "value2"
},
"hosts": ["example1", "example2"]
},
"bad_group": "should be list",
"_meta": {}
}'

@ -1,4 +1,8 @@
- name: run valid script output test cases
- name: Restrict tests to 'script'
environment:
INVENTORY_TEST_MODE: '{{ item.mode | default(omit) }}'
block:
- name: run valid script output test cases
include_tasks: test_valid_inventory.yml
loop:
- mode: no_profile
@ -74,7 +78,7 @@
- inventory_data.mygroup | length == 1
- inventory_data.mygroup.hosts == ["myhost"]
- name: run invalid script output test cases
- name: run invalid script output test cases
include_tasks: test_broken_inventory.yml
loop:
- {mode: bad_shebang, script_name: bad_shebang, expected_error: Failed to execute inventory script command}
@ -93,3 +97,7 @@
- {mode: invalid_group_vars_type, expected_error: Value contains 'mygroup.vars' which is 'list' instead of 'dict'}
- {mode: no_meta_hostvars_host_nonzero_rc, expected_error: Inventory script returned non-zero exit code 1}
- {mode: no_meta_hostvars_host_invalid_json, expected_error: Inventory script result for host 'myhost' could not be parsed as JSON}
- mode: bad_types
script_name: bad_types
deprecation: "Group 'bad_group' was converted to 'dict' from 'str'" # this deprecation is removed in 2.23
expected_error: "Value contains 'bad_group.hosts' which is 'str' instead of 'list'"

@ -2,8 +2,8 @@
shell: ansible-inventory -i {{ role_path | quote }}/{{ item.script_name | default('script_inventory_fixture.py') }} --list --export
changed_when: false
environment:
INVENTORY_TEST_MODE: '{{ item.mode | default(omit) }}'
INVENTORY_EMIT_STDERR: '1'
ANSIBLE_DEPRECATION_WARNINGS: '{{ "deprecation" in item }}'
ignore_errors: true
register: inv_out
@ -12,3 +12,4 @@
that:
- inv_out.stderr is contains("this is stderr") if item.script_name is undefined else true
- inv_out.stderr is regex(item.expected_error)
- item.deprecation is undefined or inv_out.stderr is regex(item.deprecation)

@ -4,7 +4,6 @@
environment:
ANSIBLE_INVENTORY_PLUGIN_SCRIPT_STDERR: '{{ item.show_stderr | default(omit) }}'
ANSIBLE_DEPRECATION_WARNINGS: 1 # some tests assert deprecation warnings
INVENTORY_TEST_MODE: '{{ item.mode | default(omit) }}'
INVENTORY_EMIT_STDERR: '{{ item.emit_stderr | default(omit) }}'
register: inv_out

Loading…
Cancel
Save