test_shadow.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. # -*- coding: utf-8 -*-
  2. '''
  3. integration tests for shadow linux
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import random
  8. import string
  9. import os
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.unit import skipIf
  13. from tests.support.helpers import destructiveTest, flaky, skip_if_not_root
  14. # Import Salt libs
  15. import salt.utils.files
  16. import salt.utils.platform
  17. import salt.modules.shadow
  18. from salt.ext.six.moves import range
  19. @skip_if_not_root
  20. @skipIf(not salt.utils.platform.is_linux(), 'These tests can only be run on linux')
  21. class ShadowModuleTest(ModuleCase):
  22. '''
  23. Validate the linux shadow system module
  24. '''
  25. def setUp(self):
  26. '''
  27. Get current settings
  28. '''
  29. self._password = self.run_function('shadow.gen_password', ['Password1234'])
  30. if 'ERROR' in self._password:
  31. self.fail('Failed to generate password: {0}'.format(self._password))
  32. super(ShadowModuleTest, self).setUp()
  33. os_grain = self.run_function('grains.item', ['kernel'])
  34. if os_grain['kernel'] not in 'Linux':
  35. self.skipTest(
  36. 'Test not applicable to \'{kernel}\' kernel'.format(
  37. **os_grain
  38. )
  39. )
  40. self._test_user = self.__random_string()
  41. self._no_user = self.__random_string()
  42. self._password = salt.modules.shadow.gen_password('Password1234')
  43. def __random_string(self, size=6):
  44. '''
  45. Generates a random username
  46. '''
  47. return 'tu-' + ''.join(
  48. random.choice(string.ascii_lowercase + string.digits)
  49. for x in range(size)
  50. )
  51. @destructiveTest
  52. def test_info(self):
  53. '''
  54. Test shadow.info
  55. '''
  56. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  57. self.run_function('user.add', [self._test_user])
  58. # Correct Functionality
  59. ret = self.run_function('shadow.info', [self._test_user])
  60. self.assertEqual(ret['name'], self._test_user)
  61. # User does not exist
  62. ret = self.run_function('shadow.info', [self._no_user])
  63. self.assertEqual(ret['name'], '')
  64. @destructiveTest
  65. def test_del_password(self):
  66. '''
  67. Test shadow.del_password
  68. '''
  69. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  70. self.run_function('user.add', [self._test_user])
  71. # Correct Functionality
  72. self.assertTrue(self.run_function('shadow.del_password', [self._test_user]))
  73. self.assertEqual(
  74. self.run_function('shadow.info', [self._test_user])['passwd'], '')
  75. # User does not exist
  76. self.assertFalse(self.run_function('shadow.del_password', [self._no_user]))
  77. @destructiveTest
  78. def test_set_password(self):
  79. '''
  80. Test shadow.set_password
  81. '''
  82. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  83. self.run_function('user.add', [self._test_user])
  84. # Correct Functionality
  85. self.assertTrue(self.run_function('shadow.set_password', [self._test_user, self._password]))
  86. # User does not exist
  87. self.assertFalse(self.run_function('shadow.set_password', [self._no_user, self._password]))
  88. @destructiveTest
  89. def test_set_inactdays(self):
  90. '''
  91. Test shadow.set_inactdays
  92. '''
  93. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  94. self.run_function('user.add', [self._test_user])
  95. # Correct Functionality
  96. self.assertTrue(self.run_function('shadow.set_inactdays', [self._test_user, 12]))
  97. # User does not exist (set_inactdays return None is user does not exist)
  98. self.assertFalse(self.run_function('shadow.set_inactdays', [self._no_user, 12]))
  99. @destructiveTest
  100. def test_set_maxdays(self):
  101. '''
  102. Test shadow.set_maxdays
  103. '''
  104. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  105. self.run_function('user.add', [self._test_user])
  106. # Correct Functionality
  107. self.assertTrue(self.run_function('shadow.set_maxdays', [self._test_user, 12]))
  108. # User does not exist (set_inactdays return None is user does not exist)
  109. self.assertFalse(self.run_function('shadow.set_maxdays', [self._no_user, 12]))
  110. @destructiveTest
  111. def test_set_mindays(self):
  112. '''
  113. Test shadow.set_mindays
  114. '''
  115. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  116. self.run_function('user.add', [self._test_user])
  117. # Correct Functionality
  118. self.assertTrue(self.run_function('shadow.set_mindays', [self._test_user, 12]))
  119. # User does not exist (set_inactdays return None is user does not exist)
  120. self.assertFalse(self.run_function('shadow.set_mindays', [self._no_user, 12]))
  121. @flaky
  122. @destructiveTest
  123. def test_lock_password(self):
  124. '''
  125. Test shadow.lock_password
  126. '''
  127. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  128. self.run_function('user.add', [self._test_user])
  129. self.run_function('shadow.set_password', [self._test_user, self._password])
  130. # Correct Functionality
  131. self.assertTrue(self.run_function('shadow.lock_password', [self._test_user]))
  132. # User does not exist (set_inactdays return None is user does not exist)
  133. self.assertFalse(self.run_function('shadow.lock_password', [self._no_user]))
  134. @destructiveTest
  135. def test_unlock_password(self):
  136. '''
  137. Test shadow.lock_password
  138. '''
  139. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  140. self.run_function('user.add', [self._test_user])
  141. self.run_function('shadow.set_password', [self._test_user, self._password])
  142. # Correct Functionality
  143. self.assertTrue(self.run_function('shadow.unlock_password', [self._test_user]))
  144. # User does not exist (set_inactdays return None is user does not exist)
  145. self.assertFalse(self.run_function('shadow.unlock_password', [self._no_user]))
  146. @destructiveTest
  147. def test_set_warndays(self):
  148. '''
  149. Test shadow.set_warndays
  150. '''
  151. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  152. self.run_function('user.add', [self._test_user])
  153. # Correct Functionality
  154. self.assertTrue(self.run_function('shadow.set_warndays', [self._test_user, 12]))
  155. # User does not exist (set_inactdays return None is user does not exist)
  156. self.assertFalse(self.run_function('shadow.set_warndays', [self._no_user, 12]))
  157. @destructiveTest
  158. def test_set_date(self):
  159. '''
  160. Test shadow.set_date
  161. '''
  162. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  163. self.run_function('user.add', [self._test_user])
  164. # Correct Functionality
  165. self.assertTrue(self.run_function('shadow.set_date', [self._test_user, '2016-08-19']))
  166. # User does not exist (set_inactdays return None is user does not exist)
  167. self.assertFalse(self.run_function('shadow.set_date', [self._no_user, '2016-08-19']))
  168. @destructiveTest
  169. def test_set_expire(self):
  170. '''
  171. Test shadow.set_exipre
  172. '''
  173. self.addCleanup(self.run_function, 'user.delete', [self._test_user])
  174. self.run_function('user.add', [self._test_user])
  175. # Correct Functionality
  176. self.assertTrue(self.run_function('shadow.set_expire', [self._test_user, '2016-08-25']))
  177. # User does not exist (set_inactdays return None is user does not exist)
  178. self.assertFalse(self.run_function('shadow.set_expire', [self._no_user, '2016-08-25']))
  179. @destructiveTest
  180. def test_set_del_root_password(self):
  181. '''
  182. Test set/del password for root
  183. '''
  184. # saving shadow file
  185. if not os.access("/etc/shadow", os.R_OK | os.W_OK):
  186. self.skipTest('Could not save initial state of /etc/shadow')
  187. def restore_shadow_file(contents):
  188. # restore shadow file
  189. with salt.utils.files.fopen('/etc/shadow', 'w') as wfh:
  190. wfh.write(contents)
  191. with salt.utils.files.fopen('/etc/shadow', 'r') as rfh:
  192. contents = rfh.read()
  193. self.addCleanup(restore_shadow_file, contents)
  194. # set root password
  195. self.assertTrue(self.run_function('shadow.set_password', ['root', self._password]))
  196. self.assertEqual(self.run_function('shadow.info', ['root'])['passwd'], self._password)
  197. # delete root password
  198. self.assertTrue(self.run_function('shadow.del_password', ['root']))
  199. self.assertEqual(self.run_function('shadow.info', ['root'])['passwd'], '')