TestAmbariClient.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 mock.mock import MagicMock, patch
  18. from ambari_client.ambari_api import AmbariClient
  19. from ambari_client.core.errors import BadRequest
  20. import unittest
  21. class TestAmbariClient(unittest.TestCase):
  22. def test_init(self):
  23. """
  24. AmbariClient is the top-level root resources.
  25. This testcase checks if when the init method was called &
  26. the httpclient was initialized
  27. """
  28. client = AmbariClient("localhost", 8080, "admin", "admin", version=1)
  29. self.assertEqual(client.version, 1, "version should be 1")
  30. self.assertEqual(client.host_url, "http://localhost:8080/api/v1",
  31. "host_url should be http://localhost:8080/api/v1")
  32. client = AmbariClient(host_name="localhost", user_name="admin", password="admin")
  33. self.assertEqual(client.version, 1, "version should be 1")
  34. self.assertEqual(client.host_url, "http://localhost:8080/api/v1",
  35. "host_url should be http://localhost:8080/api/v1")
  36. client = AmbariClient(host_name="localhost")
  37. self.assertEqual(client.version, 1, "version should be 1")
  38. self.assertEqual(client.host_url, "http://localhost:8080/api/v1",
  39. "host_url should be http://localhost:8080/api/v1")
  40. @patch("ambari_client.core.http_client.HttpClient")
  41. def test_get_all_clusters_valid(self , http_client):
  42. """
  43. Get all clusters.
  44. This testcase checks if get_all_clusters returns a list of ModelList.
  45. """
  46. http_client_mock = MagicMock()
  47. http_client.return_value = http_client_mock
  48. mocked_code = "200"
  49. mocked_content = "text/plain"
  50. expected_output = {'items': [{'cluster_name': u'test1', 'version': u'HDP-1.2.1'}]}
  51. linestring = open('json/get_all_clusters.json', 'r').read()
  52. mocked_response = linestring
  53. http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content
  54. client = AmbariClient("localhost", 8080, "admin", "admin", version=1 , client=http_client_mock)
  55. all_clusters = client.get_all_clusters()
  56. self.assertEqual(len(all_clusters), 1, "There should be a cluster from the response")
  57. self.assertEqual(all_clusters.to_json_dict(), expected_output, "to_json_dict should convert ModelList")
  58. @patch("ambari_client.core.http_client.HttpClient")
  59. def test_get_hosts_clusters_valid(self , http_client):
  60. """
  61. Get all hosts.
  62. This testcase checks if get_all_hosts returns a list of ModelList.
  63. """
  64. http_client_mock = MagicMock()
  65. http_client.return_value = http_client_mock
  66. mocked_code = "200"
  67. mocked_content = "text/plain"
  68. linestring = open('json/get_all_hosts.json', 'r').read()
  69. mocked_response = linestring
  70. http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content
  71. client = AmbariClient("localhost", 8080, "admin", "admin", version=1 , client=http_client_mock)
  72. all_hosts = client.get_all_hosts()
  73. self.assertEqual(len(all_hosts), 12, "There should be 12 hosts from the response")
  74. @patch("ambari_client.core.http_client.HttpClient")
  75. def test_get_host_valid(self , http_client):
  76. """
  77. Get host
  78. This testcase checks if client.get_host returns a correct host
  79. """
  80. http_client_mock = MagicMock()
  81. http_client.returned_obj = http_client_mock
  82. mocked_code = "200"
  83. mocked_content = "text/plain"
  84. linestring = open('json/get_host.json', 'r').read()
  85. mocked_response = linestring
  86. expected_dict_output = {'ip': '10.0.2.15', 'host_name': 'dev06.hortonworks.com', 'rack_info': '/default-rack'}
  87. http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content
  88. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  89. host = client.get_host('dev06.hortonworks.com')
  90. self.assertEqual(host.to_json_dict(), expected_dict_output)
  91. @patch("ambari_client.core.http_client.HttpClient")
  92. def test_get_cluster_valid(self , http_client):
  93. """
  94. Get all clusters.
  95. This testcase checks if get_all_clusters returns a list of ModelList.
  96. """
  97. http_client_mock = MagicMock()
  98. http_client.returned_obj = http_client_mock
  99. mocked_code = "200"
  100. mocked_content = "text/plain"
  101. linestring = open('json/get_cluster.json', 'r').read()
  102. mocked_response = linestring
  103. expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'}
  104. http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content
  105. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  106. cluster = client.get_cluster('test1')
  107. self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ")
  108. self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel")
  109. @patch("ambari_client.core.http_client.HttpClient")
  110. def test_get_cluster_services_valid(self , http_client):
  111. """
  112. Get all services of a cluster.
  113. This testcase checks if get_all_services returns a list of ModelList.
  114. """
  115. http_client_mock = MagicMock()
  116. http_client.returned_obj = http_client_mock
  117. mocked_code = "200"
  118. mocked_content = "text/plain"
  119. expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'}
  120. http_client_mock.invoke.side_effect = http_client_invoke_side_effects
  121. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  122. cluster = client.get_cluster('test1')
  123. serviceList = cluster.get_all_services()
  124. self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ")
  125. self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel")
  126. self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response")
  127. @patch("ambari_client.core.http_client.HttpClient")
  128. def test_get_cluster_service_valid(self , http_client):
  129. """
  130. Get the service of a cluster
  131. This testcase checks if get_service returns a list of ServiceModel.
  132. """
  133. http_client_mock = MagicMock()
  134. http_client.returned_obj = http_client_mock
  135. mocked_code = "200"
  136. mocked_content = "text/plain"
  137. expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'}
  138. http_client_mock.invoke.side_effect = http_client_invoke_side_effects
  139. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  140. cluster = client.get_cluster('test1')
  141. serviceList = cluster.get_all_services()
  142. ganglia = cluster.get_service("GANGLIA")
  143. self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ")
  144. self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel")
  145. self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response")
  146. self.assertEqual(str(ganglia.state), "STARTED", "The ganglia service state should be fetched as STARTED")
  147. self.assertEqual(ganglia.clusterRef.cluster_name, cluster.cluster_name, "The clusterRef value for service should be fetched ")
  148. @patch("ambari_client.core.http_client.HttpClient")
  149. def test_exceptions(self , http_client):
  150. """
  151. Test exceptions from ambari.client.core.errors
  152. """
  153. http_client_mock = MagicMock()
  154. http_client.returned_obj = http_client_mock
  155. mocked_code = "200"
  156. mocked_content = "text/plain"
  157. http_client_mock.invoke.side_effect = http_client_invoke_side_effects
  158. client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock)
  159. cluster = client.get_cluster('test1')
  160. try:
  161. cluster.delete_host('deleted_nonexistant_cluster')
  162. self.fail('Exception should have been thrown!')
  163. except BadRequest, ex:
  164. self.assertEquals(str(ex), 'exception: 400. Attempted to add unknown hosts to a cluster. These hosts have not been registered with the server: dev05')
  165. except Exception, ex:
  166. self.fail('Wrong exception thrown!')
  167. def http_client_invoke_side_effects(*args, **kwargs):
  168. print locals()
  169. mocked_code = "200"
  170. mocked_content = "text/plain"
  171. if args[1] == "//clusters/test1":
  172. mocked_response = open('json/get_cluster.json', 'r').read()
  173. return mocked_response, mocked_code , mocked_content
  174. elif args[1] == "//hosts":
  175. mocked_response = open('json/get_all_hosts.json', 'r').read()
  176. return mocked_response, mocked_code , mocked_content
  177. elif args[1] == "//clusters/test6/hosts/r01wn01":
  178. mocked_response = open('json/get_cluster_host.json', 'r').read()
  179. return mocked_response, mocked_code , mocked_content
  180. elif args[1] == "//clusters/test6/hosts?fields=*":
  181. mocked_response = open('json/get_cluster_hosts.json', 'r').read()
  182. return mocked_response, mocked_code , mocked_content
  183. elif args[1] == "//clusters/test6/services/GANGLIA":
  184. mocked_response = open('json/get_cluster_service.json', 'r').read()
  185. return mocked_response, mocked_code , mocked_content
  186. elif args[1] == "//clusters/test1/services?fields=*":
  187. mocked_response = open('json/get_cluster_services.json', 'r').read()
  188. return mocked_response, mocked_code , mocked_content
  189. elif args[1] == "//clusters/test6/hosts/r01wn01/host_components/NAMENODE":
  190. mocked_response = open('json/get_host_component.json', 'r').read()
  191. return mocked_response, mocked_code , mocked_content
  192. elif args[1] == "//clusters/test6/hosts/r01wn01/host_components?ServiceComponentInfo":
  193. mocked_response = open('json/get_host_components.json', 'r').read()
  194. return mocked_response, mocked_code , mocked_content
  195. elif args[1] == "//clusters/test6/services/GANGLIA/components/GANGLIA_MONITOR":
  196. mocked_response = open('json/get_service_component.json', 'r').read()
  197. return mocked_response, mocked_code , mocked_content
  198. elif args[1] == "//clusters/test6/services/GANGLIA/components?fields=*":
  199. mocked_response = open('json/get_service_components.json', 'r').read()
  200. return mocked_response, mocked_code , mocked_content
  201. elif args[1] == "//clusters/test1/services/GANGLIA":
  202. mocked_response = open('json/get_service.json', 'r').read()
  203. return mocked_response, mocked_code , mocked_content
  204. elif args[1] == "//clusters/test1/hosts/deleted_nonexistant_cluster":
  205. mocked_response = open('json/error_adding_host.json', 'r').read()
  206. return mocked_response, mocked_code , mocked_content