Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #242 from napalm-automation/develop
Browse files Browse the repository at this point in the history
napalm-base 0.23.3 release
  • Loading branch information
ktbyers authored May 3, 2017
2 parents c89dd65 + 111de1e commit 6f37907
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 29 deletions.
9 changes: 4 additions & 5 deletions napalm_base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ def __del__(self):
is released (unlocked).
"""
try:
self.close()
except OSError as e:
# Ignore if socket was already closed
if 'is closed' in str(e):
pass
if self.is_alive()["is_alive"]:
self.close()
except NotImplementedError:
pass

@staticmethod
def __raise_clean_exception(exc_type, exc_value, exc_traceback):
Expand Down
10 changes: 10 additions & 0 deletions napalm_base/test/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@

class TestConfigNetworkDriver(object):

@classmethod
def setup_class(cls):
"""Added for py.test/nosetests compatibility"""
cls.setUpClass()

@classmethod
def teardown_class(cls):
"""Added for py.test/nosetests compatibility"""
cls.tearDownClass()

@classmethod
def tearDownClass(cls):
cls.device.load_replace_candidate(filename='%s/initial.conf' % cls.vendor)
Expand Down
47 changes: 28 additions & 19 deletions napalm_base/utils/string_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,35 @@ def hyphen_range(string):


def convert_uptime_string_seconds(uptime):
'''
Convert uptime strings to seconds. The string can be formatted various ways, eg.
1 hour, 56 minutes
'''
regex1 = (r"((?P<weeks>\d+) week(s)?,\s+)?((?P<days>\d+) day(s)?,\s+)?((?P<hours>\d+) "
r"hour(s)?,\s+)?((?P<minutes>\d+) minute(s)?)")
regex_1 = re.compile(regex1)
regex_2 = re.compile(r"((?P<hours>\d+)):((?P<minutes>\d+)):((?P<seconds>\d+))")
regex3 = (r"((?P<weeks>\d+)w)?((?P<days>\d+)d)?((?P<hours>\d+)h)?"
r"((?P<minutes>\d+)m)?((?P<seconds>\d+)s)?")
regex_3 = re.compile(regex3)
regex_list = [regex_1, regex_2, regex_3]
uptime_dict = dict()
'''Convert uptime strings to seconds. The string can be formatted various ways.'''
regex_list = [
# n years, n weeks, n days, n hours, n minutes where each of the fields except minutes
# is optional. Additionally, can be either singular or plural
(r"((?P<years>\d+) year(s)?,\s+)?((?P<weeks>\d+) week(s)?,\s+)?"
r"((?P<days>\d+) day(s)?,\s+)?((?P<hours>\d+) "
r"hour(s)?,\s+)?((?P<minutes>\d+) minute(s)?)"),
# n days, HH:MM:SS where each field is required (except for days)
(r"((?P<days>\d+) day(s)?,\s+)?"
r"((?P<hours>\d+)):((?P<minutes>\d+)):((?P<seconds>\d+))"),
# 7w6d5h4m3s where each field is optional
(r"((?P<weeks>\d+)w)?((?P<days>\d+)d)?((?P<hours>\d+)h)?"
r"((?P<minutes>\d+)m)?((?P<seconds>\d+)s)?"),
]
regex_list = [re.compile(x) for x in regex_list]

uptime_dict = {}
for regex in regex_list:
uptime_dict = regex.search(uptime)
if uptime_dict is not None:
uptime_dict = uptime_dict.groupdict()
match = regex.search(uptime)
if match:
uptime_dict = match.groupdict()
break
uptime_dict = dict()
uptime_seconds = 0

uptime_seconds = 0
for unit, value in uptime_dict.items():
if value is not None:
if unit == 'weeks':
if unit == 'years':
uptime_seconds += int(value) * 31536000
elif unit == 'weeks':
uptime_seconds += int(value) * 604800
elif unit == 'days':
uptime_seconds += int(value) * 86400
Expand All @@ -110,4 +115,8 @@ def convert_uptime_string_seconds(uptime):
uptime_seconds += int(value)
else:
raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime))

if not uptime_dict:
raise Exception('Unrecognized uptime string:{}'.format(uptime))

return uptime_seconds
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="napalm-base",
version='0.23.2',
version='0.23.3',
packages=find_packages(),
author="David Barroso",
author_email="[email protected]",
Expand Down
63 changes: 59 additions & 4 deletions test/unit/TestHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import napalm_base.helpers
import napalm_base.exceptions
from napalm_base.base import NetworkDriver
from napalm_base.utils.string_parsers import convert_uptime_string_seconds


class TestBaseHelpers(unittest.TestCase):
Expand Down Expand Up @@ -302,21 +303,75 @@ def test_ip(self):
'2001:db8:85a3::8a2e:370:7334'
)

def test_convert_uptime_string_seconds(self):
"""
Tests the parser function ```convert_uptime_string_seconds```:
* check if all raw uptime strings passed return the expected uptime in seconds
"""

# Regex 1
self.assertEqual(convert_uptime_string_seconds('24 days, 11 hours, 25 minutes'), 2114700)
self.assertEqual(convert_uptime_string_seconds('1 hour, 5 minutes'), 3900)
self.assertEqual(convert_uptime_string_seconds('1 year, 2 weeks, 5 minutes'), 32745900)
self.assertEqual(
convert_uptime_string_seconds('95 weeks, 2 days, 10 hours, 58 minutes'), 57668280)
self.assertEqual(
convert_uptime_string_seconds('26 weeks, 2 days, 7 hours, 7 minutes'), 15923220)
self.assertEqual(
convert_uptime_string_seconds('19 weeks, 2 days, 2 hours, 2 minutes'), 11671320)
self.assertEqual(
convert_uptime_string_seconds('15 weeks, 3 days, 5 hours, 57 minutes'), 9352620)
self.assertEqual(
convert_uptime_string_seconds('1 year, 8 weeks, 15 minutes'), 36375300)
self.assertEqual(
convert_uptime_string_seconds('8 weeks, 2 hours, 5 minutes'), 4845900)
self.assertEqual(
convert_uptime_string_seconds('8 weeks, 2 hours, 1 minute'), 4845660)
self.assertEqual(
convert_uptime_string_seconds('2 years, 40 weeks, 1 day, 22 hours, 3 minutes'),
87429780)
self.assertEqual(
convert_uptime_string_seconds('2 years, 40 weeks, 1 day, 19 hours, 46 minutes'),
87421560)
self.assertEqual(
convert_uptime_string_seconds('1 year, 39 weeks, 15 hours, 23 minutes'), 55178580)
self.assertEqual(
convert_uptime_string_seconds('33 weeks, 19 hours, 12 minutes'), 20027520)
self.assertEqual(
convert_uptime_string_seconds('33 weeks, 19 hours, 8 minutes'), 20027280)
self.assertEqual(
convert_uptime_string_seconds('33 weeks, 19 hours, 10 minutes'), 20027400)
self.assertEqual(
convert_uptime_string_seconds('51 weeks, 5 days, 13 hours, 0 minutes'), 31323600)
self.assertEqual(
convert_uptime_string_seconds('51 weeks, 5 days, 12 hours, 57 minutes'), 31323420)
self.assertEqual(
convert_uptime_string_seconds('51 weeks, 5 days, 12 hours, 55 minutes'), 31323300)
self.assertEqual(
convert_uptime_string_seconds('51 weeks, 5 days, 12 hours, 58 minutes'), 31323480)

# Regex 2
self.assertEqual(convert_uptime_string_seconds('114 days, 22:27:32'), 9930452)
self.assertEqual(convert_uptime_string_seconds('0 days, 22:27:32'), 80852)
self.assertEqual(convert_uptime_string_seconds('365 days, 5:01:44'), 31554104)

# Regex 3
self.assertEqual(convert_uptime_string_seconds('7w6d5h4m3s'), 4770243)
self.assertEqual(convert_uptime_string_seconds('95w2d10h58m'), 57668280)
self.assertEqual(convert_uptime_string_seconds('1h5m'), 3900)


class FakeNetworkDriver(NetworkDriver):

def __init__(self):

"""Connection details not needed."""

pass

def load_merge_candidate(self, config=None):

"""
This method is called at the end of the helper ```load_template```.
To check whether the test arrives at the very end of the helper function,
will return True instead of raising NotImplementedError exception.
"""

return True

0 comments on commit 6f37907

Please sign in to comment.