1
0

test_mac_sysctl.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. import random
  8. import salt.utils.files
  9. from salt.exceptions import CommandExecutionError
  10. from tests.support.case import ModuleCase
  11. from tests.support.helpers import destructiveTest, runs_on, skip_if_not_root, slowTest
  12. # Module Variables
  13. ASSIGN_CMD = "net.inet.icmp.icmplim"
  14. CONFIG = "/etc/sysctl.conf"
  15. @destructiveTest
  16. @skip_if_not_root
  17. @runs_on(kernel="Darwin")
  18. class DarwinSysctlModuleTest(ModuleCase):
  19. """
  20. Integration tests for the darwin_sysctl module
  21. """
  22. def setUp(self):
  23. """
  24. Sets up the test requirements
  25. """
  26. super(DarwinSysctlModuleTest, self).setUp()
  27. # Data needed for cleanup
  28. self.has_conf = False
  29. self.val = self.run_function("sysctl.get", [ASSIGN_CMD])
  30. # If sysctl file is present, make a copy
  31. # Remove original file so we can replace it with test files
  32. if os.path.isfile(CONFIG):
  33. self.has_conf = True
  34. try:
  35. self.conf = self.__copy_sysctl()
  36. except CommandExecutionError:
  37. msg = "Could not copy file: {0}"
  38. raise CommandExecutionError(msg.format(CONFIG))
  39. os.remove(CONFIG)
  40. @slowTest
  41. def test_assign(self):
  42. """
  43. Tests assigning a single sysctl parameter
  44. """
  45. try:
  46. rand = random.randint(0, 500)
  47. while rand == self.val:
  48. rand = random.randint(0, 500)
  49. self.run_function("sysctl.assign", [ASSIGN_CMD, rand])
  50. info = int(self.run_function("sysctl.get", [ASSIGN_CMD]))
  51. try:
  52. self.assertEqual(rand, info)
  53. except AssertionError:
  54. self.run_function("sysctl.assign", [ASSIGN_CMD, self.val])
  55. raise
  56. except CommandExecutionError:
  57. self.run_function("sysctl.assign", [ASSIGN_CMD, self.val])
  58. raise
  59. @slowTest
  60. def test_persist_new_file(self):
  61. """
  62. Tests assigning a sysctl value to a system without a sysctl.conf file
  63. """
  64. # Always start with a clean/known sysctl.conf state
  65. if os.path.isfile(CONFIG):
  66. os.remove(CONFIG)
  67. try:
  68. self.run_function("sysctl.persist", [ASSIGN_CMD, 10])
  69. line = "{0}={1}".format(ASSIGN_CMD, 10)
  70. found = self.__check_string(CONFIG, line)
  71. self.assertTrue(found)
  72. except CommandExecutionError:
  73. os.remove(CONFIG)
  74. raise
  75. @slowTest
  76. def test_persist_already_set(self):
  77. """
  78. Tests assigning a sysctl value that is already set in sysctl.conf file
  79. """
  80. # Always start with a clean/known sysctl.conf state
  81. if os.path.isfile(CONFIG):
  82. os.remove(CONFIG)
  83. try:
  84. self.run_function("sysctl.persist", [ASSIGN_CMD, 50])
  85. ret = self.run_function("sysctl.persist", [ASSIGN_CMD, 50])
  86. self.assertEqual(ret, "Already set")
  87. except CommandExecutionError:
  88. os.remove(CONFIG)
  89. raise
  90. @slowTest
  91. def test_persist_apply_change(self):
  92. """
  93. Tests assigning a sysctl value and applying the change to system
  94. """
  95. # Always start with a clean/known sysctl.conf state
  96. if os.path.isfile(CONFIG):
  97. os.remove(CONFIG)
  98. try:
  99. rand = random.randint(0, 500)
  100. while rand == self.val:
  101. rand = random.randint(0, 500)
  102. self.run_function("sysctl.persist", [ASSIGN_CMD, rand], apply_change=True)
  103. info = int(self.run_function("sysctl.get", [ASSIGN_CMD]))
  104. self.assertEqual(info, rand)
  105. except CommandExecutionError:
  106. os.remove(CONFIG)
  107. raise
  108. def __copy_sysctl(self):
  109. """
  110. Copies an existing sysconf file and returns temp file path. Copied
  111. file will be restored in tearDown
  112. """
  113. # Create new temporary file path and open needed files
  114. temp_path = salt.utils.files.mkstemp()
  115. with salt.utils.files.fopen(CONFIG, "r") as org_conf:
  116. with salt.utils.files.fopen(temp_path, "w") as temp_sysconf:
  117. # write sysctl lines to temp file
  118. for line in org_conf:
  119. temp_sysconf.write(line)
  120. return temp_path
  121. def __restore_sysctl(self):
  122. """
  123. Restores the original sysctl.conf file from temporary copy
  124. """
  125. # If sysctl testing file exists, delete it
  126. if os.path.isfile(CONFIG):
  127. os.remove(CONFIG)
  128. # write temp lines to sysctl file to restore
  129. with salt.utils.files.fopen(self.conf, "r") as temp_sysctl:
  130. with salt.utils.files.fopen(CONFIG, "w") as sysctl:
  131. for line in temp_sysctl:
  132. sysctl.write(line)
  133. # delete temporary file
  134. os.remove(self.conf)
  135. def __check_string(self, conf_file, to_find):
  136. """
  137. Returns True if given line is present in file
  138. """
  139. with salt.utils.files.fopen(conf_file, "r") as f_in:
  140. for line in f_in:
  141. if to_find in salt.utils.stringutils.to_unicode(line):
  142. return True
  143. return False
  144. def tearDown(self):
  145. """
  146. Clean up after tests
  147. """
  148. ret = self.run_function("sysctl.get", [ASSIGN_CMD])
  149. if ret != self.val:
  150. self.run_function("sysctl.assign", [ASSIGN_CMD, self.val])
  151. if self.has_conf is True:
  152. # restore original sysctl file
  153. self.__restore_sysctl()
  154. if self.has_conf is False and os.path.isfile(CONFIG):
  155. # remove sysctl.conf created by tests
  156. os.remove(CONFIG)