mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fix UnboundLocalError when address cannot be resolved in win_network.connect
This commit is contained in:
parent
f009241137
commit
348e930dff
3 changed files with 31 additions and 13 deletions
1
changelog/53371.fixed
Normal file
1
changelog/53371.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Fixed UnboundLocalError when using win_network.connect
|
|
@ -1,8 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Module for gathering and managing network information
|
||||
"""
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import datetime
|
||||
import hashlib
|
||||
|
@ -112,7 +110,7 @@ def ping(host, timeout=False, return_boolean=False):
|
|||
"-n",
|
||||
"4",
|
||||
"-w",
|
||||
six.text_type(timeout),
|
||||
str(timeout),
|
||||
salt.utils.network.sanitize_host(host),
|
||||
]
|
||||
else:
|
||||
|
@ -265,7 +263,7 @@ def get_route(ip):
|
|||
|
||||
salt '*' network.get_route 10.10.10.10
|
||||
"""
|
||||
cmd = "Find-NetRoute -RemoteIPAddress {0}".format(ip)
|
||||
cmd = "Find-NetRoute -RemoteIPAddress {}".format(ip)
|
||||
out = __salt__["cmd.run"](cmd, shell="powershell", python_shell=True)
|
||||
regexp = re.compile(
|
||||
r"^IPAddress\s+:\s(?P<source>[\d\.:]+)?.*"
|
||||
|
@ -498,7 +496,10 @@ def connect(host, port=None, **kwargs):
|
|||
):
|
||||
address = host
|
||||
else:
|
||||
address = "{0}".format(salt.utils.network.sanitize_host(host))
|
||||
address = "{}".format(salt.utils.network.sanitize_host(host))
|
||||
|
||||
# just in case we encounter error on getaddrinfo
|
||||
_address = ("unknown",)
|
||||
|
||||
try:
|
||||
if proto == "udp":
|
||||
|
@ -538,13 +539,13 @@ def connect(host, port=None, **kwargs):
|
|||
skt.shutdown(2)
|
||||
except Exception as exc: # pylint: disable=broad-except
|
||||
ret["result"] = False
|
||||
ret["comment"] = "Unable to connect to {0} ({1}) on {2} port {3}".format(
|
||||
ret["comment"] = "Unable to connect to {} ({}) on {} port {}".format(
|
||||
host, _address[0], proto, port
|
||||
)
|
||||
return ret
|
||||
|
||||
ret["result"] = True
|
||||
ret["comment"] = "Successfully connected to {0} ({1}) on {2} port {3}".format(
|
||||
ret["comment"] = "Successfully connected to {} ({}) on {} port {}".format(
|
||||
host, _address[0], proto, port
|
||||
)
|
||||
return ret
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
||||
"""
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
import socket
|
||||
|
||||
import salt.modules.win_network as win_network
|
||||
|
||||
|
@ -24,7 +23,7 @@ except ImportError:
|
|||
HAS_WMI = False
|
||||
|
||||
|
||||
class Mockwmi(object):
|
||||
class Mockwmi:
|
||||
"""
|
||||
Mock wmi class
|
||||
"""
|
||||
|
@ -35,7 +34,7 @@ class Mockwmi(object):
|
|||
pass
|
||||
|
||||
|
||||
class Mockwinapi(object):
|
||||
class Mockwinapi:
|
||||
"""
|
||||
Mock winapi class
|
||||
"""
|
||||
|
@ -43,7 +42,7 @@ class Mockwinapi(object):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
class winapi(object):
|
||||
class winapi:
|
||||
"""
|
||||
Mock winapi class
|
||||
"""
|
||||
|
@ -51,7 +50,7 @@ class Mockwinapi(object):
|
|||
def __init__(self):
|
||||
pass
|
||||
|
||||
class Com(object):
|
||||
class Com:
|
||||
"""
|
||||
Mock Com method
|
||||
"""
|
||||
|
@ -312,3 +311,20 @@ class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
|
|||
"source": "10.0.0.15",
|
||||
},
|
||||
)
|
||||
|
||||
def test_connect_53371(self):
|
||||
"""
|
||||
Test that UnboundLocalError is not thrown on socket.gaierror
|
||||
as reported in #53371
|
||||
"""
|
||||
with patch(
|
||||
"socket.getaddrinfo",
|
||||
MagicMock(side_effect=socket.gaierror("[Errno 11004] getaddrinfo failed")),
|
||||
):
|
||||
rtn = win_network.connect("test-server", 80)
|
||||
assert rtn
|
||||
assert not rtn["result"]
|
||||
assert (
|
||||
rtn["comment"]
|
||||
== "Unable to connect to test-server (unknown) on tcp port 80"
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue