mirror of https://github.com/ansible/ansible.git
test: Unit tests for validation methods (#75061)
* check_required_one_of() * check_required_by() * check_required_if() * check_missing_parameters() Fixes: #55994 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>pull/75092/head
parent
6dbfd73174
commit
b0ae3f8a8d
@ -0,0 +1,3 @@
|
||||
---
|
||||
minor_changes:
|
||||
- validation testcases for check_* APIs (https://github.com/ansible/ansible/issues/55994).
|
@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.common.validation import check_required_one_of
|
||||
from ansible.module_utils.common.validation import check_missing_parameters
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def arguments_terms():
|
||||
return {"path": ""}
|
||||
|
||||
|
||||
def test_check_missing_parameters():
|
||||
assert check_missing_parameters([], {}) == []
|
||||
|
||||
|
||||
def test_check_missing_parameters_list():
|
||||
expected = "missing required arguments: path"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_missing_parameters({}, ["path"])
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_missing_parameters_positive():
|
||||
assert check_missing_parameters({"path": "/foo"}, ["path"]) == []
|
@ -0,0 +1,98 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.common.validation import check_required_by
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def path_arguments_terms():
|
||||
return {
|
||||
"path": ["mode", "owner"],
|
||||
}
|
||||
|
||||
|
||||
def test_check_required_by():
|
||||
arguments_terms = {}
|
||||
params = {}
|
||||
assert check_required_by(arguments_terms, params) == {}
|
||||
|
||||
|
||||
def test_check_required_by_missing():
|
||||
arguments_terms = {
|
||||
"force": "force_reason",
|
||||
}
|
||||
params = {"force": True}
|
||||
expected = "missing parameter(s) required by 'force': force_reason"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_by(arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_by_multiple(path_arguments_terms):
|
||||
params = {
|
||||
"path": "/foo/bar",
|
||||
}
|
||||
expected = "missing parameter(s) required by 'path': mode, owner"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_by(path_arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_by_single(path_arguments_terms):
|
||||
params = {"path": "/foo/bar", "mode": "0700"}
|
||||
expected = "missing parameter(s) required by 'path': owner"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_by(path_arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_by_missing_none(path_arguments_terms):
|
||||
params = {
|
||||
"path": "/foo/bar",
|
||||
"mode": "0700",
|
||||
"owner": "root",
|
||||
}
|
||||
assert check_required_by(path_arguments_terms, params)
|
||||
|
||||
|
||||
def test_check_required_by_options_context(path_arguments_terms):
|
||||
params = {"path": "/foo/bar", "mode": "0700"}
|
||||
|
||||
options_context = ["foo_context"]
|
||||
|
||||
expected = "missing parameter(s) required by 'path': owner found in foo_context"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_by(path_arguments_terms, params, options_context)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_by_missing_multiple_options_context(path_arguments_terms):
|
||||
params = {
|
||||
"path": "/foo/bar",
|
||||
}
|
||||
options_context = ["foo_context"]
|
||||
|
||||
expected = (
|
||||
"missing parameter(s) required by 'path': mode, owner found in foo_context"
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_by(path_arguments_terms, params, options_context)
|
||||
|
||||
assert to_native(e.value) == expected
|
@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.common.validation import check_required_if
|
||||
|
||||
|
||||
def test_check_required_if():
|
||||
arguments_terms = {}
|
||||
params = {}
|
||||
assert check_required_if(arguments_terms, params) == []
|
||||
|
||||
|
||||
def test_check_required_if_missing():
|
||||
arguments_terms = [["state", "present", ("path",)]]
|
||||
params = {"state": "present"}
|
||||
expected = "state is present but all of the following are missing: path"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_if(arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_if_missing_required():
|
||||
arguments_terms = [["state", "present", ("path", "owner"), True]]
|
||||
params = {"state": "present"}
|
||||
expected = "state is present but any of the following are missing: path, owner"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_if(arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_if_missing_multiple():
|
||||
arguments_terms = [["state", "present", ("path", "owner")]]
|
||||
params = {
|
||||
"state": "present",
|
||||
}
|
||||
expected = "state is present but all of the following are missing: path, owner"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_if(arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_if_missing_multiple():
|
||||
arguments_terms = [["state", "present", ("path", "owner")]]
|
||||
params = {
|
||||
"state": "present",
|
||||
}
|
||||
options_context = ["foo_context"]
|
||||
expected = "state is present but all of the following are missing: path, owner found in foo_context"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_if(arguments_terms, params, options_context)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_if_multiple():
|
||||
arguments_terms = [["state", "present", ("path", "owner")]]
|
||||
params = {
|
||||
"state": "present",
|
||||
"path": "/foo",
|
||||
"owner": "root",
|
||||
}
|
||||
options_context = ["foo_context"]
|
||||
assert check_required_if(arguments_terms, params) == []
|
||||
assert check_required_if(arguments_terms, params, options_context) == []
|
@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright: (c) 2021, Ansible Project
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import pytest
|
||||
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.common.validation import check_required_one_of
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def arguments_terms():
|
||||
return [["path", "owner"]]
|
||||
|
||||
|
||||
def test_check_required_one_of():
|
||||
assert check_required_one_of([], {}) == []
|
||||
|
||||
|
||||
def test_check_required_one_of_missing(arguments_terms):
|
||||
params = {"state": "present"}
|
||||
expected = "one of the following is required: path, owner"
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_one_of(arguments_terms, params)
|
||||
|
||||
assert to_native(e.value) == expected
|
||||
|
||||
|
||||
def test_check_required_one_of_provided(arguments_terms):
|
||||
params = {"state": "present", "path": "/foo"}
|
||||
assert check_required_one_of(arguments_terms, params) == []
|
||||
|
||||
|
||||
def test_check_required_one_of_context(arguments_terms):
|
||||
params = {"state": "present"}
|
||||
expected = "one of the following is required: path, owner found in foo_context"
|
||||
option_context = ["foo_context"]
|
||||
|
||||
with pytest.raises(TypeError) as e:
|
||||
check_required_one_of(arguments_terms, params, option_context)
|
||||
|
||||
assert to_native(e.value) == expected
|
Loading…
Reference in New Issue