TestLiveStatus.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. from unittest import TestCase
  18. from ambari_agent.LiveStatus import LiveStatus
  19. from ambari_agent.AmbariConfig import AmbariConfig
  20. import os, sys, StringIO
  21. from ambari_agent import ActualConfigHandler
  22. from mock.mock import patch
  23. import pprint
  24. from ambari_agent import StatusCheck
  25. class TestLiveStatus(TestCase):
  26. def setUp(self):
  27. # disable stdout
  28. out = StringIO.StringIO()
  29. sys.stdout = out
  30. LiveStatus.SERVICES = [
  31. "HDFS", "MAPREDUCE", "GANGLIA", "HBASE",
  32. "NAGIOS", "ZOOKEEPER", "OOZIE",
  33. "KERBEROS", "TEMPLETON", "HIVE",
  34. "YARN", "MAPREDUCE2", "FLUME", "TEZ",
  35. "FALCON", "STORM"
  36. ]
  37. LiveStatus.CLIENT_COMPONENTS = [
  38. {"serviceName" : "HBASE",
  39. "componentName" : "HBASE_CLIENT"},
  40. {"serviceName" : "HDFS",
  41. "componentName" : "HDFS_CLIENT"},
  42. {"serviceName" : "MAPREDUCE",
  43. "componentName" : "MAPREDUCE_CLIENT"},
  44. {"serviceName" : "ZOOKEEPER",
  45. "componentName" : "ZOOKEEPER_CLIENT"},
  46. {"serviceName" : "OOZIE",
  47. "componentName" : "OOZIE_CLIENT"},
  48. {"serviceName" : "HCATALOG",
  49. "componentName" : "HCAT"},
  50. {"serviceName" : "HIVE",
  51. "componentName" : "HIVE_CLIENT"},
  52. {"serviceName" : "YARN",
  53. "componentName" : "YARN_CLIENT"},
  54. {"serviceName" : "MAPREDUCE2",
  55. "componentName" : "MAPREDUCE2_CLIENT"},
  56. {"serviceName" : "PIG",
  57. "componentName" : "PIG"},
  58. {"serviceName" : "SQOOP",
  59. "componentName" : "SQOOP"},
  60. {"serviceName" : "TEZ",
  61. "componentName" : "TEZ_CLIENT"},
  62. {"serviceName" : "FALCON",
  63. "componentName" : "FALCON_CLIENT"}
  64. ]
  65. LiveStatus.COMPONENTS = [
  66. {"serviceName" : "HDFS",
  67. "componentName" : "DATANODE"},
  68. {"serviceName" : "HDFS",
  69. "componentName" : "NAMENODE"},
  70. {"serviceName" : "HDFS",
  71. "componentName" : "SECONDARY_NAMENODE"},
  72. {"serviceName" : "HDFS",
  73. "componentName" : "JOURNALNODE"},
  74. {"serviceName" : "HDFS",
  75. "componentName" : "ZKFC"},
  76. {"serviceName" : "MAPREDUCE",
  77. "componentName" : "JOBTRACKER"},
  78. {"serviceName" : "MAPREDUCE",
  79. "componentName" : "TASKTRACKER"},
  80. {"serviceName" : "GANGLIA",
  81. "componentName" : "GANGLIA_SERVER"},
  82. {"serviceName" : "GANGLIA",
  83. "componentName" : "GANGLIA_MONITOR"},
  84. {"serviceName" : "HBASE",
  85. "componentName" : "HBASE_MASTER"},
  86. {"serviceName" : "HBASE",
  87. "componentName" : "HBASE_REGIONSERVER"},
  88. {"serviceName" : "NAGIOS",
  89. "componentName" : "NAGIOS_SERVER"},
  90. {"serviceName" : "FLUME",
  91. "componentName" : "FLUME_SERVER"},
  92. {"serviceName" : "ZOOKEEPER",
  93. "componentName" : "ZOOKEEPER_SERVER"},
  94. {"serviceName" : "OOZIE",
  95. "componentName" : "OOZIE_SERVER"},
  96. {"serviceName" : "HCATALOG",
  97. "componentName" : "HCATALOG_SERVER"},
  98. {"serviceName" : "KERBEROS",
  99. "componentName" : "KERBEROS_SERVER"},
  100. {"serviceName" : "HIVE",
  101. "componentName" : "HIVE_SERVER"},
  102. {"serviceName" : "HIVE",
  103. "componentName" : "HIVE_METASTORE"},
  104. {"serviceName" : "HIVE",
  105. "componentName" : "MYSQL_SERVER"},
  106. {"serviceName" : "WEBHCAT",
  107. "componentName" : "WEBHCAT_SERVER"},
  108. {"serviceName" : "YARN",
  109. "componentName" : "RESOURCEMANAGER"},
  110. {"serviceName" : "YARN",
  111. "componentName" : "NODEMANAGER"},
  112. {"serviceName" : "YARN",
  113. "componentName" : "APP_TIMELINE_SERVER"},
  114. {"serviceName" : "MAPREDUCE2",
  115. "componentName" : "HISTORYSERVER"},
  116. {"serviceName" : "FALCON",
  117. "componentName" : "FALCON_SERVER"},
  118. {"serviceName" : "STORM",
  119. "componentName" : "NIMBUS"},
  120. {"serviceName" : "STORM",
  121. "componentName" : "STORM_REST_API"},
  122. {"serviceName" : "STORM",
  123. "componentName" : "SUPERVISOR"},
  124. {"serviceName" : "STORM",
  125. "componentName" : "STORM_UI_SERVER"},
  126. {"serviceName" : "STORM",
  127. "componentName" : "DRPC_SERVER"}
  128. ]
  129. def tearDown(self):
  130. # enable stdout
  131. sys.stdout = sys.__stdout__
  132. @patch.object(ActualConfigHandler.ActualConfigHandler, "read_actual_component")
  133. def test_build(self, read_actual_component_mock):
  134. for component in LiveStatus.COMPONENTS:
  135. config = AmbariConfig().getConfig()
  136. config.set('agent', 'prefix', "ambari_agent" + os.sep + "dummy_files")
  137. livestatus = LiveStatus('', component['serviceName'], component['componentName'], {}, config, {})
  138. livestatus.versionsHandler.versionsFilePath = "ambari_agent" + os.sep + "dummy_files" + os.sep + "dummy_current_stack"
  139. result = livestatus.build()
  140. print "LiveStatus of {0}: {1}".format(component['serviceName'], str(result))
  141. self.assertEquals(len(result) > 0, True, 'Livestatus should not be empty')
  142. if component['componentName'] == 'GANGLIA_SERVER':
  143. self.assertEquals(result['stackVersion'],'{"stackName":"HDP","stackVersion":"1.2.2"}',
  144. 'Livestatus should contain component stack version')
  145. # Test build status for CLIENT component (in LiveStatus.CLIENT_COMPONENTS)
  146. read_actual_component_mock.return_value = "some tags"
  147. livestatus = LiveStatus('c1', 'HDFS', 'HDFS_CLIENT', { }, config, {})
  148. result = livestatus.build()
  149. self.assertTrue(len(result) > 0, 'Livestatus should not be empty')
  150. self.assertTrue(result.has_key('configurationTags'))
  151. # Test build status with forsed_component_status
  152. ## Alive
  153. livestatus = LiveStatus('c1', 'HDFS', 'HDFS_CLIENT', { }, config, {})
  154. result = livestatus.build(forsed_component_status = LiveStatus.LIVE_STATUS)
  155. self.assertTrue(len(result) > 0, 'Livestatus should not be empty')
  156. self.assertTrue(result['status'], LiveStatus.LIVE_STATUS)
  157. ## Dead
  158. livestatus = LiveStatus('c1', 'HDFS', 'HDFS_CLIENT', { }, config, {})
  159. result = livestatus.build(forsed_component_status = LiveStatus.DEAD_STATUS)
  160. self.assertTrue(len(result) > 0, 'Livestatus should not be empty')
  161. self.assertTrue(result['status'], LiveStatus.DEAD_STATUS)
  162. livestatus = LiveStatus('c1', 'TEZ', 'TEZ_CLIENT', { }, config, {})
  163. result = livestatus.build(forsed_component_status = LiveStatus.LIVE_STATUS)
  164. self.assertTrue(len(result) > 0, 'Livestatus should not be empty')
  165. self.assertTrue(result['status'], LiveStatus.LIVE_STATUS)
  166. @patch.object(ActualConfigHandler.ActualConfigHandler, "read_actual_component")
  167. @patch.object(StatusCheck.StatusCheck, "getStatus")
  168. def test_build_predefined(self, getStatus_mock, read_actual_component_mock):
  169. read_actual_component_mock.return_value = "actual_component"
  170. """
  171. Tests that if live status us defined (using default parameter),
  172. then no StatusCheck is executed
  173. """
  174. config = AmbariConfig().getConfig()
  175. config.set('agent', 'prefix', "ambari_agent" + os.sep + "dummy_files")
  176. livestatus = LiveStatus('', 'SOME_UNKNOWN_SERVICE',
  177. 'SOME_UNKNOWN_COMPONENT', {}, config, {})
  178. livestatus.versionsHandler.versionsFilePath = "ambari_agent" + \
  179. os.sep + "dummy_files" + os.sep + "dummy_current_stack"
  180. result = livestatus.build(forsed_component_status = "STARTED")
  181. result_str = pprint.pformat(result)
  182. self.assertEqual(result_str,
  183. "{'clusterName': '',\n "
  184. "'componentName': 'SOME_UNKNOWN_COMPONENT',\n "
  185. "'configurationTags': 'actual_component',\n "
  186. "'msg': '',\n 'serviceName': 'SOME_UNKNOWN_SERVICE',\n "
  187. "'stackVersion': '',\n 'status': 'STARTED'}")
  188. self.assertFalse(getStatus_mock.called)