From 830b95b48b46dd3dbe41381e26b25336eccd1c28 Mon Sep 17 00:00:00 2001 From: Antoine Meillet Date: Thu, 9 Sep 2021 17:31:07 +0200 Subject: [PATCH] Support singular form in uptime string (#6) Sometimes the uptime is only "1 something", so we need the regex to accept those values. --- napalm_servertech_pro2/utils.py | 4 ++-- tests/utils/test_utils.py | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/napalm_servertech_pro2/utils.py b/napalm_servertech_pro2/utils.py index eeabad2..95787df 100644 --- a/napalm_servertech_pro2/utils.py +++ b/napalm_servertech_pro2/utils.py @@ -4,8 +4,8 @@ def convert_uptime(uptime): """Converts a ServerTech uptime string to a number of seconds.""" time_regex = ( - r"^(?P\d+) days (?P\d+) hours" - r" (?P\d+) minutes (?P\d+) seconds$" + r"^(?P\d+) days? (?P\d+) hours?" + r" (?P\d+) minutes? (?P\d+) seconds?$" ) m = re.match( time_regex, diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 8590f8f..309701f 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -8,7 +8,10 @@ def test_convert_uptime(): uptime = "11 days 4 hours 8 minutes 6 seconds" assert utils.convert_uptime(uptime) == 965286 - uptime = "0 days 0 hours 0 minutes 1 seconds" + uptime = "133 days 1 hour 12 minutes 15 seconds" + assert isinstance(utils.convert_uptime(uptime), int) + + uptime = "0 days 0 hours 0 minutes 1 second" assert utils.convert_uptime(uptime) == 1 with pytest.raises(ValueError):