def __init__(self, config=None, descriptors=descriptors, metrics=metrics): super(Config, self).__init__(config=config, descriptors=descriptors.copy(), metrics=metrics.copy()) for pattern_list in [pl for pl, d in self.descriptors.values() if 'list' in pl]: val = getattr(self, pattern_list) or [] val = val if isinstance(val, list) else [val] pl = PatternList() for item in val: if not isinstance(item, (list, tuple)): item = [item] if 'status_code' in pattern_list: value = [text_type(int(v) if isinstance(v, float) else v) for v in item] else: value = [text_type(v) for v in item] pl.update(*value) setattr(self, pattern_list, pl) if self.report_status_codes and self.report_status_code_groups: raise TypeError('Cannot simultaneously ReportStatusCodes and ReportStatusCodeGroups. ' 'Please specify desired StatusCodes and set ReportStatusCodeGroups to selectively ' 'report metrics.') self.update_pattern_lists() self.set_will_report_flags() if self.verbose: collectd.info(str(self))
def test_update(): pl = PatternList('1') assert pl.elements == ['1'] assert len(pl.patterns) == 1 assert [pat.pattern for pat in pl.patterns] == [translate('1')] pl.update('2', '3') assert pl.elements == ['1', '2', '3'] assert len(pl.patterns) == 3 assert [pat.pattern for pat in pl.patterns ] == [translate(str(i)) for i in range(1, 4)]
def test_multiple_patterns(): pl = PatternList('one', 'two') assert pl.matches('one') == ['one'] assert pl.matches('__one__') == [] assert pl.matches('two') == ['two'] assert pl.matches('__two__') == [] assert pl.matches('one', 'two') == ['one', 'two'] assert pl.matches('one', 'one', 'two', 'two', 'one', 'two') == ['one', 'two'] assert pl.matches('two', 'two', 'one', 'two') == ['two', 'one']
def test_multiple_patterns_multiple_wildcards(): pl = PatternList('*one*', '*two*') assert pl.matches('__one') == ['__one'] assert pl.matches('one__') == ['one__'] assert pl.matches('__one__') == ['__one__'] assert pl.matches('__two') == ['__two'] assert pl.matches('two__') == ['two__'] assert pl.matches('__two__') == ['__two__'] assert pl.matches('__three__') == [] assert pl.matches('two', 'one', '__two__', '__one__', 'three') == ['two', 'one', '__two__', '__one__']
def test_single_pattern(): pl = PatternList('one') assert pl.matches('one') == ['one'] assert pl.matches('one', 'two') == ['one'] assert pl.matches('two', 'one') == ['one'] assert pl.matches('__one__') == [] assert pl.matches('__one__', '__two__') == [] assert pl.matches('one', 'one', 'two', 'one') == ['one']
def test_filter_by_pattern_lists(white, black, expected_hits, expected_misses): whitelist, blacklist = [PatternList(*ls) for ls in (white, black)] hits, misses = filter_by_pattern_lists([one, two, three, four], whitelist, blacklist) assert hits == expected_hits assert misses == expected_misses
def test_multiple_patterns_multiple_camel_wildcards(): pl = PatternList('*one*two', '*two*three*four') assert pl.matches('__one____two') == ['__one____two'] assert pl.matches('one____two') == ['one____two'] assert pl.matches('onetwo') == ['onetwo'] assert pl.matches('onetwo__') == [] assert pl.matches('onewo__') == [] assert pl.matches('_two_three_four') == ['_two_three_four'] assert pl.matches('two_threefour') == ['two_threefour'] assert pl.matches('_twothree_four') == ['_twothree_four'] assert pl.matches('_twothree_four_') == [] assert pl.matches('_twohree_four_') == [] assert pl.matches('onetwo', 'onetwo', 'twothreefour', 'twothreefour') == ['onetwo', 'twothreefour']
def test_unicode_pattern(): pl = PatternList(u'߃ß') assert pl.matches(u'߃ß') == [u'߃ß']