Updated FlavorsXMLSerialization tests to use etree and validation instead of minidom

This commit is contained in:
Alex Meade
2011-08-24 14:08:25 -04:00
parent bde2e87603
commit 026efcd174
4 changed files with 99 additions and 93 deletions

View File

@@ -0,0 +1,10 @@
<element name="flavor" ns="http://docs.openstack.org/compute/api/v1.1"
xmlns="http://relaxng.org/ns/structure/1.0">
<attribute name="name"> <text/> </attribute>
<attribute name="id"> <text/> </attribute>
<attribute name="ram"> <text/> </attribute>
<attribute name="disk"> <text/> </attribute>
<zeroOrMore>
<externalRef href="../atom-link.rng"/>
</zeroOrMore>
</element>

View File

@@ -0,0 +1,6 @@
<element name="flavors" xmlns="http://relaxng.org/ns/structure/1.0"
ns="http://docs.openstack.org/compute/api/v1.1">
<zeroOrMore>
<externalRef href="flavor.rng"/>
</zeroOrMore>
</element>

View File

@@ -0,0 +1,12 @@
<element name="flavors" ns="http://docs.openstack.org/compute/api/v1.1"
xmlns="http://relaxng.org/ns/structure/1.0">
<zeroOrMore>
<element name="flavor">
<attribute name="name"> <text/> </attribute>
<attribute name="id"> <text/> </attribute>
<zeroOrMore>
<externalRef href="../atom-link.rng"/>
</zeroOrMore>
</element>
</zeroOrMore>
</element>

View File

