mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #55077 from garethgreenaway/55061_fix_network_settings_beacon_wildcard_interface
[master] fixes to network_settings beacon
This commit is contained in:
commit
4907e614bc
2 changed files with 97 additions and 10 deletions
|
@ -12,6 +12,7 @@ try:
|
|||
IP = IPDB()
|
||||
HAS_PYROUTE2 = True
|
||||
except ImportError:
|
||||
IP = None
|
||||
HAS_PYROUTE2 = False
|
||||
|
||||
import ast
|
||||
|
@ -139,7 +140,7 @@ def beacon(config):
|
|||
|
||||
ret = []
|
||||
interfaces = []
|
||||
expanded_config = {}
|
||||
expanded_config = {'interfaces': {}}
|
||||
|
||||
global LAST_STATS
|
||||
|
||||
|
@ -157,20 +158,19 @@ def beacon(config):
|
|||
log.debug('_stats %s', _stats)
|
||||
# Get list of interfaces included in config that are registered in the
|
||||
# system, including interfaces defined by wildcards (eth*, wlan*)
|
||||
for interface in _config.get('interfaces', {}):
|
||||
if interface in _stats:
|
||||
interfaces.append(interface)
|
||||
for interface_config in _config.get('interfaces', {}):
|
||||
if interface_config in _stats:
|
||||
interfaces.append(interface_config)
|
||||
else:
|
||||
# No direct match, try with * wildcard regexp
|
||||
interface_regexp = interface.replace('*', '[0-9]+')
|
||||
for interface in _stats:
|
||||
match = re.search(interface_regexp, interface)
|
||||
for interface_stat in _stats:
|
||||
match = re.search(interface_config, interface_stat)
|
||||
if match:
|
||||
interfaces.append(match.group())
|
||||
expanded_config[match.group()] = config['interfaces'][interface]
|
||||
interfaces.append(interface_stat)
|
||||
expanded_config['interfaces'][interface_stat] = _config['interfaces'][interface_config]
|
||||
|
||||
if expanded_config:
|
||||
config.update(expanded_config)
|
||||
_config['interfaces'].update(expanded_config['interfaces'])
|
||||
|
||||
# config updated so update _config
|
||||
list(map(_config.update, config))
|
||||
|
|
|
@ -5,6 +5,7 @@ from __future__ import absolute_import
|
|||
|
||||
# Salt testing libs
|
||||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import patch, MagicMock
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
try:
|
||||
from pyroute2 import IPDB
|
||||
|
@ -19,6 +20,15 @@ import logging
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MockIPClass(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
|
||||
def by_name(self):
|
||||
return {}
|
||||
|
||||
|
||||
class NetworkSettingsBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test case for salt.beacons.network_settings
|
||||
|
@ -47,6 +57,83 @@ class NetworkSettingsBeaconTestCase(TestCase, LoaderModuleMockMixin):
|
|||
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
def test_interface(self):
|
||||
config = [{'interfaces': {'enp14s0u1u2': {'promiscuity': None}}}]
|
||||
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '0',
|
||||
'group': '0'}})
|
||||
|
||||
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '1',
|
||||
'group': '0'}})
|
||||
|
||||
ret = network_settings.validate(config)
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
with patch.object(network_settings, 'LAST_STATS', {}), \
|
||||
patch.object(network_settings, 'IP', MockIPClass), \
|
||||
patch('salt.beacons.network_settings._copy_interfaces_info',
|
||||
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
|
||||
ret = network_settings.beacon(config)
|
||||
self.assertEqual(ret, [])
|
||||
|
||||
ret = network_settings.beacon(config)
|
||||
_expected = [{'interface': 'enp14s0u1u2',
|
||||
'tag': 'enp14s0u1u2',
|
||||
'change': {'promiscuity': '1'}
|
||||
}]
|
||||
self.assertEqual(ret, _expected)
|
||||
|
||||
def test_interface_no_change(self):
|
||||
config = [{'interfaces': {'enp14s0u1u2': {'promiscuity': None}}}]
|
||||
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '0',
|
||||
'group': '0'}})
|
||||
|
||||
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '0',
|
||||
'group': '0'}})
|
||||
|
||||
ret = network_settings.validate(config)
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
with patch.object(network_settings, 'LAST_STATS', {}), \
|
||||
patch.object(network_settings, 'IP', MockIPClass), \
|
||||
patch('salt.beacons.network_settings._copy_interfaces_info',
|
||||
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
|
||||
ret = network_settings.beacon(config)
|
||||
self.assertEqual(ret, [])
|
||||
|
||||
ret = network_settings.beacon(config)
|
||||
self.assertEqual(ret, [])
|
||||
|
||||
def test_wildcard_interface(self):
|
||||
config = [{'interfaces': {'en*': {'promiscuity': None}}}]
|
||||
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '0',
|
||||
'group': '0'}})
|
||||
|
||||
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
|
||||
'promiscuity': '1',
|
||||
'group': '0'}})
|
||||
|
||||
ret = network_settings.validate(config)
|
||||
self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
||||
|
||||
with patch.object(network_settings, 'LAST_STATS', {}), \
|
||||
patch.object(network_settings, 'IP', MockIPClass), \
|
||||
patch('salt.beacons.network_settings._copy_interfaces_info',
|
||||
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
|
||||
ret = network_settings.beacon(config)
|
||||
self.assertEqual(ret, [])
|
||||
|
||||
ret = network_settings.beacon(config)
|
||||
_expected = [{'interface': 'enp14s0u1u2',
|
||||
'tag': 'enp14s0u1u2',
|
||||
'change': {'promiscuity': '1'}
|
||||
}]
|
||||
self.assertEqual(ret, _expected)
|
||||
|
||||
|
||||
@skipIf(not HAS_PYROUTE2, 'no pyroute2 installed, skipping')
|
||||
class Pyroute2TestCase(TestCase):
|
||||
|
|
Loading…
Add table
Reference in a new issue