test_network_settings.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import skipIf, TestCase
  6. from tests.support.mock import NO_MOCK, NO_MOCK_REASON
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. try:
  9. from pyroute2 import IPDB
  10. HAS_PYROUTE2 = True
  11. except ImportError:
  12. HAS_PYROUTE2 = False
  13. # Salt libs
  14. import salt.beacons.network_settings as network_settings
  15. import logging
  16. log = logging.getLogger(__name__)
  17. @skipIf(NO_MOCK, NO_MOCK_REASON)
  18. class NetworkSettingsBeaconTestCase(TestCase, LoaderModuleMockMixin):
  19. '''
  20. Test case for salt.beacons.network_settings
  21. '''
  22. def setup_loader_modules(self):
  23. return {
  24. network_settings: {
  25. '__context__': {},
  26. '__salt__': {},
  27. }
  28. }
  29. def test_non_list_config(self):
  30. config = {}
  31. ret = network_settings.validate(config)
  32. self.assertEqual(ret, (False, 'Configuration for network_settings'
  33. ' beacon must be a list.'))
  34. def test_empty_config(self):
  35. config = [{}]
  36. ret = network_settings.validate(config)
  37. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  38. @skipIf(not HAS_PYROUTE2, 'no pyroute2 installed, skipping')
  39. class Pyroute2TestCase(TestCase):
  40. def test_interface_dict_fields(self):
  41. with IPDB() as ipdb:
  42. for attr in network_settings.ATTRS:
  43. # ipdb.interfaces is a dict-like object, that
  44. # contains interface definitions. Interfaces can
  45. # be referenced both with indices and names.
  46. #
  47. # ipdb.interfaces[1] is an interface with index 1,
  48. # that is the loopback interface.
  49. self.assertIn(attr, ipdb.interfaces[1])