def cleanup_stale_iprules(): """ Clean test ip rules that may have been left by the test run. They may exists on the system due to some buggy test that ran and has not properly cleaned after itself. In case any stale entries have been detected, attempt to clean everything and raise an error. """ commands = [ 'bash', '-c', 'while ip rule delete prio {} 2>/dev/null; do true; done'.format( sourceroute.RULE_PRIORITY ), ] cmd.exec_sync(commands) yield IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) rules = [ r for r in IPRule.rules() if r.to == IPV4_ADDRESS1 or r.prio == sourceroute.RULE_PRIORITY ] if rules: for rule in rules: try: IPRule.delete(rule) logging.warning('Rule (%s) has been removed', rule) except Exception as e: logging.error('Error removing rule (%s): %s', rule, e) raise StaleIPRulesError()
class IPRuleTest(unittest.TestCase): IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) def test_add_delete_and_read_rule(self): rule = IPRuleData(to=IPV4_ADDRESS1, iif='lo', table='main', prio=999) with self.create_rule(rule): rules = [ r for r in IPRuleTest.IPRule.rules() if r.to == IPV4_ADDRESS1 ] self.assertEqual(1, len(rules)) self.assertEqual(rules[0].iif, 'lo') self.assertEqual(rules[0].table, 'main') self.assertEqual(rules[0].prio, 999) def test_delete_non_existing_rule(self): rule = IPRuleData(to=IPV4_ADDRESS1, iif='lo', table='main') with self.assertRaises(IPRuleDeleteError): IPRuleTest.IPRule.delete(rule) def test_add_rule_with_invalid_address(self): rule = IPRuleData(to=IPV4_ADDRESS1, iif='shrubbery_shruberry', table='main') with self.assertRaises(IPRuleAddError): with self.create_rule(rule): pass @contextmanager def create_rule(self, rule_data): IPRuleTest.IPRule.add(rule_data) try: yield finally: IPRuleTest.IPRule.delete(rule_data)
def cleanup_stale_iprules(): """ Clean test ip rules that may have been left by the test run. They may exists on the system due to some buggy test that ran and has not properly cleaned after itself. In case any stale entries have been detected, attempt to clean everything and raise an error. """ commands = [ 'bash', '-c', 'while ip rule delete prio {} 2>/dev/null; do true; done'.format( sourceroute.RULE_PRIORITY) ] cmd.exec_sync(commands) yield IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) rules = [r for r in IPRule.rules() if r.to == IPV4_ADDRESS1 or r.prio == sourceroute.RULE_PRIORITY] if rules: for rule in rules: try: IPRule.delete(rule) logging.warning('Rule (%s) has been removed', rule) except Exception as e: logging.error('Error removing rule (%s): %s', rule, e) raise StaleIPRulesError()
def _cleanup_stale_iprules(): """ Clean test ip rules that may have been left by the test run. They may exists on the system due to some buggy test that ran and has not properly cleaned after itself. In case any stale entries have been detected, attempt to clean everything and raise an error. """ IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) rules = [r for r in IPRule.rules() if r.to == IPV4_ADDRESS1] if rules: for rule in rules: try: IPRule.delete(rule) logging.warning('Rule (%s) has been removed', rule) except Exception as e: logging.error('Error removing rule (%s): %s', rule, e) raise StaleIPRulesError()
from vdsm.common.constants import P_VDSM_RUN from vdsm.network.ip import route as ip_route from vdsm.network.ip import rule as ip_rule from vdsm.network.ip.route import IPRouteData from vdsm.network.ip.route import IPRouteError, IPRouteDeleteError from vdsm.network.ip.rule import IPRuleData from vdsm.network.ip.rule import IPRuleError from .ipwrapper import Route from .ipwrapper import routeShowTable from .ipwrapper import Rule from .ipwrapper import ruleList IPRoute = ip_route.driver(ip_route.Drivers.IPROUTE2) IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) TRACKED_INTERFACES_FOLDER = P_VDSM_RUN + 'trackedInterfaces' RULE_PRIORITY = 32000 class StaticSourceRoute(object): def __init__(self, device, ipaddr, mask, gateway): self.device = device self._ipaddr = ipaddr self._mask = mask self._gateway = gateway self._table = str(self._generateTableId(ipaddr)) if ipaddr else None self._network = self._parse_network(ipaddr, mask)
from vdsm.common.constants import P_VDSM_RUN from vdsm.network.ip import route as ip_route from vdsm.network.ip import rule as ip_rule from vdsm.network.ip.route import IPRouteData from vdsm.network.ip.route import IPRouteError, IPRouteDeleteError from vdsm.network.ip.rule import IPRuleData from vdsm.network.ip.rule import IPRuleError from .ipwrapper import Route from .ipwrapper import routeShowTable from .ipwrapper import Rule from .ipwrapper import ruleList IPRoute = ip_route.driver(ip_route.Drivers.IPROUTE2) IPRule = ip_rule.driver(ip_rule.Drivers.IPROUTE2) TRACKED_INTERFACES_FOLDER = P_VDSM_RUN + 'trackedInterfaces' RULE_PRIORITY = 32000 class StaticSourceRoute(object): def __init__(self, device, ipaddr, mask, gateway): self.device = device self._ipaddr = ipaddr self._mask = mask self._gateway = gateway self._table = str(self._generateTableId(ipaddr)) if ipaddr else None self._network = self._parse_network(ipaddr, mask)
def ip_rule_driver(): return ip_rule.driver(ip_rule.Drivers.IPROUTE2)