http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/model/status.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/status.py b/ambari-client/src/main/python/ambari_client/model/status.py old mode 100644 new mode 100755 index 657afdd..1eba35d --- a/ambari-client/src/main/python/ambari_client/model/status.py +++ b/ambari-client/src/main/python/ambari_client/model/status.py @@ -16,25 +16,43 @@ import logging from ambari_client.model.base_model import BaseModel -from ambari_client.model.utils import retain_self_helper -from ambari_client.model.paths import BOOTSTRAP_PATH -LOG = logging.getLogger(__name__) +from ambari_client.model import paths , utils +LOG = logging.getLogger(__name__) class StatusModel(BaseModel): - RO_ATTR = () - RW_ATTR = ('status','requestId') + """ + The ServiceModel class + """ + RO_ATTR = ('id',) + RW_ATTR = ('status', 'requestId', "message") REF_ATTR = ('cluster_name',) - def __init__(self, resource_root, status ,requestId=None): + def __init__(self, resource_root, status , requestId=None, message=None): #BaseModel.__init__(self, **locals()) - retain_self_helper(**locals()) + utils.retain_self_helper(BaseModel, **locals()) def __str__(self): - return "<<StatusModel>> = %s (requestId = %s)" % (self.status, self.requestId) + return "<<StatusModel>> status = %s ; requestId = %s ;message = %s" % (self.status, self._get_id() , self._get_message()) - def get_request_path(self): - return BOOTSTRAP_PATH + '/' + self.requestId + def get_bootstrap_path(self): + return paths.BOOTSTRAP_PATH + '/' + self.requestId + def get_request_path(self): + return paths.REQUEST_PATH % (self._get_id()) + + def _get_message(self): + if hasattr(self, 'message'): + return self.message + else: + None + + def _get_id(self): + if hasattr(self, 'requestId') and self.requestId: + return self.requestId + elif hasattr(self, 'id') and self.id: + return self.id + else: + None
http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/model/utils.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/model/utils.py b/ambari-client/src/main/python/ambari_client/model/utils.py old mode 100644 new mode 100755 index 904154d..4a7fa5e --- a/ambari-client/src/main/python/ambari_client/model/utils.py +++ b/ambari-client/src/main/python/ambari_client/model/utils.py @@ -17,109 +17,153 @@ import logging import sys +import unicodedata LOG = logging.getLogger(__name__) + ref_dic = {"cluster_name":"clusterRef"} ref_class_dic = {"ClusterModelRef":"cluster_name"} -ref_pkg_dic={"ClusterModelRef":"ambari_client.model.cluster"} - +ref_pkg_dic = {"ClusterModelRef":"ambari_client.model.cluster"} +LIST_KEY = "items" + class ModelUtils(object): @staticmethod - def get_model_list(member_cls, dic, resource_root , RESOURCE_KEY_WORD): + def get_model_list(member_list_clss, member_cls, collection_dict, resource_root , RESOURCE_KEY_WORD): + """ + create a model. + @param member_list_clss : model_list class. + @param model_cls : model class. + @param collection_dict : collection dict used for creating the list of objects. + @param resource_root : resource object. + @param RESOURCE_KEY_WORD : tsake subset of model_dict based on this key. + @return: A ModelList object. + """ #print locals() - from ambari_client.model.base_model import ModelList - json_list = dic[ModelList.LIST_KEY] - LOG.debug ("get_model_list : getting the "+str(ModelList.LIST_KEY)+ " value---> \n\t"+str(json_list) ) + json_list = [] + + #remove items + if isinstance(collection_dict, dict) and collection_dict.has_key(LIST_KEY): + json_list = collection_dict[LIST_KEY] + LOG.debug("get_model_list: collection_dict is dict ? %s ; has_key = %s" % (isinstance(collection_dict, dict), collection_dict.has_key(LIST_KEY))) + LOG.debug ("get_model_list: collection_dict has %s ;subset = %s" % (LIST_KEY, str(json_list))) + else: + json_list = collection_dict + LOG.error("get_model_list: collection_dict is dict ? %s ; has_key = %s" % (isinstance(collection_dict, dict), collection_dict.has_key(LIST_KEY))) + LOG.debug ("get_model_list: json_list value : \n\t" + str(json_list)) if isinstance(json_list, list): json_list_new = [ x.get(RESOURCE_KEY_WORD) for x in json_list] + LOG.debug("get_model_list: json_list is list ? %s ; " % (isinstance(json_list, list))) + else: + json_list_new = [json_list] + LOG.error("get_model_list: json_list is list ? %s ; " % (isinstance(json_list, list))) - LOG.debug ("get_model_list: creating a array for "+str(RESOURCE_KEY_WORD)+" value---> \n\t"+str(json_list_new)) - objects = [ ModelUtils.create_model(member_cls,x, resource_root ,RESOURCE_KEY_WORD) for x in json_list_new ] + LOG.debug ("get_model_list: json_list_new used for creating ModelList \n\t" + str(json_list_new)) + objects = [ ModelUtils.create_model(member_cls, x, resource_root , RESOURCE_KEY_WORD) for x in json_list_new ] LOG.debug (objects) - return ModelList(objects) + return member_list_clss(objects) + @staticmethod - def create_model(member_cls, dic, resource_root,RESOURCE_KEY_WORD): + def create_model(model_cls, model_dict, resource_root, RESOURCE_KEY_WORD, exception_cls=None): + """ + create a model. + @param model_cls : model class. + @param model_dict : model dict used for creating the object. + @param resource_root : resource object. + @param RESOURCE_KEY_WORD : tsake subset of model_dict based on this key. + @return: A model_cls object. + """ #print locals() rw_dict = { } - LOG.debug (" create_model : dic = "+str(dic)) - if isinstance(dic, dict) and dic.has_key(RESOURCE_KEY_WORD): - dic=dic[RESOURCE_KEY_WORD] - LOG.debug (" dic.items() 2 = "+str(dic.items())) - for k, v in dic.items(): - LOG.debug (k) - LOG.debug (v) - if k in member_cls.RW_ATTR: + LOG.debug ("model_dict = " + str(model_dict)) + + if isinstance(model_dict, dict) and model_dict.has_key(RESOURCE_KEY_WORD): + model_dict = model_dict[RESOURCE_KEY_WORD] + LOG.debug ("model_dict has %s ;subset = %s" % (RESOURCE_KEY_WORD, str(model_dict.items()))) + if isinstance(model_dict, dict) and model_dict.has_key("Requests"): + model_dict = model_dict["Requests"] + LOG.debug ("model_dict has Requests ;subset = %s" % (str(model_dict.items()))) + if isinstance(model_dict, dict) and model_dict.has_key("status") and exception_cls: + LOG.debug ("model_dict has status ,might be a exception from Ambari ;model_cls = %s ;exception_clss = %s" % + (str(model_cls), str(exception_cls))) + return ModelUtils.create_model(exception_cls, model_dict, resource_root, "NO_KEY") + + + for k, v in model_dict.items(): + LOG.debug("key = %s ; value = %s " % (str(k), str(v))) + if k in model_cls.RW_ATTR: LOG.debug (k + " is there in RW_ATTR") rw_dict[k] = v - del dic[k] + del model_dict[k] rw_dict = get_unicode_kw(rw_dict) - obj = member_cls(resource_root, **rw_dict) - + obj = model_cls(resource_root, **rw_dict) - for attr in member_cls.RO_ATTR: + for attr in model_cls.RO_ATTR: obj._setattr(attr, None) - - for k, v in dic.items(): - if k in member_cls.RO_ATTR: + for k, v in model_dict.items(): + if k in model_cls.RO_ATTR: obj._setattr(k, v) else: - LOG.debug("Unexpected attribute '%s' in %s json" %(k, member_cls.__name__)) + LOG.debug("Unexpected attribute '%s' in %s json" % (k, model_cls.__name__)) - for attr in member_cls.REF_ATTR: - LOG.debug(attr) + for attr in model_cls.REF_ATTR: + LOG.debug("%s found as reference var" % (attr)) obj._setattr(getREF_class_name(attr), None) - - for k, v in dic.items(): - if k in member_cls.REF_ATTR: + for k, v in model_dict.items(): + if k in model_cls.REF_ATTR: obj._setattr(getREF_class_name(k), v) else: - LOG.debug("Unexpected attribute '%s' in %s json" %(k, member_cls.__name__)) - + LOG.debug("Unknown attribute '%s' found in model_dict for %s " % (k, model_cls.__name__)) return obj - - -def getREF_class_name( REF_name): +#get attribute with REF +def getREF_class_name(REF_name): if ref_dic.has_key(REF_name): return ref_dic[str(REF_name)] else: return None -def getREF_var_name( REF_name): +def getREF_var_name(REF_name): if ref_class_dic.has_key(REF_name): return ref_class_dic[str(REF_name)] else: return None + def get_REF_object(ref_class_name): """ Gets the Ref object based on class_name """ - class_ref=getattr(sys.modules[ref_pkg_dic[ref_class_name]], ref_class_name) - LOG.debug( class_ref ) + class_ref = getattr(sys.modules[ref_pkg_dic[ref_class_name]], ref_class_name) + LOG.debug(class_ref) return class_ref -def get_unicode( v): - import unicodedata + +def get_unicode(v): + #import unicodedata if v: - v = unicodedata.normalize('NFKD', v).encode('ascii', 'ignore') - LOG.debug( v ) + if isinstance(v, unicode): + v = unicodedata.normalize('NFKD', v).encode('ascii', 'ignore') + LOG.debug(v) + elif isinstance(v, str): + LOG.debug("warning: string found while expecting unicode %s" % v) return v - -def retain_self_helper(self=None, **kwargs): + + +def retain_self_helper(memclass, self=None, **kwargs): #print locals() - from ambari_client.model.base_model import BaseModel - BaseModel.__init__(self, **kwargs) - + #from ambari_client.model.base_model import BaseModel + memclass.__init__(self, **kwargs) + + def get_unicode_kw(dic): """ We use unicode strings as keys in kwargs. @@ -128,3 +172,43 @@ def get_unicode_kw(dic): for k, v in dic.iteritems(): res[str(k)] = v return res + + +def get_config_type(service_name): + """ + get the config type based on service_name + """ + if service_name == "HDFS": + type = "hdfs-site" + elif service_name == "HDFS": + type = "core-site" + elif service_name == "MAPREDUCE": + type = "mapred-site" + elif service_name == "HBASE": + type = "hbase-site" + elif service_name == "OOZIE": + type = "oozie-site" + elif service_name == "HIVE": + type = "hive-site" + elif service_name == "WEBHCAT": + type = "webhcat-site" + else: + type = "global" + return type + + +def get_key_value(dictt , key): + """ + Search for some random key in the dict + """ + if isinstance(dictt, dict) and dictt.has_key(key): + return dictt[key] + elif isinstance(dictt, dict) and not dictt.has_key(key): + #check if values has it? + for v in dictt.values(): + if isinstance(v, dict): + return get_key_value(v, key) + elif isinstance(v, list): + for l in list: + return get_key_value(l, key) + http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/resources/__init__.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/resources/__init__.py b/ambari-client/src/main/python/ambari_client/resources/__init__.py old mode 100644 new mode 100755 http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/resources/clusters.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/resources/clusters.py b/ambari-client/src/main/python/ambari_client/resources/clusters.py old mode 100644 new mode 100755 index 0e3f0be..ca3cca6 --- a/ambari-client/src/main/python/ambari_client/resources/clusters.py +++ b/ambari-client/src/main/python/ambari_client/resources/clusters.py @@ -19,46 +19,26 @@ from ambari_client.model import cluster __docformat__ = "epytext" -def get_cluster(resource_root, cluster_name): +def _get_cluster(resource_root, cluster_name): """ Get a cluster by cluster_name @param resource_root: The root Resource. - @param cluster_name: Cluster cluster_name + @param cluster_name: Cluster's name @return: ClusterModel object """ - return cluster.get_cluster(resource_root, cluster_name) - + return cluster._get_cluster(resource_root, cluster_name) -def get_all_clusters(root_resource): +def _get_all_clusters(root_resource): """ Get all clusters in Ambari. @param root_resource: The root Resource object. @return: A list of ClusterModel objects in ModelList. """ - return cluster.get_all_clusters(root_resource) + return cluster._get_all_clusters(root_resource) -def create_cluster(root_resource, cluster_name, version): - """ - Create a cluster - @param root_resource: The root Resource. - @param cluster_name: Cluster cluster_name - @param version: HDP version - @return: An ClusterModel object - """ - return cluster.create_cluster(root_resource, cluster_name, version) - - -def delete_cluster(root_resource, cluster_name): - """ - Create a cluster - @param root_resource: The root Resource. - @param cluster_name: Cluster cluster_name - """ - return cluster.delete_cluster(root_resource, cluster_name) - -def create_cluster(root_resource, cluster_name, version): +def _create_cluster(root_resource, cluster_name, version): """ Create a cluster @param root_resource: The root Resource. @@ -66,13 +46,13 @@ def create_cluster(root_resource, cluster_name, version): @param version: HDP version @return: An ClusterModel object """ - return cluster.create_cluster(root_resource, cluster_name, version) + return cluster._create_cluster(root_resource, cluster_name, version) -def delete_cluster(root_resource, cluster_name): +def _delete_cluster(root_resource, cluster_name): """ Create a cluster @param root_resource: The root Resource. @param cluster_name: Cluster cluster_name """ - return cluster.delete_cluster(root_resource, cluster_name) \ No newline at end of file + return cluster._delete_cluster(root_resource, cluster_name) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/resources/hosts.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/resources/hosts.py b/ambari-client/src/main/python/ambari_client/resources/hosts.py old mode 100644 new mode 100755 index cc4bcc4..c1a262c --- a/ambari-client/src/main/python/ambari_client/resources/hosts.py +++ b/ambari-client/src/main/python/ambari_client/resources/hosts.py @@ -1,4 +1,4 @@ - # +# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -19,54 +19,54 @@ from ambari_client.model import host __docformat__ = "epytext" -def create_host(root_resource, host_name, ip, rack_info=None): +def _create_host(root_resource, host_name, ip, rack_info=None): """ Create a host @param root_resource: The root Resource. @param host_name: Host name @param ip: IP address @param rack_info: Rack id. Default None - @return: An HostModel object + @return: A HostModel object """ - return host.create_host(root_resource, host_name, ip, rack_info=None) + return host._create_host(root_resource, host_name, ip, rack_info=None) -def get_host(root_resource, host_name): + +def _get_host(root_resource, host_name): """ - Lookup a host by id + Lookup a host by name @param root_resource: The root Resource. @param host_name: Host name - @return: An HostModel object + @return: A HostModel object """ - return host.get_host(root_resource, host_name) + return host._get_host(root_resource, host_name) + -def get_all_hosts(root_resource): +def _get_all_hosts(root_resource): """ Get all hosts @param root_resource: The root Resource. @return: A list of HostModel objects. """ - return host.get_all_hosts(root_resource) - + return host._get_all_hosts(root_resource) - -def delete_host(root_resource, host_name): +def _delete_host(root_resource, host_name): """ Delete a host by id @param root_resource: The root Resource. @param host_name: Host name @return: The deleted HostModel object """ - return host.delete_host(root_resource,host_name) - + return host._delete_host(root_resource, host_name) -def bootstrap_hosts(root_resource , hosts_list ,ssh_key): +def _bootstrap_hosts(root_resource , hosts_list , ssh_key): """ Bootstrap hosts. - @param hosts list of host_names. + @param hosts_list: list of host_names. + @param ssh_key: ssh key for password-less access. @return: A StatusModel object. """ - return host.bootstrap_hosts(root_resource, hosts_list ,ssh_key) \ No newline at end of file + return host._bootstrap_hosts(root_resource, hosts_list , ssh_key) http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/resources/services.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/resources/services.py b/ambari-client/src/main/python/ambari_client/resources/services.py deleted file mode 100644 index abddbec..0000000 --- a/ambari-client/src/main/python/ambari_client/resources/services.py +++ /dev/null @@ -1,40 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from ambari_client.model import service -__docformat__ = "epytext" - - -def get_service(resource_root, service_name, cluster_name="default"): - """ - Get a service by service_name - @param resource_root: The root Resource . - @param service_name: Service service_name. - @param cluster_name: Cluster service_name. - @return: ServiceModel object. - """ - return service.get_service(resource_root, service_name, cluster_name) - - - -def get_all_services(resource_root, cluster_name="default"): - """ - Get all services. - @param resource_root: The root Resource. - @param cluster_name: Cluster name. - @return: A list of ServiceModel objects in ModelList. - """ - return service.get_all_services(resource_root, cluster_name) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/ambari_client/resources/stacks.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/ambari_client/resources/stacks.py b/ambari-client/src/main/python/ambari_client/resources/stacks.py new file mode 100755 index 0000000..f4ef518 --- /dev/null +++ b/ambari-client/src/main/python/ambari_client/resources/stacks.py @@ -0,0 +1,41 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ambari_client.model import stack , component + +__docformat__ = "epytext" + + +def _get_config(root_resource, version, service_name): + """ + Get service configurations from stack + @param version: The HDP version. + @param service_name: service name + @return: A ConfigModel object + """ + return stack._get_configuration_from_stack(root_resource, version, service_name) + + +def _get_components(root_resource, version, service_name): + """ + Get service components from stack + @param version: The HDP version. + @param service_name: service name + @return: A ComponentModel object + """ + return stack._get_components_from_stack(root_resource, version, service_name) + http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/main/python/setup.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/main/python/setup.py b/ambari-client/src/main/python/setup.py old mode 100644 new mode 100755 http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/packages/tarball/all.xml ---------------------------------------------------------------------- diff --git a/ambari-client/src/packages/tarball/all.xml b/ambari-client/src/packages/tarball/all.xml old mode 100644 new mode 100755 http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/TestAmbariClient.py ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/TestAmbariClient.py b/ambari-client/src/test/python/TestAmbariClient.py old mode 100644 new mode 100755 index ed0ff9c..3f97009 --- a/ambari-client/src/test/python/TestAmbariClient.py +++ b/ambari-client/src/test/python/TestAmbariClient.py @@ -21,7 +21,7 @@ limitations under the License. from mock import MagicMock, patch from ambari_client.ambari_api import AmbariClient -from ambari_client.person import Person + import unittest class TestAmbariClient(unittest.TestCase): @@ -32,25 +32,25 @@ class TestAmbariClient(unittest.TestCase): This testcase checks if when the init method was called & the httpclient was initialized """ - client = AmbariClient("localhost", 8080, "admin","admin",version=1) + client = AmbariClient("localhost", 8080, "admin", "admin", version=1) self.assertEqual(client.version, 1, "version should be 1") - self.assertEqual(client.host_url, "http://localhost:8080/api/v1", + self.assertEqual(client.host_url, "http://localhost:8080/api/v1", "host_url should be http://localhost:8080/api/v1") - client = AmbariClient(host_name="localhost",user_name="admin",password="admin") + client = AmbariClient(host_name="localhost", user_name="admin", password="admin") self.assertEqual(client.version, 1, "version should be 1") - self.assertEqual(client.host_url, "http://localhost:8080/api/v1", + self.assertEqual(client.host_url, "http://localhost:8080/api/v1", "host_url should be http://localhost:8080/api/v1") client = AmbariClient(host_name="localhost") self.assertEqual(client.version, 1, "version should be 1") - self.assertEqual(client.host_url, "http://localhost:8080/api/v1", + self.assertEqual(client.host_url, "http://localhost:8080/api/v1", "host_url should be http://localhost:8080/api/v1") @patch("ambari_client.core.http_client.HttpClient") - def test_get_all_clusters_valid(self ,http_client): + def test_get_all_clusters_valid(self , http_client): """ Get all clusters. This testcase checks if get_all_clusters returns a list of ModelList. @@ -60,22 +60,44 @@ class TestAmbariClient(unittest.TestCase): mocked_code = "200" mocked_content = "text/plain" - expected_output ={'items': [{'cluster_name': u'test1', 'version': u'HDP-1.2.1'}]} + expected_output = {'items': [{'cluster_name': u'test1', 'version': u'HDP-1.2.1'}]} linestring = open('json/get_all_clusters.json', 'r').read() mocked_response = linestring http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content - client = AmbariClient("localhost", 8080, "admin","admin",version=1 ,client= http_client_mock) + client = AmbariClient("localhost", 8080, "admin", "admin", version=1 , client=http_client_mock) all_clusters = client.get_all_clusters() self.assertEqual(len(all_clusters), 1, "There should be a cluster from the response") self.assertEqual(all_clusters.to_json_dict(), expected_output, "to_json_dict should convert ModelList") + @patch("ambari_client.core.http_client.HttpClient") + def test_get_hosts_clusters_valid(self , http_client): + """ + Get all hosts. + This testcase checks if get_all_hosts returns a list of ModelList. + """ + http_client_mock = MagicMock() + http_client.return_value = http_client_mock + + mocked_code = "200" + mocked_content = "text/plain" + + linestring = open('json/get_all_hosts.json', 'r').read() + mocked_response = linestring + http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content + + client = AmbariClient("localhost", 8080, "admin", "admin", version=1 , client=http_client_mock) + all_hosts = client.get_all_hosts() + + self.assertEqual(len(all_hosts), 12, "There should be 12 hosts from the response") + + @patch("ambari_client.core.http_client.HttpClient") - def test_get_cluster_valid(self ,http_client): + def test_get_cluster_valid(self , http_client): """ Get all clusters. This testcase checks if get_all_clusters returns a list of ModelList. @@ -86,11 +108,11 @@ class TestAmbariClient(unittest.TestCase): mocked_content = "text/plain" linestring = open('json/get_cluster.json', 'r').read() - mocked_response = linestring + mocked_response = linestring expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'} http_client_mock.invoke.return_value = mocked_response , mocked_code , mocked_content - client = AmbariClient("localhost", 8080, "admin","admin",version=1,client= http_client_mock) + client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) cluster = client.get_cluster('test1') self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ") @@ -99,9 +121,9 @@ class TestAmbariClient(unittest.TestCase): @patch("ambari_client.core.http_client.HttpClient") - def test_get_all_services_valid(self ,http_client): + def test_get_cluster_services_valid(self , http_client): """ - Get all services. + Get all services of a cluster. This testcase checks if get_all_services returns a list of ModelList. """ http_client_mock = MagicMock() @@ -112,7 +134,7 @@ class TestAmbariClient(unittest.TestCase): expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'} http_client_mock.invoke.side_effect = http_client_invoke_side_effects - client = AmbariClient("localhost", 8080, "admin","admin",version=1,client= http_client_mock) + client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) cluster = client.get_cluster('test1') serviceList = cluster.get_all_services() @@ -121,9 +143,9 @@ class TestAmbariClient(unittest.TestCase): self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response") @patch("ambari_client.core.http_client.HttpClient") - def test_get_service_valid(self ,http_client): + def test_get_cluster_service_valid(self , http_client): """ - Get the service. + Get the service of a cluster This testcase checks if get_service returns a list of ServiceModel. """ http_client_mock = MagicMock() @@ -134,7 +156,7 @@ class TestAmbariClient(unittest.TestCase): expected_dict_output = {'cluster_name': u'test1', 'version': u'HDP-1.2.1'} http_client_mock.invoke.side_effect = http_client_invoke_side_effects - client = AmbariClient("localhost", 8080, "admin","admin",version=1,client= http_client_mock) + client = AmbariClient("localhost", 8080, "admin", "admin", version=1, client=http_client_mock) cluster = client.get_cluster('test1') serviceList = cluster.get_all_services() ganglia = cluster.get_service("GANGLIA") @@ -142,11 +164,13 @@ class TestAmbariClient(unittest.TestCase): self.assertEqual(cluster.cluster_name, "test1", "cluster_name should be test1 ") self.assertEqual(cluster.to_json_dict(), expected_dict_output, "to_json_dict should convert ClusterModel") self.assertEqual(len(serviceList), 3, "There should be a 3 services from the response") - #self.assertEqual(str(ganglia.state), "STARTED", "The ganglia service state should be fetched as STARTED") + self.assertEqual(str(ganglia.state), "STARTED", "The ganglia service state should be fetched as STARTED") self.assertEqual(ganglia.clusterRef.cluster_name, cluster.cluster_name, "The clusterRef value for service should be fetched ") + + def http_client_invoke_side_effects(*args, **kwargs): print locals() mocked_code = "200" @@ -154,9 +178,33 @@ def http_client_invoke_side_effects(*args, **kwargs): if args[1] == "//clusters/test1": mocked_response = open('json/get_cluster.json', 'r').read() return mocked_response, mocked_code , mocked_content + elif args[1] == "//hosts": + mocked_response = open('json/get_all_hosts.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/hosts/r01wn01": + mocked_response = open('json/get_cluster_host.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/hosts?fields=*": + mocked_response = open('json/get_cluster_hosts.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/services/GANGLIA": + mocked_response = open('json/get_cluster_service.json', 'r').read() + return mocked_response, mocked_code , mocked_content elif args[1] == "//clusters/test1/services?fields=*": - mocked_response = open('json/get_all_services.json', 'r').read() + mocked_response = open('json/get_cluster_services.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/hosts/r01wn01/host_components/NAMENODE": + mocked_response = open('json/get_host_component.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/hosts/r01wn01/host_components?ServiceComponentInfo": + mocked_response = open('json/get_host_components.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/services/GANGLIA/components/GANGLIA_MONITOR": + mocked_response = open('json/get_service_component.json', 'r').read() + return mocked_response, mocked_code , mocked_content + elif args[1] == "//clusters/test6/services/GANGLIA/components?fields=*": + mocked_response = open('json/get_service_components.json', 'r').read() return mocked_response, mocked_code , mocked_content elif args[1] == "//clusters/test1/services/GANGLIA": mocked_response = open('json/get_service.json', 'r').read() - return mocked_response, mocked_code , mocked_content \ No newline at end of file + return mocked_response, mocked_code , mocked_content http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/json/get_all_clusters.json ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/json/get_all_clusters.json b/ambari-client/src/test/python/json/get_all_clusters.json old mode 100644 new mode 100755 http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/json/get_all_hosts.json ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/json/get_all_hosts.json b/ambari-client/src/test/python/json/get_all_hosts.json new file mode 100755 index 0000000..2aca5c4 --- /dev/null +++ b/ambari-client/src/test/python/json/get_all_hosts.json @@ -0,0 +1,77 @@ +{ + "href" : "http://localhost:8080/api/v1/hosts", + "items" : [ + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-83", + "Hosts" : { + "host_name" : "apspal44-83" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-84", + "Hosts" : { + "host_name" : "apspal44-84" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-85", + "Hosts" : { + "host_name" : "apspal44-85" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-86", + "Hosts" : { + "host_name" : "apspal44-86" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-87", + "Hosts" : { + "host_name" : "apspal44-87" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-88", + "Hosts" : { + "host_name" : "apspal44-88" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/apspal44-89", + "Hosts" : { + "host_name" : "apspal44-89" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/r01hn01", + "Hosts" : { + "host_name" : "r01hn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/r01mgt", + "Hosts" : { + "host_name" : "r01mgt" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/r01wn01", + "Hosts" : { + "host_name" : "r01wn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/r01wn02", + "Hosts" : { + "host_name" : "r01wn02" + } + }, + { + "href" : "http://localhost:8080/api/v1/hosts/r01wn03", + "Hosts" : { + "host_name" : "r01wn03" + } + } + ] +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/json/get_all_services.json ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/json/get_all_services.json b/ambari-client/src/test/python/json/get_all_services.json deleted file mode 100644 index 53bb2da..0000000 --- a/ambari-client/src/test/python/json/get_all_services.json +++ /dev/null @@ -1,120 +0,0 @@ -{ - "href" : "http://localhost:8080/api/v1/clusters/test1/services?fields=*", - "items" : [ - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/GANGLIA", - "ServiceInfo" : { - "cluster_name" : "test1", - "state" : "STARTED", - "service_name" : "GANGLIA", - "desired_configs" : { - "global" : "version1" - } - }, - "components" : [ - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/GANGLIA/components/GANGLIA_MONITOR", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "GANGLIA_MONITOR", - "service_name" : "GANGLIA" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/GANGLIA/components/GANGLIA_SERVER", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "GANGLIA_SERVER", - "service_name" : "GANGLIA" - } - } - ] - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/MAPREDUCE", - "ServiceInfo" : { - "cluster_name" : "test1", - "state" : "STARTED", - "service_name" : "MAPREDUCE", - "desired_configs" : { - "mapred-site" : "version1", - "global" : "version1", - "core-site" : "version1" - } - }, - "components" : [ - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/MAPREDUCE/components/TASKTRACKER", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "TASKTRACKER", - "service_name" : "MAPREDUCE" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/MAPREDUCE/components/MAPREDUCE_CLIENT", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "MAPREDUCE_CLIENT", - "service_name" : "MAPREDUCE" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/MAPREDUCE/components/JOBTRACKER", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "JOBTRACKER", - "service_name" : "MAPREDUCE" - } - } - ] - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/HDFS", - "ServiceInfo" : { - "cluster_name" : "test1", - "state" : "STARTED", - "service_name" : "HDFS", - "desired_configs" : { - "global" : "version1", - "hdfs-site" : "version1", - "core-site" : "version1" - } - }, - "components" : [ - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/HDFS/components/SECONDARY_NAMENODE", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "SECONDARY_NAMENODE", - "service_name" : "HDFS" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/HDFS/components/HDFS_CLIENT", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "HDFS_CLIENT", - "service_name" : "HDFS" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/HDFS/components/NAMENODE", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "NAMENODE", - "service_name" : "HDFS" - } - }, - { - "href" : "http://localhost:8080/api/v1/clusters/test1/services/HDFS/components/DATANODE", - "ServiceComponentInfo" : { - "cluster_name" : "test1", - "component_name" : "DATANODE", - "service_name" : "HDFS" - } - } - ] - } - ] -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/json/get_cluster.json ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/json/get_cluster.json b/ambari-client/src/test/python/json/get_cluster.json old mode 100644 new mode 100755 http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/dd5b2aa4/ambari-client/src/test/python/json/get_cluster_host.json ---------------------------------------------------------------------- diff --git a/ambari-client/src/test/python/json/get_cluster_host.json b/ambari-client/src/test/python/json/get_cluster_host.json new file mode 100755 index 0000000..a9f6d96 --- /dev/null +++ b/ambari-client/src/test/python/json/get_cluster_host.json @@ -0,0 +1,250 @@ +{ + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01", + "Hosts" : { + "cluster_name" : "test6", + "cpu_count" : 24, + "disk_info" : [ + { + "available" : "25938900", + "used" : "5743652", + "percent" : "19%", + "size" : "33378088", + "type" : "ext4", + "mountpoint" : "/" + }, + { + "available" : "49525536", + "used" : "0", + "percent" : "0%", + "size" : "49525536", + "type" : "tmpfs", + "mountpoint" : "/dev/shm" + }, + { + "available" : "433221", + "used" : "37023", + "percent" : "8%", + "size" : "495844", + "type" : "ext4", + "mountpoint" : "/boot" + }, + { + "available" : "3020752", + "used" : "71284", + "percent" : "3%", + "size" : "3257512", + "type" : "ext4", + "mountpoint" : "/home" + }, + { + "available" : "547125892", + "used" : "404652", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/1" + }, + { + "available" : "547305068", + "used" : "225476", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/2" + }, + { + "available" : "547325924", + "used" : "204620", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/3" + }, + { + "available" : "547320928", + "used" : "209616", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/4" + }, + { + "available" : "547315544", + "used" : "215000", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/5" + }, + { + "available" : "547327008", + "used" : "203536", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/6" + }, + { + "available" : "547310644", + "used" : "219900", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/7" + }, + { + "available" : "547320544", + "used" : "210000", + "percent" : "1%", + "size" : "576831992", + "type" : "ext4", + "mountpoint" : "/data/8" + } + ], + "host_health_report" : "", + "host_name" : "r01wn01", + "host_state" : "HEALTHY", + "host_status" : "HEALTHY", + "ip" : "10.104.44.95", + "last_agent_env" : { + "stackFoldersAndFiles" : [ ], + "rpms" : [ + { + "name" : "nagios", + "installed" : true, + "version" : "nagios-3.5.0-99.x86_64" + }, + { + "name" : "ganglia", + "installed" : false + }, + { + "name" : "hadoop", + "installed" : true, + "version" : "hadoop-1.2.0.1.3.0.0-107.el6.x86_64" + }, + { + "name" : "hadoop-lzo", + "installed" : true, + "version" : "hadoop-lzo-0.5.0-1.x86_64" + }, + { + "name" : "hbase", + "installed" : false + }, + { + "name" : "oozie", + "installed" : true, + "version" : "oozie-3.3.2.1.3.0.0-107.el6.noarch" + }, + { + "name" : "sqoop", + "installed" : false + }, + { + "name" : "pig", + "installed" : false + }, + { + "name" : "zookeeper", + "installed" : true, + "version" : "zookeeper-3.4.5.1.3.0.0-107.el6.noarch" + }, + { + "name" : "hive", + "installed" : true, + "version" : "hive-0.11.0.1.3.0.0-107.el6.noarch" + }, + { + "name" : "libconfuse", + "installed" : true, + "version" : "libconfuse-2.6-3.el6.x86_64" + }, + { + "name" : "ambari-log4j", + "installed" : true, + "version" : "ambari-log4j-1.2.3.6-1.noarch" + } + ], + "alternatives" : [ ], + "existingUsers" : [ ], + "existingRepos" : [ + "unable_to_determine" + ], + "installedPackages" : [ ], + "hostHealth" : { + "activeJavaProcs" : [ ], + "agentTimeStampAtReporting" : 1377776815389, + "serverTimeStampAtReporting" : 1377776801984, + "liveServices" : [ + { + "name" : "ntpd", + "desc" : "ntpd is stopped\n", + "status" : "Unhealthy" + } + ], + "diskStatus" : [ + { + "available" : "25032456", + "used" : "6650096", + "percent" : "21%", + "size" : "33378088", + "type" : "ext4", + "mountpoint" : "/" + } + ] + } + }, + "last_heartbeat_time" : 1377776827188, + "last_registration_time" : 1377605551901, + "os_arch" : "x86_64", + "os_type" : "redhat6", + "ph_cpu_count" : 2, + "public_host_name" : "r01wn01", + "rack_info" : "/default-rack", + "total_mem" : 99048488, + "desired_configs" : { } + }, + "host_components" : [ + { + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01/host_components/GANGLIA_MONITOR", + "HostRoles" : { + "cluster_name" : "test6", + "component_name" : "GANGLIA_MONITOR", + "host_name" : "r01wn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01/host_components/GANGLIA_SERVER", + "HostRoles" : { + "cluster_name" : "test6", + "component_name" : "GANGLIA_SERVER", + "host_name" : "r01wn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01/host_components/MAPREDUCE_CLIENT", + "HostRoles" : { + "cluster_name" : "test6", + "component_name" : "MAPREDUCE_CLIENT", + "host_name" : "r01wn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01/host_components/NAGIOS_SERVER", + "HostRoles" : { + "cluster_name" : "test6", + "component_name" : "NAGIOS_SERVER", + "host_name" : "r01wn01" + } + }, + { + "href" : "http://localhost:8080/api/v1/clusters/test6/hosts/r01wn01/host_components/NAMENODE", + "HostRoles" : { + "cluster_name" : "test6", + "component_name" : "NAMENODE", + "host_name" : "r01wn01" + } + } + ] +} \ No newline at end of file
