Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grains/core: ignore HOST_NOT_FOUND errno in fqdns() #51706

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def linux_distribution(**kwargs):

_INTERFACES = {}

# Possible value for h_errno defined in netdb.h
HOST_NOT_FOUND = 1
NO_DATA = 4


def _windows_cpudata():
'''
Expand Down Expand Up @@ -2225,7 +2229,7 @@ def fqdns():
try:
fqdns.add(socket.getfqdn(socket.gethostbyaddr(ip)[0]))
except socket.herror as err:
if err.errno == 0:
if err.errno in (0, HOST_NOT_FOUND, NO_DATA):
# No FQDN for this IP address, so we don't need to know this all the time.
log.debug("Unable to resolve address %s: %s", ip, err)
else:
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,38 @@ def test_fqdns_return(self):
self.assertEqual(len(fqdns['fqdns']), len(ret['fqdns']))
self.assertEqual(set(fqdns['fqdns']), set(ret['fqdns']))

@skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
@patch.object(salt.utils, 'is_windows', MagicMock(return_value=False))
@patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4']))
@patch('salt.utils.network.ip_addrs6', MagicMock(return_value=[]))
def test_fqdns_socket_error(self):
'''
test the behavior on non-critical socket errors of the dns grain
'''
def _gen_gethostbyaddr(errno):
def _gethostbyaddr(_):
herror = socket.herror()
herror.errno = errno
raise herror
return _gethostbyaddr

for errno in (0, core.HOST_NOT_FOUND, core.NO_DATA):
mock_log = MagicMock()
with patch.object(socket, 'gethostbyaddr',
side_effect=_gen_gethostbyaddr(errno)):
with patch('salt.grains.core.log', mock_log):
self.assertEqual(core.fqdns(), {'fqdns': []})
mock_log.debug.assert_called_once()
mock_log.error.assert_not_called()

mock_log = MagicMock()
with patch.object(socket, 'gethostbyaddr',
side_effect=_gen_gethostbyaddr(-1)):
with patch('salt.grains.core.log', mock_log):
self.assertEqual(core.fqdns(), {'fqdns': []})
mock_log.debug.assert_not_called()
mock_log.error.assert_called_once()

def test_core_virtual(self):
'''
test virtual grain with cmd virt-what
Expand Down