Skip to content

Commit

Permalink
Add next_hop_ip value to unique route id string, other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
grybak-arista committed Oct 15, 2015
1 parent c1eb13f commit 5c3d4ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 88 deletions.
22 changes: 8 additions & 14 deletions pyeapi/api/staticroute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<ip_dest>[^\s]+)\s
# (?P<next_hop>[^\s$]+)
# [\s|$]{0,1}(?P<next_hop_ip>\d+\.\d+\.\d+\.\d+)?
# [\s|$](?P<distance>\d+)
# [\s|$]{1}(?:tag\s(?P<tag>\d+))?
# [\s|$]{1}(?:name\s(?P<name>.+))?
# ''', re.X)
# Define the regex to match ip route lines (by lines in regex):
# 'ip route' header
# ip_dest
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/running_config.text
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 8 additions & 64 deletions test/system/test_api_staticroute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand All @@ -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.
Expand Down
22 changes: 13 additions & 9 deletions test/unit/test_api_staticroute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down

0 comments on commit 5c3d4ae

Please sign in to comment.