Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an error on invalid FA.isa value #80040

Merged
merged 6 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/isa-value-check.yml
@@ -0,0 +1,2 @@
minor_changes:
- Raise an error when an incorrect ``isa`` type is passed to ``FieldAttribute``.
2 changes: 2 additions & 0 deletions lib/ansible/playbook/base.py
Expand Up @@ -486,6 +486,8 @@ def get_validated_value(self, name, attribute, value, templar):
if not isinstance(value, attribute.class_type):
raise TypeError("%s is not a valid %s (got a %s instead)" % (name, attribute.class_type, type(value)))
value.post_validate(templar=templar)
else:
raise AnsibleAssertionError(f"Unknown value for attribute.isa: {attribute.isa}")
return value

def set_to_context(self, name):
Expand Down
6 changes: 3 additions & 3 deletions lib/ansible/playbook/loop_control.py
Expand Up @@ -25,9 +25,9 @@

class LoopControl(FieldAttributeBase):

loop_var = NonInheritableFieldAttribute(isa='str', default='item', always_post_validate=True)
index_var = NonInheritableFieldAttribute(isa='str', always_post_validate=True)
label = NonInheritableFieldAttribute(isa='str')
loop_var = NonInheritableFieldAttribute(isa='string', default='item', always_post_validate=True)
index_var = NonInheritableFieldAttribute(isa='string', always_post_validate=True)
label = NonInheritableFieldAttribute(isa='string')
pause = NonInheritableFieldAttribute(isa='float', default=0, always_post_validate=True)
extended = NonInheritableFieldAttribute(isa='bool', always_post_validate=True)
extended_allitems = NonInheritableFieldAttribute(isa='bool', default=True, always_post_validate=True)
Expand Down
11 changes: 1 addition & 10 deletions lib/ansible/playbook/role/metadata.py
Expand Up @@ -41,7 +41,7 @@ class RoleMetadata(Base, CollectionSearch):

allow_duplicates = NonInheritableFieldAttribute(isa='bool', default=False)
dependencies = NonInheritableFieldAttribute(isa='list', default=list)
galaxy_info = NonInheritableFieldAttribute(isa='GalaxyInfo')
galaxy_info = NonInheritableFieldAttribute(isa='dict')
argument_specs = NonInheritableFieldAttribute(isa='dict', default=dict)

def __init__(self, owner=None):
Expand Down Expand Up @@ -110,15 +110,6 @@ def _load_dependencies(self, attr, ds):
except AssertionError as e:
raise AnsibleParserError("A malformed list of role dependencies was encountered.", obj=self._ds, orig_exc=e)

def _load_galaxy_info(self, attr, ds):
'''
This is a helper loading function for the galaxy info entry
in the metadata, which returns a GalaxyInfo object rather than
a simple dictionary.
'''

return ds

def serialize(self):
return dict(
allow_duplicates=self._allow_duplicates,
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/playbook/task.py
Expand Up @@ -75,7 +75,7 @@ class Task(Base, Conditional, Taggable, CollectionSearch):
delegate_to = FieldAttribute(isa='string')
delegate_facts = FieldAttribute(isa='bool')
failed_when = NonInheritableFieldAttribute(isa='list', default=list)
loop = NonInheritableFieldAttribute()
loop = NonInheritableFieldAttribute(isa='list')
loop_control = NonInheritableFieldAttribute(isa='class', class_type=LoopControl, default=LoopControl)
notify = FieldAttribute(isa='list')
poll = NonInheritableFieldAttribute(isa='int', default=C.DEFAULT_POLL_INTERVAL)
Expand Down
11 changes: 6 additions & 5 deletions test/units/playbook/test_base.py
Expand Up @@ -21,7 +21,7 @@

from units.compat import unittest

from ansible.errors import AnsibleParserError
from ansible.errors import AnsibleParserError, AnsibleAssertionError
from ansible.module_utils.six import string_types
from ansible.playbook.attribute import FieldAttribute, NonInheritableFieldAttribute
from ansible.template import Templar
Expand Down Expand Up @@ -581,10 +581,11 @@ def test_attr_list_required_empty_string(self):
bsc.post_validate, templar)

def test_attr_unknown(self):
a_list = ['some string']
ds = {'test_attr_unknown_isa': a_list}
bsc = self._base_validate(ds)
self.assertEqual(bsc.test_attr_unknown_isa, a_list)
self.assertRaises(
AnsibleAssertionError,
self._base_validate,
{'test_attr_unknown_isa': True}
)

def test_attr_method(self):
ds = {'test_attr_method': 'value from the ds'}
Expand Down