TestPropertiesFileResource.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env python
  2. """
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. Ambari Agent
  17. """
  18. import os
  19. import time
  20. from unittest import TestCase
  21. from mock.mock import patch, MagicMock
  22. from resource_management.core import Environment, sudo
  23. from resource_management.core.system import System
  24. from resource_management.libraries import PropertiesFile
  25. @patch.object(System, "os_family", new='redhat')
  26. class TestPropertiesFIleResource(TestCase):
  27. """
  28. PropertiesFile="resource_management.libraries.providers.properties_file.PropertiesFileProvider"
  29. Testing PropertiesFile(PropertiesFileProvider) with different 'properties dictionary'
  30. """
  31. @patch("resource_management.core.providers.system._ensure_metadata")
  32. @patch.object(sudo, "create_file")
  33. @patch.object(sudo, "path_exists")
  34. @patch.object(sudo, "path_isdir")
  35. @patch.object(time, "asctime")
  36. def test_action_create_empty_properties_without_dir(self,
  37. time_asctime_mock,
  38. os_path_isdir_mock,
  39. os_path_exists_mock,
  40. create_file_mock,
  41. ensure_mock):
  42. """
  43. Tests if 'action_create' - creates new non existent file and write proper data
  44. 1) properties={}
  45. 2) dir=None
  46. """
  47. os_path_isdir_mock.side_effect = [False, True]
  48. os_path_exists_mock.return_value = False
  49. time_asctime_mock.return_value = 'Today is Wednesday'
  50. with Environment('/') as env:
  51. PropertiesFile('/somewhere_in_system/one_file.properties',
  52. dir=None,
  53. properties={}
  54. )
  55. create_file_mock.assert_called_with('/somewhere_in_system/one_file.properties', u'# Generated by Apache Ambari. Today is Wednesday\n \n \n')
  56. ensure_mock.assert_called()
  57. @patch("resource_management.core.providers.system._ensure_metadata")
  58. @patch.object(sudo, "create_file")
  59. @patch.object(sudo, "path_exists")
  60. @patch.object(sudo, "path_isdir")
  61. @patch.object(time, "asctime")
  62. def test_action_create_empty_properties_with_dir(self,
  63. time_asctime_mock,
  64. os_path_isdir_mock,
  65. os_path_exists_mock,
  66. create_file_mock,
  67. ensure_mock):
  68. """
  69. Tests if 'action_create' - creates new non existent file and write proper data
  70. 1) properties={}
  71. 2) dir='Some directory that exist '
  72. """
  73. os_path_isdir_mock.side_effect = [False, True]
  74. os_path_exists_mock.return_value = False
  75. time_asctime_mock.return_value = 'Some other day'
  76. with Environment('/') as env:
  77. PropertiesFile('file.txt',
  78. dir="/dir/and/dir",
  79. properties={},
  80. )
  81. create_file_mock.assert_called_with('/dir/and/dir/file.txt', u'# Generated by Apache Ambari. Some other day\n \n \n')
  82. ensure_mock.assert_called()
  83. @patch("resource_management.core.providers.system._ensure_metadata")
  84. @patch.object(sudo, "create_file")
  85. @patch.object(sudo, "path_exists")
  86. @patch.object(sudo, "path_isdir")
  87. @patch.object(time, "asctime")
  88. def test_action_create_properties_simple(self,
  89. time_asctime_mock,
  90. os_path_isdir_mock,
  91. os_path_exists_mock,
  92. create_file_mock,
  93. ensure_mock):
  94. """
  95. Tests if 'action_create' - creates new non existent file and write proper data
  96. 1) properties={"Some property":"Some value"}
  97. 2) dir=None
  98. """
  99. os_path_isdir_mock.side_effect = [False, True]
  100. os_path_exists_mock.return_value = False
  101. time_asctime_mock.return_value = 777
  102. with Environment('/') as env:
  103. PropertiesFile('/dir/new_file',
  104. properties={'property1': 'value1'},
  105. )
  106. create_file_mock.assert_called_with('/dir/new_file', u'# Generated by Apache Ambari. 777\n \nproperty1=value1\n \n')
  107. ensure_mock.assert_called()
  108. @patch("resource_management.core.providers.system._ensure_metadata")
  109. @patch.object(sudo, "create_file")
  110. @patch.object(sudo, "path_exists")
  111. @patch.object(sudo, "path_isdir")
  112. @patch.object(time, "asctime")
  113. def test_action_create_properties_with_metacharacters(self,
  114. time_asctime_mock,
  115. os_path_isdir_mock,
  116. os_path_exists_mock,
  117. create_file_mock,
  118. ensure_mock):
  119. """
  120. Tests if 'action_create' - creates new non existent file and write proper data
  121. 1) properties={"":"", "Some property":"Metacharacters: -%{} ${a.a}/"}
  122. 2) dir=None
  123. """
  124. os_path_isdir_mock.side_effect = [False, True]
  125. os_path_exists_mock.return_value = False
  126. time_asctime_mock.return_value = 777
  127. with Environment('/') as env:
  128. PropertiesFile('/dir/new_file',
  129. properties={"": "",
  130. "prop.1": "'.'yyyy-MM-dd-HH",
  131. "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
  132. "prop.2": "INFO, openjpa",
  133. "prop.4": "${oozie.log.dir}/oozie.log",
  134. "prop.empty": "",
  135. },
  136. )
  137. create_file_mock.assert_called_with('/dir/new_file', 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")
  138. ensure_mock.assert_called()
  139. @patch("resource_management.core.providers.system._ensure_metadata")
  140. @patch.object(sudo, "read_file")
  141. @patch.object(sudo, "create_file")
  142. @patch.object(sudo, "path_exists")
  143. @patch.object(sudo, "path_isdir")
  144. @patch.object(time, "asctime")
  145. def test_action_create_properties_rewrite_content(self,
  146. time_asctime_mock,
  147. os_path_isdir_mock,
  148. os_path_exists_mock,
  149. create_file_mock,
  150. read_file_mock,
  151. ensure_mock):
  152. """
  153. Tests if 'action_create' - rewrite file that exist
  154. 1) properties={"Some property":"Some value"}
  155. 2) dir="Some dir"
  156. """
  157. os_path_isdir_mock.side_effect = [False, True]
  158. os_path_exists_mock.return_value = True
  159. time_asctime_mock.return_value = 777
  160. read_file_mock.return_value = 'old-content'
  161. with Environment('/') as env:
  162. PropertiesFile('new_file',
  163. dir='/dir1',
  164. properties={'property_1': 'value1'},
  165. )
  166. read_file_mock.assert_called()
  167. create_file_mock.assert_called_with('/dir1/new_file', u'# Generated by Apache Ambari. 777\n \nproperty_1=value1\n \n')
  168. ensure_mock.assert_called()