Merge pull request #50147 from garethgreenaway/50129_network_get_route_fails_dash_in_name

[2018.3] Fix to modules/network.py
This commit is contained in:
Mike Place 2018-10-22 08:31:02 -06:00 committed by GitHub
commit 593c7ff085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View file

@ -1704,7 +1704,7 @@ def get_route(ip):
if __grains__['kernel'] == 'Linux':
cmd = 'ip route get {0}'.format(ip)
out = __salt__['cmd.run'](cmd, python_shell=True)
regexp = re.compile(r'(via\s+(?P<gateway>[\w\.:]+))?\s+dev\s+(?P<interface>[\w\.\:]+)\s+.*src\s+(?P<source>[\w\.:]+)')
regexp = re.compile(r'(via\s+(?P<gateway>[\w\.:]+))?\s+dev\s+(?P<interface>[\w\.\:\-]+)\s+.*src\s+(?P<source>[\w\.:]+)')
m = regexp.search(out.splitlines()[0])
ret = {
'destination': ip,

View file

@ -5,6 +5,7 @@
# Import Python Libs
from __future__ import absolute_import, unicode_literals, print_function
import logging
import socket
import os.path
@ -26,6 +27,8 @@ import salt.modules.network as network
from salt.exceptions import CommandExecutionError
from salt._compat import ipaddress
log = logging.getLogger(__name__)
@skipIf(NO_MOCK, NO_MOCK_REASON)
class NetworkTestCase(TestCase, LoaderModuleMockMixin):
@ -355,3 +358,47 @@ class NetworkTestCase(TestCase, LoaderModuleMockMixin):
with patch.dict(network.__grains__, {'kernel': 'Linux'}):
self.assertListEqual(network.default_route('inet'), [])
def test_get_route(self):
'''
Test for return output from get_route
'''
mock_iproute = MagicMock(return_value='8.8.8.8 via 10.10.10.1 dev eth0 src 10.10.10.10 uid 0\ncache')
with patch.dict(network.__grains__, {'kernel': 'Linux'}):
with patch.dict(network.__salt__, {'cmd.run': mock_iproute}):
expected = {'interface': 'eth0',
'source': '10.10.10.10',
'destination': '8.8.8.8',
'gateway': '10.10.10.1'}
ret = network.get_route('8.8.8.8')
self.assertEqual(ret, expected)
mock_iproute = MagicMock(return_value='8.8.8.8 via 10.10.10.1 dev eth0.1 src 10.10.10.10 uid 0\ncache')
with patch.dict(network.__grains__, {'kernel': 'Linux'}):
with patch.dict(network.__salt__, {'cmd.run': mock_iproute}):
expected = {'interface': 'eth0.1',
'source': '10.10.10.10',
'destination': '8.8.8.8',
'gateway': '10.10.10.1'}
ret = network.get_route('8.8.8.8')
self.assertEqual(ret, expected)
mock_iproute = MagicMock(return_value='8.8.8.8 via 10.10.10.1 dev eth0:1 src 10.10.10.10 uid 0\ncache')
with patch.dict(network.__grains__, {'kernel': 'Linux'}):
with patch.dict(network.__salt__, {'cmd.run': mock_iproute}):
expected = {'interface': 'eth0:1',
'source': '10.10.10.10',
'destination': '8.8.8.8',
'gateway': '10.10.10.1'}
ret = network.get_route('8.8.8.8')
self.assertEqual(ret, expected)
mock_iproute = MagicMock(return_value='8.8.8.8 via 10.10.10.1 dev lan-br0 src 10.10.10.10 uid 0\ncache')
with patch.dict(network.__grains__, {'kernel': 'Linux'}):
with patch.dict(network.__salt__, {'cmd.run': mock_iproute}):
expected = {'interface': 'lan-br0',
'source': '10.10.10.10',
'destination': '8.8.8.8',
'gateway': '10.10.10.1'}
ret = network.get_route('8.8.8.8')
self.assertEqual(ret, expected)