Module: check_mk
Branch: master
Commit: e7514868705733ccde13297b98f61c40d9223db3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e7514868705733…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Sat Dec 29 19:56:45 2018 +0100
Cleaned up livestatus socket URL parsing a bit
Change-Id: I447122928e5b0d32f06f080a1a6a92667aaa8c6f
---
livestatus/api/python/livestatus.py | 19 +++++++------------
tests/unit/livestatus/test_livestatus_unit.py | 5 +++--
2 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/livestatus/api/python/livestatus.py b/livestatus/api/python/livestatus.py
index 3822e9e..f458bbc 100644
--- a/livestatus/api/python/livestatus.py
+++ b/livestatus/api/python/livestatus.py
@@ -313,20 +313,15 @@ class SingleSiteConnection(Helpers):
def _parse_socket_url(self, url):
# type: (str) -> Tuple[socket.AddressFamily, Union[str, tuple]]
"""Parses a Livestatus socket URL to address family and
address"""
- parts = url.split(":")
- if parts[0] == "unix":
- if len(parts) != 2:
- raise MKLivestatusConfigError(
- "Invalid livestatus unix URL: %s. "
- "Correct example is 'unix:/var/run/nagios/rw/live'"
% url)
+ family_txt, url = url.split(":", 1)
+ if family_txt == "unix":
+ return socket.AF_UNIX, url
- return socket.AF_UNIX, parts[1]
-
- elif parts[0] == "tcp":
+ elif family_txt == "tcp":
try:
- host = parts[1]
- port = int(parts[2])
- except:
+ host, port_txt = url.rsplit(":", 1)
+ port = int(port_txt)
+ except ValueError:
raise MKLivestatusConfigError("Invalid livestatus tcp URL
'%s'. "
"Correct example is
'tcp:somehost:6557'" % url)
return socket.AF_INET, (host, port)
diff --git a/tests/unit/livestatus/test_livestatus_unit.py
b/tests/unit/livestatus/test_livestatus_unit.py
index ecdb8b4..49fafec 100644
--- a/tests/unit/livestatus/test_livestatus_unit.py
+++ b/tests/unit/livestatus/test_livestatus_unit.py
@@ -56,15 +56,16 @@ def test_livestatus_local_connection(sock_path):
@pytest.mark.parametrize("socket_url,result", [
("unix:/omd/sites/heute/tmp/run/live", (socket.AF_UNIX,
"/omd/sites/heute/tmp/run/live")),
+ ("unix:/omd/sites/heute/tmp/run/li:ve", (socket.AF_UNIX,
"/omd/sites/heute/tmp/run/li:ve")),
("tcp:127.0.0.1:1234", (socket.AF_INET, ("127.0.0.1", 1234))),
- ("tcp:127.0.0.1:1234:3", (socket.AF_INET, ("127.0.0.1", 1234))),
+ ("tcp:126.0.0.1:abc", None),
("xyz:bla", None),
])
def test_single_site_connection_socketurl(socket_url, result, monkeypatch):
live = livestatus.SingleSiteConnection(socket_url)
if result is None:
- with pytest.raises(livestatus.MKLivestatusConfigError, match="Invalid
livestatus URL"):
+ with pytest.raises(livestatus.MKLivestatusConfigError, match="Invalid
livestatus"):
live._parse_socket_url(socket_url)
return