Fix os-hosts extension can't return xml response correctly.

Also add os-hosts integrated testing template files.
fix bug 1075575
fix bug 1070167.

DocImpact

Change-Id: I6b9f9e58e7cbe10a931d2c83d9baf9dcee74de08
This commit is contained in:
Yaguang Tang 2012-11-09 01:43:05 +08:00
parent 6c5bbd0eaa
commit a7e5180a04
11 changed files with 199 additions and 11 deletions

View File

@ -0,0 +1,31 @@
{
"host": [
{
"resource": {
"cpu": 1,
"disk_gb": 1028,
"host": "5ca60c6792a1442f9471ff575443f94d",
"memory_mb": 8192,
"project": "(total)"
}
},
{
"resource": {
"cpu": 0,
"disk_gb": 0,
"host": "5ca60c6792a1442f9471ff575443f94d",
"memory_mb": 512,
"project": "(used_now)"
}
},
{
"resource": {
"cpu": 0,
"disk_gb": 0,
"host": "5ca60c6792a1442f9471ff575443f94d",
"memory_mb": 0,
"project": "(used_max)"
}
}
]
}

View File

@ -0,0 +1,24 @@
<?xml version='1.0' encoding='UTF-8'?>
<host>
<resource>
<project>(total)</project>
<memory_mb>8192</memory_mb>
<host>ecf3458ac6bf4a299cc2e0efa740f426</host>
<cpu>1</cpu>
<disk_gb>1028</disk_gb>
</resource>
<resource>
<project>(used_now)</project>
<memory_mb>512</memory_mb>
<host>ecf3458ac6bf4a299cc2e0efa740f426</host>
<cpu>0</cpu>
<disk_gb>0</disk_gb>
</resource>
<resource>
<project>(used_max)</project>
<memory_mb>0</memory_mb>
<host>ecf3458ac6bf4a299cc2e0efa740f426</host>
<cpu>0</cpu>
<disk_gb>0</disk_gb>
</resource>
</host>

View File

@ -0,0 +1,24 @@
{
"hosts": [
{
"host_name": "c5a4acad61bc463ab5f60d164d942516",
"service": "compute",
"zone": "nova"
},
{
"host_name": "9112426abe5f4934a81b494fcdee8c5b",
"service": "cert",
"zone": "nova"
},
{
"host_name": "d483b9e6124d47f2a6ad14e57716a778",
"service": "network",
"zone": "nova"
},
{
"host_name": "3f9c73ba6d634092a70d6640f53930c8",
"service": "scheduler",
"zone": "nova"
}
]
}

View File

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<hosts>
<host host_name="e5e145b49d244d97982c922e101859fd" service="compute"/>
<host host_name="6d85457c4d884047b41999540c2bf454" service="cert"/>
<host host_name="1f9a3cef66bd450aa004655ef239ddee" service="network"/>
<host host_name="c6fd14f8e3be44628e76ae4a6e88cffb" service="scheduler"/>
</hosts>

View File

@ -34,11 +34,7 @@ authorize = extensions.extension_authorizer('compute', 'hosts')
class HostIndexTemplate(xmlutil.TemplateBuilder):
def construct(self):
def shimmer(obj, do_raise=False):
# A bare list is passed in; we need to wrap it in a dict
return dict(hosts=obj)
root = xmlutil.TemplateElement('hosts', selector=shimmer)
root = xmlutil.TemplateElement('hosts')
elem = xmlutil.SubTemplateElement(root, 'host', selector='hosts')
elem.set('host_name')
elem.set('service')

View File