@@ -17,16 +17,21 @@
import json
import webob
import xml.dom.minidom as minidom
from lxml import etree
from nova.api.openstack import flavors
import nova.db.api
from nova import exception
from nova import test
from nova.api.openstack import xmlutil
from nova.tests.api.openstack import fakes
from nova import wsgi
NS = "{http://docs.openstack.org/compute/api/v1.1}"
ATOMNS = "{http://www.w3.org/2005/Atom}"
def stub_flavor(flavorid, name, memory_mb="256", local_gb="10"):
return {
"flavorid": str(flavorid),
@@ -265,7 +270,7 @@ class FlavorsXMLSerializationTest(test.TestCase):
def test_show(self):
serializer = flavors.FlavorXMLSerializer()
input = {
fixture = {
"flavor": {
"id": "12",
"name": "asdf",
@@ -284,29 +289,25 @@ class FlavorsXMLSerializationTest(test.TestCase):
},
}
output = serializer.serialize(input, 'show')
actual = minidom.parseString(output.replace(" ", ""))
output = serializer.serialize(fixture, 'show')
print output
root = etree.XML(output)
xmlutil.validate_schema(root, 'flavor')
flavor_dict = fixture['flavor']
expected = minidom.parseString("""
<flavor xmlns="http://docs.openstack.org/compute/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom"
id="12"
name="asdf"
ram="256"
disk="10">
<atom:link href="http://localhost/v1.1/fake/flavors/12"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/12"
rel="bookmark"/>
</flavor>
""".replace(" ", ""))
for key in ['name', 'id', 'ram', 'disk']:
self.assertEqual(root.get(key), str(flavor_dict[key]))
self.assertEqual(expected.toxml(), actual.toxml())
link_nodes = root.findall('{0}link'.format(ATOMNS))
self.assertEqual(len(link_nodes), 2)
for i, link in enumerate(flavor_dict['links']):
for key, value in link.items():
self.assertEqual(link_nodes[i].get(key), value)
def test_show_handles_integers(self):
serializer = flavors.FlavorXMLSerializer()
input = {
fixture = {
"flavor": {
"id": 12,
"name": "asdf",
@@ -325,29 +326,25 @@ class FlavorsXMLSerializationTest(test.TestCase):
},
}
output = serializer.serialize(input, 'show')
actual = minidom.parseString(output.replace(" ", ""))
output = serializer.serialize(fixture, 'show')
print output
root = etree.XML(output)
xmlutil.validate_schema(root, 'flavor')
flavor_dict = fixture['flavor']
expected = minidom.parseString("""
<flavor xmlns="http://docs.openstack.org/compute/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom"
id="12"
name="asdf"
ram="256"
disk="10">
<atom:link href="http://localhost/v1.1/fake/flavors/12"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/12"
rel="bookmark"/>
</flavor>
""".replace(" ", ""))
for key in ['name', 'id', 'ram', 'disk']:
self.assertEqual(root.get(key), str(flavor_dict[key]))
self.assertEqual(expected.toxml(), actual.toxml())
link_nodes = root.findall('{0}link'.format(ATOMNS))
self.assertEqual(len(link_nodes), 2)
for i, link in enumerate(flavor_dict['links']):
for key, value in link.items():
self.assertEqual(link_nodes[i].get(key), value)
def test_detail(self):
serializer = flavors.FlavorXMLSerializer()
input = {
fixture = {
"flavors": [
{
"id": "23",
@@ -383,39 +380,28 @@ class FlavorsXMLSerializationTest(test.TestCase):
],
}
output = serializer.serialize(input, 'detail')
actual = minidom.parseString(output.replace(" ", ""))
output = serializer.serialize(fixture, 'detail')
print output
root = etree.XML(output)
xmlutil.validate_schema(root, 'flavors')
flavor_elems = root.findall('{0}flavor'.format(NS))
self.assertEqual(len(flavor_elems), 2)
for i, flavor_elem in enumerate(flavor_elems):
flavor_dict = fixture['flavors'][i]
expected = minidom.parseString("""
<flavors xmlns="http://docs.openstack.org/compute/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom">
<flavor id="23"
name="flavor 23"
ram="512"
disk="20">
<atom:link href="http://localhost/v1.1/fake/flavors/23"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/23"
rel="bookmark"/>
</flavor>
<flavor id="13"
name="flavor 13"
ram="256"
disk="10">
<atom:link href="http://localhost/v1.1/fake/flavors/13"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/13"
rel="bookmark"/>
</flavor>
</flavors>
""".replace(" ", "") % locals())
for key in ['name', 'id', 'ram', 'disk']:
self.assertEqual(flavor_elem.get(key), str(flavor_dict[key]))
self.assertEqual(expected.toxml(), actual.toxml())
link_nodes = flavor_elem.findall('{0}link'.format(ATOMNS))
self.assertEqual(len(link_nodes), 2)
for i, link in enumerate(flavor_dict['links']):
for key, value in link.items():
self.assertEqual(link_nodes[i].get(key), value)
def test_index(self):
serializer = flavors.FlavorXMLSerializer()
input = {
fixture = {
"flavors": [
{
"id": "23",
@@ -451,42 +437,34 @@ class FlavorsXMLSerializationTest(test.TestCase):
],
}
output = serializer.serialize(input, 'index')
actual = minidom.parseString(output.replace(" ", ""))
output = serializer.serialize(fixture, 'index')
print output
root = etree.XML(output)
xmlutil.validate_schema(root, 'flavors_index')
flavor_elems = root.findall('{0}flavor'.format(NS))
self.assertEqual(len(flavor_elems), 2)
for i, flavor_elem in enumerate(flavor_elems):
flavor_dict = fixture['flavors'][i]
expected = minidom.parseString("""
<flavors xmlns="http://docs.openstack.org/compute/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom">
<flavor id="23" name="flavor 23">
<atom:link href="http://localhost/v1.1/fake/flavors/23"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/23"
rel="bookmark"/>
</flavor>
<flavor id="13" name="flavor 13">
<atom:link href="http://localhost/v1.1/fake/flavors/13"
rel="self"/>
<atom:link href="http://localhost/fake/flavors/13"
rel="bookmark"/>
</flavor>
</flavors>
""".replace(" ", "") % locals())
for key in ['name', 'id']:
self.assertEqual(flavor_elem.get(key), str(flavor_dict[key]))
self.assertEqual(expected.toxml(), actual.toxml())
link_nodes = flavor_elem.findall('{0}link'.format(ATOMNS))
self.assertEqual(len(link_nodes), 2)
for i, link in enumerate(flavor_dict['links']):
for key, value in link.items():
self.assertEqual(link_nodes[i].get(key), value)
def test_index_empty(self):
serializer = flavors.FlavorXMLSerializer()
input = {
fixture = {
"flavors": [],
}
output = serializer.serialize(input, 'index')
actual = minidom.parseString(output.replace(" ", ""))
expected = minidom.parseString("""
<flavors xmlns="http://docs.openstack.org/compute/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom" />
""".replace(" ", "") % locals())
self.assertEqual(expected.toxml(), actual.toxml())
output = serializer.serialize(fixture, 'index')
print output
root = etree.XML(output)
xmlutil.validate_schema(root, 'flavors_index')
flavor_elems = root.findall('{0}flavor'.format(NS))
self.assertEqual(len(flavor_elems), 0)