Support to do list of hypervisors in detail

1. Added another model to capture Hypervisor in detail
2. Added another action to list hypervisor in detail in client.
3. Added hypervisor_hostname attribute to server.
4. Converting hypervisor_version from int to string while
   building the model.Changed corresponding unit tests

Change-Id: Ib62f8bc807e01e5a3d2b5dcc773b24c7a8c12fe8
This commit is contained in:
Sumanth Nagadavalli 2013-07-12 19:24:23 +05:30
parent 2cc07dfcdc
commit 83d78f8bc6
9 changed files with 539 additions and 193 deletions

View File

@ -15,7 +15,8 @@ limitations under the License.
"""
from cafe.engine.clients.rest import AutoMarshallingRestClient
from cloudcafe.compute.hypervisors_api.model.hypervisors import Hypervisor
from cloudcafe.compute.hypervisors_api.model.hypervisor\
import HypervisorsMin, Hypervisors
class HypervisorsClient(AutoMarshallingRestClient):
@ -50,7 +51,19 @@ class HypervisorsClient(AutoMarshallingRestClient):
"""
url = "{url}/os-hypervisors".format(url=self.url)
hypervisor_res = self.request('GET', url,
response_entity_type=Hypervisor,
response_entity_type=HypervisorsMin,
requestslib_kwargs=requestslib_kwargs)
return hypervisor_res
def list_hypervisors_with_detail(self, requestslib_kwargs=None):
"""
@summary: Returns a list of hypervisors
@return: List of hypervisors
@rtype: C{list}
"""
url = "{url}/os-hypervisors/detail".format(url=self.url)
hypervisor_res = self.request('GET', url,
response_entity_type=Hypervisors,
requestslib_kwargs=requestslib_kwargs)
return hypervisor_res
@ -64,6 +77,6 @@ class HypervisorsClient(AutoMarshallingRestClient):
url = "{url}/os-hypervisors/{hypervisor_hostname}/servers".\
format(url=self.url, hypervisor_hostname=hypervisor_hostname)
hypervisor_res = self.request('GET', url,
response_entity_type=Hypervisor,
response_entity_type=HypervisorsMin,
requestslib_kwargs=requestslib_kwargs)
return hypervisor_res

View File

