class IpRouteCommand(IpDeviceCommandBase):
COMMAND = 'route'
+ def __init__(self, parent, table=None):
+ super(IpRouteCommand, self).__init__(parent)
+ self._table = table
+
+ def table(self, table):
+ """Return an instance of IpRouteCommand which works on given table"""
+ return IpRouteCommand(self._parent, table)
+
+ def _table_args(self):
+ return ['table', self._table] if self._table else []
+
def add_gateway(self, gateway, metric=None, table=None):
ip_version = get_ip_version(gateway)
args = ['replace', 'default', 'via', gateway]
args += ['dev', self.name]
if table:
args += ['table', table]
+ else:
+ args += self._table_args()
self._as_root([ip_version], tuple(args))
def delete_gateway(self, gateway, table=None):
'dev', self.name]
if table:
args += ['table', table]
+ else:
+ args += self._table_args()
try:
self._as_root([ip_version], tuple(args))
except RuntimeError as rte:
def list_onlink_routes(self, ip_version):
def iterate_routes():
- output = self._run([ip_version],
- ('list',
- 'dev', self.name,
- 'scope', 'link'))
+ args = ['list', 'dev', self.name, 'scope', 'link']
+ args += self._table_args()
+ output = self._run([ip_version], tuple(args))
for line in output.split('\n'):
line = line.strip()
if line and not line.count('src'):
def add_onlink_route(self, cidr):
ip_version = get_ip_version(cidr)
- self._as_root([ip_version],
- ('replace', cidr,
- 'dev', self.name,
- 'scope', 'link'))
+ args = ['replace', cidr, 'dev', self.name, 'scope', 'link']
+ args += self._table_args()
+ self._as_root([ip_version], tuple(args))
def delete_onlink_route(self, cidr):
ip_version = get_ip_version(cidr)
- self._as_root([ip_version],
- ('del', cidr,
- 'dev', self.name,
- 'scope', 'link'))
+ args = ['del', cidr, 'dev', self.name, 'scope', 'link']
+ args += self._table_args()
+ self._as_root([ip_version], tuple(args))
def get_gateway(self, scope=None, filters=None, ip_version=None):
options = [ip_version] if ip_version else []
args = ['list', 'dev', self.name]
+ args += self._table_args()
if filters:
args += filters
'dev', self.parent.name,
'table', self.table))
+ def test_add_gateway_subtable(self):
+ self.route_cmd.table(self.table).add_gateway(self.gateway, self.metric)
+ self._assert_sudo([self.ip_version],
+ ('replace', 'default',
+ 'via', self.gateway,
+ 'metric', self.metric,
+ 'dev', self.parent.name,
+ 'table', self.table))
+
def test_del_gateway_success(self):
self.route_cmd.delete_gateway(self.gateway, table=self.table)
self._assert_sudo([self.ip_version],
'dev', self.parent.name,
'table', self.table))
+ def test_del_gateway_success_subtable(self):
+ self.route_cmd.table(table=self.table).delete_gateway(self.gateway)
+ self._assert_sudo([self.ip_version],
+ ('del', 'default',
+ 'via', self.gateway,
+ 'dev', self.parent.name,
+ 'table', self.table))
+
def test_del_gateway_cannot_find_device(self):
self.parent._as_root.side_effect = RuntimeError("Cannot find device")
'dev', self.parent.name,
'table', self.table))
+ def test_list_onlink_routes_subtable(self):
+ self.parent._run.return_value = (
+ "10.0.0.0/22\n"
+ "172.24.4.0/24 proto kernel src 172.24.4.2\n")
+ routes = self.route_cmd.table(self.table).list_onlink_routes(
+ self.ip_version)
+ self.assertEqual(['10.0.0.0/22'], routes)
+ self._assert_call([self.ip_version],
+ ('list', 'dev', self.parent.name, 'scope', 'link',
+ 'table', self.table))
+
+ def test_add_onlink_route_subtable(self):
+ self.route_cmd.table(self.table).add_onlink_route(self.cidr)
+ self._assert_sudo([self.ip_version],
+ ('replace', self.cidr,
+ 'dev', self.parent.name,
+ 'scope', 'link',
+ 'table', self.table))
+
+ def test_delete_onlink_route_subtable(self):
+ self.route_cmd.table(self.table).delete_onlink_route(self.cidr)
+ self._assert_sudo([self.ip_version],
+ ('del', self.cidr,
+ 'dev', self.parent.name,
+ 'scope', 'link',
+ 'table', self.table))
+
class TestIPv6IpRouteCommand(TestIpRouteCommand):
def setUp(self):