From 5c3d4ae4389d50c05a9fd51b2d7f31087de04f02 Mon Sep 17 00:00:00 2001 From: Gary Rybak Date: Thu, 15 Oct 2015 09:02:52 -0600 Subject: [PATCH] Add next_hop_ip value to unique route id string, other fixes --- pyeapi/api/staticroute.py | 22 ++++----- test/fixtures/running_config.text | 2 +- test/system/test_api_staticroute.py | 72 ++++------------------------- test/unit/test_api_staticroute.py | 22 +++++---- 4 files changed, 30 insertions(+), 88 deletions(-) diff --git a/pyeapi/api/staticroute.py b/pyeapi/api/staticroute.py index 2a8ac9e..f9e985c 100644 --- a/pyeapi/api/staticroute.py +++ b/pyeapi/api/staticroute.py @@ -56,15 +56,6 @@ from pyeapi.api import EntityCollection -# Create the regular expression for matching ip route strings -# ROUTES_RE = re.compile(r'''(?<=^ip\sroute\s) -# (?P[^\s]+)\s -# (?P[^\s$]+) -# [\s|$]{0,1}(?P\d+\.\d+\.\d+\.\d+)? -# [\s|$](?P\d+) -# [\s|$]{1}(?:tag\s(?P\d+))? -# [\s|$]{1}(?:name\s(?P.+))? -# ''', re.X) # Define the regex to match ip route lines (by lines in regex): # 'ip route' header # ip_dest @@ -80,9 +71,11 @@ r' (\d+)' r'(?: tag (\d+))?' r'(?: name (\S+))?', re.M) -# (?<=^ip route )(\d+\.\d+\.\d+\.\d+\/\d+) (\d+\.\d+\.\d+\.\d+|[^\s]+)(?: (\d+\.\d+\.\d+\.\d+))?( \d+)(?: tag (\d+))?(?: name (\S+))? -ROUTE_ID = "%s--%s--%s" +# Define a format for the unique route id +# The four parts in order are ip_dest, next_hop, next_hop_ip, and distance +ROUTE_ID = "%s--%s--%s--%s" + class StaticRoute(EntityCollection): """The StaticRoute class provides a configuration instance @@ -93,7 +86,7 @@ class StaticRoute(EntityCollection): def __str__(self): return 'StaticRoute' - def get(self, ip_dest, next_hop, distance): + def get(self, ip_dest, next_hop, distance, next_hop_ip=None): """Retrieves the ip route information for the route specified by the ip_dest, next_hop, and distance parameters @@ -116,7 +109,7 @@ def get(self, ip_dest, next_hop, distance): distance = 1 # Make the unique route_id for the requested route - route_id = ROUTE_ID % (ip_dest, next_hop, distance) + route_id = ROUTE_ID % (ip_dest, next_hop, next_hop_ip, distance) # Return the route configuration if found, or return None return self.getall().get(route_id) @@ -148,7 +141,8 @@ def getall(self): # Build a unique route_id from the ip_dest, next_hop, and distance route_id = ROUTE_ID % \ - (route['ip_dest'], route['next_hop'], route['distance']) + (route['ip_dest'], route['next_hop'], + route['next_hop_ip'], route['distance']) # Update the routes dict routes.update({route_id: route}) diff --git a/test/fixtures/running_config.text b/test/fixtures/running_config.text index b1b395e..0a6666e 100644 --- a/test/fixtures/running_config.text +++ b/test/fixtures/running_config.text @@ -1585,7 +1585,7 @@ ip virtual-router mac-address advertisement-interval 30 ! no ipv6 hardware fib nexthop-index ! -ip route 0.0.0.0/0 192.168.1.254 1 tag 0 +ip route 0.0.0.0/0 192.68.1.254 1 tag 0 ip route 1.2.3.0/24 Ethernet1 1.1.1.1 1 tag 1 name test1 ip route 1.2.3.0/24 Ethernet1 1.1.1.1 10 tag 1 name test1 ip route 1.2.3.0/24 Ethernet1 10.1.1.1 20 tag 1 name test1 diff --git a/test/system/test_api_staticroute.py b/test/system/test_api_staticroute.py index 9e4fe2b..34461f0 100644 --- a/test/system/test_api_staticroute.py +++ b/test/system/test_api_staticroute.py @@ -65,25 +65,15 @@ def _next_hop(): def _distance(): - # XXX - # distance = choice(DISTANCES) - # if distance: - return random_int(1, 255) - # return distance + return random_int(1, 255) def _tag(): - # tag = choice(TAGS) - # if tag: - return random_int(0, 255) - # return tag + return random_int(0, 255) def _route_name(): - # route_name = choice(ROUTE_NAMES) - # if route_name: - return random_string(minchar=4, maxchar=10) - # return route_name + return random_string(minchar=4, maxchar=10) class TestApiStaticroute(DutSystemTest): @@ -141,7 +131,8 @@ def test_get(self): 'tag': tag, 'route_name': route_name} - result = dut.api('staticroute').get(ip_dest, next_hop, distance) + result = dut.api('staticroute').get(ip_dest, next_hop, distance, + next_hop_ip=next_hop_ip) # Make sure the funtion returns a true result (match found) self.assertTrue(result) @@ -173,21 +164,21 @@ def test_getall(self): dut.config([route1, route2, route3]) routes = { - '1.2.3.0/24--Ethernet1--10': + '1.2.3.0/24--Ethernet1--1.1.1.1--10': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '1.1.1.1', 'distance': '10', 'tag': '1', 'route_name': 'test1'}, - '1.2.3.0/24--Ethernet1--1': + '1.2.3.0/24--Ethernet1--1.1.1.1--1': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '1.1.1.1', 'distance': '1', 'tag': '1', 'route_name': 'test10'}, - '1.2.3.0/24--Ethernet1--2': + '1.2.3.0/24--Ethernet1--1.1.1.1--2': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '1.1.1.1', @@ -202,53 +193,6 @@ def test_getall(self): # Assert that the result dict is equivalent to the routes dict self.assertEqual(result, routes) - # # Check that all three routes are returned when only using - # # the ip dest and next hop for the search - # exp_size = 3 - # result = dut.api('staticroute').get_all('1.2.3.0/24', 'Ethernet1') - # size = len(result) - # # Assert that size of returned list is as expected - # self.assertEqual(size, exp_size) - # # Assert each expected route is in the list correctly - # self.assertTrue(route1 in result) - # self.assertTrue(route2 in result) - # self.assertTrue(route3 in result) - # - # # Check that routes 1 and 3 are returned when specifying - # # route name as 'test1' - # exp_size = 2 - # result = dut.api('staticroute').get_all('1.2.3.0/24', 'Ethernet1', - # route_name='test1') - # size = len(result) - # # Assert that size of returned list is as expected - # self.assertEqual(size, exp_size) - # # Assert each expected route is in the list correctly - # self.assertTrue(route1 in result) - # self.assertTrue(route3 in result) - # - # # Check that routes 1 and 2 are returned when specifying - # # tag as '1' - # exp_size = 2 - # result = dut.api('staticroute').get_all('1.2.3.0/24', 'Ethernet1', - # tag='1') - # size = len(result) - # # Assert that size of returned list is as expected - # self.assertEqual(size, exp_size) - # # Assert each expected route is in the list correctly - # self.assertTrue(route1 in result) - # self.assertTrue(route2 in result) - # - # # Check that only route 1 is returned when specifying the - # # distance as 10 - # exp_size = 1 - # result = dut.api('staticroute').get_all('1.2.3.0/24', 'Ethernet1', - # distance='10') - # size = len(result) - # # Assert that size of returned list is as expected - # self.assertEqual(size, exp_size) - # # Assert each expected route is in the list correctly - # self.assertTrue(route1 in result) - def test_delete(self): # Validate the delete function returns without an error # when deleting routes with varying parameters included. diff --git a/test/unit/test_api_staticroute.py b/test/unit/test_api_staticroute.py index 808c65b..fb53292 100644 --- a/test/unit/test_api_staticroute.py +++ b/test/unit/test_api_staticroute.py @@ -62,29 +62,33 @@ def test_get(self): ip_dest = '0.0.0.0/0' next_hop = '192.68.1.254' + next_hop_ip = None distance = '1' route = dict(ip_dest=ip_dest, next_hop=next_hop, - next_hop_ip=None, + next_hop_ip=next_hop_ip, distance=distance, tag='0', route_name=None) - result = self.instance.get(ip_dest, next_hop, distance) + result = self.instance.get(ip_dest, next_hop, distance, + next_hop_ip=next_hop_ip) self.assertEqual(result, route) ip_dest = '1.2.3.0/24' next_hop = 'Ethernet1' + next_hop_ip = '1.1.1.1' distance = '1' route = dict(ip_dest=ip_dest, next_hop=next_hop, - next_hop_ip='1.1.1.1', + next_hop_ip=next_hop_ip, distance='1', tag='1', route_name='test1') - result = self.instance.get(ip_dest, next_hop, distance) + result = self.instance.get(ip_dest, next_hop, distance, + next_hop_ip=next_hop_ip) self.assertEqual(result, route) - def test_get_all(self): + def test_getall(self): # Test retrieval of all static route entries # Assumes running_config.text file contains the following # ip route specifications, and that no additional routes @@ -96,28 +100,28 @@ def test_get_all(self): # ip route 1.2.3.0/24 Ethernet1 10.1.1.1 20 tag 1 name test1 routes = { - '0.0.0.0/0--192.68.1.254--1': + '0.0.0.0/0--192.68.1.254--None--1': {'ip_dest': '0.0.0.0/0', 'next_hop': '192.68.1.254', 'next_hop_ip': None, 'distance': '1', 'tag': '0', 'route_name': None}, - '1.2.3.0/24--Ethernet1--1': + '1.2.3.0/24--Ethernet1--1.1.1.1--1': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '1.1.1.1', 'distance': '1', 'tag': '1', 'route_name': 'test1'}, - '1.2.3.0/24--Ethernet1--10': + '1.2.3.0/24--Ethernet1--1.1.1.1--10': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '1.1.1.1', 'distance': '10', 'tag': '1', 'route_name': 'test1'}, - '1.2.3.0/24--Ethernet1--20': + '1.2.3.0/24--Ethernet1--10.1.1.1--20': {'ip_dest': '1.2.3.0/24', 'next_hop': 'Ethernet1', 'next_hop_ip': '10.1.1.1',