TestUserResource.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. '''
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16. from unittest import TestCase
  17. from mock.mock import patch, MagicMock
  18. from resource_management.core import Environment, Fail
  19. from resource_management.core.system import System
  20. from resource_management.core.resources import User
  21. import pwd
  22. import subprocess
  23. import os
  24. @patch.object(System, "os_family", new = 'redhat')
  25. @patch.object(os, "environ", new = {'PATH':'/bin'})
  26. class TestUserResource(TestCase):
  27. @patch.object(subprocess, "Popen")
  28. @patch.object(pwd, "getpwnam")
  29. def test_action_create_nonexistent(self, getpwnam_mock, popen_mock):
  30. subproc_mock = MagicMock()
  31. subproc_mock.returncode = 0
  32. popen_mock.return_value = subproc_mock
  33. getpwnam_mock.return_value = None
  34. with Environment('/') as env:
  35. user = User("mapred", action = "create", shell = "/bin/bash")
  36. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E useradd -m -s /bin/bash mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  37. self.assertEqual(popen_mock.call_count, 1)
  38. @patch.object(subprocess, "Popen")
  39. @patch.object(pwd, "getpwnam")
  40. def test_action_create_existent(self, getpwnam_mock, popen_mock):
  41. subproc_mock = MagicMock()
  42. subproc_mock.returncode = 0
  43. popen_mock.return_value = subproc_mock
  44. getpwnam_mock.return_value = 1
  45. with Environment('/') as env:
  46. user = User("mapred", action = "create", shell = "/bin/bash")
  47. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/bash mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  48. self.assertEqual(popen_mock.call_count, 1)
  49. @patch.object(subprocess, "Popen")
  50. @patch.object(pwd, "getpwnam")
  51. def test_action_delete(self, getpwnam_mock, popen_mock):
  52. subproc_mock = MagicMock()
  53. subproc_mock.returncode = 0
  54. popen_mock.return_value = subproc_mock
  55. getpwnam_mock.return_value = 1
  56. with Environment('/') as env:
  57. user = User("mapred", action = "remove", shell = "/bin/bash")
  58. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', 'userdel mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  59. self.assertEqual(popen_mock.call_count, 1)
  60. @patch.object(subprocess, "Popen")
  61. @patch.object(pwd, "getpwnam")
  62. def test_attribute_comment(self, getpwnam_mock, popen_mock):
  63. subproc_mock = MagicMock()
  64. subproc_mock.returncode = 0
  65. popen_mock.return_value = subproc_mock
  66. getpwnam_mock.return_value = 1
  67. with Environment('/') as env:
  68. user = User("mapred", action = "create", comment = "testComment",
  69. shell = "/bin/bash")
  70. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -c testComment -s /bin/bash mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  71. self.assertEqual(popen_mock.call_count, 1)
  72. @patch.object(subprocess, "Popen")
  73. @patch.object(pwd, "getpwnam")
  74. def test_attribute_home(self, getpwnam_mock, popen_mock):
  75. subproc_mock = MagicMock()
  76. subproc_mock.returncode = 0
  77. popen_mock.return_value = subproc_mock
  78. getpwnam_mock.return_value = 1
  79. with Environment('/') as env:
  80. user = User("mapred", action = "create", home = "/test/home",
  81. shell = "/bin/bash")
  82. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/bash -d /test/home mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  83. self.assertEqual(popen_mock.call_count, 1)
  84. @patch.object(subprocess, "Popen")
  85. @patch.object(pwd, "getpwnam")
  86. def test_attribute_password(self, getpwnam_mock, popen_mock):
  87. subproc_mock = MagicMock()
  88. subproc_mock.returncode = 0
  89. popen_mock.return_value = subproc_mock
  90. getpwnam_mock.return_value = 1
  91. with Environment('/') as env:
  92. user = User("mapred", action = "create", password = "secure",
  93. shell = "/bin/bash")
  94. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/bash -p secure mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  95. self.assertEqual(popen_mock.call_count, 1)
  96. @patch.object(subprocess, "Popen")
  97. @patch.object(pwd, "getpwnam")
  98. def test_attribute_shell(self, getpwnam_mock, popen_mock):
  99. subproc_mock = MagicMock()
  100. subproc_mock.returncode = 0
  101. popen_mock.return_value = subproc_mock
  102. getpwnam_mock.return_value = 1
  103. with Environment('/') as env:
  104. user = User("mapred", action = "create", shell = "/bin/sh")
  105. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/sh mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  106. self.assertEqual(popen_mock.call_count, 1)
  107. @patch.object(subprocess, "Popen")
  108. @patch.object(pwd, "getpwnam")
  109. def test_attribute_uid(self, getpwnam_mock, popen_mock):
  110. subproc_mock = MagicMock()
  111. subproc_mock.returncode = 0
  112. popen_mock.return_value = subproc_mock
  113. getpwnam_mock.return_value = 1
  114. with Environment('/') as env:
  115. user = User("mapred", action = "create", uid = "1", shell = "/bin/bash")
  116. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/bash -u 1 mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  117. self.assertEqual(popen_mock.call_count, 1)
  118. @patch.object(subprocess, "Popen")
  119. @patch.object(pwd, "getpwnam")
  120. def test_attribute_gid(self, getpwnam_mock, popen_mock):
  121. subproc_mock = MagicMock()
  122. subproc_mock.returncode = 0
  123. popen_mock.return_value = subproc_mock
  124. getpwnam_mock.return_value = 1
  125. with Environment('/') as env:
  126. user = User("mapred", action = "create", gid = "1", shell = "/bin/bash")
  127. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -s /bin/bash -g 1 mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  128. self.assertEqual(popen_mock.call_count, 1)
  129. @patch.object(subprocess, "Popen")
  130. @patch.object(pwd, "getpwnam")
  131. def test_attribute_groups(self, getpwnam_mock, popen_mock):
  132. subproc_mock = MagicMock()
  133. subproc_mock.returncode = 0
  134. popen_mock.return_value = subproc_mock
  135. getpwnam_mock.return_value = 1
  136. with Environment('/') as env:
  137. user = User("mapred", action = "create", groups = ['1','2','3'],
  138. shell = "/bin/bash")
  139. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E usermod -G 1,2,3 -s /bin/bash mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  140. self.assertEqual(popen_mock.call_count, 1)
  141. @patch.object(subprocess, "Popen")
  142. @patch.object(pwd, "getpwnam")
  143. def test_missing_shell_argument(self, getpwnam_mock, popen_mock):
  144. subproc_mock = MagicMock()
  145. subproc_mock.returncode = 0
  146. popen_mock.return_value = subproc_mock
  147. getpwnam_mock.return_value = None
  148. with Environment('/') as env:
  149. user = User("mapred", action = "create")
  150. popen_mock.assert_called_with(['/bin/bash', '--login', '--noprofile', '-c', "/usr/bin/sudo PATH=/bin -H -E useradd -m mapred"], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env={'PATH': '/bin'}, cwd=None)
  151. self.assertEqual(popen_mock.call_count, 1)