@ -0,0 +1,264 @@
"""
Copyright 2013 Rackspace
Licensed 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.
"""
import json
import xml.etree.ElementTree as ET
from cafe.engine.models.base import AutoMarshallingModel, AutoMarshallingListModel
from cloudcafe.compute.common.equality_tools import EqualityTools
from cloudcafe.compute.servers_api.models.servers import Server
class Service(AutoMarshallingModel):
def __init__(self, host=None, id_=None):
super(Service, self).__init__()
self.host = host
self.id_ = id_
@classmethod
def _dict_to_obj(cls, json_dict):
return Service(host=json_dict.get('host'),
id_=json_dict.get('id'))
class Hypervisor(AutoMarshallingModel):
def __init__(self, id_=None, hypervisor_hostname=None,
servers=None, cpu_info=None, free_disk_gb=None,
hypervisor_version=None, disk_available_least=None,
local_gb=None, free_ram_mb=None, vcpus_used=None,
hypervisor_type=None, local_gb_used=None,
memory_mb_used=None, memory_mb=None,
current_workload=None, vcpus=None,
running_vms=None, service=None):
super(Hypervisor, self).__init__()
self.id_ = id_
self.hypervisor_hostname = hypervisor_hostname
self.servers = servers
self.cpu_info = cpu_info
self.free_disk_gb = free_disk_gb
self.hypervisor_version = hypervisor_version
self.disk_available_least = disk_available_least
self.local_gb = local_gb
self.free_ram_mb = free_ram_mb
self.vcpus_used = vcpus_used
self.hypervisor_type = hypervisor_type
self.local_gb_used = local_gb_used
self.memory_mb_used = memory_mb_used
self.memory_mb = memory_mb
self.current_workload = current_workload
self.vcpus = vcpus
self.running_vms = running_vms
self.service = service
def __eq__(self, other):
"""
@summary: Overrides the default equals
@param other: Hypervisor object to compare with
@type other: Hypervisor
@return: True if Host objects are equal, False otherwise
@rtype: bool
"""
return EqualityTools.are_objects_equal(self, other)
def __ne__(self, other):
"""
@summary: Overrides the default not-equals
@param other: Hypervisor object to compare with
@type other: Hypervisor
@return: True if Hypervisor objects are not equal, False otherwise
@rtype: bool
"""
return not self.__eq__(other)
@classmethod
def _dict_to_obj(cls, hypervisor_dict):
args = {
'id_': hypervisor_dict.get('id'),
'hypervisor_hostname': hypervisor_dict.get('hypervisor_hostname'),
'cpu_info': hypervisor_dict.get('cpu_info'),
'free_disk_gb': hypervisor_dict.get('free_disk_gb'),
'hypervisor_version': hypervisor_dict.get('hypervisor_version'),
'disk_available_least': hypervisor_dict.get(
'disk_available_least'),
'local_gb': hypervisor_dict.get('local_gb'),
'free_ram_mb': hypervisor_dict.get('free_ram_mb'),
'vcpus_used': hypervisor_dict.get('vcpus_used'),
'hypervisor_type': hypervisor_dict.get('hypervisor_type'),
'local_gb_used': hypervisor_dict.get('local_gb_used'),
'memory_mb_used': hypervisor_dict.get('memory_mb_used'),
'memory_mb': hypervisor_dict.get('memory_mb'),
'current_workload': hypervisor_dict.get('current_workload'),
'vcpus': hypervisor_dict.get('vcpus'),
'running_vms': hypervisor_dict.get('running_vms'),
'service': Service._dict_to_obj(hypervisor_dict.get('service'))
}
return Hypervisor(**args)
@classmethod
def _int_to_version_str(cls, version_int):
"""
@param version_int: The version in munge form of integer
ex: 6000000
@type version_int: Integer
@return: dot seperated version string
ex: '6.0.0'
"""
if isinstance(version_int, str):
version_int = int(version_int)
major = version_int // 1000000
minor = (version_int - major * 1000000) // 1000
patch = (version_int - major * 1000000 - minor * 1000)
return '{0}.{1}.{2}'.format(major, minor, patch)
class Hypervisors(AutoMarshallingListModel):
@classmethod
def _json_to_obj(cls, serialized_str):
"""
@summary: Returns an instance of a Hypervisor
based on the json serialized_str
passed in.
@param serialized_str: JSON serialized string
@type serialized_str: String
@return: List of Hypervisor
@rtype: List
"""
json_dict = json.loads(serialized_str)
hypervisors = Hypervisors()
for hypervisor_dict in json_dict.get('hypervisors'):
hypervisors.append(Hypervisor._dict_to_obj(hypervisor_dict))
ret = hypervisors
return ret
@classmethod
def _xml_to_obj(cls, serialized_str):
"""
@summary: Returns an instance of a Hypervisor
based on the xml serialized_str
passed in.
@param serialized_str: XML serialized string
@type serialized_str: String
@return: List of Hypervisors
@rtype: List
"""
element = ET.fromstring(serialized_str)
hypervisors = Hypervisors()
for hypervisor_ele in element.findall('hypervisor'):
hypervisor_dict = hypervisor_ele.attrib
hypervisor_dict['service'] = hypervisor_ele.find('service').attrib
hyp_obj = Hypervisor._dict_to_obj(hypervisor_dict)
hypervisors.append(hyp_obj)
return hypervisors
class HypervisorMin(AutoMarshallingModel):
def __init__(self, id=None, hypervisor_hostname=None,
servers=None):
super(HypervisorMin, self).__init__()
self.id = int(id)
self.hypervisor_hostname = hypervisor_hostname
self.servers = servers
def __eq__(self, other):
"""
@summary: Overrides the default equals
@param other: HypervisorMin object to compare with
@type other: HypervisorMin
@return: True if HypervisorMin objects are equal, False otherwise
@rtype: bool
"""
return EqualityTools.are_objects_equal(self, other)
def __ne__(self, other):
"""
@summary: Overrides the default not-equals
@param other: HypervisorMin object to compare with
@type other: HypervisorMin
@return: True if HypervisorMin objects are not equal, False otherwise
@rtype: bool
"""
return not self.__eq__(other)
class HypervisorsMin(AutoMarshallingListModel):
@classmethod
def _json_to_obj(cls, serialized_str):
"""
@summary: Returns a list of a HypervisorMin
based on the json serialized_str
passed in.
@param serialized_str: JSON serialized string
@type serialized_str: String
@return: List of HypervisorMin
@rtype: List
"""
json_dict = json.loads(serialized_str)
hypervisors = HypervisorsMin()
for hypervisor_dict in json_dict['hypervisors']:
id = hypervisor_dict['id']
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
if 'servers' in hypervisor_dict.keys():
servers = []
for server_dict in hypervisor_dict['servers']:
servers.append(Server._dict_to_obj(server_dict))
hypervisors.append(HypervisorMin(id, hypervisor_hostname,
servers))
else:
servers = None
hypervisor_dict.update({"servers": servers})
hypervisors.append(HypervisorMin(id, hypervisor_hostname,
servers))
return hypervisors
@classmethod
def _xml_to_obj(cls, serialized_str):
"""
@summary: Returns a list of a HypervisorMin
based on the xml serialized_str
passed in.
@param serialized_str: XML serialized string
@type serialized_str: String
@return: List of HypervisorMin
@rtype: List
"""
element = ET.fromstring(serialized_str)
hypervisors = HypervisorsMin()
for hypervisor in element.findall('hypervisor'):
hypervisor_dict = hypervisor.attrib
id = hypervisor_dict['id']
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
if "servers" in [elem.tag for elem in hypervisor.iter()]:
servers = []
for server in hypervisor.iter('server'):
servers.append(Server._dict_to_obj(server.attrib))
hypervisors.append(HypervisorMin(id, hypervisor_hostname,
servers))
else:
servers = None
hypervisor.attrib.update({"servers": servers})
hypervisors.append(HypervisorMin(id, hypervisor_hostname,
servers))
return hypervisors

