def testRuleFromText(self): _getRuleAttrs = lambda x: (x.table, x.source, x.destination, x. srcDevice, x.detached) good_rules = { '1: from all lookup main': ('main', None, None, None, False), '2: from 10.0.0.0/8 to 20.0.0.0/8 lookup table_100': ('table_100', '10.0.0.0/8', '20.0.0.0/8', None, False), '3: from all to 8.8.8.8 lookup table_200': ('table_200', None, '8.8.8.8', None, False), '4: from all to 5.0.0.0/8 iif dummy0 [detached] lookup 500': ('500', None, '5.0.0.0/8', 'dummy0', True), '5: from all to 5.0.0.0/8 dev dummy0 lookup 500': ('500', None, '5.0.0.0/8', 'dummy0', False) } for text, attributes in good_rules.iteritems(): rule = Rule.fromText(text) self.assertEqual(_getRuleAttrs(rule), attributes) bad_rules = [ '32766: from all lookup main foo', '2766: lookup main', '276: from 8.8.8.8' '32: from 10.0.0.0/8 to 264.0.0.0/8 lookup table_100' ] for text in bad_rules: self.assertRaises(ValueError, Rule.fromText, text)
def rules(): rules_data = ruleList() for rule_data in rules_data: try: r = Rule.fromText(rule_data) yield IPRuleData(r.destination, r.source, r.srcDevice, r.table) except ValueError: logging.warning('Could not parse rule %s', rule_data)
def testRuleFromText(self): _getRuleAttrs = lambda x: ( x.table, x.source, x.destination, x.srcDevice, x.detached, x.prio, ) good_rules = { '1: from all lookup main': ('main', None, None, None, False, 1), '2: from 10.0.0.0/8 to 20.0.0.0/8 lookup table_100': ( 'table_100', '10.0.0.0/8', '20.0.0.0/8', None, False, 2, ), '3: from all to 8.8.8.8 lookup table_200': ( 'table_200', None, '8.8.8.8', None, False, 3, ), '4: from all to 5.0.0.0/8 iif dummy0 [detached] lookup 500': ( '500', None, '5.0.0.0/8', 'dummy0', True, 4, ), '5: from all to 5.0.0.0/8 dev dummy0 lookup 500': ( '500', None, '5.0.0.0/8', 'dummy0', False, 5, ), } for text, attributes in good_rules.items(): rule = Rule.fromText(text) assert _getRuleAttrs(rule) == attributes bad_rules = [ '32766: from all lookup main foo', '2766: lookup main', '276: from 8.8.8.8' '32: from 10.0.0.0/8 to 264.0.0.0/8 lookup table_100', ] for text in bad_rules: pytest.raises(ValueError, Rule.fromText, text)
def testRuleFromText(self): _getRuleAttrs = lambda x: (x.table, x.source, x.destination, x.srcDevice, x.detached) good_rules = { '1: from all lookup main': ('main', None, None, None, False), '2: from 10.0.0.0/8 to 20.0.0.0/8 lookup table_100': ('table_100', '10.0.0.0/8', '20.0.0.0/8', None, False), '3: from all to 8.8.8.8 lookup table_200': ('table_200', None, '8.8.8.8', None, False), '4: from all to 5.0.0.0/8 iif dummy0 [detached] lookup 500': ('500', None, '5.0.0.0/8', 'dummy0', True), '5: from all to 5.0.0.0/8 dev dummy0 lookup 500': ('500', None, '5.0.0.0/8', 'dummy0', False)} for text, attributes in good_rules.iteritems(): rule = Rule.fromText(text) self.assertEqual(_getRuleAttrs(rule), attributes) bad_rules = ['32766: from all lookup main foo', '2766: lookup main', '276: from 8.8.8.8' '32: from 10.0.0.0/8 to 264.0.0.0/8 lookup table_100'] for text in bad_rules: self.assertRaises(ValueError, Rule.fromText, text)
def delete(rule_data): r = rule_data with _translate_iproute2_exception(IPRuleDeleteError, rule_data): ruleDel(Rule(r.table, r.src, r.to, r.iif))
def add(rule_data): r = rule_data with _translate_iproute2_exception(IPRuleAddError, rule_data): ruleAdd(Rule(r.table, r.src, r.to, r.iif))