Exemple #1
0
 def test_get_resource_name_from_stash(self):
     stash = [
         {
             'address': 'test'
         }
     ]
     self.assertEqual({'address': 'test'}, get_resource_name_from_stash(stash=stash))
Exemple #2
0
def its_value_condition_contain(_step_obj, condition, value, _stash=EmptyStash):
    if condition not in ('must', 'must not'):
        raise TerraformComplianceNotImplemented('Condition should be one of: `must`, `must not`')

    values = _step_obj.context.stash if _stash is EmptyStash else _stash
    # TODO: Update here for checking values in a list or dict.

    if isinstance(values, list):
        for elem in values:
            values = its_value_condition_contain(_step_obj, condition, value, elem)

    if isinstance(values, (int, bool, str, float)):
        values = dict(values=values,
                      address=_step_obj.context.address if hasattr(_step_obj.context, 'address') else _step_obj.context.addresses)

    found_values = seek_value_in_dict(value, values)
    condition = condition == 'must'

    if condition and not found_values:
        if isinstance(values, list):
            objects = []
            for elem in values:
                objects.append(elem.get('address', '???'))
            objects = ', '.join(objects)
        else:
            objects = values.get('address')

        Error(_step_obj, '{} could not found in {}.'.format(value, objects))

    elif not condition and found_values:
        Error(_step_obj, '{} found in {}.'.format(value,
                                                  get_resource_name_from_stash(found_values).get('address')))

    return values
Exemple #3
0
def its_value_condition_match_the_search_regex_regex(_step_obj, condition, search_regex, _stash=EmptyStash, case_insensitive=True):
    def fail(condition, name=None):
        text = 'matches' if condition == 'must not' else 'does not match'
        name = name if (name is not None or name is not False) else _step_obj.context.name
        pattern = 'Null/None' if regex == '\x00' else regex
        Error(_step_obj, '{} property in {} {} {} with {} {} regex. '
                         'It is set to {}.'.format(_step_obj.context.property_name,
                                                   name,
                                                   _step_obj.context.type,
                                                   text,
                                                   pattern,
                                                   regex_flag_error_text,
                                                   values))

    regex = r'{}'.format(search_regex)
    values = _step_obj.context.stash if _stash is EmptyStash else _stash
    regex_flags = re.IGNORECASE if case_insensitive else 0
    regex_flag_error_text = 'case insensitive' if case_insensitive else 'case sensitive'

    if isinstance(values, (str, int, bool, float)) or values is None:
        matches = re.match(regex, str(values), flags=regex_flags)

        if (condition == 'must' and matches is None) or (condition == "must not" and matches is not None):
            _stash = get_resource_name_from_stash(_step_obj.context.stash, _stash, _step_obj.context.address)
            fail(condition, name=_stash.get('address'))

    elif isinstance(values, list):
        for value in values:
            its_value_condition_match_the_search_regex_regex(_step_obj,
                                                             condition,
                                                             search_regex,
                                                             value,
                                                             case_insensitive=case_insensitive)

    elif isinstance(values, dict):
        if not hasattr(_step_obj.context, 'address'):
            _step_obj.context.address = None

        _step_obj.context.address = values.get('address', _step_obj.context.address)

        if 'values' in values:
            if values['values'] is None and regex == '\x00' and condition == 'must not':
                values = values['values']
                fail(condition, name=_stash.get('address'))
            else:
                its_value_condition_match_the_search_regex_regex(_step_obj,
                                                                 condition,
                                                                 search_regex,
                                                                 values.get('values'),
                                                                 case_insensitive=case_insensitive)

        else:
            for key, value in values.items():
                its_value_condition_match_the_search_regex_regex(_step_obj,
                                                                 condition,
                                                                 search_regex,
                                                                 value,
                                                                 case_insensitive=case_insensitive)
def any_of_its_values_condition_match_the_search_regex_regex(
        _step_obj, condition, search_regex, _stash=EmptyStash):
    def fail(condition, name=None):
        text = 'matches' if condition == 'must not' else 'does not match'
        name = name if (name is not None
                        or name is not False) else _step_obj.context.name
        pattern = 'Null/None' if regex == '\x00' else regex
        Error(
            _step_obj, '{} property in {} {} {} with {} {} regex. '
            'It is set to {}.'.format(_step_obj.context.property_name, name,
                                      _step_obj.context.type, text, pattern,
                                      regex_flag_error_text, values))

    found = False

    def search(values):
        nonlocal found
        if found:
            return True

        if isinstance(values, (str, int, bool, float)) or values is None:
            matches = match.regex_match(regex, str(values))

            if (condition == 'must'
                    and matches is not None) or (condition == "must not"
                                                 and matches is None):
                found = True
                return found

        elif isinstance(values, list):
            return any(map(search, values))

        elif isinstance(values, dict):
            if not hasattr(_step_obj.context, 'address'):
                _step_obj.context.address = None

            _step_obj.context.address = values.get('address',
                                                   _step_obj.context.address)

            if 'values' in values:
                return search(values['values'])
            else:
                return any(map(search, values.values()))

        return False

    match = _step_obj.context.match
    regex = r'{}'.format(search_regex)
    values = _step_obj.context.stash if _stash is EmptyStash else _stash
    regex_flag_error_text = 'case insensitive' if not match.case_sensitive else 'case sensitive'

    if not search(values):
        _stash = get_resource_name_from_stash(_step_obj.context.stash, _stash,
                                              _step_obj.context.address)
        fail(condition, name=_stash.get('address'))
def its_value_condition_match_the_search_regex_regex(_step_obj,
                                                     condition,
                                                     search_regex,
                                                     _stash=EmptyStash):
    def fail(condition, name=None):
        text = 'matches' if condition == 'must not' else 'does not match'
        name = name if (name is not None
                        or name is not False) else _step_obj.context.name
        pattern = 'Null/None' if regex == '\x00' else regex
        raise Failure('{} property in {} {} {} with {} regex. '
                      'It is set to {}.'.format(
                          _step_obj.context.property_name, name,
                          _step_obj.context.type, text, pattern, values))

    regex = r'{}'.format(search_regex)
    values = _step_obj.context.stash if _stash is EmptyStash else _stash

    if type(values) is str or type(values) is int or type(values) is bool:
        matches = re.match(regex, str(values), flags=re.IGNORECASE)

        if (condition == 'must'
                and matches is None) or (condition == "must not"
                                         and matches is not None):
            _stash = get_resource_name_from_stash(_step_obj.context.stash,
                                                  _stash)
            fail(condition, name=_stash.get('address'))

    elif type(values) is list:
        for value in values:
            its_value_condition_match_the_search_regex_regex(
                _step_obj, condition, search_regex, value)

    elif type(values) is dict:
        if 'values' in values:
            if values[
                    'values'] is None and regex == '\x00' and condition == 'must not':
                values = values['values']
                fail(condition, name=_stash.get('address'))
            else:
                its_value_condition_match_the_search_regex_regex(
                    _step_obj, condition, search_regex, values.get('values'))

        else:
            for key, value in values.items():
                its_value_condition_match_the_search_regex_regex(
                    _step_obj, condition, search_regex, value)
Exemple #6
0
 def test_get_resource_name_from_stash_address_exists(self):
     stash = {}
     self.assertEqual({'address': 'test'}, get_resource_name_from_stash(stash=stash, address='test'))