test_pw_user.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.integration.modules.pw_user
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import python libs
  8. from __future__ import absolute_import, unicode_literals, print_function
  9. import string
  10. import random
  11. # Import Salt Testing libs
  12. from tests.support.case import ModuleCase
  13. from tests.support.helpers import destructiveTest, skip_if_not_root
  14. # Import 3rd-party libs
  15. from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
  16. class PwUserModuleTest(ModuleCase):
  17. def setUp(self):
  18. super(PwUserModuleTest, self).setUp()
  19. os_grain = self.run_function('grains.item', ['kernel'])
  20. if os_grain['kernel'] != 'FreeBSD':
  21. self.skipTest(
  22. 'Test not applicable to \'{kernel}\' kernel'.format(
  23. **os_grain
  24. )
  25. )
  26. def __random_string(self, size=6):
  27. return ''.join(
  28. random.choice(string.ascii_uppercase + string.digits)
  29. for x in range(size)
  30. )
  31. @destructiveTest
  32. @skip_if_not_root
  33. def test_groups_includes_primary(self):
  34. # Let's create a user, which usually creates the group matching the
  35. # name
  36. uname = self.__random_string()
  37. if self.run_function('user.add', [uname]) is not True:
  38. # Skip because creating is not what we're testing here
  39. self.run_function('user.delete', [uname, True, True])
  40. self.skipTest('Failed to create user')
  41. try:
  42. uinfo = self.run_function('user.info', [uname])
  43. self.assertIn(uname, uinfo['groups'])
  44. # This uid is available, store it
  45. uid = uinfo['uid']
  46. self.run_function('user.delete', [uname, True, True])
  47. # Now, a weird group id
  48. gname = self.__random_string()
  49. if self.run_function('group.add', [gname]) is not True:
  50. self.run_function('group.delete', [gname, True, True])
  51. self.skipTest('Failed to create group')
  52. ginfo = self.run_function('group.info', [gname])
  53. # And create the user with that gid
  54. if self.run_function('user.add', [uname, uid, ginfo['gid']]) is False:
  55. # Skip because creating is not what we're testing here
  56. self.run_function('user.delete', [uname, True, True])
  57. self.skipTest('Failed to create user')
  58. uinfo = self.run_function('user.info', [uname])
  59. self.assertIn(gname, uinfo['groups'])
  60. except AssertionError:
  61. self.run_function('user.delete', [uname, True, True])
  62. raise