test_nilrt_ip.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. """
  3. integration tests for nilirt_ip
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import shutil
  7. import time
  8. import pytest
  9. from tests.support.case import ModuleCase
  10. @pytest.mark.destructive_test
  11. @pytest.mark.skip_if_not_root
  12. @pytest.mark.skipif(
  13. 'grains["os_family"] != "NILinuxRT"', reason="Tests applicable only to NILinuxRT"
  14. )
  15. class Nilrt_ipModuleTest(ModuleCase):
  16. """
  17. Validate the nilrt_ip module
  18. """
  19. @classmethod
  20. def setUpClass(cls):
  21. cls.initialState = {}
  22. @classmethod
  23. def tearDownClass(cls):
  24. cls.initialState = None
  25. def setUp(self):
  26. """
  27. Get current settings
  28. """
  29. # save files from var/lib/connman*
  30. super(Nilrt_ipModuleTest, self).setUp()
  31. shutil.move("/var/lib/connman", "/tmp/connman")
  32. def tearDown(self):
  33. """
  34. Reset to original settings
  35. """
  36. # restore files
  37. shutil.move("/tmp/connman", "/var/lib/connman")
  38. self.run_function("service.restart", ["connman"])
  39. time.sleep(10) # wait 10 seconds for connman to be fully loaded
  40. interfaces = self.__interfaces()
  41. for interface in interfaces:
  42. self.run_function("ip.up", [interface])
  43. def __connected(self, interface):
  44. return interface["up"]
  45. def __interfaces(self):
  46. interfaceList = []
  47. for iface in self.run_function("ip.get_interfaces_details")["interfaces"]:
  48. interfaceList.append(iface["connectionid"])
  49. return interfaceList
  50. def test_down(self):
  51. interfaces = self.__interfaces()
  52. for interface in interfaces:
  53. result = self.run_function("ip.down", [interface])
  54. self.assertTrue(result)
  55. info = self.run_function("ip.get_interfaces_details")
  56. for interface in info["interfaces"]:
  57. self.assertFalse(self.__connected(interface))
  58. def test_up(self):
  59. interfaces = self.__interfaces()
  60. # first down all interfaces
  61. for interface in interfaces:
  62. self.run_function("ip.down", [interface])
  63. # up interfaces
  64. for interface in interfaces:
  65. result = self.run_function("ip.up", [interface])
  66. self.assertTrue(result)
  67. info = self.run_function("ip.get_interfaces_details")
  68. for interface in info["interfaces"]:
  69. self.assertTrue(self.__connected(interface))
  70. def test_set_dhcp_linklocal_all(self):
  71. interfaces = self.__interfaces()
  72. for interface in interfaces:
  73. result = self.run_function("ip.set_dhcp_linklocal_all", [interface])
  74. self.assertTrue(result)
  75. info = self.run_function("ip.get_interfaces_details")
  76. for interface in info["interfaces"]:
  77. self.assertEqual(interface["ipv4"]["requestmode"], "dhcp_linklocal")
  78. def test_static_all(self):
  79. interfaces = self.__interfaces()
  80. for interface in interfaces:
  81. result = self.run_function(
  82. "ip.set_static_all",
  83. [
  84. interface,
  85. "192.168.10.4",
  86. "255.255.255.0",
  87. "192.168.10.1",
  88. "8.8.4.4 8.8.8.8",
  89. ],
  90. )
  91. self.assertTrue(result)
  92. info = self.run_function("ip.get_interfaces_details")
  93. for interface in info["interfaces"]:
  94. self.assertIn("8.8.4.4", interface["ipv4"]["dns"])
  95. self.assertIn("8.8.8.8", interface["ipv4"]["dns"])
  96. self.assertEqual(interface["ipv4"]["requestmode"], "static")
  97. self.assertEqual(interface["ipv4"]["address"], "192.168.10.4")
  98. self.assertEqual(interface["ipv4"]["netmask"], "255.255.255.0")
  99. self.assertEqual(interface["ipv4"]["gateway"], "192.168.10.1")