test_shadow.py 8.8 KB

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