ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject git commit: AMBARI-3873. Unittests for User resource an all it's attributes (Eugene Chekanskiy via dlysnichenko)
Date Mon, 25 Nov 2013 17:28:24 GMT
Updated Branches:
  refs/heads/trunk b8cc8ea55 -> 1daa58e25


AMBARI-3873. Unittests for User resource an all it's attributes (Eugene Chekanskiy via dlysnichenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1daa58e2
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1daa58e2
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1daa58e2

Branch: refs/heads/trunk
Commit: 1daa58e251896e98a6a9e6e9a78ba9396b920b88
Parents: b8cc8ea
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Mon Nov 25 19:27:21 2013 +0200
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Mon Nov 25 19:27:21 2013 +0200

----------------------------------------------------------------------
 .../resource_management/TestUserResource.py     | 202 +++++++++++++++++++
 1 file changed, 202 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1daa58e2/ambari-agent/src/test/python/resource_management/TestUserResource.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/resource_management/TestUserResource.py b/ambari-agent/src/test/python/resource_management/TestUserResource.py
new file mode 100644
index 0000000..1a7ab4b
--- /dev/null
+++ b/ambari-agent/src/test/python/resource_management/TestUserResource.py
@@ -0,0 +1,202 @@
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+from unittest import TestCase
+from mock.mock import patch, MagicMock
+
+from resource_management.core import Environment, Fail
+from resource_management.core.system import System
+from resource_management.core.resources import User
+import pwd
+import subprocess
+
+@patch.object(System, "platform", new = 'redhat')
+class TestUserResource(TestCase):
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_action_create_nonexistent(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = None
+    with Environment('/') as env:
+      user = User("mapred", action = "create")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'useradd -m -s /bin/bash
mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_action_create_existent(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred", action = "create")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/bash mapred'],
shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_action_delete(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred", action = "remove")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'userdel mapred'], shell=False,
preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_comment(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  comment = "testComment")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -c testComment
-s /bin/bash mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_home(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  home = "/test/home")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/bash -d
/test/home mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_password(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  password = "secure")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/bash -p
secure mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_shell(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  shell = "/bin/sh")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/sh mapred'],
shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_uid(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  uid = "1")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/bash -u
1 mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_gid(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  gid = "1")
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -s /bin/bash -g
1 mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)
+
+  @patch.object(subprocess, "Popen")
+  @patch.object(pwd, "getpwnam")
+  def test_attribute_groups(self, getpwnam_mock, popen_mock):
+    subproc_mock = MagicMock()
+    subproc_mock.returncode = 0
+    popen_mock.return_value = subproc_mock
+    getpwnam_mock.return_value = 1
+
+    with Environment('/') as env:
+      user = User("mapred",
+                  action = "create",
+                  groups = ['1','2','3'])
+    env.run()
+
+    popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'usermod -G 1,2,3 -s /bin/bash
mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None)
+    self.assertEqual(popen_mock.call_count, 1)
+    self.assertTrue(user.is_updated)


Mime
View raw message