Skip to content

Commit 6f43700

Browse files
Fixing the wildcard support in the network_settings beacon. Adding tests.
1 parent d04f97a commit 6f43700

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

salt/beacons/network_settings.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def beacon(config):
139139

140140
ret = []
141141
interfaces = []
142-
expanded_config = {}
142+
expanded_config = {'interfaces': {}}
143143

144144
global LAST_STATS
145145

@@ -157,20 +157,19 @@ def beacon(config):
157157
log.debug('_stats %s', _stats)
158158
# Get list of interfaces included in config that are registered in the
159159
# system, including interfaces defined by wildcards (eth*, wlan*)
160-
for interface in _config.get('interfaces', {}):
161-
if interface in _stats:
162-
interfaces.append(interface)
160+
for interface_config in _config.get('interfaces', {}):
161+
if interface_config in _stats:
162+
interfaces.append(interface_config)
163163
else:
164164
# No direct match, try with * wildcard regexp
165-
interface_regexp = interface.replace('*', '[0-9]+')
166-
for interface in _stats:
167-
match = re.search(interface_regexp, interface)
165+
for interface_stat in _stats:
166+
match = re.search(interface_config, interface_stat)
168167
if match:
169-
interfaces.append(match.group())
170-
expanded_config[match.group()] = config['interfaces'][interface]
168+
interfaces.append(interface_stat)
169+
expanded_config['interfaces'][interface_stat] = _config['interfaces'][interface_config]
171170

172171
if expanded_config:
173-
config.update(expanded_config)
172+
_config['interfaces'].update(expanded_config['interfaces'])
174173

175174
# config updated so update _config
176175
list(map(_config.update, config))

tests/unit/beacons/test_network_settings.py

+75
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# Salt testing libs
77
from tests.support.unit import skipIf, TestCase
8+
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
89
from tests.support.mixins import LoaderModuleMockMixin
910
try:
1011
from pyroute2 import IPDB
@@ -47,6 +48,80 @@ def test_empty_config(self):
4748

4849
self.assertEqual(ret, (True, 'Valid beacon configuration'))
4950

51+
def test_interface(self):
52+
config = [{'interfaces': {'enp14s0u1u2': {'promiscuity': None}}}]
53+
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
54+
'promiscuity': '0',
55+
'group': '0'}})
56+
57+
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
58+
'promiscuity': '1',
59+
'group': '0'}})
60+
61+
ret = network_settings.validate(config)
62+
self.assertEqual(ret, (True, 'Valid beacon configuration'))
63+
64+
with patch.object(network_settings, 'LAST_STATS', {}), \
65+
patch('salt.beacons.network_settings._copy_interfaces_info',
66+
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
67+
ret = network_settings.beacon(config)
68+
self.assertEqual(ret, [])
69+
70+
ret = network_settings.beacon(config)
71+
_expected = [{'interface': 'enp14s0u1u2',
72+
'tag': 'enp14s0u1u2',
73+
'change': {'promiscuity': '1'}
74+
}]
75+
self.assertEqual(ret, _expected)
76+
77+
def test_interface_no_change(self):
78+
config = [{'interfaces': {'enp14s0u1u2': {'promiscuity': None}}}]
79+
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
80+
'promiscuity': '0',
81+
'group': '0'}})
82+
83+
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
84+
'promiscuity': '0',
85+
'group': '0'}})
86+
87+
ret = network_settings.validate(config)
88+
self.assertEqual(ret, (True, 'Valid beacon configuration'))
89+
90+
with patch.object(network_settings, 'LAST_STATS', {}), \
91+
patch('salt.beacons.network_settings._copy_interfaces_info',
92+
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
93+
ret = network_settings.beacon(config)
94+
self.assertEqual(ret, [])
95+
96+
ret = network_settings.beacon(config)
97+
self.assertEqual(ret, [])
98+
99+
def test_wildcard_interface(self):
100+
config = [{'interfaces': {'en*': {'promiscuity': None}}}]
101+
LAST_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
102+
'promiscuity': '0',
103+
'group': '0'}})
104+
105+
NEW_STATS = network_settings._copy_interfaces_info({'enp14s0u1u2': {'family': '0',
106+
'promiscuity': '1',
107+
'group': '0'}})
108+
109+
ret = network_settings.validate(config)
110+
self.assertEqual(ret, (True, 'Valid beacon configuration'))
111+
112+
with patch.object(network_settings, 'LAST_STATS', {}), \
113+
patch('salt.beacons.network_settings._copy_interfaces_info',
114+
MagicMock(side_effect=[LAST_STATS, NEW_STATS])):
115+
ret = network_settings.beacon(config)
116+
self.assertEqual(ret, [])
117+
118+
ret = network_settings.beacon(config)
119+
_expected = [{'interface': 'enp14s0u1u2',
120+
'tag': 'enp14s0u1u2',
121+
'change': {'promiscuity': '1'}
122+
}]
123+
self.assertEqual(ret, _expected)
124+
50125

51126
@skipIf(not HAS_PYROUTE2, 'no pyroute2 installed, skipping')
52127
class Pyroute2TestCase(TestCase):

0 commit comments

Comments
 (0)