test_user.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Imports Standards
  5. import os
  6. # Import salt libs
  7. import salt.utils.platform
  8. import salt.utils.user
  9. from tests.support.mock import patch
  10. from tests.support.runtests import this_user
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase, skipIf
  13. # Import Conditionals
  14. try:
  15. import grp
  16. HAS_GRP = True
  17. except ImportError:
  18. HAS_GRP = False
  19. try:
  20. import pwd
  21. HAS_PWD = True
  22. except ImportError:
  23. HAS_PWD = False
  24. class TestUser(TestCase):
  25. @skipIf(HAS_GRP is False or HAS_PWD is False, "Module grp or pwd is missing")
  26. @skipIf(salt.utils.platform.is_windows(), "Module not available on Windows")
  27. def test_chugid_and_umask(self):
  28. running_user = this_user()
  29. running_group = grp.getgrgid(os.getgid()).gr_name
  30. gids = {30: "expectedgroup", 20: running_group}
  31. getgrnams = {
  32. "expectedgroup": grp.struct_group(
  33. ("expectedgroup", "*", 30, ["expecteduser"])
  34. ),
  35. running_group: grp.struct_group((running_group, "*", 20, [running_user])),
  36. }
  37. getpwnams = {
  38. "expecteduser": pwd.struct_passwd(
  39. ("expecteduser", "x", 30, 30, "-", "-", "-")
  40. ),
  41. running_user: pwd.struct_passwd((running_user, "x", 20, 20, "-", "-", "-")),
  42. }
  43. def getgrnam(group):
  44. return getgrnams[group]
  45. def getpwnam(user):
  46. return getpwnams[user]
  47. def getgrgid(gid):
  48. return getgrnams[gids[gid]]
  49. with patch("grp.getgrgid", getgrgid):
  50. with patch("grp.getgrnam", getgrnam):
  51. with patch("pwd.getpwnam", getpwnam):
  52. with patch("salt.utils.user.chugid") as chugid_mock:
  53. salt.utils.user.chugid_and_umask(
  54. "expecteduser", umask=None, group=running_group
  55. )
  56. chugid_mock.assert_called_with("expecteduser", running_group)
  57. salt.utils.user.chugid_and_umask(
  58. "expecteduser", umask=None, group=None
  59. )
  60. chugid_mock.assert_called_with("expecteduser", "expectedgroup")