View File

@ -1,109 +0,0 @@
"""
Copyright 2013 Rackspace
Licensed 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.
"""
import json
import xml.etree.ElementTree as ET
from cafe.engine.models.base import AutoMarshallingModel
from cloudcafe.compute.common.equality_tools import EqualityTools
from cloudcafe.compute.servers_api.models.servers import Server
class Hypervisor(AutoMarshallingModel):
def __init__(self, id=None, hypervisor_hostname=None,
servers=None):
super(Hypervisor, self).__init__()
self.id = id
self.hypervisor_hostname = hypervisor_hostname
self.servers = servers
def __eq__(self, other):
"""
@summary: Overrides the default equals
@param other: Hypervisor object to compare with
@type other: Hypervisor
@return: True if Host objects are equal, False otherwise
@rtype: bool
"""
return EqualityTools.are_objects_equal(self, other)
def __ne__(self, other):
"""
@summary: Overrides the default not-equals
@param other: Hypervisor object to compare with
@type other: Hypervisor
@return: True if Hypervisor objects are not equal, False otherwise
@rtype: bool
"""
return not self.__eq__(other)
@classmethod
def _json_to_obj(cls, serialized_str):
"""
@summary: Returns an instance of a Hypervisor
based on the json serialized_str
passed in.
@param serialized_str: JSON serialized string
@type serialized_str: String
@return: List of Hypervisors
@rtype: List
"""
json_dict = json.loads(serialized_str)
hypervisors = []
for hypervisor_dict in json_dict['hypervisors']:
id = hypervisor_dict['id']
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
if 'servers' in hypervisor_dict.keys():
servers = []
for server_dict in hypervisor_dict['servers']:
servers.append(Server._dict_to_obj(server_dict))
return Hypervisor(id, hypervisor_hostname, servers)
else:
servers = None
hypervisor_dict.update({"servers": servers})
hypervisors.append(Hypervisor(id, hypervisor_hostname,
servers))
return hypervisors
@classmethod
def _xml_to_obj(cls, serialized_str):
"""
@summary: Returns an instance of a Hypervisor
based on the xml serialized_str
passed in.
@param serialized_str: XML serialized string
@type serialized_str: String
@return: List of Hypervisors
@rtype: List
"""
element = ET.fromstring(serialized_str)
hypervisors = []
for hypervisor in element.findall('hypervisor'):
hypervisor_dict = hypervisor.attrib
id = hypervisor_dict['id']
hypervisor_hostname = hypervisor_dict['hypervisor_hostname']
if "servers" in [elem.tag for elem in hypervisor.iter()]:
servers = []
for server in hypervisor.iter('server'):
servers.append(Server._dict_to_obj(server.attrib))
return Hypervisor(id, hypervisor_hostname, servers)
else:
servers = None
hypervisor.attrib.update({"servers": servers})
hypervisors.append(Hypervisor(id, hypervisor_hostname,
servers))
return hypervisors

