mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
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:
parent
0ece06ef66
commit
311796994b
2 changed files with 48 additions and 1 deletions
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue