test_nilrt_ip.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. """
  2. integration tests for nilirt_ip
  3. """
  4. import re
  5. import shutil
  6. import time
  7. import salt.modules.nilrt_ip as ip
  8. import salt.utils.files
  9. import salt.utils.platform
  10. from salt.ext.six.moves import configparser
  11. from tests.support.case import ModuleCase
  12. from tests.support.helpers import (
  13. destructiveTest,
  14. requires_system_grains,
  15. runs_on,
  16. skip_if_not_root,
  17. )
  18. from tests.support.unit import skipIf
  19. try:
  20. import pyiface
  21. from pyiface.ifreqioctls import IFF_LOOPBACK, IFF_RUNNING
  22. except ImportError:
  23. pyiface = None
  24. try:
  25. from requests.structures import CaseInsensitiveDict
  26. except ImportError:
  27. CaseInsensitiveDict = None
  28. INTERFACE_FOR_TEST = "eth1"
  29. @skip_if_not_root
  30. @destructiveTest
  31. @skipIf(not pyiface, "The python pyiface package is not installed")
  32. @skipIf(not CaseInsensitiveDict, "The python package requests is not installed")
  33. @runs_on(os_family="NILinuxRT", reason="Tests applicable only to NILinuxRT")
  34. class NilrtIpModuleTest(ModuleCase):
  35. """
  36. Validate the nilrt_ip module
  37. """
  38. @requires_system_grains
  39. @classmethod
  40. def setUpClass(cls, grains): # pylint: disable=arguments-differ
  41. cls.initialState = {}
  42. cls.grains = grains
  43. @classmethod
  44. def tearDownClass(cls):
  45. cls.initialState = cls.grains = None
  46. @staticmethod
  47. def setup_loader_modules():
  48. """
  49. Setup loader modules
  50. """
  51. return {ip: {}}
  52. def setUp(self):
  53. """
  54. Get current settings
  55. """
  56. # save files from var/lib/connman*
  57. super().setUp()
  58. if self.grains["lsb_distrib_id"] == "nilrt":
  59. shutil.move("/etc/natinst/share/ni-rt.ini", "/tmp/ni-rt.ini")
  60. else:
  61. shutil.move("/var/lib/connman", "/tmp/connman")
  62. def tearDown(self):
  63. """
  64. Reset to original settings
  65. """
  66. # restore files
  67. if self.grains["lsb_distrib_id"] == "nilrt":
  68. shutil.move("/tmp/ni-rt.ini", "/etc/natinst/share/ni-rt.ini")
  69. self.run_function("cmd.run", ["/etc/init.d/networking restart"])
  70. else:
  71. shutil.move("/tmp/connman", "/var/lib/connman")
  72. self.run_function("service.restart", ["connman"])
  73. time.sleep(10) # wait 10 seconds for connman to be fully loaded
  74. interfaces = self.__interfaces()
  75. for interface in interfaces:
  76. self.run_function("ip.up", [interface.name])
  77. @staticmethod
  78. def __connected(interface):
  79. """
  80. Check if an interface is up or down
  81. :param interface: pyiface.Interface object
  82. :return: True, if interface is up, otherwise False.
  83. """
  84. return interface.flags & IFF_RUNNING != 0
  85. @staticmethod
  86. def __interfaces():
  87. """
  88. Return the list of all interfaces without loopback
  89. """
  90. return [
  91. interface
  92. for interface in pyiface.getIfaces()
  93. if interface.flags & IFF_LOOPBACK == 0
  94. ]
  95. def __check_ethercat(self):
  96. """
  97. Check if ethercat is installed.
  98. :return: True if ethercat is installed, otherwise False.
  99. """
  100. if self.grains["lsb_distrib_id"] != "nilrt":
  101. return False
  102. with salt.utils.files.fopen("/etc/natinst/share/ni-rt.ini", "r") as config_file:
  103. config_parser = configparser.RawConfigParser(dict_type=CaseInsensitiveDict)
  104. config_parser.readfp(config_file)
  105. return (
  106. "ethercat"
  107. in config_parser.get(
  108. "lvrt", "AdditionalNetworkProtocols", fallback=""
  109. ).lower()
  110. )
  111. def test_down(self):
  112. """
  113. Test ip.down function
  114. """
  115. interfaces = self.__interfaces()
  116. for interface in interfaces:
  117. result = self.run_function("ip.down", [interface.name])
  118. self.assertTrue(result)
  119. info = self.run_function("ip.get_interfaces_details", timeout=300)
  120. for interface in info["interfaces"]:
  121. if self.grains["lsb_distrib_id"] == "nilrt":
  122. self.assertEqual(interface["adapter_mode"], "disabled")
  123. self.assertFalse(
  124. self.__connected(pyiface.Interface(name=interface["connectionid"]))
  125. )
  126. def test_up(self):
  127. """
  128. Test ip.up function
  129. """
  130. interfaces = self.__interfaces()
  131. # first down all interfaces
  132. for interface in interfaces:
  133. self.run_function("ip.down", [interface.name])
  134. self.assertFalse(self.__connected(interface))
  135. # up interfaces
  136. for interface in interfaces:
  137. result = self.run_function("ip.up", [interface.name])
  138. self.assertTrue(result)
  139. if self.grains["lsb_distrib_id"] == "nilrt":
  140. info = self.run_function("ip.get_interfaces_details", timeout=300)
  141. for interface in info["interfaces"]:
  142. self.assertEqual(interface["adapter_mode"], "tcpip")
  143. def test_set_dhcp_linklocal_all(self):
  144. """
  145. Test ip.set_dhcp_linklocal_all function
  146. """
  147. interfaces = self.__interfaces()
  148. for interface in interfaces:
  149. result = self.run_function("ip.set_dhcp_linklocal_all", [interface.name])
  150. self.assertTrue(result)
  151. info = self.run_function("ip.get_interfaces_details", timeout=300)
  152. for interface in info["interfaces"]:
  153. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  154. if self.grains["lsb_distrib_id"] == "nilrt":
  155. self.assertEqual(interface["adapter_mode"], "tcpip")
  156. def test_set_dhcp_only_all(self):
  157. """
  158. Test ip.set_dhcp_only_all function
  159. """
  160. if self.grains["lsb_distrib_id"] != "nilrt":
  161. self.skipTest("Test not applicable to newer nilrt")
  162. interfaces = self.__interfaces()
  163. for interface in interfaces:
  164. result = self.run_function("ip.set_dhcp_only_all", [interface.name])
  165. self.assertTrue(result)
  166. info = self.run_function("ip.get_interfaces_details", timeout=300)
  167. for interface in info["interfaces"]:
  168. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_only")
  169. self.assertEqual(interface["adapter_mode"], "tcpip")
  170. def test_set_linklocal_only_all(self):
  171. """
  172. Test ip.set_linklocal_only_all function
  173. """
  174. if self.grains["lsb_distrib_id"] != "nilrt":
  175. self.skipTest("Test not applicable to newer nilrt")
  176. interfaces = self.__interfaces()
  177. for interface in interfaces:
  178. result = self.run_function("ip.set_linklocal_only_all", [interface.name])
  179. self.assertTrue(result)
  180. info = self.run_function("ip.get_interfaces_details", timeout=300)
  181. for interface in info["interfaces"]:
  182. self.assertEqual(interface["ipv4"]["requestmode"], "linklocal_only")
  183. self.assertEqual(interface["adapter_mode"], "tcpip")
  184. def test_static_all(self):
  185. """
  186. Test ip.set_static_all function
  187. """
  188. interfaces = self.__interfaces()
  189. for interface in interfaces:
  190. result = self.run_function(
  191. "ip.set_static_all",
  192. [
  193. interface.name,
  194. "192.168.10.4",
  195. "255.255.255.0",
  196. "192.168.10.1",
  197. "8.8.4.4 8.8.8.8",
  198. ],
  199. )
  200. self.assertTrue(result)
  201. info = self.run_function("ip.get_interfaces_details", timeout=300)
  202. for interface in info["interfaces"]:
  203. if self.grains["lsb_distrib_id"] != "nilrt":
  204. self.assertIn("8.8.4.4", interface["ipv4"]["dns"])
  205. self.assertIn("8.8.8.8", interface["ipv4"]["dns"])
  206. else:
  207. self.assertEqual(interface["ipv4"]["dns"], ["8.8.4.4"])
  208. self.assertEqual(interface["adapter_mode"], "tcpip")
  209. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  210. self.assertEqual(interface["ipv4"]["address"], "192.168.10.4")
  211. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  212. self.assertEqual(interface["ipv4"]["gateway"], "192.168.10.1")
  213. def test_supported_adapter_modes(self):
  214. """
  215. Test supported adapter modes for each interface
  216. """
  217. if self.grains["lsb_distrib_id"] != "nilrt":
  218. self.skipTest("Test is just for older nilrt distros")
  219. interface_pattern = re.compile("^eth[0-9]+$")
  220. info = self.run_function("ip.get_interfaces_details", timeout=300)
  221. for interface in info["interfaces"]:
  222. if interface["connectionid"] == "eth0":
  223. self.assertEqual(interface["supported_adapter_modes"], ["tcpip"])
  224. else:
  225. self.assertIn("tcpip", interface["supported_adapter_modes"])
  226. if not interface_pattern.match(interface["connectionid"]):
  227. self.assertNotIn("ethercat", interface["supported_adapter_modes"])
  228. elif self.__check_ethercat():
  229. self.assertIn("ethercat", interface["supported_adapter_modes"])
  230. def test_ethercat(self):
  231. """
  232. Test ip.set_ethercat function
  233. """
  234. if not self.__check_ethercat():
  235. self.skipTest("Test is just for systems with Ethercat")
  236. self.assertTrue(self.run_function("ip.set_ethercat", [INTERFACE_FOR_TEST, 19]))
  237. info = self.run_function("ip.get_interfaces_details", timeout=300)
  238. for interface in info["interfaces"]:
  239. if interface["connectionid"] == INTERFACE_FOR_TEST:
  240. self.assertEqual(interface["adapter_mode"], "ethercat")
  241. self.assertEqual(int(interface["ethercat"]["masterid"]), 19)
  242. break
  243. self.assertTrue(
  244. self.run_function("ip.set_dhcp_linklocal_all", [INTERFACE_FOR_TEST])
  245. )
  246. info = self.run_function("ip.get_interfaces_details", timeout=300)
  247. for interface in info["interfaces"]:
  248. if interface["connectionid"] == INTERFACE_FOR_TEST:
  249. self.assertEqual(interface["adapter_mode"], "tcpip")
  250. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  251. break
  252. @destructiveTest
  253. def test_dhcp_disable(self):
  254. """
  255. Test cases:
  256. - dhcp -> disable
  257. - disable -> dhcp
  258. """
  259. if self.grains["lsb_distrib_id"] == "nilrt":
  260. self.skipTest("Test is just for newer nilrt distros")
  261. self.assertTrue(
  262. self.run_function("ip.set_dhcp_linklocal_all", [INTERFACE_FOR_TEST])
  263. )
  264. info = self.run_function("ip.get_interfaces_details", timeout=300)
  265. for interface in info["interfaces"]:
  266. if interface["connectionid"] == INTERFACE_FOR_TEST:
  267. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  268. break
  269. self.assertTrue(self.run_function("ip.disable", [INTERFACE_FOR_TEST]))
  270. info = self.run_function("ip.get_interfaces_details", timeout=300)
  271. for interface in info["interfaces"]:
  272. if interface["connectionid"] == INTERFACE_FOR_TEST:
  273. self.assertEqual(interface["ipv4"]["requestmode"], "disabled")
  274. break
  275. self.assertTrue(
  276. self.run_function("ip.set_dhcp_linklocal_all", [INTERFACE_FOR_TEST])
  277. )
  278. info = self.run_function("ip.get_interfaces_details", timeout=300)
  279. for interface in info["interfaces"]:
  280. if interface["connectionid"] == INTERFACE_FOR_TEST:
  281. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  282. break
  283. @destructiveTest
  284. def test_dhcp_static(self):
  285. """
  286. Test cases:
  287. - dhcp -> static
  288. - static -> dhcp
  289. """
  290. if self.grains["lsb_distrib_id"] == "nilrt":
  291. self.skipTest("Test is just for newer nilrt distros")
  292. self.assertTrue(
  293. self.run_function("ip.set_dhcp_linklocal_all", [INTERFACE_FOR_TEST])
  294. )
  295. info = self.run_function("ip.get_interfaces_details", timeout=300)
  296. for interface in info["interfaces"]:
  297. if interface["connectionid"] == INTERFACE_FOR_TEST:
  298. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  299. break
  300. self.assertTrue(
  301. self.run_function(
  302. "ip.set_static_all",
  303. [
  304. INTERFACE_FOR_TEST,
  305. "192.168.1.125",
  306. "255.255.255.0",
  307. "192.168.1.1",
  308. "8.8.8.8 8.8.8.4",
  309. ],
  310. )
  311. )
  312. info = self.run_function("ip.get_interfaces_details", timeout=300)
  313. for interface in info["interfaces"]:
  314. if interface["connectionid"] == INTERFACE_FOR_TEST:
  315. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  316. self.assertEqual(interface["ipv4"]["address"], "192.168.1.125")
  317. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  318. self.assertIn("8.8.8.4", interface["ipv4"]["dns"])
  319. self.assertIn("8.8.8.8", interface["ipv4"]["dns"])
  320. break
  321. self.assertTrue(
  322. self.run_function("ip.set_dhcp_linklocal_all", [INTERFACE_FOR_TEST])
  323. )
  324. info = self.run_function("ip.get_interfaces_details", timeout=300)
  325. for interface in info["interfaces"]:
  326. if interface["connectionid"] == INTERFACE_FOR_TEST:
  327. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  328. break
  329. @destructiveTest
  330. def test_static_disable(self):
  331. """
  332. Test cases:
  333. - static -> disable
  334. - disable -> static
  335. """
  336. if self.grains["lsb_distrib_id"] == "nilrt":
  337. self.skipTest("Test is just for newer nilrt distros")
  338. self.assertTrue(
  339. self.run_function(
  340. "ip.set_static_all",
  341. [
  342. INTERFACE_FOR_TEST,
  343. "192.168.1.125",
  344. "255.255.255.0",
  345. "192.168.1.1",
  346. "8.8.8.8",
  347. ],
  348. )
  349. )
  350. info = self.run_function("ip.get_interfaces_details", timeout=300)
  351. for interface in info["interfaces"]:
  352. if interface["connectionid"] == INTERFACE_FOR_TEST:
  353. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  354. self.assertEqual(interface["ipv4"]["address"], "192.168.1.125")
  355. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  356. self.assertEqual(interface["ipv4"]["dns"], ["8.8.8.8"])
  357. break
  358. self.assertTrue(self.run_function("ip.disable", [INTERFACE_FOR_TEST]))
  359. info = self.run_function("ip.get_interfaces_details", timeout=300)
  360. for interface in info["interfaces"]:
  361. if interface["connectionid"] == INTERFACE_FOR_TEST:
  362. self.assertEqual(interface["ipv4"]["requestmode"], "disabled")
  363. break
  364. self.assertTrue(
  365. self.run_function(
  366. "ip.set_static_all",
  367. [INTERFACE_FOR_TEST, "192.168.1.125", "255.255.255.0", "192.168.1.1"],
  368. )
  369. )
  370. info = self.run_function("ip.get_interfaces_details", timeout=300)
  371. for interface in info["interfaces"]:
  372. if interface["connectionid"] == INTERFACE_FOR_TEST:
  373. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  374. self.assertEqual(interface["ipv4"]["address"], "192.168.1.125")
  375. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  376. self.assertEqual(interface["ipv4"]["dns"], [])
  377. break