1
0

test_nilrt_ip.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for nilirt_ip
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import re
  7. import shutil
  8. import time
  9. import salt.modules.nilrt_ip as ip
  10. import salt.utils.files
  11. import salt.utils.platform
  12. from salt.ext import six
  13. from salt.ext.six.moves import configparser
  14. from tests.support.case import ModuleCase
  15. from tests.support.helpers import (
  16. destructiveTest,
  17. requires_system_grains,
  18. runs_on,
  19. skip_if_not_root,
  20. )
  21. from tests.support.unit import skipIf
  22. try:
  23. import pyiface
  24. from pyiface.ifreqioctls import IFF_LOOPBACK, IFF_RUNNING
  25. except ImportError:
  26. pyiface = None
  27. try:
  28. from requests.structures import CaseInsensitiveDict
  29. except ImportError:
  30. CaseInsensitiveDict = None
  31. @skip_if_not_root
  32. @destructiveTest
  33. @skipIf(not pyiface, "The python pyiface package is not installed")
  34. @skipIf(not CaseInsensitiveDict, "The python package requests is not installed")
  35. @runs_on(os_family="NILinuxRT", reason="Tests applicable only to NILinuxRT")
  36. class NilrtIpModuleTest(ModuleCase):
  37. """
  38. Validate the nilrt_ip module
  39. """
  40. @requires_system_grains
  41. @classmethod
  42. def setUpClass(cls, grains): # pylint: disable=arguments-differ
  43. cls.initialState = {}
  44. cls.grains = grains
  45. @classmethod
  46. def tearDownClass(cls):
  47. cls.initialState = cls.grains = None
  48. @staticmethod
  49. def setup_loader_modules():
  50. """
  51. Setup loader modules
  52. """
  53. return {ip: {}}
  54. def setUp(self):
  55. """
  56. Get current settings
  57. """
  58. # save files from var/lib/connman*
  59. super(NilrtIpModuleTest, self).setUp()
  60. if self.grains["lsb_distrib_id"] == "nilrt":
  61. shutil.move("/etc/natinst/share/ni-rt.ini", "/tmp/ni-rt.ini")
  62. else:
  63. shutil.move("/var/lib/connman", "/tmp/connman")
  64. def tearDown(self):
  65. """
  66. Reset to original settings
  67. """
  68. # restore files
  69. if self.grains["lsb_distrib_id"] == "nilrt":
  70. shutil.move("/tmp/ni-rt.ini", "/etc/natinst/share/ni-rt.ini")
  71. self.run_function("cmd.run", ["/etc/init.d/networking restart"])
  72. else:
  73. shutil.move("/tmp/connman", "/var/lib/connman")
  74. self.run_function("service.restart", ["connman"])
  75. time.sleep(10) # wait 10 seconds for connman to be fully loaded
  76. interfaces = self.__interfaces()
  77. for interface in interfaces:
  78. self.run_function("ip.up", [interface.name])
  79. @staticmethod
  80. def __connected(interface):
  81. """
  82. Check if an interface is up or down
  83. :param interface: pyiface.Interface object
  84. :return: True, if interface is up, otherwise False.
  85. """
  86. return interface.flags & IFF_RUNNING != 0
  87. @staticmethod
  88. def __interfaces():
  89. """
  90. Return the list of all interfaces without loopback
  91. """
  92. return [
  93. interface
  94. for interface in pyiface.getIfaces()
  95. if interface.flags & IFF_LOOPBACK == 0
  96. ]
  97. def __check_ethercat(self):
  98. """
  99. Check if ethercat is installed.
  100. :return: True if ethercat is installed, otherwise False.
  101. """
  102. if self.grains["lsb_distrib_id"] != "nilrt":
  103. return False
  104. with salt.utils.files.fopen("/etc/natinst/share/ni-rt.ini", "r") as config_file:
  105. config_parser = configparser.RawConfigParser(dict_type=CaseInsensitiveDict)
  106. config_parser.readfp(config_file)
  107. if six.PY2:
  108. if (
  109. config_parser.has_option("lvrt", "AdditionalNetworkProtocols")
  110. and "ethercat"
  111. in config_parser.get("lvrt", "AdditionalNetworkProtocols").lower()
  112. ):
  113. return True
  114. return False
  115. else:
  116. return (
  117. "ethercat"
  118. in config_parser.get(
  119. "lvrt", "AdditionalNetworkProtocols", fallback=""
  120. ).lower()
  121. )
  122. def test_down(self):
  123. """
  124. Test ip.down function
  125. """
  126. interfaces = self.__interfaces()
  127. for interface in interfaces:
  128. result = self.run_function("ip.down", [interface.name])
  129. self.assertTrue(result)
  130. info = self.run_function("ip.get_interfaces_details", timeout=300)
  131. for interface in info["interfaces"]:
  132. self.assertEqual(interface["adapter_mode"], "disabled")
  133. self.assertFalse(
  134. self.__connected(pyiface.Interface(name=interface["connectionid"]))
  135. )
  136. def test_up(self):
  137. """
  138. Test ip.up function
  139. """
  140. interfaces = self.__interfaces()
  141. # first down all interfaces
  142. for interface in interfaces:
  143. self.run_function("ip.down", [interface.name])
  144. self.assertFalse(self.__connected(interface))
  145. # up interfaces
  146. for interface in interfaces:
  147. result = self.run_function("ip.up", [interface.name])
  148. self.assertTrue(result)
  149. info = self.run_function("ip.get_interfaces_details", timeout=300)
  150. for interface in info["interfaces"]:
  151. self.assertEqual(interface["adapter_mode"], "tcpip")
  152. def test_set_dhcp_linklocal_all(self):
  153. """
  154. Test ip.set_dhcp_linklocal_all function
  155. """
  156. interfaces = self.__interfaces()
  157. for interface in interfaces:
  158. result = self.run_function("ip.set_dhcp_linklocal_all", [interface.name])
  159. self.assertTrue(result)
  160. info = self.run_function("ip.get_interfaces_details", timeout=300)
  161. for interface in info["interfaces"]:
  162. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  163. self.assertEqual(interface["adapter_mode"], "tcpip")
  164. def test_set_dhcp_only_all(self):
  165. """
  166. Test ip.set_dhcp_only_all function
  167. """
  168. if self.grains["lsb_distrib_id"] != "nilrt":
  169. self.skipTest("Test not applicable to newer nilrt")
  170. interfaces = self.__interfaces()
  171. for interface in interfaces:
  172. result = self.run_function("ip.set_dhcp_only_all", [interface.name])
  173. self.assertTrue(result)
  174. info = self.run_function("ip.get_interfaces_details", timeout=300)
  175. for interface in info["interfaces"]:
  176. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_only")
  177. self.assertEqual(interface["adapter_mode"], "tcpip")
  178. def test_set_linklocal_only_all(self):
  179. """
  180. Test ip.set_linklocal_only_all function
  181. """
  182. if self.grains["lsb_distrib_id"] != "nilrt":
  183. self.skipTest("Test not applicable to newer nilrt")
  184. interfaces = self.__interfaces()
  185. for interface in interfaces:
  186. result = self.run_function("ip.set_linklocal_only_all", [interface.name])
  187. self.assertTrue(result)
  188. info = self.run_function("ip.get_interfaces_details", timeout=300)
  189. for interface in info["interfaces"]:
  190. self.assertEqual(interface["ipv4"]["requestmode"], "linklocal_only")
  191. self.assertEqual(interface["adapter_mode"], "tcpip")
  192. def test_static_all(self):
  193. """
  194. Test ip.set_static_all function
  195. """
  196. interfaces = self.__interfaces()
  197. for interface in interfaces:
  198. result = self.run_function(
  199. "ip.set_static_all",
  200. [
  201. interface.name,
  202. "192.168.10.4",
  203. "255.255.255.0",
  204. "192.168.10.1",
  205. "8.8.4.4 8.8.8.8",
  206. ],
  207. )
  208. self.assertTrue(result)
  209. info = self.run_function("ip.get_interfaces_details", timeout=300)
  210. for interface in info["interfaces"]:
  211. self.assertEqual(interface["adapter_mode"], "tcpip")
  212. if self.grains["lsb_distrib_id"] != "nilrt":
  213. self.assertIn("8.8.4.4", interface["ipv4"]["dns"])
  214. self.assertIn("8.8.8.8", interface["ipv4"]["dns"])
  215. else:
  216. self.assertEqual(interface["ipv4"]["dns"], ["8.8.4.4"])
  217. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  218. self.assertEqual(interface["ipv4"]["address"], "192.168.10.4")
  219. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  220. self.assertEqual(interface["ipv4"]["gateway"], "192.168.10.1")
  221. def test_supported_adapter_modes(self):
  222. """
  223. Test supported adapter modes for each interface
  224. """
  225. interface_pattern = re.compile("^eth[0-9]+$")
  226. info = self.run_function("ip.get_interfaces_details", timeout=300)
  227. for interface in info["interfaces"]:
  228. if interface["connectionid"] == "eth0":
  229. self.assertEqual(interface["supported_adapter_modes"], ["tcpip"])
  230. else:
  231. self.assertIn("tcpip", interface["supported_adapter_modes"])
  232. if not interface_pattern.match(interface["connectionid"]):
  233. self.assertNotIn("ethercat", interface["supported_adapter_modes"])
  234. elif self.__check_ethercat():
  235. self.assertIn("ethercat", interface["supported_adapter_modes"])
  236. def test_ethercat(self):
  237. """
  238. Test ip.set_ethercat function
  239. """
  240. if not self.__check_ethercat():
  241. self.skipTest("Test is just for systems with Ethercat")
  242. self.assertTrue(self.run_function("ip.set_ethercat", ["eth1", 19]))
  243. info = self.run_function("ip.get_interfaces_details", timeout=300)
  244. for interface in info["interfaces"]:
  245. if interface["connectionid"] == "eth1":
  246. self.assertEqual(interface["adapter_mode"], "ethercat")
  247. self.assertEqual(int(interface["ethercat"]["masterid"]), 19)
  248. break
  249. self.assertTrue(self.run_function("ip.set_dhcp_linklocal_all", ["eth1"]))
  250. info = self.run_function("ip.get_interfaces_details", timeout=300)
  251. for interface in info["interfaces"]:
  252. if interface["connectionid"] == "eth1":
  253. self.assertEqual(interface["adapter_mode"], "tcpip")
  254. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  255. break