rpc.py: check if port is None during client init

RPC client constructor accepts optional port,
  but does not check if it's None in case of
  remote connection.
This patch adds the check and new error message
  related to it.

Case where port is unexpectedly None
  can be reproduced by starting spdkcli
  when SPDK socket does not exist.

Change-Id: I46e0b99547204c6fdeac421e5de9d6991387e207
Signed-off-by: Vitaliy Mysak <vitaliy.mysak@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/460974
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: Karol Latecki <karol.latecki@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
This commit is contained in:
Vitaliy Mysak 2019-07-09 20:56:14 +00:00 committed by Darek Stojaczyk
parent a15dcb0bf0
commit 9b81498be8

View File

@ -38,7 +38,8 @@ class JSONRPCClient(object):
self._logger.debug("Trying to connect to UNIX socket: %s", addr)
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(addr)
elif ':' in addr:
elif port:
if ':' in addr:
self._logger.debug("Trying to connect to IPv6 address addr:%s, port:%i", addr, port)
for res in socket.getaddrinfo(addr, port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP):
af, socktype, proto, canonname, sa = res
@ -48,6 +49,8 @@ class JSONRPCClient(object):
self._logger.debug("Trying to connect to IPv4 address addr:%s, port:%i'", addr, port)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((addr, port))
else:
raise socket.error("Unix socket '%s' does not exist" % addr)
except socket.error as ex:
raise JSONRPCException("Error while connecting to %s\n"
"Error details: %s" % (addr, ex))