TestHeartbeat.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python2.6
  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.Heartbeat import Heartbeat
  19. from ambari_agent.ActionQueue import ActionQueue
  20. from ambari_agent.LiveStatus import LiveStatus
  21. from ambari_agent import AmbariConfig
  22. import socket
  23. import os
  24. import time
  25. from mock.mock import patch, MagicMock, call
  26. from ambari_agent.StackVersionsFileHandler import StackVersionsFileHandler
  27. import StringIO
  28. import sys
  29. class TestHeartbeat(TestCase):
  30. def setUp(self):
  31. # disable stdout
  32. out = StringIO.StringIO()
  33. sys.stdout = out
  34. def tearDown(self):
  35. # enable stdout
  36. sys.stdout = sys.__stdout__
  37. def test_build(self):
  38. actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
  39. heartbeat = Heartbeat(actionQueue)
  40. result = heartbeat.build(100)
  41. print "Heartbeat: " + str(result)
  42. self.assertEquals(result['hostname'] != '', True, "hostname should not be empty")
  43. self.assertEquals(result['responseId'], 100)
  44. self.assertEquals(result['componentStatus'] is not None, True, "Heartbeat should contain componentStatus")
  45. self.assertEquals(result['reports'] is not None, True, "Heartbeat should contain reports")
  46. self.assertEquals(result['timestamp'] >= 1353679373880L, True)
  47. self.assertEquals(len(result['nodeStatus']), 2)
  48. self.assertEquals(result['nodeStatus']['cause'], "NONE")
  49. self.assertEquals(result['nodeStatus']['status'], "HEALTHY")
  50. # result may or may NOT have an agentEnv structure in it
  51. self.assertEquals((len(result) is 6) or (len(result) is 7), True)
  52. self.assertEquals(not heartbeat.reports, True, "Heartbeat should not contain task in progress")
  53. @patch.object(StackVersionsFileHandler, "read_stack_version")
  54. def test_heartbeat_with_status(self, read_stack_version_method):
  55. actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
  56. read_stack_version_method.return_value="1.3.0"
  57. heartbeat = Heartbeat(actionQueue)
  58. statusCommand = {
  59. "serviceName" : 'HDFS',
  60. "commandType" : "STATUS_COMMAND",
  61. "clusterName" : "",
  62. "componentName" : "DATANODE",
  63. 'configurations':{'global' : {}}
  64. }
  65. actionQueue.put(statusCommand)
  66. actionQueue.start()
  67. time.sleep(0.1)
  68. actionQueue.stop()
  69. actionQueue.join()
  70. result = heartbeat.build(101)
  71. self.assertEquals(len(result['componentStatus']) > 0, True, 'Heartbeat should contain status of HDFS components')
  72. @patch.object(StackVersionsFileHandler, "read_stack_version")
  73. def test_heartbeat_with_status_multiple(self, read_stack_version_method):
  74. actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
  75. actionQueue.IDLE_SLEEP_TIME = 0.01
  76. read_stack_version_method.return_value="1.3.0"
  77. heartbeat = Heartbeat(actionQueue)
  78. actionQueue.start()
  79. max_number_of_status_entries = 0
  80. for i in range(1,5):
  81. statusCommand = {
  82. "serviceName" : 'HDFS',
  83. "commandType" : "STATUS_COMMAND",
  84. "clusterName" : "",
  85. "componentName" : "DATANODE",
  86. 'configurations':{'global' : {}}
  87. }
  88. actionQueue.put(statusCommand)
  89. time.sleep(0.1)
  90. result = heartbeat.build(101)
  91. number_of_status_entries = len(result['componentStatus'])
  92. # print "Heartbeat with status: " + str(result) + " XXX " + str(number_of_status_entries)
  93. if max_number_of_status_entries < number_of_status_entries:
  94. max_number_of_status_entries = number_of_status_entries
  95. actionQueue.stop()
  96. actionQueue.join()
  97. NUMBER_OF_COMPONENTS = 1
  98. self.assertEquals(max_number_of_status_entries == NUMBER_OF_COMPONENTS, True)
  99. def test_heartbeat_with_task_in_progress(self):
  100. actionQueue = ActionQueue(AmbariConfig.AmbariConfig().getConfig())
  101. actionQueue.commandInProgress= {
  102. 'role' : "role",
  103. 'actionId' : "actionId",
  104. 'taskId' : "taskId",
  105. 'stdout' : "stdout",
  106. 'clusterName' : "clusterName",
  107. 'stderr' : 'none',
  108. 'exitCode' : 777,
  109. 'serviceName' : "serviceName",
  110. 'status' : 'IN_PROGRESS',
  111. 'configurations':{'global' : {}},
  112. 'roleCommand' : 'START'
  113. }
  114. heartbeat = Heartbeat(actionQueue)
  115. result = heartbeat.build(100)
  116. #print "Heartbeat: " + str(result)
  117. self.assertEquals(len(result['reports']), 1)
  118. self.assertEquals(result['reports'][0]['role'], "role")
  119. self.assertEquals(result['reports'][0]['actionId'], "actionId")
  120. self.assertEquals(result['reports'][0]['taskId'], "taskId")
  121. self.assertEquals(result['reports'][0]['stdout'], "...")
  122. self.assertEquals(result['reports'][0]['clusterName'], "clusterName")
  123. self.assertEquals(result['reports'][0]['stderr'], "...")
  124. self.assertEquals(result['reports'][0]['exitCode'], 777)
  125. self.assertEquals(result['reports'][0]['serviceName'], "serviceName")
  126. self.assertEquals(result['reports'][0]['status'], "IN_PROGRESS")
  127. self.assertEquals(result['reports'][0]['roleCommand'], "START")
  128. pass