Updated Branches: refs/heads/trunk e5c6e1130 -> dab89e695
AMBARI-3817. Unittests for Group resource an all it's attributes (Eugene Chekanskiy via dlysnichenko) Project: http://git-wip-us.apache.org/repos/asf/incubator-ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ambari/commit/dab89e69 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ambari/tree/dab89e69 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ambari/diff/dab89e69 Branch: refs/heads/trunk Commit: dab89e6950bf9b02072c8452fcb1e7297883aa63 Parents: e5c6e11 Author: Lisnichenko Dmitro <dlysniche...@hortonworks.com> Authored: Tue Nov 19 21:53:40 2013 +0200 Committer: Lisnichenko Dmitro <dlysniche...@hortonworks.com> Committed: Tue Nov 19 21:53:40 2013 +0200 ---------------------------------------------------------------------- .../resource_management/TestGroupResource.py | 138 +++++++++++++++++++ 1 file changed, 138 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dab89e69/ambari-agent/src/test/python/resource_management/TestGroupResource.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/test/python/resource_management/TestGroupResource.py b/ambari-agent/src/test/python/resource_management/TestGroupResource.py new file mode 100644 index 0000000..0704359 --- /dev/null +++ b/ambari-agent/src/test/python/resource_management/TestGroupResource.py @@ -0,0 +1,138 @@ +''' +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.resources import Group +from resource_management.core.system import System + +import subprocess +import grp + + +@patch.object(System, "platform", new = 'redhat') +class TestGroupResource(TestCase): + + @patch.object(grp, "getgrnam") + @patch.object(subprocess, "Popen") + def test_action_create_nonexistent(self, popen_mock, getgrnam_mock): + subproc_mock = MagicMock() + subproc_mock.returncode = 0 + popen_mock.return_value = subproc_mock + getgrnam_mock.side_effect = KeyError() + + with Environment('/') as env: + Group('hadoop', + action='create', + password='secure' + ) + env.run() + + self.assertEqual(popen_mock.call_count, 1) + popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'groupadd -p secure hadoop'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) + getgrnam_mock.assert_called_with('hadoop') + + + @patch.object(grp, "getgrnam") + @patch.object(subprocess, "Popen") + def test_action_create_existent(self, popen_mock, getgrnam_mock): + subproc_mock = MagicMock() + subproc_mock.returncode = 0 + popen_mock.return_value = subproc_mock + getgrnam_mock.return_value = "mapred" + + with Environment('/') as env: + Group('mapred', + action='create', + gid=2, + password='secure' + ) + env.run() + + self.assertEqual(popen_mock.call_count, 1) + popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'groupmod -p secure -g 2 mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) + getgrnam_mock.assert_called_with('mapred') + + + @patch.object(grp, "getgrnam") + @patch.object(subprocess, "Popen") + def test_action_create_fail(self, popen_mock, getgrnam_mock): + subproc_mock = MagicMock() + subproc_mock.returncode = 1 + popen_mock.return_value = subproc_mock + getgrnam_mock.return_value = "mapred" + + try: + with Environment('/') as env: + Group('mapred', + action='create', + gid=2, + password='secure' + ) + env.run() + self.fail("Action 'create' should fail when checked_call fails") + except Fail: + pass + self.assertEqual(popen_mock.call_count, 1) + popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'groupmod -p secure -g 2 mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) + getgrnam_mock.assert_called_with('mapred') + + + @patch.object(grp, "getgrnam") + @patch.object(subprocess, "Popen") + def test_action_remove(self, popen_mock, getgrnam_mock): + + subproc_mock = MagicMock() + subproc_mock.returncode = 0 + popen_mock.return_value = subproc_mock + getgrnam_mock.return_value = "mapred" + + with Environment('/') as env: + Group('mapred', + action='remove' + ) + env.run() + + self.assertEqual(popen_mock.call_count, 1) + popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'groupdel mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) + getgrnam_mock.assert_called_with('mapred') + + + @patch.object(grp, "getgrnam") + @patch.object(subprocess, "Popen") + def test_action_remove_fail(self, popen_mock, getgrnam_mock): + + subproc_mock = MagicMock() + subproc_mock.returncode = 1 + popen_mock.return_value = subproc_mock + getgrnam_mock.return_value = "mapred" + + try: + with Environment('/') as env: + Group('mapred', + action='remove' + ) + env.run() + self.fail("Action 'delete' should fail when checked_call fails") + except Fail: + pass + + self.assertEqual(popen_mock.call_count, 1) + popen_mock.assert_called_with(['/bin/bash', '--login', '-c', 'groupdel mapred'], shell=False, preexec_fn=None, stderr=-2, stdout=-1, env=None, cwd=None) + getgrnam_mock.assert_called_with('mapred')