fix parsing of sockstat -4

This commit is contained in:
Alexander Weidinger 2017-01-22 01:09:47 +01:00
parent 2786e209c6
commit 30fe5641c7
2 changed files with 18 additions and 2 deletions

View file

@ -1032,8 +1032,11 @@ def _freebsd_remotes_on(port, which_end):
continue # ignore header
if len(chunks) < 2:
continue
local = chunks[5]
remote = chunks[6]
# sockstat -4 -c -p 4506 does this with high PIDs:
# USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
# salt-master python2.781106 35 tcp4 192.168.12.34:4506 192.168.12.45:60143
local = chunks[-2]
remote = chunks[-1]
lhost, lport = local.split(':')
rhost, rport = remote.split(':')
if which_end == 'local' and int(lport) != port: # ignore if local port not port

View file

@ -92,6 +92,11 @@ USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
root python2.7 1294 41 tcp4 127.0.0.1:61115 127.0.0.1:4506
'''
FREEBSD_SOCKSTAT_WITH_FAT_PID = '''\
USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
salt-master python2.781106 35 tcp4 127.0.0.1:61115 127.0.0.1:4506
'''
@skipIf(NO_MOCK, NO_MOCK_REASON)
class NetworkTestCase(TestCase):
@ -237,6 +242,14 @@ class NetworkTestCase(TestCase):
remotes = network._freebsd_remotes_on('4506', 'remote')
self.assertEqual(remotes, set(['127.0.0.1']))
def test_freebsd_remotes_on_with_fat_pid(self):
with patch('salt.utils.is_sunos', lambda: False):
with patch('salt.utils.is_freebsd', lambda: True):
with patch('subprocess.check_output',
return_value=FREEBSD_SOCKSTAT_WITH_FAT_PID):
remotes = network._freebsd_remotes_on('4506', 'remote')
self.assertEqual(remotes, set(['127.0.0.1']))
@patch('platform.node', MagicMock(return_value='nodename'))
@patch('socket.gethostname', MagicMock(return_value='hostname'))
@patch('socket.getfqdn', MagicMock(return_value='hostname.domainname.blank'))