Fixing scenario where the interface for the default route has a dash in it, regular expression used in get_route currently doesn't not account for this.. Adding some additional tests for network.get_route.

This commit is contained in:
Gareth J. Greenaway 2018-10-20 15:55:13 -07:00
parent 0ece06ef66
commit 311796994b
No known key found for this signature in database
GPG key ID: 10B62F8A7CAD7A41
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

@ -26,6 +26,9 @@ import salt.modules.network as network
from salt.exceptions import CommandExecutionError
from salt._compat import ipaddress
import logging
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)