TestHeartbeat.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. import unittest
  19. import socket
  20. import os
  21. import time
  22. from mock.mock import patch, MagicMock, call
  23. import StringIO
  24. import sys
  25. from ambari_agent.RecoveryManager import RecoveryManager
  26. with patch("platform.linux_distribution", return_value = ('Suse','11','Final')):
  27. from ambari_agent.Hardware import Hardware
  28. from ambari_agent.Heartbeat import Heartbeat
  29. from ambari_agent.ActionQueue import ActionQueue
  30. from ambari_agent.LiveStatus import LiveStatus
  31. from ambari_agent import AmbariConfig
  32. from ambari_agent.StackVersionsFileHandler import StackVersionsFileHandler
  33. from ambari_agent.HostInfo import HostInfoLinux
  34. from only_for_platform import not_for_platform, PLATFORM_WINDOWS
  35. @not_for_platform(PLATFORM_WINDOWS)
  36. class TestHeartbeat(TestCase):
  37. def setUp(self):
  38. # disable stdout
  39. out = StringIO.StringIO()
  40. sys.stdout = out
  41. def tearDown(self):
  42. # enable stdout
  43. sys.stdout = sys.__stdout__
  44. def test_build(self):
  45. config = AmbariConfig.AmbariConfig().getConfig()
  46. config.set('agent', 'prefix', 'tmp')
  47. config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  48. config.set('agent', 'tolerate_download_failures', "true")
  49. dummy_controller = MagicMock()
  50. actionQueue = ActionQueue(config, dummy_controller)
  51. heartbeat = Heartbeat(actionQueue)
  52. result = heartbeat.build(100)
  53. print "Heartbeat: " + str(result)
  54. self.assertEquals(result['hostname'] != '', True, "hostname should not be empty")
  55. self.assertEquals(result['responseId'], 100)
  56. self.assertEquals(result['componentStatus'] is not None, True, "Heartbeat should contain componentStatus")
  57. self.assertEquals(result['reports'] is not None, True, "Heartbeat should contain reports")
  58. self.assertEquals(result['timestamp'] >= 1353679373880L, True)
  59. self.assertEquals(len(result['nodeStatus']), 2)
  60. self.assertEquals(result['nodeStatus']['cause'], "NONE")
  61. self.assertEquals(result['nodeStatus']['status'], "HEALTHY")
  62. # result may or may NOT have an agentEnv structure in it
  63. self.assertEquals((len(result) is 6) or (len(result) is 7), True)
  64. self.assertEquals(not heartbeat.reports, True, "Heartbeat should not contain task in progress")
  65. @patch.object(Hardware, "_chk_mount", new = MagicMock(return_value=True))
  66. @patch.object(ActionQueue, "result")
  67. @patch.object(HostInfoLinux, "register")
  68. def test_no_mapping(self, register_mock, result_mock):
  69. result_mock.return_value = {
  70. 'reports': [{'status': 'IN_PROGRESS',
  71. 'stderr': 'Read from /tmp/errors-3.txt',
  72. 'stdout': 'Read from /tmp/output-3.txt',
  73. 'clusterName': u'cc',
  74. 'roleCommand': u'INSTALL',
  75. 'serviceName': u'HDFS',
  76. 'role': u'DATANODE',
  77. 'actionId': '1-1',
  78. 'taskId': 3,
  79. 'exitCode': 777}],
  80. 'componentStatus': [{'status': 'HEALTHY', 'componentName': 'NAMENODE'}]
  81. }
  82. config = AmbariConfig.AmbariConfig().getConfig()
  83. config.set('agent', 'prefix', 'tmp')
  84. config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  85. config.set('agent', 'tolerate_download_failures', "true")
  86. dummy_controller = MagicMock()
  87. actionQueue = ActionQueue(config, dummy_controller)
  88. heartbeat = Heartbeat(actionQueue)
  89. hb = heartbeat.build(id = 10, state_interval=1, componentsMapped=True)
  90. self.assertEqual(register_mock.call_args_list[0][0][1], True)
  91. register_mock.reset_mock()
  92. hb = heartbeat.build(id = 0, state_interval=1, componentsMapped=True)
  93. self.assertEqual(register_mock.call_args_list[0][0][1], False)
  94. @patch.object(ActionQueue, "result")
  95. def test_build_long_result(self, result_mock):
  96. config = AmbariConfig.AmbariConfig().getConfig()
  97. config.set('agent', 'prefix', 'tmp')
  98. config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  99. config.set('agent', 'tolerate_download_failures', "true")
  100. dummy_controller = MagicMock()
  101. dummy_controller.recovery_manager = RecoveryManager()
  102. actionQueue = ActionQueue(config, dummy_controller)
  103. result_mock.return_value = {
  104. 'reports': [{'status': 'IN_PROGRESS',
  105. 'stderr': 'Read from /tmp/errors-3.txt',
  106. 'stdout': 'Read from /tmp/output-3.txt',
  107. 'clusterName': u'cc',
  108. 'roleCommand': u'INSTALL',
  109. 'serviceName': u'HDFS',
  110. 'role': u'DATANODE',
  111. 'actionId': '1-1',
  112. 'taskId': 3,
  113. 'exitCode': 777},
  114. {'status': 'COMPLETED',
  115. 'stderr': 'stderr',
  116. 'stdout': 'out',
  117. 'clusterName': 'clusterName',
  118. 'roleCommand': 'UPGRADE',
  119. 'serviceName': 'serviceName',
  120. 'role': 'role',
  121. 'actionId': 17,
  122. 'taskId': 'taskId',
  123. 'exitCode': 0},
  124. {'status': 'FAILED',
  125. 'stderr': 'stderr',
  126. 'stdout': 'out',
  127. 'clusterName': u'cc',
  128. 'roleCommand': u'INSTALL',
  129. 'serviceName': u'HDFS',
  130. 'role': u'DATANODE',
  131. 'actionId': '1-1',
  132. 'taskId': 3,
  133. 'exitCode': 13},
  134. {'status': 'COMPLETED',
  135. 'stderr': 'stderr',
  136. 'stdout': 'out',
  137. 'clusterName': u'cc',
  138. 'configurationTags': {'global': {'tag': 'v1'}},
  139. 'roleCommand': u'INSTALL',
  140. 'serviceName': u'HDFS',
  141. 'role': u'DATANODE',
  142. 'actionId': '1-1',
  143. 'taskId': 3,
  144. 'exitCode': 0}
  145. ],
  146. 'componentStatus': [
  147. {'status': 'HEALTHY', 'componentName': 'DATANODE'},
  148. {'status': 'UNHEALTHY', 'componentName': 'NAMENODE'},
  149. ],
  150. }
  151. heartbeat = Heartbeat(actionQueue)
  152. hb = heartbeat.build(10)
  153. hb['hostname'] = 'hostname'
  154. hb['timestamp'] = 'timestamp'
  155. expected = {'nodeStatus':
  156. {'status': 'HEALTHY',
  157. 'cause': 'NONE'},
  158. 'recoveryReport': {'summary': 'DISABLED'},
  159. 'timestamp': 'timestamp', 'hostname': 'hostname',
  160. 'responseId': 10, 'reports': [
  161. {'status': 'IN_PROGRESS', 'roleCommand': u'INSTALL',
  162. 'serviceName': u'HDFS', 'role': u'DATANODE', 'actionId': '1-1',
  163. 'stderr': 'Read from /tmp/errors-3.txt',
  164. 'stdout': 'Read from /tmp/output-3.txt', 'clusterName': u'cc',
  165. 'taskId': 3, 'exitCode': 777},
  166. {'status': 'COMPLETED', 'roleCommand': 'UPGRADE',
  167. 'serviceName': 'serviceName', 'role': 'role', 'actionId': 17,
  168. 'stderr': 'stderr', 'stdout': 'out', 'clusterName': 'clusterName',
  169. 'taskId': 'taskId', 'exitCode': 0},
  170. {'status': 'FAILED', 'roleCommand': u'INSTALL', 'serviceName': u'HDFS',
  171. 'role': u'DATANODE', 'actionId': '1-1', 'stderr': 'stderr',
  172. 'stdout': 'out', 'clusterName': u'cc', 'taskId': 3, 'exitCode': 13},
  173. {'status': 'COMPLETED', 'stdout': 'out',
  174. 'configurationTags': {'global': {'tag': 'v1'}}, 'taskId': 3,
  175. 'exitCode': 0, 'roleCommand': u'INSTALL', 'clusterName': u'cc',
  176. 'serviceName': u'HDFS', 'role': u'DATANODE', 'actionId': '1-1',
  177. 'stderr': 'stderr'}], 'componentStatus': [
  178. {'status': 'HEALTHY', 'componentName': 'DATANODE'},
  179. {'status': 'UNHEALTHY', 'componentName': 'NAMENODE'}]}
  180. self.assertEqual.__self__.maxDiff = None
  181. self.assertEquals(hb, expected)
  182. @patch.object(Hardware, "_chk_mount", new = MagicMock(return_value=True))
  183. @patch.object(HostInfoLinux, 'register')
  184. def test_heartbeat_no_host_check_cmd_in_queue(self, register_mock):
  185. config = AmbariConfig.AmbariConfig().getConfig()
  186. config.set('agent', 'prefix', 'tmp')
  187. config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  188. config.set('agent', 'tolerate_download_failures', "true")
  189. dummy_controller = MagicMock()
  190. actionQueue = ActionQueue(config, dummy_controller)
  191. statusCommand = {
  192. "serviceName" : 'HDFS',
  193. "commandType" : "STATUS_COMMAND",
  194. "clusterName" : "c1",
  195. "componentName" : "DATANODE",
  196. 'configurations':{'global' : {}}
  197. }
  198. actionQueue.put_status([statusCommand])
  199. heartbeat = Heartbeat(actionQueue)
  200. heartbeat.build(12, 6)
  201. self.assertTrue(register_mock.called)
  202. args, kwargs = register_mock.call_args_list[0]
  203. self.assertFalse(args[2])
  204. self.assertFalse(args[1])
  205. @patch.object(Hardware, "_chk_mount", new = MagicMock(return_value=True))
  206. @patch.object(HostInfoLinux, 'register')
  207. def test_heartbeat_host_check_no_cmd(self, register_mock):
  208. config = AmbariConfig.AmbariConfig().getConfig()
  209. config.set('agent', 'prefix', 'tmp')
  210. config.set('agent', 'cache_dir', "/var/lib/ambari-agent/cache")
  211. config.set('agent', 'tolerate_download_failures', "true")
  212. dummy_controller = MagicMock()
  213. actionQueue = ActionQueue(config, dummy_controller)
  214. heartbeat = Heartbeat(actionQueue)
  215. heartbeat.build(12, 6)
  216. self.assertTrue(register_mock.called)
  217. args, kwargs = register_mock.call_args_list[0]
  218. self.assertFalse(args[1])
  219. self.assertFalse(args[2])
  220. if __name__ == "__main__":
  221. unittest.main(verbosity=2)