TestXmlConfigResource.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 XmlConfig
  25. @patch.object(System, "os_family", new='redhat')
  26. class TestXmlConfigResource(TestCase):
  27. """
  28. XmlConfig="resource_management.libraries.providers.xml_config.XmlConfigProvider",
  29. Testing XmlConfig(XmlConfigProvider) with different 'resource configurations'
  30. """
  31. @patch("resource_management.core.providers.system._ensure_metadata")
  32. @patch.object(sudo, "create_file")
  33. @patch.object(os.path, "exists")
  34. @patch.object(os.path, "isdir")
  35. @patch.object(time, "asctime")
  36. def test_action_create_empty_xml_config(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 'create' action - creates new non existent xml file and write proper data
  44. where configurations={}
  45. """
  46. os_path_isdir_mock.side_effect = [False, True]
  47. os_path_exists_mock.return_value = False
  48. time_asctime_mock.return_value = 'Wed 2014-02'
  49. with Environment('/') as env:
  50. XmlConfig('file.xml',
  51. conf_dir='/dir/conf',
  52. configurations={},
  53. configuration_attributes={}
  54. )
  55. create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n </configuration>')
  56. @patch("resource_management.core.providers.system._ensure_metadata")
  57. @patch.object(sudo, "create_file")
  58. @patch.object(os.path, "exists")
  59. @patch.object(os.path, "isdir")
  60. @patch.object(time, "asctime")
  61. def test_action_create_simple_xml_config(self,
  62. time_asctime_mock,
  63. os_path_isdir_mock,
  64. os_path_exists_mock,
  65. create_file_mock,
  66. ensure_mock):
  67. """
  68. Tests if 'create' action - creates new non existent xml file and write proper data
  69. where configurations={"Some conf":"Some value"}
  70. """
  71. os_path_isdir_mock.side_effect = [False, True]
  72. os_path_exists_mock.return_value = False
  73. time_asctime_mock.return_value = 'Wed 2014-02'
  74. with Environment('/') as env:
  75. XmlConfig('file.xml',
  76. conf_dir='/dir/conf',
  77. configurations={'property1': 'value1'},
  78. configuration_attributes={'attr': {'property1': 'attr_value'}}
  79. )
  80. create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name>property1</name>\n <value>value1</value>\n <attr>attr_value</attr>\n </property>\n \n </configuration>')
  81. @patch("resource_management.core.providers.system._ensure_metadata")
  82. @patch.object(sudo, "create_file")
  83. @patch.object(os.path, "exists")
  84. @patch.object(os.path, "isdir")
  85. @patch.object(time, "asctime")
  86. def test_action_create_xml_config_with_metacharacters(self,
  87. time_asctime_mock,
  88. os_path_isdir_mock,
  89. os_path_exists_mock,
  90. create_file_mock,
  91. ensure_mock):
  92. """
  93. Tests if 'create' action - creates new non existent xml file and write proper data
  94. where configurations={"Some conf":"Some metacharacters"}
  95. """
  96. os_path_isdir_mock.side_effect = [False, True]
  97. os_path_exists_mock.return_value = False
  98. time_asctime_mock.return_value = 'Wed 2014-02'
  99. with Environment('/') as env:
  100. XmlConfig('file.xml',
  101. conf_dir='/dir/conf',
  102. configurations={"": "",
  103. "prop.1": "'.'yyyy-MM-dd-HH",
  104. "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
  105. "prop.2": "INFO, openjpa",
  106. "prop.4": "${oozie.log.dir}/oozie.log",
  107. "prop.empty": "",
  108. },
  109. configuration_attributes={
  110. "": {
  111. "prop.1": "should_not_be_printed",
  112. "prop.2": "should_not_be_printed",
  113. },
  114. "attr1": {
  115. "prop.1": "x",
  116. "prop.8": "not_existed",
  117. },
  118. "attr2": {
  119. "prop.4": "value4",
  120. "prop.3": "value3"
  121. },
  122. "attr_empty": {
  123. },
  124. "attr_value_empty": {
  125. "prop.4": "",
  126. "prop.empty": ""
  127. }
  128. })
  129. create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>prop.1</name>\n <value>&#39;.&#39;yyyy-MM-dd-HH</value>\n <attr1>x</attr1>\n </property>\n \n <property>\n <name>prop.2</name>\n <value>INFO, openjpa</value>\n </property>\n \n <property>\n <name>prop.3</name>\n <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n <attr2>value3</attr2>\n </property>\n \n <property>\n <name>prop.4</name>\n <value>${oozie.log.dir}/oozie.log</value>\n <attr_value_empty></attr_value_empty>\n <attr2>value4</attr2>\n </property>\n \n <property>\n <name>prop.empty</name>\n <value></value>\n <attr_value_empty></attr_value_empty>\n </property>\n \n </configuration>')
  130. @patch("resource_management.core.providers.system._ensure_metadata")
  131. @patch.object(sudo, "create_file")
  132. @patch.object(os.path, "exists")
  133. @patch.object(os.path, "isdir")
  134. @patch.object(time, "asctime")
  135. def test_action_create_xml_config_sorted_by_key(self,
  136. time_asctime_mock,
  137. os_path_isdir_mock,
  138. os_path_exists_mock,
  139. create_file_mock,
  140. ensure_mock):
  141. """
  142. Tests if 'create' action - creates new non existent xml file and writes proper data
  143. where configurations={"Key":"Value"} are stored in sorted by key order
  144. """
  145. os_path_isdir_mock.side_effect = [False, True]
  146. os_path_exists_mock.return_value = False
  147. time_asctime_mock.return_value = 'Wed 2014-02'
  148. with Environment('/') as env:
  149. XmlConfig('file.xml',
  150. conf_dir='/dir/conf',
  151. configurations={"": "",
  152. "third": "should be third",
  153. "first": "should be first",
  154. "z_last": "should be last",
  155. "second": "should be second",
  156. },
  157. configuration_attributes={}
  158. )
  159. create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>first</name>\n <value>should be first</value>\n </property>\n \n <property>\n <name>second</name>\n <value>should be second</value>\n </property>\n \n <property>\n <name>third</name>\n <value>should be third</value>\n </property>\n \n <property>\n <name>z_last</name>\n <value>should be last</value>\n </property>\n \n </configuration>')
  160. @patch("resource_management.libraries.providers.xml_config.File")
  161. @patch.object(os.path, "exists")
  162. @patch.object(os.path, "isdir")
  163. def test_action_create_arguments(self, os_path_isdir_mock ,os_path_exists_mock, file_mock):
  164. os_path_isdir_mock.side_effect = [False, True]
  165. os_path_exists_mock.return_value = False
  166. with Environment() as env:
  167. XmlConfig('xmlFile.xml',
  168. conf_dir='/dir/conf',
  169. configurations={'property1': 'value1'},
  170. configuration_attributes={'attr': {'property1': 'attr_value'}},
  171. mode = 0755,
  172. owner = "hdfs",
  173. group = "hadoop",
  174. encoding = "Code"
  175. )
  176. self.assertEqual(file_mock.call_args[0][0],'/dir/conf/xmlFile.xml')
  177. call_args = file_mock.call_args[1].copy()
  178. del call_args['content']
  179. self.assertEqual(call_args,{'owner': 'hdfs', 'group': 'hadoop', 'mode': 0755, 'encoding' : 'Code'})