You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ao...@apache.org on 2014/03/07 16:33:38 UTC
git commit: AMBARI-4993. Unittests for Directory resource an all it's
attributes (aonishuk)
Repository: ambari
Updated Branches:
refs/heads/trunk 5c95b13c9 -> 8da623ed5
AMBARI-4993. Unittests for Directory resource an all it's attributes
(aonishuk)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/8da623ed
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/8da623ed
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/8da623ed
Branch: refs/heads/trunk
Commit: 8da623ed5da454b75b2935ca2f951e2659128c91
Parents: 5c95b13
Author: Andrew Onischuk <ao...@hortonworks.com>
Authored: Fri Mar 7 07:30:02 2014 -0800
Committer: Andrew Onischuk <ao...@hortonworks.com>
Committed: Fri Mar 7 07:33:17 2014 -0800
----------------------------------------------------------------------
.../TestDirectoryResource.py | 176 +++++++++++++++++++
1 file changed, 176 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/8da623ed/ambari-agent/src/test/python/resource_management/TestDirectoryResource.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/resource_management/TestDirectoryResource.py b/ambari-agent/src/test/python/resource_management/TestDirectoryResource.py
new file mode 100644
index 0000000..866486e
--- /dev/null
+++ b/ambari-agent/src/test/python/resource_management/TestDirectoryResource.py
@@ -0,0 +1,176 @@
+'''
+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
+import os
+import shutil
+from resource_management.core.system import System
+from resource_management.core import Environment, Fail
+from resource_management.core.resources import Directory
+
+@patch.object(System, "os_family", new = 'redhat')
+class TestFileResource(TestCase):
+
+ @patch.object(os.path, "exists")
+ @patch.object(os, "makedirs")
+ @patch.object(os.path, "isdir")
+ @patch.object(os, "stat")
+ @patch.object(os,"chmod")
+ @patch.object(os,"chown")
+ @patch("resource_management.core.providers.system._coerce_uid")
+ @patch("resource_management.core.providers.system._coerce_gid")
+ def test_create_directory_recursive(self, _coerce_gid_mock, _coerce_uid_mock,
+ os_chown_mock, os_chmod_mock, os_stat_mock,
+ isdir_mock, os_makedirs_mock,
+ os_path_exists_mock):
+ os_path_exists_mock.return_value = False
+ isdir_mock.return_value = True
+ _coerce_uid_mock.return_value = 66
+ _coerce_gid_mock.return_value = 77
+ os_stat_mock.return_value = type("", (), dict(st_mode=0755, st_uid=0, st_gid=0))()
+
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='create',
+ mode=0777,
+ owner="hdfs",
+ group="hadoop",
+ recursive=True
+ )
+
+ os_makedirs_mock.assert_called_with('/a/b/c/d', 0777)
+ os_chmod_mock.assert_called_with('/a/b/c/d', 0777)
+ os_chown_mock.assert_any_call('/a/b/c/d', 66, -1)
+ os_chown_mock.assert_any_call('/a/b/c/d', -1, 77)
+
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "dirname")
+ @patch.object(os.path, "isdir")
+ @patch.object(os, "mkdir")
+ @patch.object(os, "stat")
+ @patch.object(os,"chmod")
+ @patch.object(os,"chown")
+ @patch("resource_management.core.providers.system._coerce_uid")
+ @patch("resource_management.core.providers.system._coerce_gid")
+ def test_create_directory_not_recursive(self, _coerce_gid_mock, _coerce_uid_mock,
+ os_chown_mock, os_chmod_mock, os_stat_mock,
+ mkdir_mock, isdir_mock, os_dirname_mock,
+ os_path_exists_mock):
+ os_path_exists_mock.return_value = False
+ os_dirname_mock.return_value = "/a/b/c"
+ isdir_mock.return_value = True
+ _coerce_uid_mock.return_value = 66
+ _coerce_gid_mock.return_value = 77
+ os_stat_mock.return_value = type("", (), dict(st_mode=0755, st_uid=0, st_gid=0))()
+
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='create',
+ mode=0777,
+ owner="hdfs",
+ group="hadoop"
+ )
+
+ mkdir_mock.assert_called_with('/a/b/c/d', 0777)
+ os_chmod_mock.assert_called_with('/a/b/c/d', 0777)
+ os_chown_mock.assert_any_call('/a/b/c/d', 66, -1)
+ os_chown_mock.assert_any_call('/a/b/c/d', -1, 77)
+
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "dirname")
+ @patch.object(os.path, "isdir")
+ def test_create_directory_failed_no_parent(self, isdir_mock, os_dirname_mock,
+ os_path_exists_mock):
+ os_path_exists_mock.return_value = False
+ os_dirname_mock.return_value = "/a/b/c"
+ isdir_mock.return_value = False
+
+
+ try:
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='create',
+ mode=0777,
+ owner="hdfs",
+ group="hadoop"
+ )
+ self.fail("Must fail because parent directory /a/b/c doesn't exist")
+ except Fail as e:
+ self.assertEqual("Applying Directory['/a/b/c/d'] failed, parent directory /a/b/c doesn't exist",
+ str(e))
+
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "isdir")
+ def test_create_directory_path_is_file_or_line(self, isdir_mock, os_path_exists_mock):
+ os_path_exists_mock.return_value = True
+ isdir_mock.return_value = False
+
+ try:
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='create',
+ mode=0777,
+ owner="hdfs",
+ group="hadoop"
+ )
+ self.fail("Must fail because file /a/b/c/d already exists")
+ except Fail as e:
+ self.assertEqual("Applying Directory['/a/b/c/d'] failed, file /a/b/c/d already exists",
+ str(e))
+
+ @patch.object(shutil, "rmtree")
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "isdir")
+ def test_delete_directory(self, isdir_mock, os_path_exists_mock, rmtree_mock):
+ os_path_exists_mock.return_value = True
+ isdir_mock.return_value = True
+
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='delete'
+ )
+
+ rmtree_mock.assert_called_with('/a/b/c/d')
+
+ @patch.object(os.path, "exists")
+ def test_delete_noexisting_directory(self, os_path_exists_mock):
+ os_path_exists_mock.return_value = False
+
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='delete'
+ )
+
+ @patch.object(shutil, "rmtree")
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "isdir")
+ def test_delete_directory_with_path_to_file(self, isdir_mock, os_path_exists_mock, rmtree_mock):
+ os_path_exists_mock.return_value = True
+ isdir_mock.return_value = False
+
+ try:
+ with Environment('/') as env:
+ Directory('/a/b/c/d',
+ action='delete'
+ )
+ self.fail("Must fail because /a/b/c/d is not a directory")
+ except Fail as e:
+ self.assertEqual("Applying Directory['/a/b/c/d'] failed, /a/b/c/d is not a directory",
+ str(e))
\ No newline at end of file