1
0

test_rh_ip.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch)
  14. # Import Salt Libs
  15. from salt.ext.six.moves import range
  16. import salt.modules.rh_ip as rh_ip
  17. # Import 3rd-party libs
  18. import jinja2.exceptions
  19. from salt.ext import six
  20. class RhipTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. Test cases for salt.modules.rh_ip
  23. '''
  24. def setup_loader_modules(self):
  25. return {rh_ip: {}}
  26. def test_error_message_iface_should_process_non_str_expected(self):
  27. values = [1, True, False, 'no-kaboom']
  28. iface = 'ethtest'
  29. option = 'test'
  30. msg = rh_ip._error_msg_iface(iface, option, values)
  31. self.assertTrue(msg.endswith('[1|True|False|no-kaboom]'), msg)
  32. def test_error_message_network_should_process_non_str_expected(self):
  33. values = [1, True, False, 'no-kaboom']
  34. msg = rh_ip._error_msg_network('fnord', values)
  35. self.assertTrue(msg.endswith('[1|True|False|no-kaboom]'), msg)
  36. def test_build_bond(self):
  37. '''
  38. Test to create a bond script in /etc/modprobe.d with the passed
  39. settings and load the bonding kernel module.
  40. '''
  41. with patch.dict(rh_ip.__grains__, {'osrelease': 'osrelease'}):
  42. with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
  43. mock = jinja2.exceptions.TemplateNotFound('foo')
  44. with patch.object(jinja2.Environment, 'get_template',
  45. MagicMock(side_effect=mock)):
  46. self.assertEqual(rh_ip.build_bond('iface'), '')
  47. with patch.dict(rh_ip.__salt__, {'kmod.load':
  48. MagicMock(return_value=None)}):
  49. with patch.object(rh_ip, '_write_file_iface',
  50. return_value=None):
  51. with patch.object(rh_ip, '_read_temp', return_value='A'):
  52. self.assertEqual(rh_ip.build_bond('iface', test='A'),
  53. 'A')
  54. with patch.object(rh_ip, '_read_file', return_value='A'):
  55. self.assertEqual(rh_ip.build_bond('iface', test=None),
  56. 'A')
  57. def test_build_interface(self):
  58. '''
  59. Test to build an interface script for a network interface.
  60. '''
  61. with patch.dict(rh_ip.__grains__, {'os': 'Fedora', 'osmajorrelease': 26}):
  62. with patch.object(rh_ip, '_raise_error_iface', return_value=None):
  63. self.assertRaises(AttributeError,
  64. rh_ip.build_interface,
  65. 'iface', 'slave', True)
  66. with patch.dict(rh_ip.__salt__, {'network.interfaces': lambda: {'eth': True}}):
  67. self.assertRaises(AttributeError,
  68. rh_ip.build_interface,
  69. 'iface', 'eth', True, netmask='255.255.255.255', prefix=32,
  70. test=True)
  71. self.assertRaises(AttributeError,
  72. rh_ip.build_interface,
  73. 'iface', 'eth', True, ipaddrs=['A'],
  74. test=True)
  75. self.assertRaises(AttributeError,
  76. rh_ip.build_interface,
  77. 'iface', 'eth', True, ipv6addrs=['A'],
  78. test=True)
  79. for osrelease in range(5, 8):
  80. with patch.dict(rh_ip.__grains__, {'os': 'RedHat', 'osrelease': six.text_type(osrelease)}):
  81. with patch.object(rh_ip, '_raise_error_iface', return_value=None):
  82. with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
  83. mock = jinja2.exceptions.TemplateNotFound('foo')
  84. with patch.object(jinja2.Environment,
  85. 'get_template',
  86. MagicMock(side_effect=mock)):
  87. self.assertEqual(rh_ip.build_interface('iface',
  88. 'vlan',
  89. True), '')
  90. with patch.object(rh_ip, '_read_temp', return_value='A'):
  91. with patch.object(jinja2.Environment,
  92. 'get_template', MagicMock()):
  93. self.assertEqual(rh_ip.build_interface('iface',
  94. 'vlan',
  95. True,
  96. test='A'),
  97. 'A')
  98. with patch.object(rh_ip, '_write_file_iface',
  99. return_value=None):
  100. with patch.object(os.path, 'join',
  101. return_value='A'):
  102. with patch.object(rh_ip, '_read_file',
  103. return_value='A'):
  104. self.assertEqual(rh_ip.build_interface
  105. ('iface', 'vlan',
  106. True), 'A')
  107. if osrelease > 6:
  108. with patch.dict(rh_ip.__salt__, {'network.interfaces': lambda: {'eth': True}}):
  109. self.assertEqual(rh_ip.build_interface
  110. ('iface', 'eth', True,
  111. ipaddrs=['127.0.0.1/8']), 'A')
  112. self.assertEqual(rh_ip.build_interface
  113. ('iface', 'eth', True,
  114. ipv6addrs=['fc00::1/128']), 'A')
  115. def test_build_routes(self):
  116. '''
  117. Test to build a route script for a network interface.
  118. '''
  119. with patch.dict(rh_ip.__grains__, {'osrelease': '5.0'}):
  120. with patch.object(rh_ip, '_parse_routes', MagicMock()):
  121. mock = jinja2.exceptions.TemplateNotFound('foo')
  122. with patch.object(jinja2.Environment,
  123. 'get_template', MagicMock(side_effect=mock)):
  124. self.assertEqual(rh_ip.build_routes('iface'), '')
  125. with patch.object(jinja2.Environment,
  126. 'get_template', MagicMock()):
  127. with patch.object(rh_ip, '_read_temp', return_value=['A']):
  128. self.assertEqual(rh_ip.build_routes('i', test='t'), ['A', 'A'])
  129. with patch.object(rh_ip, '_read_file', return_value=['A']):
  130. with patch.object(os.path, 'join', return_value='A'):
  131. with patch.object(rh_ip, '_write_file_iface',
  132. return_value=None):
  133. self.assertEqual(rh_ip.build_routes('i',
  134. test=None),
  135. ['A', 'A'])
  136. def test_down(self):
  137. '''
  138. Test to shutdown a network interface
  139. '''
  140. with patch.dict(rh_ip.__salt__, {'cmd.run':
  141. MagicMock(return_value='A')}):
  142. self.assertEqual(rh_ip.down('iface', 'iface_type'), 'A')
  143. self.assertEqual(rh_ip.down('iface', 'slave'), None)
  144. def test_get_bond(self):
  145. '''
  146. Test to return the content of a bond script
  147. '''
  148. with patch.object(os.path, 'join', return_value='A'):
  149. with patch.object(rh_ip, '_read_file', return_value='A'):
  150. self.assertEqual(rh_ip.get_bond('iface'), 'A')
  151. def test_get_interface(self):
  152. '''
  153. Test to return the contents of an interface script
  154. '''
  155. with patch.object(os.path, 'join', return_value='A'):
  156. with patch.object(rh_ip, '_read_file', return_value='A'):
  157. self.assertEqual(rh_ip.get_interface('iface'), 'A')
  158. def test_up(self):
  159. '''
  160. Test to start up a network interface
  161. '''
  162. with patch.dict(rh_ip.__salt__, {'cmd.run':
  163. MagicMock(return_value='A')}):
  164. self.assertEqual(rh_ip.up('iface', 'iface_type'), 'A')
  165. self.assertEqual(rh_ip.up('iface', 'slave'), None)
  166. def test_get_routes(self):
  167. '''
  168. Test to return the contents of the interface routes script.
  169. '''
  170. with patch.object(os.path, 'join', return_value='A'):
  171. with patch.object(rh_ip, '_read_file', return_value=['A']):
  172. self.assertEqual(rh_ip.get_routes('iface'), ['A', 'A'])
  173. def test_get_network_settings(self):
  174. '''
  175. Test to return the contents of the global network script.
  176. '''
  177. with patch.object(rh_ip, '_read_file', return_value='A'):
  178. self.assertEqual(rh_ip.get_network_settings(), 'A')
  179. def test_apply_network_settings(self):
  180. '''
  181. Test to apply global network configuration.
  182. '''
  183. with patch.dict(rh_ip.__salt__, {'service.restart':
  184. MagicMock(return_value=True)}):
  185. self.assertTrue(rh_ip.apply_network_settings())
  186. def test_build_network_settings(self):
  187. '''
  188. Test to build the global network script.
  189. '''
  190. with patch.object(rh_ip, '_parse_rh_config', MagicMock()):
  191. with patch.object(rh_ip, '_parse_network_settings', MagicMock()):
  192. mock = jinja2.exceptions.TemplateNotFound('foo')
  193. with patch.object(jinja2.Environment,
  194. 'get_template', MagicMock(side_effect=mock)):
  195. self.assertEqual(rh_ip.build_network_settings(), '')
  196. with patch.object(jinja2.Environment,
  197. 'get_template', MagicMock()):
  198. with patch.object(rh_ip, '_read_temp', return_value='A'):
  199. self.assertEqual(rh_ip.build_network_settings
  200. (test='t'), 'A')
  201. with patch.object(rh_ip, '_write_file_network',
  202. return_value=None):
  203. with patch.object(rh_ip, '_read_file',
  204. return_value='A'):
  205. self.assertEqual(rh_ip.build_network_settings
  206. (test=None), 'A')