test_network.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  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 (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.network as network
  16. import logging
  17. log = logging.getLogger(__name__)
  18. class MockNetwork(object):
  19. '''
  20. Mock network class
  21. '''
  22. def __init__(self):
  23. pass
  24. @staticmethod
  25. def interfaces():
  26. '''
  27. Mock interface method
  28. '''
  29. return {'salt': {'up': 1}}
  30. class MockGrains(object):
  31. '''
  32. Mock Grains class
  33. '''
  34. def __init__(self):
  35. pass
  36. @staticmethod
  37. def grains(lis, bol):
  38. '''
  39. Mock grains method
  40. '''
  41. return {'A': 'B'}
  42. class NetworkTestCase(TestCase, LoaderModuleMockMixin):
  43. '''
  44. Validate the network state
  45. '''
  46. def setup_loader_modules(self):
  47. return {network: {}}
  48. def test_managed(self):
  49. '''
  50. Test to ensure that the named interface is configured properly
  51. '''
  52. with patch('salt.states.network.salt.utils.network', MockNetwork()), \
  53. patch('salt.states.network.salt.loader', MockGrains()):
  54. ret = {'name': 'salt',
  55. 'changes': {},
  56. 'result': False,
  57. 'comment': ''}
  58. change = {'interface': '--- \n+++ \n@@ -1 +1 @@\n-A\n+B',
  59. 'status': 'Interface salt restart to validate'}
  60. mock = MagicMock(side_effect=[AttributeError, 'A', 'A', 'A', 'A', 'A'])
  61. with patch.dict(network.__salt__, {"ip.get_interface": mock}):
  62. self.assertDictEqual(network.managed('salt',
  63. 'stack', test='a'), ret)
  64. mock = MagicMock(return_value='B')
  65. with patch.dict(network.__salt__, {"ip.build_interface": mock}):
  66. mock = MagicMock(side_effect=AttributeError)
  67. with patch.dict(network.__salt__, {"ip.get_bond": mock}):
  68. self.assertDictEqual(network.managed('salt',
  69. 'bond',
  70. test='a'), ret)
  71. ret.update({'comment': 'Interface salt is set to be'
  72. ' updated:\n--- \n+++ \n@@ -1 +1 @@\n-A\n+B',
  73. 'result': None})
  74. self.assertDictEqual(network.managed('salt', 'stack',
  75. test='a'), ret)
  76. mock = MagicMock(return_value=True)
  77. with patch.dict(network.__salt__, {"ip.down": mock}):
  78. with patch.dict(network.__salt__, {"ip.up": mock}):
  79. ret.update({'comment': 'Interface salt updated.',
  80. 'result': True,
  81. 'changes': change})
  82. self.assertDictEqual(network.managed('salt', 'stack'),
  83. ret)
  84. with patch.dict(network.__grains__, {"A": True}):
  85. with patch.dict(network.__salt__,
  86. {"saltutil.refresh_modules": mock}
  87. ):
  88. ret.update({'result': True,
  89. 'changes': {'interface': '--- \n+'
  90. '++ \n@@ -1 +1 @@\n-A'
  91. '\n+B',
  92. 'status': 'Interface'
  93. ' salt down'}})
  94. self.assertDictEqual(network.managed('salt',
  95. 'stack',
  96. False),
  97. ret)
  98. def test_routes(self):
  99. '''
  100. Test to manage network interface static routes.
  101. '''
  102. ret = {'name': 'salt',
  103. 'changes': {},
  104. 'result': False,
  105. 'comment': ''}
  106. mock = MagicMock(side_effect=[AttributeError, False, False, "True",
  107. False, False])
  108. with patch.dict(network.__salt__, {"ip.get_routes": mock}):
  109. self.assertDictEqual(network.routes('salt'), ret)
  110. mock = MagicMock(side_effect=[False, True, '', True, True])
  111. with patch.dict(network.__salt__, {"ip.build_routes": mock}):
  112. ret.update({'result': True,
  113. 'comment': 'Interface salt routes are up to date.'
  114. })
  115. self.assertDictEqual(network.routes('salt', test='a'), ret)
  116. ret.update({'comment': 'Interface salt routes are'
  117. ' set to be added.',
  118. 'result': None})
  119. self.assertDictEqual(network.routes('salt', test='a'), ret)
  120. ret.update({'comment': 'Interface salt routes are set to be'
  121. ' updated:\n--- \n+++ \n@@ -1,4 +0,0 @@\n-T\n-r'
  122. '\n-u\n-e'})
  123. self.assertDictEqual(network.routes('salt', test='a'), ret)
  124. mock = MagicMock(side_effect=[AttributeError, True])
  125. with patch.dict(network.__salt__,
  126. {"ip.apply_network_settings": mock}):
  127. ret.update({'changes': {'network_routes':
  128. 'Added interface salt routes.'},
  129. 'comment': '',
  130. 'result': False})
  131. self.assertDictEqual(network.routes('salt'), ret)
  132. ret.update({'changes': {'network_routes':
  133. 'Added interface salt routes.'},
  134. 'comment': 'Interface salt routes added.',
  135. 'result': True})
  136. self.assertDictEqual(network.routes('salt'), ret)
  137. def test_system(self):
  138. '''
  139. Test to ensure that global network settings
  140. are configured properly
  141. '''
  142. ret = {'name': 'salt',
  143. 'changes': {},
  144. 'result': False,
  145. 'comment': ''}
  146. with patch.dict(network.__opts__, {"test": True}):
  147. mock = MagicMock(side_effect=[AttributeError, False, False, 'As'])
  148. with patch.dict(network.__salt__,
  149. {"ip.get_network_settings": mock}):
  150. self.assertDictEqual(network.system('salt'), ret)
  151. mock = MagicMock(side_effect=[False, True, ''])
  152. with patch.dict(network.__salt__,
  153. {"ip.build_network_settings": mock}):
  154. ret.update({'comment': 'Global network settings'
  155. ' are up to date.',
  156. 'result': True})
  157. self.assertDictEqual(network.system('salt'), ret)
  158. ret.update({'comment': 'Global network settings are set to'
  159. ' be added.',
  160. 'result': None})
  161. self.assertDictEqual(network.system('salt'), ret)
  162. ret.update({'comment': 'Global network settings are set to'
  163. ' be updated:\n--- \n+++ \n@@ -1,2 +0,0'
  164. ' @@\n-A\n-s'})
  165. self.assertDictEqual(network.system('salt'), ret)
  166. with patch.dict(network.__opts__, {"test": False}):
  167. mock = MagicMock(side_effect=[False, False])
  168. with patch.dict(network.__salt__,
  169. {"ip.get_network_settings": mock}):
  170. mock = MagicMock(side_effect=[True, True])
  171. with patch.dict(network.__salt__,
  172. {"ip.build_network_settings": mock}):
  173. mock = MagicMock(side_effect=[AttributeError, True])
  174. with patch.dict(network.__salt__,
  175. {"ip.apply_network_settings": mock}):
  176. ret.update({'changes': {'network_settings':
  177. 'Added global network'
  178. ' settings.'},
  179. 'comment': '',
  180. 'result': False})
  181. self.assertDictEqual(network.system('salt'), ret)
  182. ret.update({'changes': {'network_settings':
  183. 'Added global network'
  184. ' settings.'},
  185. 'comment': 'Global network settings'
  186. ' are up to date.',
  187. 'result': True})
  188. self.assertDictEqual(network.system('salt'), ret)