|
5 | 5 |
|
6 | 6 | # Salt testing libs
|
7 | 7 | from tests.support.unit import skipIf, TestCase
|
| 8 | +from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock |
8 | 9 | from tests.support.mixins import LoaderModuleMockMixin
|
9 | 10 | try:
|
10 | 11 | from pyroute2 import IPDB
|
@@ -47,6 +48,80 @@ def test_empty_config(self):
|
47 | 48 |
|
48 | 49 | self.assertEqual(ret, (True, 'Valid beacon configuration'))
|
49 | 50 |
|
| 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 | + |
50 | 125 |
|
51 | 126 | @skipIf(not HAS_PYROUTE2, 'no pyroute2 installed, skipping')
|
52 | 127 | class Pyroute2TestCase(TestCase):
|
|
0 commit comments