Skip to content

Fixed uptime_string_to_seconds for uptime strings without seconds - #927

Open
TheeCoderAhmed wants to merge 1 commit into
networktocode:developfrom
TheeCoderAhmed:fix/uptime-string-without-seconds
Open

TheeCoderAhmed wants to merge 1 commit into
networktocode:developfrom
TheeCoderAhmed:fix/uptime-string-without-seconds

Conversation

@TheeCoderAhmed

Copy link
Copy Markdown

New Pull Request

Have you:

  • Updated the README if necessary? (not needed)
  • Updated any configuration settings? (not needed)
  • Written a unit test?

Change Notes

Fixes #615

uptime_string_to_seconds("2w1d") raises ValueError: Unable to parse uptime string. The compact pattern in UPTIME_REGEX_PATTERNS requires a trailing <n>s, so a compact uptime with no seconds matches nothing.

This adds a fourth pattern for compact uptimes without seconds. It goes last, so any string the existing patterns already match gets the same result as before. It is anchored to the whole string because uptime_string_to_seconds uses re.search and every unit in the pattern is optional. Without the anchors, strings like "123" or "1mo" would match and return a wrong value, or 0, instead of raising.

Input Before After
2w1d ValueError 1296000
1y3w ValueError 33350400
3d02h ValueError 266400
thisismytime, empty string, 123, 1mo, 1h 5m, up for 2w1d ValueError ValueError

Justification

The issue reports this form in the up-time value of a NETCONF query to a Cisco IOS-XE device. The question in the issue about which units that field can contain is still open. I don't have device output to check against, so the new pattern accepts the same units in the same order as the existing compact pattern, without the seconds. A bare 1y still raises, as it does in the existing pattern.

What I ran, on Python 3.12:

  • tests/unit/test_time.py: the five new uptime cases fail with ValueError: Unable to parse uptime string. on the default branch. All 27 tests in the file pass with the change.
  • ruff check and ruff format --check on the changed Python files.

I did not run the rest of the test suite, pylint, mypy, or the Docker based invoke tasks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uptime string fails parsing if missing seconds

1 participant