test_pw_user.py 2.6 KB

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