test_bridge.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import MagicMock, patch
  11. # Import Salt Libs
  12. import salt.modules.bridge as bridge
  13. class BridgeTestCase(TestCase, LoaderModuleMockMixin):
  14. '''
  15. Test cases for salt.modules.bridge
  16. '''
  17. def setup_loader_modules(self):
  18. return {bridge: {}}
  19. def test_show(self):
  20. '''
  21. Test for Returns bridges interfaces
  22. along with enslaved physical interfaces
  23. '''
  24. mock = MagicMock(return_value=True)
  25. with patch.object(bridge, '_os_dispatch', mock):
  26. self.assertTrue(bridge.show('br'))
  27. def test_list_(self):
  28. '''
  29. Test for Returns the machine's bridges list
  30. '''
  31. mock = MagicMock(return_value=None)
  32. with patch.object(bridge, '_os_dispatch', mock):
  33. self.assertEqual(bridge.list_(), None)
  34. mock = MagicMock(return_value=['A', 'B'])
  35. with patch.object(bridge, '_os_dispatch', mock):
  36. self.assertEqual(bridge.list_(), ['A', 'B'])
  37. def test_interfaces(self):
  38. '''
  39. Test for Returns interfaces attached to a bridge
  40. '''
  41. self.assertEqual(bridge.interfaces(), None)
  42. mock = MagicMock(return_value={'interfaces': 'A'})
  43. with patch.object(bridge, '_os_dispatch', mock):
  44. self.assertEqual(bridge.interfaces('br'), 'A')
  45. def test_find_interfaces(self):
  46. '''
  47. Test for Returns the bridge to which the interfaces are bond to
  48. '''
  49. mock = MagicMock(return_value=None)
  50. with patch.object(bridge, '_os_dispatch', mock):
  51. self.assertEqual(bridge.find_interfaces(), None)
  52. mock = MagicMock(return_value={'interfaces': 'A'})
  53. with patch.object(bridge, '_os_dispatch', mock):
  54. self.assertEqual(bridge.find_interfaces(), {})
  55. def test_add(self):
  56. '''
  57. Test for Creates a bridge
  58. '''
  59. mock = MagicMock(return_value='A')
  60. with patch.object(bridge, '_os_dispatch', mock):
  61. self.assertEqual(bridge.add(), 'A')
  62. def test_delete(self):
  63. '''
  64. Test for Deletes a bridge
  65. '''
  66. mock = MagicMock(return_value='A')
  67. with patch.object(bridge, '_os_dispatch', mock):
  68. self.assertEqual(bridge.delete(), 'A')
  69. def test_addif(self):
  70. '''
  71. Test for Adds an interface to a bridge
  72. '''
  73. mock = MagicMock(return_value='A')
  74. with patch.object(bridge, '_os_dispatch', mock):
  75. self.assertEqual(bridge.addif(), 'A')
  76. def test_delif(self):
  77. '''
  78. Test for Removes an interface from a bridge
  79. '''
  80. mock = MagicMock(return_value='A')
  81. with patch.object(bridge, '_os_dispatch', mock):
  82. self.assertEqual(bridge.delif(), 'A')
  83. def test_stp(self):
  84. '''
  85. Test for Sets Spanning Tree Protocol state for a bridge
  86. '''
  87. with patch.dict(bridge.__grains__, {'kernel': 'Linux'}):
  88. mock = MagicMock(return_value='Linux')
  89. with patch.object(bridge, '_os_dispatch', mock):
  90. self.assertEqual(bridge.stp(), 'Linux')
  91. with patch.dict(bridge.__grains__, {'kernel': 'FreeBSD'}):
  92. mock = MagicMock(return_value='FreeBSD')
  93. with patch.object(bridge, '_os_dispatch', mock):
  94. self.assertEqual(bridge.stp(), 'FreeBSD')
  95. with patch.dict(bridge.__grains__, {'kernel': None}):
  96. self.assertFalse(bridge.stp())