TestCheckHost.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 json
  18. import os
  19. import socket
  20. from resource_management import Script,ConfigDictionary
  21. from mock.mock import patch
  22. from mock.mock import MagicMock
  23. from unittest import TestCase
  24. check_host = __import__('check_host')
  25. from check_host import CheckHost
  26. class TestCheckHost(TestCase):
  27. @patch("os.path.isfile")
  28. @patch.object(Script, 'get_config')
  29. @patch("resource_management.libraries.script.Script.put_structured_out")
  30. def testJavaHomeAvailableCheck(self, structured_out_mock, mock_config, os_isfile_mock):
  31. # test, java home exists
  32. os_isfile_mock.return_value = True
  33. mock_config.return_value = {"commandParams" : {"check_execute_list" : "java_home_check",
  34. "java_home" : "test_java_home"}}
  35. checkHost = CheckHost()
  36. checkHost.actionexecute(None)
  37. self.assertEquals(os_isfile_mock.call_args[0][0], 'test_java_home/bin/java')
  38. self.assertEquals(structured_out_mock.call_args[0][0], {'java_home_check': {'message': 'Java home exists!',
  39. 'exit_code': 0}})
  40. # test, java home doesn't exist
  41. os_isfile_mock.reset_mock()
  42. os_isfile_mock.return_value = False
  43. checkHost.actionexecute(None)
  44. self.assertEquals(os_isfile_mock.call_args[0][0], 'test_java_home/bin/java')
  45. self.assertEquals(structured_out_mock.call_args[0][0], {'java_home_check': {"message": "Java home doesn't exist!",
  46. "exit_code" : 1}})
  47. @patch.object(Script, 'get_config')
  48. @patch("check_host.Execute")
  49. @patch("resource_management.libraries.script.Script.put_structured_out")
  50. @patch("subprocess.Popen")
  51. @patch("check_host.format")
  52. @patch("os.path.isfile")
  53. def testDBConnectionCheck(self, isfile_mock, format_mock, popenMock, structured_out_mock, execute_mock, mock_config):
  54. # test, download DBConnectionVerification.jar failed
  55. mock_config.return_value = {"commandParams" : {"check_execute_list" : "db_connection_check",
  56. "java_home" : "test_java_home",
  57. "ambari_server_host" : "test_host",
  58. "jdk_location" : "test_jdk_location",
  59. "db_name" : "mysql",
  60. "db_connection_url" : "test_db_connection_url",
  61. "user_name" : "test_user_name",
  62. "user_passwd" : "test_user_passwd",
  63. "jdk_name" : "test_jdk_name"}}
  64. execute_mock.side_effect = Exception("test exception")
  65. isfile_mock.return_value = True
  66. checkHost = CheckHost()
  67. checkHost.actionexecute(None)
  68. self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check': {'message': 'Error downloading ' \
  69. 'DBConnectionVerification.jar from Ambari Server resources. Check network access to Ambari ' \
  70. 'Server.\ntest exception', 'exit_code': 1}})
  71. self.assertEquals(format_mock.call_args_list[2][0][0], "/bin/sh -c 'cd /usr/lib/ambari-agent/ && curl -kf " \
  72. "--retry 5 {jdk_location}{check_db_connection_jar_name} -o {check_db_connection_jar_name}'")
  73. self.assertEquals(format_mock.call_args_list[3][0][0], "[ -f /usr/lib/ambari-agent/{check_db_connection_jar_name}]")
  74. # test, download jdbc driver failed
  75. mock_config.return_value = {"commandParams" : {"check_execute_list" : "db_connection_check",
  76. "java_home" : "test_java_home",
  77. "ambari_server_host" : "test_host",
  78. "jdk_location" : "test_jdk_location",
  79. "db_name" : "oracle",
  80. "db_connection_url" : "test_db_connection_url",
  81. "user_name" : "test_user_name",
  82. "user_passwd" : "test_user_passwd",
  83. "jdk_name" : "test_jdk_name"}}
  84. format_mock.reset_mock()
  85. execute_mock.reset_mock()
  86. p = MagicMock()
  87. execute_mock.side_effect = [p, Exception("test exception")]
  88. checkHost.actionexecute(None)
  89. self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check': {'message': 'Error downloading JDBC ' \
  90. 'connector from Ambari Server resources. Confirm you ran ambari-server setup to ' \
  91. 'install JDBC connector. Use "ambari-server setup --help" for more information. Check ' \
  92. 'network access to Ambari Server.\ntest exception', 'exit_code': 1}})
  93. self.assertEquals(format_mock.call_args_list[4][0][0], "/bin/sh -c 'cd /usr/lib/ambari-agent/ && curl -kf " \
  94. "--retry 5 {jdbc_url} -o {jdbc_name}'")
  95. self.assertEquals(format_mock.call_args_list[5][0][0], "[ -f /usr/lib/ambari-agent/{jdbc_name}]")
  96. # test, no connection to remote db
  97. mock_config.return_value = {"commandParams" : {"check_execute_list" : "db_connection_check",
  98. "java_home" : "test_java_home",
  99. "ambari_server_host" : "test_host",
  100. "jdk_location" : "test_jdk_location",
  101. "db_name" : "postgresql",
  102. "db_connection_url" : "test_db_connection_url",
  103. "user_name" : "test_user_name",
  104. "user_passwd" : "test_user_passwd",
  105. "jdk_name" : "test_jdk_name"}}
  106. format_mock.reset_mock()
  107. execute_mock.reset_mock()
  108. execute_mock.side_effect = [p, p]
  109. s = MagicMock()
  110. s.communicate.return_value = ("test message", "")
  111. s.returncode = 1
  112. popenMock.return_value = s
  113. checkHost.actionexecute(None)
  114. self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check': {'message': 'test message',
  115. 'exit_code': 1}})
  116. self.assertEquals(format_mock.call_args[0][0],'{java64_home}/bin/java -cp /usr/lib/ambari-agent/{check_db_' \
  117. 'connection_jar_name}:/usr/lib/ambari-agent/{jdbc_name} org.' \
  118. 'apache.ambari.server.DBConnectionVerification {db_connection_url} ' \
  119. '{user_name} {user_passwd!p} {jdbc_driver}')
  120. # test, db connection success
  121. execute_mock.reset_mock()
  122. execute_mock.side_effect = [p, p]
  123. s.returncode = 0
  124. checkHost.actionexecute(None)
  125. self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check':
  126. {'message': 'DB connection check completed successfully!', 'exit_code': 0}})
  127. #test jdk_name and java home are not available
  128. mock_config.return_value = {"commandParams" : {"check_execute_list" : "db_connection_check",
  129. "java_home" : "test_java_home",
  130. "ambari_server_host" : "test_host",
  131. "jdk_location" : "test_jdk_location",
  132. "db_connection_url" : "test_db_connection_url",
  133. "user_name" : "test_user_name",
  134. "user_passwd" : "test_user_passwd",
  135. "db_name" : "postgresql"}}
  136. isfile_mock.return_value = False
  137. checkHost.actionexecute(None)
  138. self.assertEquals(structured_out_mock.call_args[0][0], {'db_connection_check': {'message': 'Custom java is not ' \
  139. 'available on host. Please install it. Java home should be the same as on server. \n', 'exit_code': 1}})
  140. @patch("socket.gethostbyname")
  141. @patch.object(Script, 'get_config')
  142. @patch("resource_management.libraries.script.Script.put_structured_out")
  143. def testHostResolution(self, structured_out_mock, mock_config, mock_socket):
  144. mock_socket.return_value = "192.168.1.1"
  145. jsonFilePath = os.path.join("../resources/custom_actions", "check_host_ip_addresses.json")
  146. with open(jsonFilePath, "r") as jsonFile:
  147. jsonPayload = json.load(jsonFile)
  148. mock_config.return_value = ConfigDictionary(jsonPayload)
  149. checkHost = CheckHost()
  150. checkHost.actionexecute(None)
  151. # ensure the correct function was called
  152. self.assertTrue(structured_out_mock.called)
  153. structured_out_mock.assert_called_with({'host_resolution_check':
  154. {'failures': [],
  155. 'message': 'All hosts resolved to an IP address.',
  156. 'failed_count': 0,
  157. 'success_count': 5,
  158. 'exit_code': 0}})
  159. # try it now with errors
  160. mock_socket.side_effect = socket.error
  161. checkHost.actionexecute(None)
  162. structured_out_mock.assert_called_with({'host_resolution_check':
  163. {'failures': [
  164. {'cause': (), 'host': u'c6401.ambari.apache.org', 'type': 'FORWARD_LOOKUP'},
  165. {'cause': (), 'host': u'c6402.ambari.apache.org', 'type': 'FORWARD_LOOKUP'},
  166. {'cause': (), 'host': u'c6403.ambari.apache.org', 'type': 'FORWARD_LOOKUP'},
  167. {'cause': (), 'host': u'foobar', 'type': 'FORWARD_LOOKUP'},
  168. {'cause': (), 'host': u'!!!', 'type': 'FORWARD_LOOKUP'}],
  169. 'message': 'There were 5 host(s) that could not resolve to an IP address.',
  170. 'failed_count': 5, 'success_count': 0, 'exit_code': 0}})
  171. @patch.object(Script, 'get_config')
  172. @patch("resource_management.libraries.script.Script.put_structured_out")
  173. def testInvalidCheck(self, structured_out_mock, mock_config):
  174. jsonFilePath = os.path.join("../resources/custom_actions", "invalid_check.json")
  175. with open(jsonFilePath, "r") as jsonFile:
  176. jsonPayload = json.load(jsonFile)
  177. mock_config.return_value = ConfigDictionary(jsonPayload)
  178. checkHost = CheckHost()
  179. checkHost.actionexecute(None)
  180. # ensure the correct function was called
  181. self.assertTrue(structured_out_mock.called)
  182. structured_out_mock.assert_called_with({})