1
0

test_mac_sysctl.py 6.3 KB

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