TestActualConfigHandler.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. '''
  17. import tempfile
  18. from unittest import TestCase
  19. import os
  20. import logging
  21. from mock.mock import patch
  22. from ambari_agent.LiveStatus import LiveStatus
  23. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  24. from ambari_agent.AmbariConfig import AmbariConfig
  25. from ambari_agent.ActualConfigHandler import ActualConfigHandler
  26. class TestActualConfigHandler(TestCase):
  27. def setUp(self):
  28. LiveStatus.SERVICES = [
  29. "HDFS", "MAPREDUCE", "GANGLIA", "HBASE",
  30. "NAGIOS", "ZOOKEEPER", "OOZIE",
  31. "KERBEROS", "TEMPLETON", "HIVE",
  32. "YARN", "MAPREDUCE2", "FLUME", "TEZ",
  33. "FALCON", "STORM"
  34. ]
  35. LiveStatus.CLIENT_COMPONENTS = [
  36. {"serviceName" : "HBASE",
  37. "componentName" : "HBASE_CLIENT"},
  38. {"serviceName" : "HDFS",
  39. "componentName" : "HDFS_CLIENT"},
  40. {"serviceName" : "MAPREDUCE",
  41. "componentName" : "MAPREDUCE_CLIENT"},
  42. {"serviceName" : "ZOOKEEPER",
  43. "componentName" : "ZOOKEEPER_CLIENT"},
  44. {"serviceName" : "OOZIE",
  45. "componentName" : "OOZIE_CLIENT"},
  46. {"serviceName" : "HCATALOG",
  47. "componentName" : "HCAT"},
  48. {"serviceName" : "HIVE",
  49. "componentName" : "HIVE_CLIENT"},
  50. {"serviceName" : "YARN",
  51. "componentName" : "YARN_CLIENT"},
  52. {"serviceName" : "MAPREDUCE2",
  53. "componentName" : "MAPREDUCE2_CLIENT"},
  54. {"serviceName" : "PIG",
  55. "componentName" : "PIG"},
  56. {"serviceName" : "SQOOP",
  57. "componentName" : "SQOOP"},
  58. {"serviceName" : "TEZ",
  59. "componentName" : "TEZ_CLIENT"},
  60. {"serviceName" : "FALCON",
  61. "componentName" : "FALCON_CLIENT"}
  62. ]
  63. LiveStatus.COMPONENTS = [
  64. {"serviceName" : "HDFS",
  65. "componentName" : "DATANODE"},
  66. {"serviceName" : "HDFS",
  67. "componentName" : "NAMENODE"},
  68. {"serviceName" : "HDFS",
  69. "componentName" : "SECONDARY_NAMENODE"},
  70. {"serviceName" : "HDFS",
  71. "componentName" : "JOURNALNODE"},
  72. {"serviceName" : "HDFS",
  73. "componentName" : "ZKFC"},
  74. {"serviceName" : "MAPREDUCE",
  75. "componentName" : "JOBTRACKER"},
  76. {"serviceName" : "MAPREDUCE",
  77. "componentName" : "TASKTRACKER"},
  78. {"serviceName" : "GANGLIA",
  79. "componentName" : "GANGLIA_SERVER"},
  80. {"serviceName" : "GANGLIA",
  81. "componentName" : "GANGLIA_MONITOR"},
  82. {"serviceName" : "HBASE",
  83. "componentName" : "HBASE_MASTER"},
  84. {"serviceName" : "HBASE",
  85. "componentName" : "HBASE_REGIONSERVER"},
  86. {"serviceName" : "NAGIOS",
  87. "componentName" : "NAGIOS_SERVER"},
  88. {"serviceName" : "FLUME",
  89. "componentName" : "FLUME_SERVER"},
  90. {"serviceName" : "ZOOKEEPER",
  91. "componentName" : "ZOOKEEPER_SERVER"},
  92. {"serviceName" : "OOZIE",
  93. "componentName" : "OOZIE_SERVER"},
  94. {"serviceName" : "HCATALOG",
  95. "componentName" : "HCATALOG_SERVER"},
  96. {"serviceName" : "KERBEROS",
  97. "componentName" : "KERBEROS_SERVER"},
  98. {"serviceName" : "HIVE",
  99. "componentName" : "HIVE_SERVER"},
  100. {"serviceName" : "HIVE",
  101. "componentName" : "HIVE_METASTORE"},
  102. {"serviceName" : "HIVE",
  103. "componentName" : "MYSQL_SERVER"},
  104. {"serviceName" : "HIVE",
  105. "componentName" : "WEBHCAT_SERVER"},
  106. {"serviceName" : "YARN",
  107. "componentName" : "RESOURCEMANAGER"},
  108. {"serviceName" : "YARN",
  109. "componentName" : "NODEMANAGER"},
  110. {"serviceName" : "YARN",
  111. "componentName" : "APP_TIMELINE_SERVER"},
  112. {"serviceName" : "MAPREDUCE2",
  113. "componentName" : "HISTORYSERVER"},
  114. {"serviceName" : "FALCON",
  115. "componentName" : "FALCON_SERVER"},
  116. {"serviceName" : "STORM",
  117. "componentName" : "NIMBUS"},
  118. {"serviceName" : "STORM",
  119. "componentName" : "STORM_REST_API"},
  120. {"serviceName" : "STORM",
  121. "componentName" : "SUPERVISOR"},
  122. {"serviceName" : "STORM",
  123. "componentName" : "STORM_UI_SERVER"},
  124. {"serviceName" : "STORM",
  125. "componentName" : "DRPC_SERVER"}
  126. ]
  127. logger = logging.getLogger()
  128. def test_read_write(self):
  129. config = AmbariConfig().getConfig()
  130. tmpdir = tempfile.gettempdir()
  131. config.set('agent', 'prefix', tmpdir)
  132. tags = { "global": "version1", "core-site": "version2" }
  133. handler = ActualConfigHandler(config, tags)
  134. handler.write_actual(tags)
  135. output = handler.read_actual()
  136. self.assertEquals(tags, output)
  137. os.remove(os.path.join(tmpdir, ActualConfigHandler.CONFIG_NAME))
  138. def test_read_empty(self):
  139. config = AmbariConfig().getConfig()
  140. tmpdir = tempfile.gettempdir()
  141. config.set('agent', 'prefix', tmpdir)
  142. handler = ActualConfigHandler(config, {})
  143. conf_file = open(os.path.join(tmpdir, ActualConfigHandler.CONFIG_NAME), 'w')
  144. conf_file.write("")
  145. conf_file.close()
  146. output = handler.read_actual()
  147. self.assertEquals(None, output)
  148. os.remove(os.path.join(tmpdir, ActualConfigHandler.CONFIG_NAME))
  149. def test_read_write_component(self):
  150. config = AmbariConfig().getConfig()
  151. tmpdir = tempfile.gettempdir()
  152. config.set('agent', 'prefix', tmpdir)
  153. tags1 = { "global": "version1", "core-site": "version2" }
  154. handler = ActualConfigHandler(config, {})
  155. handler.write_actual(tags1)
  156. handler.write_actual_component('FOO', tags1)
  157. output1 = handler.read_actual_component('FOO')
  158. output2 = handler.read_actual_component('GOO')
  159. self.assertEquals(tags1, output1)
  160. self.assertEquals(None, output2)
  161. tags2 = { "global": "version1", "core-site": "version2" }
  162. handler.write_actual(tags2)
  163. output3 = handler.read_actual()
  164. output4 = handler.read_actual_component('FOO')
  165. self.assertEquals(tags2, output3)
  166. self.assertEquals(tags1, output4)
  167. os.remove(os.path.join(tmpdir, "FOO_" + ActualConfigHandler.CONFIG_NAME))
  168. os.remove(os.path.join(tmpdir, ActualConfigHandler.CONFIG_NAME))
  169. def test_write_actual_component_and_client_components(self):
  170. config = AmbariConfig().getConfig()
  171. tmpdir = tempfile.gettempdir()
  172. config.set('agent', 'prefix', tmpdir)
  173. tags1 = { "global": "version1", "core-site": "version2" }
  174. tags2 = { "global": "version33", "core-site": "version33" }
  175. handler = ActualConfigHandler(config, {})
  176. handler.write_actual_component('HDFS_CLIENT', tags1)
  177. handler.write_actual_component('HBASE_CLIENT', tags1)
  178. self.assertEquals(tags1, handler.read_actual_component('HDFS_CLIENT'))
  179. self.assertEquals(tags1, handler.read_actual_component('HBASE_CLIENT'))
  180. handler.write_actual_component('DATANODE', tags2)
  181. self.assertEquals(tags2, handler.read_actual_component('DATANODE'))
  182. self.assertEquals(tags1, handler.read_actual_component('HDFS_CLIENT'))
  183. handler.write_client_components('HDFS', tags2)
  184. self.assertEquals(tags2, handler.read_actual_component('HDFS_CLIENT'))
  185. self.assertEquals(tags1, handler.read_actual_component('HBASE_CLIENT'))
  186. os.remove(os.path.join(tmpdir, "DATANODE_" + ActualConfigHandler.CONFIG_NAME))
  187. os.remove(os.path.join(tmpdir, "HBASE_CLIENT_" + ActualConfigHandler.CONFIG_NAME))
  188. os.remove(os.path.join(tmpdir, "HDFS_CLIENT_" + ActualConfigHandler.CONFIG_NAME))
  189. @patch.object(ActualConfigHandler, "write_file")
  190. def test_write_client_components(self, write_file_mock):
  191. config = AmbariConfig().getConfig()
  192. tmpdir = tempfile.gettempdir()
  193. config.set('agent', 'prefix', tmpdir)
  194. tags0 = {"global": "version0", "core-site": "version0"}
  195. tags1 = {"global": "version1", "core-site": "version2"}
  196. tags2 = {"global": "version33", "core-site": "version33"}
  197. configTags = {'HDFS_CLIENT': tags0, 'HBASE_CLIENT': tags1}
  198. handler = ActualConfigHandler(config, configTags)
  199. self.assertEquals(tags0, handler.read_actual_component('HDFS_CLIENT'))
  200. self.assertEquals(tags1, handler.read_actual_component('HBASE_CLIENT'))
  201. handler.write_client_components('HDFS', tags2)
  202. self.assertEquals(tags2, handler.read_actual_component('HDFS_CLIENT'))
  203. self.assertEquals(tags1, handler.read_actual_component('HBASE_CLIENT'))
  204. self.assertTrue(write_file_mock.called)
  205. self.assertEqual(1, write_file_mock.call_count)
  206. @patch.object(ActualConfigHandler, "write_file")
  207. @patch.object(ActualConfigHandler, "read_file")
  208. def test_read_actual_component_inmemory(self, read_file_mock, write_file_mock):
  209. config = AmbariConfig().getConfig()
  210. tmpdir = tempfile.gettempdir()
  211. config.set('agent', 'prefix', tmpdir)
  212. tags1 = { "global": "version1", "core-site": "version2" }
  213. read_file_mock.return_value = tags1
  214. handler = ActualConfigHandler(config, {})
  215. handler.write_actual_component('NAMENODE', tags1)
  216. self.assertTrue(write_file_mock.called)
  217. self.assertEquals(tags1, handler.read_actual_component('NAMENODE'))
  218. self.assertFalse(read_file_mock.called)
  219. self.assertEquals(tags1, handler.read_actual_component('DATANODE'))
  220. self.assertTrue(read_file_mock.called)
  221. self.assertEquals(1, read_file_mock.call_count)
  222. self.assertEquals(tags1, handler.read_actual_component('DATANODE'))
  223. self.assertEquals(1, read_file_mock.call_count)