View File

@ -52,6 +52,7 @@ class Server(AutoMarshallingModel):
self.task_state = task_state or ComputeTaskStates.NONE
self.vm_state = vm_state
self.name = name
self.hypervisor_name = hypervisor_name
self.id = id
self.tenant_id = tenant_id
self.status = status
@ -76,16 +77,24 @@ class Server(AutoMarshallingModel):
self.key_name = key_name
self.host = host
self.instance_name = instance_name
self.hypervisor_name = hypervisor_name
@classmethod
def _json_to_obj(cls, serialized_str):
"""
Returns an instance of a Server based on the json serialized_str
passed in
"""
ret = None
json_dict = json.loads(serialized_str)
ret = cls._dict_to_obj(json_dict['server'])
return ret
@classmethod
def _xml_to_obj(cls, serialized_str):
"""
Returns an instance of a Server based on the xml serialized_str
passed in
"""
element = ET.fromstring(serialized_str)
cls._remove_xml_etree_namespace(
element, Constants.XML_API_NAMESPACE)
@ -100,6 +109,7 @@ class Server(AutoMarshallingModel):
@classmethod
def _xml_ele_to_obj(cls, element):
"""Helper method to turn ElementTree instance to Server instance."""
server = element.attrib
addresses = None
@ -128,7 +138,7 @@ class Server(AutoMarshallingModel):
power_state=server.get('power_state'), progress=progress,
task_state=server.get('task_state').lower(),
vm_state=server.get('vm_state'), name=server.get('name'),
tenant_id=server.get('tenantId'), status=server.get('status'),
tenant_id=server.get('tenant_id'), status=server.get('status'),
updated=server.get('updated'), created=server.get('created'),
host_id=server.get('hostId'), user_id=server.get('userId'),
accessIPv4=server.get('accessIPv4'),
@ -144,6 +154,7 @@ class Server(AutoMarshallingModel):
@classmethod
def _dict_to_obj(cls, server_dict):
"""Helper method to turn dictionary into Server instance."""
addresses = None
flavor = None
@ -263,10 +274,37 @@ class Servers(AutoMarshallingListModel):
return servers
class ServerMins(Servers):
class ServerMins(AutoMarshallingListModel):
server_type = ServerMin
@classmethod
def _json_to_obj(cls, serialized_str):
json_dict = json.loads(serialized_str)
return cls._list_to_obj(json_dict.get('servers'))
@classmethod
def _list_to_obj(cls, server_dict_list):
servers = ServerMin()
for server_dict in server_dict_list:
server = cls.server_type._dict_to_obj(server_dict)
servers.append(server)
return servers
@classmethod
def _xml_to_obj(cls, serialized_str):
element = ET.fromstring(serialized_str)
if element.tag != 'servers':
return None
return cls._xml_list_to_obj(element.findall('server'))
@classmethod
def _xml_list_to_obj(cls, xml_list):
servers = ServerMin()
for ele in xml_list:
servers.append(cls.server_type._xml_ele_to_obj(ele))
return servers
class Addresses(AutoMarshallingModel):

View File