@ -26,11 +26,12 @@ from nova.openstack.common import log as logging
from nova import test
LOG = logging.getLogger(__name__)
HOST_LIST = [
HOST_LIST = {"hosts": [
{"host_name": "host_c1", "service": "compute", "zone": "nova"},
{"host_name": "host_c2", "service": "compute", "zone": "nonova"},
{"host_name": "host_v1", "service": "volume", "zone": "nova"},
{"host_name": "host_v2", "service": "volume", "zone": "nonova"}]
}
HOST_LIST_NOVA_ZONE = [
{"host_name": "host_c1", "service": "compute", "zone": "nova"},
{"host_name": "host_v1", "service": "volume", "zone": "nova"}]
@ -130,10 +131,10 @@ class HostTestCase(test.TestCase):
def test_list_hosts(self):
"""Verify that the compute hosts are returned."""
hosts = os_hosts._list_hosts(self.req)
self.assertEqual(hosts, HOST_LIST)
self.assertEqual(hosts, HOST_LIST['hosts'])
compute_hosts = os_hosts._list_hosts(self.req, "compute")
expected = [host for host in HOST_LIST
expected = [host for host in HOST_LIST['hosts']
if host["service"] == "compute"]
self.assertEqual(compute_hosts, expected)
@ -281,12 +282,12 @@ class HostSerializerTest(test.TestCase):
tree = etree.fromstring(text)
self.assertEqual('hosts', tree.tag)
self.assertEqual(len(HOST_LIST), len(tree))
self.assertEqual(len(HOST_LIST['hosts']), len(tree))
for i in range(len(HOST_LIST)):
self.assertEqual('host', tree[i].tag)
self.assertEqual(HOST_LIST[i]['host_name'],
self.assertEqual(HOST_LIST['hosts'][i]['host_name'],
tree[i].get('host_name'))
self.assertEqual(HOST_LIST[i]['service'],
self.assertEqual(HOST_LIST['hosts'][i]['service'],
tree[i].get('service'))
def test_update_serializer_with_status(self):

View File

@ -0,0 +1,31 @@
{
"host": [
{
"resource": {
"cpu": 1,
"disk_gb": 1028,
"host": "%(host_name)s",
"memory_mb": 8192,
"project": "(total)"
}
},
{
"resource": {
"cpu": 0,
"disk_gb": 0,
"host": "%(host_name)s",
"memory_mb": 512,
"project": "(used_now)"
}
},
{
"resource": {
"cpu": 0,
"disk_gb": 0,
"host": "%(host_name)s",
"memory_mb": 0,
"project": "(used_max)"
}
}
]
}

View File

@ -0,0 +1,24 @@
<?xml version='1.0' encoding='UTF-8'?>
<host>
<resource>
<project>(total)</project>
<memory_mb>8192</memory_mb>
<host>%(host_name)s</host>
<cpu>1</cpu>
<disk_gb>1028</disk_gb>
</resource>
<resource>
<project>(used_now)</project>
<memory_mb>512</memory_mb>
<host>%(host_name)s</host>
<cpu>0</cpu>
<disk_gb>0</disk_gb>
</resource>
<resource>
<project>(used_max)</project>
<memory_mb>0</memory_mb>
<host>%(host_name)s</host>
<cpu>0</cpu>
<disk_gb>0</disk_gb>
</resource>
</host>

View File

@ -0,0 +1,24 @@
{
"hosts": [
{
"host_name": "%(host_name)s",
"service": "compute",
"zone": "nova"
},
{
"host_name": "%(host_name)s",
"service": "cert",
"zone": "nova"
},
{
"host_name": "%(host_name)s",
"service": "network",
"zone": "nova"
},
{
"host_name": "%(host_name)s",
"service": "scheduler",
"zone": "nova"
}
]
}

View File

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<hosts>
<host host_name="%(host_name)s" service="compute"/>
<host host_name="%(host_name)s" service="cert"/>
<host host_name="%(host_name)s" service="network"/>
<host host_name="%(host_name)s" service="scheduler"/>
</hosts>

View File

@ -244,6 +244,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
# '[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:'
# '[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}',
'host': self._get_host(),
'host_name': '[0-9a-z]{32}',
'glance_host': self._get_glance_host(),
'compute_host': self.compute.host,
'text': text,
@ -456,6 +457,24 @@ class FlavorsSampleXmlTest(FlavorsSampleJsonTest):
ctype = 'xml'
class HostsSampleJsonTest(ApiSampleTestBase):
extension_name = "nova.api.openstack.compute.contrib.hosts.Hosts"
def test_host_get(self):
response = self._do_get('os-hosts/%s' % self.compute.host)
subs = self._get_regexes()
return self._verify_response('host-get-resp', subs, response)
def test_hosts_list(self):
response = self._do_get('os-hosts')
subs = self._get_regexes()
return self._verify_response('hosts-list-resp', subs, response)
class HostsSampleXmlTest(HostsSampleJsonTest):
ctype = 'xml'
class FlavorsSampleAllExtensionJsonTest(FlavorsSampleJsonTest):
all_extensions = True