Adds argspec tests for required, required_one_of and required_by (#72245)

* Improve variable names.

* Add test for required.

* Add test for required_one_of.

* Add test for required_by.
This commit is contained in:
Felix Fontein 2020-10-22 21:05:59 +02:00 committed by GitHub
parent fe65144a4a
commit 1489bf9190
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 167 additions and 4 deletions

View file

@ -11,6 +11,14 @@ from ansible.module_utils.basic import AnsibleModule
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
{ {
'required': {
'required': True,
},
'required_one_of_one': {},
'required_one_of_two': {},
'required_by_one': {},
'required_by_two': {},
'required_by_three': {},
'state': { 'state': {
'type': 'str', 'type': 'str',
'choices': ['absent', 'present'], 'choices': ['absent', 'present'],
@ -20,6 +28,24 @@ def main():
'mapping': { 'mapping': {
'type': 'dict', 'type': 'dict',
}, },
'required_one_of': {
'required_one_of': [['thing', 'other']],
'type': 'list',
'elements': 'dict',
'options': {
'thing': {},
'other': {},
},
},
'required_by': {
'required_by': {'thing': 'other'},
'type': 'list',
'elements': 'dict',
'options': {
'thing': {},
'other': {},
},
},
'required_together': { 'required_together': {
'required_together': [['thing', 'other']], 'required_together': [['thing', 'other']],
'type': 'list', 'type': 'list',
@ -72,6 +98,12 @@ def main():
mutually_exclusive=( mutually_exclusive=(
('path', 'content'), ('path', 'content'),
), ),
required_one_of=(
('required_one_of_one', 'required_one_of_two'),
),
required_by={
'required_by_one': ('required_by_two', 'required_by_three'),
},
required_together=( required_together=(
('required_together_one', 'required_together_two'), ('required_together_one', 'required_together_two'),
), ),

View file

@ -1,46 +1,99 @@
- argspec:
required: value
required_one_of_one: value
- argspec:
required_one_of_one: value
register: argspec_required_fail
ignore_errors: true
- argspec:
required: value
required_one_of_two: value
- argspec:
required: value
register: argspec_required_one_of_fail
ignore_errors: true
- argspec:
required: value
required_one_of_two: value
required_by_one: value
required_by_two: value
required_by_three: value
- argspec:
required: value
required_one_of_two: value
required_by_one: value
required_by_two: value
register: argspec_required_by_fail
ignore_errors: true
- argspec: - argspec:
state: absent state: absent
required: value
required_one_of_one: value
- argspec: - argspec:
state: present state: present
required: value
required_one_of_one: value
register: argspec_required_if_fail register: argspec_required_if_fail
ignore_errors: true ignore_errors: true
- argspec: - argspec:
state: present state: present
path: foo path: foo
required: value
required_one_of_one: value
- argspec: - argspec:
state: present state: present
content: foo content: foo
required: value
required_one_of_one: value
- argspec: - argspec:
state: present state: present
content: foo content: foo
path: foo path: foo
required: value
required_one_of_one: value
register: argspec_mutually_exclusive_fail register: argspec_mutually_exclusive_fail
ignore_errors: true ignore_errors: true
- argspec: - argspec:
mapping: mapping:
foo: bar foo: bar
required: value
required_one_of_one: value
register: argspec_good_mapping register: argspec_good_mapping
- argspec: - argspec:
mapping: foo=bar mapping: foo=bar
required: value
required_one_of_one: value
register: argspec_good_mapping_kv register: argspec_good_mapping_kv
- argspec: - argspec:
mapping: !!str '{"foo": "bar"}' mapping: !!str '{"foo": "bar"}'
required: value
required_one_of_one: value
register: argspec_good_mapping_json register: argspec_good_mapping_json
- argspec: - argspec:
mapping: foo mapping: foo
required: value
required_one_of_one: value
register: argspec_bad_mapping_string register: argspec_bad_mapping_string
ignore_errors: true ignore_errors: true
- argspec: - argspec:
mapping: 1 mapping: 1
required: value
required_one_of_one: value
register: argspec_bad_mapping_int register: argspec_bad_mapping_int
ignore_errors: true ignore_errors: true
@ -48,6 +101,8 @@
mapping: mapping:
- foo - foo
- bar - bar
required: value
required_one_of_one: value
register: argspec_bad_mapping_list register: argspec_bad_mapping_list
ignore_errors: true ignore_errors: true
@ -56,14 +111,20 @@
- thing: foo - thing: foo
other: bar other: bar
another: baz another: baz
required: value
required_one_of_one: value
- argspec: - argspec:
required_together: required_together:
- another: baz - another: baz
required: value
required_one_of_one: value
- argspec: - argspec:
required_together: required_together:
- thing: foo - thing: foo
required: value
required_one_of_one: value
register: argspec_required_together_fail register: argspec_required_together_fail
ignore_errors: true ignore_errors: true
@ -71,33 +132,77 @@
required_together: required_together:
- thing: foo - thing: foo
other: bar other: bar
required: value
required_one_of_one: value
- argspec: - argspec:
required_if: required_if:
- thing: bar - thing: bar
required: value
required_one_of_one: value
- argspec: - argspec:
required_if: required_if:
- thing: foo - thing: foo
other: bar other: bar
required: value
required_one_of_one: value
- argspec: - argspec:
required_if: required_if:
- thing: foo - thing: foo
register: argpsec_required_if_fail required: value
required_one_of_one: value
register: argspec_required_if_fail_2
ignore_errors: true
- argspec:
required_one_of:
- thing: foo
other: bar
required: value
required_one_of_one: value
- argspec:
required_one_of:
- {}
required: value
required_one_of_one: value
register: argspec_required_one_of_fail_2
ignore_errors: true
- argspec:
required_by:
- thing: foo
other: bar
required: value
required_one_of_one: value
- argspec:
required_by:
- thing: foo
required: value
required_one_of_one: value
register: argspec_required_by_fail_2
ignore_errors: true ignore_errors: true
- argspec: - argspec:
json: !!str '{"foo": "bar"}' json: !!str '{"foo": "bar"}'
required: value
required_one_of_one: value
register: argspec_good_json_string register: argspec_good_json_string
- argspec: - argspec:
json: json:
foo: bar foo: bar
required: value
required_one_of_one: value
register: argspec_good_json_dict register: argspec_good_json_dict
- argspec: - argspec:
json: 1 json: 1
required: value
required_one_of_one: value
register: argspec_bad_json register: argspec_bad_json
ignore_errors: true ignore_errors: true
@ -105,41 +210,63 @@
fail_on_missing_params: fail_on_missing_params:
- needed_param - needed_param
needed_param: whatever needed_param: whatever
required: value
required_one_of_one: value
- argspec: - argspec:
fail_on_missing_params: fail_on_missing_params:
- needed_param - needed_param
required: value
required_one_of_one: value
register: argspec_fail_on_missing_params_bad register: argspec_fail_on_missing_params_bad
ignore_errors: true ignore_errors: true
- argspec: - argspec:
required_together_one: foo required_together_one: foo
required_together_two: bar required_together_two: bar
required: value
required_one_of_one: value
- argspec: - argspec:
required_together_one: foo required_together_one: foo
register: argspec_fail_required_together required: value
required_one_of_one: value
register: argspec_fail_required_together_2
ignore_errors: true ignore_errors: true
- argspec: - argspec:
suboptions_list_no_elements: suboptions_list_no_elements:
- thing: foo - thing: foo
required: value
required_one_of_one: value
register: argspec_suboptions_list_no_elements register: argspec_suboptions_list_no_elements
- argspec: - argspec:
choices_with_strings_like_bools: on choices_with_strings_like_bools: on
required: value
required_one_of_one: value
register: argspec_choices_with_strings_like_bools_true register: argspec_choices_with_strings_like_bools_true
- argspec: - argspec:
choices_with_strings_like_bools: 'on' choices_with_strings_like_bools: 'on'
required: value
required_one_of_one: value
register: argspec_choices_with_strings_like_bools_true_bool register: argspec_choices_with_strings_like_bools_true_bool
- argspec: - argspec:
choices_with_strings_like_bools: off choices_with_strings_like_bools: off
required: value
required_one_of_one: value
register: argspec_choices_with_strings_like_bools_false register: argspec_choices_with_strings_like_bools_false
- assert: - assert:
that: that:
- argspec_required_fail is failed
- argspec_required_one_of_fail is failed
- argspec_required_by_fail is failed
- argspec_required_if_fail is failed - argspec_required_if_fail is failed
- argspec_mutually_exclusive_fail is failed - argspec_mutually_exclusive_fail is failed
@ -159,7 +286,11 @@
- argspec_required_together_fail is failed - argspec_required_together_fail is failed
- argpsec_required_if_fail is failed - argspec_required_if_fail_2 is failed
- argspec_required_one_of_fail_2 is failed
- argspec_required_by_fail_2 is failed
- argspec_good_json_string is successful - argspec_good_json_string is successful
- >- - >-
@ -171,7 +302,7 @@
- argspec_fail_on_missing_params_bad is failed - argspec_fail_on_missing_params_bad is failed
- argspec_fail_required_together is failed - argspec_fail_required_together_2 is failed
- >- - >-
argspec_suboptions_list_no_elements.suboptions_list_no_elements.0 == {'thing': 'foo'} argspec_suboptions_list_no_elements.suboptions_list_no_elements.0 == {'thing': 'foo'}