@ -34,3 +34,34 @@ class HypervisorsClientMockResponse(object):
'"name": "instance-00000003"},'
'{"uuid": "9327b134-b1f5-43ec-a8f1-2b6eb153c739",'
'"name": "instance-00000005"}}]}]}')
@classmethod
def list_hypervisors_in_detail(cls):
return """
{
"hypervisors":[
{
"service":
{
"host":"parentcell",
"id":7
},
"vcpus_used":2,
"hypervisor_type":"xen",
"local_gb_used":21,
"hypervisor_hostname":"hypervisor_test",
"memory_mb_used":4608,
"memory_mb":12285,
"current_workload":0,
"vcpus":0,
"cpu_info": 2,
"running_vms":2,
"free_disk_gb":888,
"hypervisor_version":6000,
"disk_available_least":"None",
"local_gb":909,
"free_ram_mb":7677,
"id":1
}
]
}"""

View File

@ -34,11 +34,13 @@ class HypervisorsClientTest(ClientTestFixture):
auth_token=cls.AUTH_TOKEN,
serialize_format=cls.FORMAT,
deserialize_format=cls.FORMAT)
cls.hypervisors_uri = "{0}/os-hypervisors".\
format(cls.COMPUTE_API_ENDPOINT)
cls.hypervisor_servers_uri = "{0}/{1}/servers".\
format(cls.hypervisors_uri,
HYPERVISOR_HOSTNAME)
cls.hypervisors_uri = ("{0}/os-hypervisors".
format(cls.COMPUTE_API_ENDPOINT))
cls.hypervisors_in_detail_uri = ("{0}/os-hypervisors/detail".
format(cls.COMPUTE_API_ENDPOINT))
cls.hypervisor_servers_uri = ("{0}/{1}/servers".
format(cls.hypervisors_uri,
HYPERVISOR_HOSTNAME))
cls.mock_response = HypervisorsClientMockResponse()
def test_list_hypervisors(self):
@ -51,6 +53,17 @@ class HypervisorsClientTest(ClientTestFixture):
self.assertEqual(HypervisorsClientMockResponse.list_hypervisors(),
response.content)
def test_list_hypervisors_in_detail(self):
HTTPretty.register_uri(HTTPretty.GET, self.hypervisors_in_detail_uri,
body=self.mock_response.
list_hypervisors_in_detail())
response = self.hypervisor_client.list_hypervisors_with_detail()
self.assertEqual(200, response.status_code)
self._assert_default_headers_in_request(HTTPretty.last_request)
self.assertEqual(
response.content,
HypervisorsClientMockResponse.list_hypervisors_in_detail())
def test_list_hypervisor_servers(self):
HTTPretty.register_uri(HTTPretty.GET, self.hypervisor_servers_uri,
body=self.mock_response.

View File

@ -15,91 +15,84 @@ limitations under the License.
"""
import unittest2 as unittest
from cloudcafe.compute.hypervisors_api.model.hypervisors import Hypervisor
from cloudcafe.compute.hypervisors_api.model.hypervisor import Hypervisors
class HypervisorDomainTest(object):
class HypervisorsDomainTest(object):
def test_hypervisor_attr(self):
self.assertEqual(self.hypervisor[0].id, "1")
self.assertEqual(self.hypervisor[0].hypervisor_hostname,
self.assertEqual(self.hypervisors[0].id_, '1')
self.assertEqual(self.hypervisors[0].hypervisor_hostname,
"hypervisor_test")
self.assertEqual(self.hypervisors[0].vcpus_used, '2')
self.assertEqual(self.hypervisors[0].hypervisor_type, 'xen')
self.assertEqual(self.hypervisors[0].local_gb_used, '21')
self.assertEqual(self.hypervisors[0].memory_mb_used, '4608')
self.assertEqual(self.hypervisors[0].memory_mb, '12285')
self.assertEqual(self.hypervisors[0].current_workload, '0')
self.assertEqual(self.hypervisors[0].vcpus, '0')
self.assertEqual(self.hypervisors[0].cpu_info, '2')
self.assertEqual(self.hypervisors[0].running_vms, '2')
self.assertEqual(self.hypervisors[0].free_disk_gb, '888')
self.assertEqual(self.hypervisors[0].hypervisor_version, '6000000')
self.assertEqual(self.hypervisors[0].disk_available_least, 'None')
self.assertEqual(self.hypervisors[0].local_gb, '909')
self.assertEqual(self.hypervisors[0].free_ram_mb, '7677')
self.assertEqual(self.hypervisors[0].service.host, "parentcell")
self.assertEqual(self.hypervisors[0].service.id_, '7')
class HypervisorDomainJSONTest(unittest.TestCase, HypervisorDomainTest):
class HypervisorsDomainJSONTest(unittest.TestCase, HypervisorsDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_json = ('{"hypervisors": [{'
'"id": "1", '
'"hypervisor_hostname": "hypervisor_test"}]}')
cls.hypervisor = Hypervisor.deserialize(cls.hypervisor_json, "json")
cls.hypervisor_json = """
{
"hypervisors":[
{
"service":
{
"host":"parentcell",
"id":"7"
},
"vcpus_used":"2",
"hypervisor_type":"xen",
"local_gb_used":"21",
"hypervisor_hostname":"hypervisor_test",
"memory_mb_used":"4608",
"memory_mb":"12285",
"current_workload":"0",
"vcpus":"0",
"cpu_info": "2",
"running_vms":"2",
"free_disk_gb":"888",
"hypervisor_version":"6000000",
"disk_available_least":"None",
"local_gb":"909",
"free_ram_mb":"7677",
"id":"1"
}
]
}"""
cls.hypervisors = Hypervisors.deserialize(cls.hypervisor_json, "json")
class HypervisorDomainXMLTest(unittest.TestCase, HypervisorDomainTest):
class HypervisorsDomainXMLTest(unittest.TestCase, HypervisorsDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_xml = ('<?xml version="1.0" encoding="UTF-8"?>'
'<hypervisors><hypervisor id="1" '
'hypervisor_hostname="hypervisor_test"/>'
'</hypervisors>')
cls.hypervisor = Hypervisor.deserialize(cls.hypervisor_xml, "xml")
class HypervisorServerCollectionDomainTest(object):
def test_hypervisor_servers_length(self):
self.assertEqual(len(self.hypervisor.servers), 2)
def test_hypervisor_servers_attr(self):
self.assertTrue(
"server_one" in
[server.name for server in self.hypervisor.servers])
self.assertTrue(
"server_two" in
[server.name for server in self.hypervisor.servers])
self.assertTrue(
"b1ea4f1b-201c-47c5-95b9-c6fe2df39af0" in
[server.id for server in self.hypervisor.servers])
self.assertTrue(
"9327b134-b1f5-43ec-a8f1-2b6eb153c739" in
[server.id for server in self.hypervisor.servers])
class ServersDomainCollectionJSONTest(unittest.TestCase,
HypervisorServerCollectionDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_json = ('{"hypervisors": [{'
'"id": 1, '
'"hypervisor_hostname": "hypervisor_test", '
'"servers": [{'
'"uuid": '
'"b1ea4f1b-201c-47c5-95b9-c6fe2df39af0", '
'"name": "server_one"}, '
'{"uuid": '
'"9327b134-b1f5-43ec-a8f1-2b6eb153c739", '
'"name": "server_two"}]}]}')
cls.hypervisor = Hypervisor.deserialize(cls.hypervisor_json, "json")
class ServersDomainCollectionXMLTest(unittest.TestCase,
HypervisorServerCollectionDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_xml = ('<?xml version="1.0" encoding="UTF-8"?>'
'<hypervisors>'
'<hypervisor '
'id="1" '
'hypervisor_hostname="hypervisor_test">'
'<servers>'
'<server name="server_one" '
'uuid="b1ea4f1b-201c-47c5-95b9-c6fe2df39af0"/>'
'<server name="server_two" '
'uuid="9327b134-b1f5-43ec-a8f1-2b6eb153c739"/>'
'</servers></hypervisor></hypervisors>')
cls.hypervisor = Hypervisor.deserialize(cls.hypervisor_xml,
"xml")
cls.hypervisor_xml = """<?xml version="1.0" encoding="UTF-8"?>
<hypervisors>
<hypervisor
vcpus_used="2" hypervisor_type="xen"
local_gb_used="21" hypervisor_hostname="hypervisor_test"
memory_mb_used="4608" memory_mb="12285"
current_workload="0" vcpus="0" cpu_info="2"
running_vms="2" free_disk_gb="888"
hypervisor_version="6000000" disk_available_least="None"
local_gb="909" free_ram_mb="7677" id="1">
<service host="parentcell" id="7" />
</hypervisor>
</hypervisors>"""
cls.hypervisors = Hypervisors.deserialize(cls.hypervisor_xml, "xml")

View File

@ -0,0 +1,102 @@
"""
Copyright 2013 Rackspace
Licensed 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.
"""
import unittest2 as unittest
from cloudcafe.compute.hypervisors_api.model.hypervisor import HypervisorsMin
class HypervisorsMinDomainTest(object):
def test_hypervisor_attr(self):
self.assertEqual(self.hypervisor[0].id, 1)
self.assertEqual(self.hypervisor[0].hypervisor_hostname,
"hypervisor_test")
class HypervisorsMinDomainJSONTest(unittest.TestCase, HypervisorsMinDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_json = ('{"hypervisors": [{'
'"id": 1, '
'"hypervisor_hostname": "hypervisor_test"}]}')
cls.hypervisor = HypervisorsMin.deserialize(cls.hypervisor_json, "json")
class HypervisorsMinDomainXMLTest(unittest.TestCase, HypervisorsMinDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_xml = ('<?xml version="1.0" encoding="UTF-8"?>'
'<hypervisors><hypervisor id="1" '
'hypervisor_hostname="hypervisor_test"/>'
'</hypervisors>')
cls.hypervisor = HypervisorsMin.deserialize(cls.hypervisor_xml, "xml")
class HypervisorServerCollectionDomainTest(object):
def test_hypervisor_servers_length(self):
self.assertEqual(len(self.hypervisors[0].servers), 2)
def test_hypervisor_servers_attr(self):
self.assertIn("server_one", [server.name for server in
self.hypervisors[0].servers])
self.assertIn("server_two", [server.name for server in
self.hypervisors[0].servers])
self.assertIn("b1ea4f1b-201c-47c5-95b9-c6fe2df39af0",
[server.id for server in self.hypervisors[0].servers])
self.assertIn("9327b134-b1f5-43ec-a8f1-2b6eb153c739",
[server.id for server in self.hypervisors[0].servers])
class ServersDomainCollectionJSONTest(unittest.TestCase,
HypervisorServerCollectionDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_json = ('{"hypervisors": [{'
'"id": 1, '
'"hypervisor_hostname": "hypervisor_test", '
'"servers": [{'
'"uuid": '
'"b1ea4f1b-201c-47c5-95b9-c6fe2df39af0", '
'"name": "server_one"}, '
'{"uuid": '
'"9327b134-b1f5-43ec-a8f1-2b6eb153c739", '
'"name": "server_two"}]}]}')
cls.hypervisors = HypervisorsMin.deserialize(cls.hypervisor_json,
"json")
class ServersDomainCollectionXMLTest(unittest.TestCase,
HypervisorServerCollectionDomainTest):
@classmethod
def setUp(cls):
cls.hypervisor_xml = ('<?xml version="1.0" encoding="UTF-8"?>'
'<hypervisors>'
'<hypervisor '
'id="1" '
'hypervisor_hostname="hypervisor_test">'
'<servers>'
'<server name="server_one" '
'uuid="b1ea4f1b-201c-47c5-95b9-c6fe2df39af0"/>'
'<server name="server_two" '
'uuid="9327b134-b1f5-43ec-a8f1-2b6eb153c739"/>'
'</servers></hypervisor></hypervisors>')
cls.hypervisors = HypervisorsMin.deserialize(cls.hypervisor_xml,
"xml")

View File

@ -127,6 +127,7 @@ class ServerXMLDomainTest(unittest.TestCase, ServerDomainTest):
xmlns="{docs_url}/compute/api/v1.1"
status="ACTIVE" updated="2012-12-03T19:04:06Z"
hostId="123"
tenant_id="660"
name="testserver47476" created="2012-12-03T18:59:16Z"
userId="199835" tenantId="660" accessIPv4="192.168.1.10"
accessIPv6="2001:11:7811:69:cf10:c02d:ff10:fa"