Пример #1
0
    def _check_argument_values(self, spec=None, param=None):
        ''' ensure all arguments have the requested values, and there are no stray arguments '''
        if spec is None:
            spec = self.argument_spec
        if param is None:
            param = self.params
        for (k, v) in spec.items():
            choices = v.get('choices', None)
            if choices is None:
                continue
            if isinstance(choices, SEQUENCETYPE) and not isinstance(
                    choices, (binary_type, text_type)):
                if k in param:
                    # Allow one or more when type='list' param with choices
                    if isinstance(param[k], list):
                        diff_list = ", ".join(
                            [item for item in param[k] if item not in choices])
                        if diff_list:
                            choices_str = ", ".join(
                                [to_native(c) for c in choices])
                            msg = "value of %s must be one or more of: %s. Got no match for: %s" % (
                                k, choices_str, diff_list)
                            if self._options_context:
                                msg += " found in %s" % " -> ".join(
                                    self._options_context)
                            self.fail_json(msg=msg)
                    elif param[k] not in choices:
                        # PyYaml converts certain strings to bools.  If we can unambiguously convert back, do so before checking
                        # the value.  If we can't figure this out, module author is responsible.
                        lowered_choices = None
                        if param[k] == 'False':
                            lowered_choices = lenient_lowercase(choices)
                            overlap = BOOLEANS_FALSE.intersection(choices)
                            if len(overlap) == 1:
                                # Extract from a set
                                (param[k], ) = overlap

                        if param[k] == 'True':
                            if lowered_choices is None:
                                lowered_choices = lenient_lowercase(choices)
                            overlap = BOOLEANS_TRUE.intersection(choices)
                            if len(overlap) == 1:
                                (param[k], ) = overlap

                        if param[k] not in choices:
                            choices_str = ", ".join(
                                [to_native(c) for c in choices])
                            msg = "value of %s must be one of: %s, got: %s" % (
                                k, choices_str, param[k])
                            if self._options_context:
                                msg += " found in %s" % " -> ".join(
                                    self._options_context)
                            self.fail_json(msg=msg)
            else:
                msg = "internal error: choices for argument %s are not iterable: %s" % (
                    k, choices)
                if self._options_context:
                    msg += " found in %s" % " -> ".join(self._options_context)
                self.fail_json(msg=msg)
Пример #2
0
def validate_argument_values(argument_spec, parameters, options_context=None, errors=None):
    """Ensure all arguments have the requested values, and there are no stray arguments"""

    if errors is None:
        errors = []

    for param, spec in argument_spec.items():
        choices = spec.get('choices')
        if choices is None:
            continue

        if isinstance(choices, (frozenset, KeysView, Sequence)) and not isinstance(choices, (binary_type, text_type)):
            if param in parameters:
                # Allow one or more when type='list' param with choices
                if isinstance(parameters[param], list):
                    diff_list = ", ".join([item for item in parameters[param] if item not in choices])
                    if diff_list:
                        choices_str = ", ".join([to_native(c) for c in choices])
                        msg = "value of %s must be one or more of: %s. Got no match for: %s" % (param, choices_str, diff_list)
                        if options_context:
                            msg += " found in %s" % " -> ".join(options_context)
                        errors.append(msg)
                elif parameters[param] not in choices:
                    # PyYaml converts certain strings to bools. If we can unambiguously convert back, do so before checking
                    # the value. If we can't figure this out, module author is responsible.
                    lowered_choices = None
                    if parameters[param] == 'False':
                        lowered_choices = lenient_lowercase(choices)
                        overlap = BOOLEANS_FALSE.intersection(choices)
                        if len(overlap) == 1:
                            # Extract from a set
                            (parameters[param],) = overlap

                    if parameters[param] == 'True':
                        if lowered_choices is None:
                            lowered_choices = lenient_lowercase(choices)
                        overlap = BOOLEANS_TRUE.intersection(choices)
                        if len(overlap) == 1:
                            (parameters[param],) = overlap

                    if parameters[param] not in choices:
                        choices_str = ", ".join([to_native(c) for c in choices])
                        msg = "value of %s must be one of: %s, got: %s" % (param, choices_str, parameters[param])
                        if options_context:
                            msg += " found in %s" % " -> ".join(options_context)
                        errors.append(msg)
        else:
            msg = "internal error: choices for argument %s are not iterable: %s" % (param, choices)
            if options_context:
                msg += " found in %s" % " -> ".join(options_context)
            errors.append(msg)
def test_lenient_lowercase_illegal_data_type(input_data):
    """Test passing objects of illegal types to lenient_lowercase()."""
    with pytest.raises(TypeError, match='object is not iterable'):
        lenient_lowercase(input_data)
EXPECTED_LIST = [
    u'hello',
    u'ёлка',
    u'café',
    u'くらとみ',
    b'hello',
    1,
    {
        1: 'Dict'
    },
    True,
    [1],
    3.14159,
]

result_list = lenient_lowercase(INPUT_LIST)


@pytest.mark.parametrize('input_value,expected_outcome', [
    (result_list[0], EXPECTED_LIST[0]),
    (result_list[1], EXPECTED_LIST[1]),
    (result_list[2], EXPECTED_LIST[2]),
    (result_list[3], EXPECTED_LIST[3]),
    (result_list[4], EXPECTED_LIST[4]),
    (result_list[5], EXPECTED_LIST[5]),
    (result_list[6], EXPECTED_LIST[6]),
    (result_list[7], EXPECTED_LIST[7]),
    (result_list[8], EXPECTED_LIST[8]),
    (result_list[9], EXPECTED_LIST[9]),
])
def test_lenient_lowercase(input_value, expected_outcome):