ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aonis...@apache.org
Subject git commit: AMBARI-4631. unittest PropertyFile (Iryna Kuzmenko via aonishuk)
Date Wed, 12 Feb 2014 15:25:20 GMT
Updated Branches:
  refs/heads/trunk 9d6e78168 -> 40319d893


AMBARI-4631. unittest PropertyFile (Iryna Kuzmenko via aonishuk)


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

Branch: refs/heads/trunk
Commit: 40319d893f409c73951fb13331125d37e564e4e3
Parents: 9d6e781
Author: Andrew Onischuk <aonishuk@hortonworks.com>
Authored: Wed Feb 12 07:22:49 2014 -0800
Committer: Andrew Onischuk <aonishuk@hortonworks.com>
Committed: Wed Feb 12 07:23:44 2014 -0800

----------------------------------------------------------------------
 .../TestPropertiesFileResource.py               | 219 +++++++++++++++++++
 1 file changed, 219 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/40319d89/ambari-agent/src/test/python/resource_management/TestPropertiesFileResource.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/resource_management/TestPropertiesFileResource.py
b/ambari-agent/src/test/python/resource_management/TestPropertiesFileResource.py
new file mode 100644
index 0000000..1f7581b
--- /dev/null
+++ b/ambari-agent/src/test/python/resource_management/TestPropertiesFileResource.py
@@ -0,0 +1,219 @@
+#!/usr/bin/env python
+
+"""
+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.
+
+Ambari Agent
+
+"""
+import os
+import time
+from unittest import TestCase
+from mock.mock import patch, MagicMock
+from resource_management.core import Environment
+from resource_management.core.system import System
+from resource_management.libraries import PropertiesFile
+
+@patch.object(System, "os_family", new='redhat')
+class TestPropertiesFIleResource(TestCase):
+  """
+  PropertiesFile="resource_management.libraries.providers.properties_file.PropertiesFileProvider"
+  Testing PropertiesFile(PropertiesFileProvider) with different 'properties dictionary'
+  """
+
+
+  @patch("resource_management.core.providers.system._ensure_metadata")
+  @patch("__builtin__.open")
+  @patch.object(os.path, "exists")
+  @patch.object(os.path, "isdir")
+  @patch.object(time, "asctime")
+  def test_action_create_empty_properties_without_dir(self,
+                                                      time_asctime_mock,
+                                                      os_path_isdir_mock,
+                                                      os_path_exists_mock,
+                                                      open_mock,
+                                                      ensure_mock):
+    """
+    Tests if 'action_create' - creates new non existent file and write proper data
+    1) properties={}
+    2) dir=None
+    """
+    os_path_isdir_mock.side_effect = [False, True]
+    os_path_exists_mock.return_value = False
+    time_asctime_mock.return_value = 'Today is Wednesday'
+
+    result_file = MagicMock()
+    open_mock.return_value = result_file
+
+    with Environment('/') as env:
+      PropertiesFile('/somewhere_in_system/one_file.properties',
+                     dir=None,
+                     properties={}
+      )
+
+    open_mock.assert_called_with('/somewhere_in_system/one_file.properties', 'wb')
+    result_file.__enter__().write.assert_called_with( u'# Generated by Apache Ambari. Today
is Wednesday\n    \n    \n')
+    self.assertEqual(open_mock.call_count, 1)
+    ensure_mock.assert_called()
+
+
+  @patch("resource_management.core.providers.system._ensure_metadata")
+  @patch("__builtin__.open")
+  @patch.object(os.path, "exists")
+  @patch.object(os.path, "isdir")
+  @patch.object(time, "asctime")
+  def test_action_create_empty_properties_with_dir(self,
+                                                   time_asctime_mock,
+                                                   os_path_isdir_mock,
+                                                   os_path_exists_mock,
+                                                   open_mock,
+                                                   ensure_mock):
+    """
+    Tests if 'action_create' - creates new non existent file and write proper data
+    1) properties={}
+    2) dir='Some directory that exist '
+    """
+    os_path_isdir_mock.side_effect = [False, True]
+    os_path_exists_mock.return_value = False
+    time_asctime_mock.return_value = 'Some other day'
+
+    result_file = MagicMock()
+    open_mock.return_value = result_file
+
+    with Environment('/') as env:
+      PropertiesFile('file.txt',
+                     dir="/dir/and/dir",
+                     properties={},
+      )
+
+    open_mock.assert_called_with('/dir/and/dir/file.txt', 'wb')
+    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. Some
other day\n    \n    \n')
+    self.assertEqual(open_mock.call_count, 1)
+    ensure_mock.assert_called()
+
+
+  @patch("resource_management.core.providers.system._ensure_metadata")
+  @patch("__builtin__.open")
+  @patch.object(os.path, "exists")
+  @patch.object(os.path, "isdir")
+  @patch.object(time, "asctime")
+  def test_action_create_properties_simple(self,
+                                           time_asctime_mock,
+                                           os_path_isdir_mock,
+                                           os_path_exists_mock,
+                                           open_mock,
+                                           ensure_mock):
+    """
+    Tests if 'action_create' - creates new non existent file and write proper data
+    1) properties={"Some property":"Some value"}
+    2) dir=None
+    """
+
+    os_path_isdir_mock.side_effect = [False, True]
+    os_path_exists_mock.return_value = False
+    time_asctime_mock.return_value = 777
+
+    result_file = MagicMock()
+    open_mock.return_value = result_file
+
+    with Environment('/') as env:
+      PropertiesFile('/dir/new_file',
+                     properties={'property1': 'value1'},
+      )
+
+    open_mock.assert_called_with('/dir/new_file',
+                                 'wb')
+    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n
   \nproperty1=value1\n    \n')
+    self.assertEqual(open_mock.call_count, 1)
+    ensure_mock.assert_called()
+
+
+  @patch("resource_management.core.providers.system._ensure_metadata")
+  @patch("__builtin__.open")
+  @patch.object(os.path, "exists")
+  @patch.object(os.path, "isdir")
+  @patch.object(time, "asctime")
+  def test_action_create_properties_with_metacharacters(self,
+                                                        time_asctime_mock,
+                                                        os_path_isdir_mock,
+                                                        os_path_exists_mock,
+                                                        open_mock,
+                                                        ensure_mock):
+    """
+    Tests if 'action_create' - creates new non existent file and write proper data
+    1) properties={"":"", "Some property":"Metacharacters: -%{} ${a.a}/"}
+    2) dir=None
+    """
+    os_path_isdir_mock.side_effect = [False, True]
+    os_path_exists_mock.return_value = False
+    time_asctime_mock.return_value = 777
+
+    result_file = MagicMock()
+    open_mock.return_value = result_file
+
+    with Environment('/') as env:
+      PropertiesFile('/dir/new_file',
+                     properties={"": "",
+                                 "prop.1": "'.'yyyy-MM-dd-HH",
+                                 "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
+                                 "prop.2": "INFO, openjpa",
+                                 "prop.4": "${oozie.log.dir}/oozie.log",
+                                 "prop.empty": "",
+                     },
+      )
+
+    open_mock.assert_called_with('/dir/new_file','wb')
+    result_file.__enter__().write.assert_called_with(u"# Generated by Apache Ambari. 777\n
   \n=\nprop.1='.'yyyy-MM-dd-HH\nprop.2=INFO, openjpa\nprop.3=%d{ISO8601} %5p %c{1}:%L - %m%n\nprop.4=${oozie.log.dir}/oozie.log\nprop.empty=\n
   \n")
+    self.assertEqual(open_mock.call_count, 1)
+    ensure_mock.assert_called()
+
+
+  @patch("resource_management.core.providers.system._ensure_metadata")
+  @patch("__builtin__.open")
+  @patch.object(os.path, "exists")
+  @patch.object(os.path, "isdir")
+  @patch.object(time, "asctime")
+  def test_action_create_properties_rewrite_content(self,
+                                                    time_asctime_mock,
+                                                    os_path_isdir_mock,
+                                                    os_path_exists_mock,
+                                                    open_mock,
+                                                    ensure_mock):
+    """
+    Tests if 'action_create' - rewrite file that exist
+    1) properties={"Some property":"Some value"}
+    2) dir="Some dir"
+    """
+    os_path_isdir_mock.side_effect = [False, True]
+    os_path_exists_mock.return_value = True
+    time_asctime_mock.return_value = 777
+
+    result_file = MagicMock()
+    result_file.read.return_value = 'old-content'
+    open_mock.return_value = result_file
+
+    with Environment('/') as env:
+      PropertiesFile('new_file',
+                     dir='/dir1',
+                     properties={'property_1': 'value1'},
+      )
+
+    result_file.read.assert_called()
+    open_mock.assert_called_with('/dir1/new_file', 'wb')
+    result_file.__enter__().write.assert_called_with(u'# Generated by Apache Ambari. 777\n
   \nproperty_1=value1\n    \n')
+    self.assertEqual(open_mock.call_count, 2)
+    ensure_mock.assert_called()


Mime
View raw message