Skip to content

Commit

Permalink
Fix os-hosts extension can't return xml response correctly.
Browse files Browse the repository at this point in the history
Also add os-hosts integrated testing template files.
fix bug 1075575
fix bug 1070167.

DocImpact

Change-Id: I6b9f9e58e7cbe10a931d2c83d9baf9dcee74de08
  • Loading branch information
yaguangtang committed Nov 19, 2012
1 parent 6c5bbd0 commit a7e5180
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 11 deletions.
31 changes: 31 additions & 0 deletions doc/api_samples/os-hosts/host-get-resp.json
@@ -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)"
}
}
]
}
24 changes: 24 additions & 0 deletions doc/api_samples/os-hosts/host-get-resp.xml
@@ -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>
24 changes: 24 additions & 0 deletions doc/api_samples/os-hosts/hosts-list-resp.json
@@ -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"
}
]
}
7 changes: 7 additions & 0 deletions doc/api_samples/os-hosts/hosts-list-resp.xml
@@ -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>
6 changes: 1 addition & 5 deletions nova/api/openstack/compute/contrib/hosts.py
Expand Up @@ -34,11 +34,7 @@

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')
Expand Down
13 changes: 7 additions & 6 deletions nova/tests/api/openstack/compute/contrib/test_hosts.py
Expand Up @@ -26,11 +26,12 @@
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"}]
Expand Down Expand Up @@ -130,10 +131,10 @@ def _test_host_update(self, host, key, val, expected_value):
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)

Expand Down Expand Up @@ -281,12 +282,12 @@ def test_index_serializer(self):
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):
Expand Down
31 changes: 31 additions & 0 deletions nova/tests/integrated/api_samples/os-hosts/host-get-resp.json.tpl
@@ -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)"
}
}
]
}
24 changes: 24 additions & 0 deletions nova/tests/integrated/api_samples/os-hosts/host-get-resp.xml.tpl
@@ -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>
@@ -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"
}
]
}
@@ -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>
19 changes: 19 additions & 0 deletions nova/tests/integrated/test_api_samples.py
Expand Up @@ -244,6 +244,7 @@ def _get_regexes(self):
# '[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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a7e5180

Please sign in to comment.