瀏覽代碼

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

Andrew Onischuk 11 年之前
父節點
當前提交
40319d893f
共有 1 個文件被更改,包括 219 次插入0 次删除
  1. 219 0
      ambari-agent/src/test/python/resource_management/TestPropertiesFileResource.py

+ 219 - 0
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()