test_user.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. import os
  5. import grp
  6. import pwd
  7. # Import Salt Testing libs
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.helpers import this_user
  10. from tests.support.mock import (
  11. patch,
  12. NO_MOCK,
  13. NO_MOCK_REASON
  14. )
  15. # Import salt libs
  16. import salt.utils.platform
  17. import salt.utils.user
  18. class TestUser(TestCase):
  19. @skipIf(NO_MOCK, NO_MOCK_REASON)
  20. @skipIf(salt.utils.platform.is_windows(), "Module not available on Windows")
  21. def test_chugid_and_umask(self):
  22. running_user = this_user()
  23. running_group = grp.getgrgid(os.getgid()).gr_name
  24. gids = {30: 'expectedgroup', 20: running_group}
  25. getgrnams = {'expectedgroup': grp.struct_group(('expectedgroup', '*', 30, ['expecteduser'])),
  26. running_group: grp.struct_group((running_group, '*', 20, [running_user]))}
  27. getpwnams = {'expecteduser': pwd.struct_passwd(('expecteduser', 'x', 30, 30, '-', '-', '-')),
  28. running_user: pwd.struct_passwd((running_user, 'x', 20, 20, '-', '-', '-'))}
  29. def getgrnam(group):
  30. return getgrnams[group]
  31. def getpwnam(user):
  32. return getpwnams[user]
  33. def getgrgid(gid):
  34. return getgrnams[gids[gid]]
  35. with patch('grp.getgrgid', getgrgid):
  36. with patch('grp.getgrnam', getgrnam):
  37. with patch('pwd.getpwnam', getpwnam):
  38. with patch('salt.utils.user.chugid') as chugid_mock:
  39. salt.utils.user.chugid_and_umask('expecteduser', umask=None, group=running_group)
  40. chugid_mock.assert_called_with('expecteduser', running_group)
  41. salt.utils.user.chugid_and_umask('expecteduser', umask=None, group=None)
  42. chugid_mock.assert_called_with('expecteduser', 'expectedgroup')