Skip to content

Commit

Permalink
Merge pull request #51706 from aplanas/fix_fqdns
Browse files Browse the repository at this point in the history
grains/core: ignore HOST_NOT_FOUND errno in fqdns()
  • Loading branch information
dwoz authored Dec 9, 2019
2 parents 006f8fd + d4d0901 commit 5a20e90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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 @@ -2226,7 +2230,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 @@ -1002,6 +1002,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

0 comments on commit 5a20e90

Please sign in to comment.