1
0

test_network_settings.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import logging
  5. # Salt libs
  6. import salt.beacons.network_settings as network_settings
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. # Salt testing libs
  10. from tests.support.unit import TestCase, skipIf
  11. try:
  12. from pyroute2 import IPDB
  13. HAS_PYROUTE2 = True
  14. except ImportError:
  15. HAS_PYROUTE2 = False
  16. log = logging.getLogger(__name__)
  17. class MockIPClass(object):
  18. def __init__(self, *args, **kwargs):
  19. self.args = args
  20. self.kwargs = kwargs
  21. def by_name(self):
  22. return {}
  23. class NetworkSettingsBeaconTestCase(TestCase, LoaderModuleMockMixin):
  24. """
  25. Test case for salt.beacons.network_settings
  26. """
  27. def setup_loader_modules(self):
  28. return {network_settings: {"__context__": {}, "__salt__": {}}}
  29. def test_non_list_config(self):
  30. config = {}
  31. ret = network_settings.validate(config)
  32. self.assertEqual(
  33. ret, (False, "Configuration for network_settings beacon must be a list.")
  34. )
  35. def test_empty_config(self):
  36. config = [{}]
  37. ret = network_settings.validate(config)
  38. self.assertEqual(ret, (True, "Valid beacon configuration"))
  39. def test_interface(self):
  40. config = [{"interfaces": {"enp14s0u1u2": {"promiscuity": None}}}]
  41. LAST_STATS = network_settings._copy_interfaces_info(
  42. {"enp14s0u1u2": {"family": "0", "promiscuity": "0", "group": "0"}}
  43. )
  44. NEW_STATS = network_settings._copy_interfaces_info(
  45. {"enp14s0u1u2": {"family": "0", "promiscuity": "1", "group": "0"}}
  46. )
  47. ret = network_settings.validate(config)
  48. self.assertEqual(ret, (True, "Valid beacon configuration"))
  49. with patch.object(network_settings, "LAST_STATS", {}), patch.object(
  50. network_settings, "IP", MockIPClass
  51. ), patch(
  52. "salt.beacons.network_settings._copy_interfaces_info",
  53. MagicMock(side_effect=[LAST_STATS, NEW_STATS]),
  54. ):
  55. ret = network_settings.beacon(config)
  56. self.assertEqual(ret, [])
  57. ret = network_settings.beacon(config)
  58. _expected = [
  59. {
  60. "interface": "enp14s0u1u2",
  61. "tag": "enp14s0u1u2",
  62. "change": {"promiscuity": "1"},
  63. }
  64. ]
  65. self.assertEqual(ret, _expected)
  66. def test_interface_no_change(self):
  67. config = [{"interfaces": {"enp14s0u1u2": {"promiscuity": None}}}]
  68. LAST_STATS = network_settings._copy_interfaces_info(
  69. {"enp14s0u1u2": {"family": "0", "promiscuity": "0", "group": "0"}}
  70. )
  71. NEW_STATS = network_settings._copy_interfaces_info(
  72. {"enp14s0u1u2": {"family": "0", "promiscuity": "0", "group": "0"}}
  73. )
  74. ret = network_settings.validate(config)
  75. self.assertEqual(ret, (True, "Valid beacon configuration"))
  76. with patch.object(network_settings, "LAST_STATS", {}), patch.object(
  77. network_settings, "IP", MockIPClass
  78. ), patch(
  79. "salt.beacons.network_settings._copy_interfaces_info",
  80. MagicMock(side_effect=[LAST_STATS, NEW_STATS]),
  81. ):
  82. ret = network_settings.beacon(config)
  83. self.assertEqual(ret, [])
  84. ret = network_settings.beacon(config)
  85. self.assertEqual(ret, [])
  86. def test_wildcard_interface(self):
  87. config = [{"interfaces": {"en*": {"promiscuity": None}}}]
  88. LAST_STATS = network_settings._copy_interfaces_info(
  89. {"enp14s0u1u2": {"family": "0", "promiscuity": "0", "group": "0"}}
  90. )
  91. NEW_STATS = network_settings._copy_interfaces_info(
  92. {"enp14s0u1u2": {"family": "0", "promiscuity": "1", "group": "0"}}
  93. )
  94. ret = network_settings.validate(config)
  95. self.assertEqual(ret, (True, "Valid beacon configuration"))
  96. with patch.object(network_settings, "LAST_STATS", {}), patch.object(
  97. network_settings, "IP", MockIPClass
  98. ), patch(
  99. "salt.beacons.network_settings._copy_interfaces_info",
  100. MagicMock(side_effect=[LAST_STATS, NEW_STATS]),
  101. ):
  102. ret = network_settings.beacon(config)
  103. self.assertEqual(ret, [])
  104. ret = network_settings.beacon(config)
  105. _expected = [
  106. {
  107. "interface": "enp14s0u1u2",
  108. "tag": "enp14s0u1u2",
  109. "change": {"promiscuity": "1"},
  110. }
  111. ]
  112. self.assertEqual(ret, _expected)
  113. @skipIf(not HAS_PYROUTE2, "no pyroute2 installed, skipping")
  114. class Pyroute2TestCase(TestCase):
  115. def test_interface_dict_fields(self):
  116. with IPDB() as ipdb:
  117. for attr in network_settings.ATTRS:
  118. # ipdb.interfaces is a dict-like object, that
  119. # contains interface definitions. Interfaces can
  120. # be referenced both with indices and names.
  121. #
  122. # ipdb.interfaces[1] is an interface with index 1,
  123. # that is the loopback interface.
  124. self.assertIn(attr, ipdb.interfaces[1])