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

Ignore exceptions when generating the minion ID #51161

Merged
merged 2 commits into from Jan 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion salt/utils/network.py
Expand Up @@ -137,7 +137,23 @@ def filter(self, element):
def first(self):
return self and self[0] or None

hosts = DistinctList().append(socket.getfqdn()).append(platform.node()).append(socket.gethostname())
hosts = DistinctList([])

try:
hosts.append(socket.getfqdn())
except ValueError:
pass

try:
hosts.append(platform.node())
except ValueError:
pass

try:
hosts.append(socket.gethostname())
except ValueError:
pass

if not hosts:
try:
for a_nfo in socket.getaddrinfo(hosts.first() or 'localhost', None, socket.AF_INET,
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/utils/test_network.py
Expand Up @@ -181,6 +181,15 @@ def test__generate_minion_id_with_unicode_in_etc_hosts(self):
with patch('salt.utils.files.fopen', fopen_mock):
assert 'thisismyhostname' in network._generate_minion_id()

def test_generate_minion_id_with_long_hostname(self):
'''
Test that hostnames longer than 63 characters do not raise
an exception when generating the minion ID
'''
with patch('socket.gethostbyaddr') as mock_gethostbyname:
mock_gethostbyname.side_effect = UnicodeError('encoding with \'idna\' codec failed')
self.assertTrue(network.generate_minion_id())

def test_is_ip(self):
self.assertTrue(network.is_ip('10.10.0.3'))
self.assertFalse(network.is_ip('0.9.800.1000'))
Expand Down