From 026efcd174cdb1b1d0fece9611dbae358de48387 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Wed, 24 Aug 2011 14:08:25 -0400 Subject: Updated FlavorsXMLSerialization tests to use etree and validation instead of minidom --- nova/tests/api/openstack/test_flavors.py | 180 ++++++++++++++----------------- 1 file changed, 79 insertions(+), 101 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index 812bece42..ea5038b39 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -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(" ", "")) - - expected = minidom.parseString(""" - - - - - """.replace(" ", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + output = serializer.serialize(fixture, 'show') + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'flavor') + flavor_dict = fixture['flavor'] + + for key in ['name', 'id', 'ram', 'disk']: + self.assertEqual(root.get(key), str(flavor_dict[key])) + + 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(" ", "")) - - expected = minidom.parseString(""" - - - - - """.replace(" ", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + output = serializer.serialize(fixture, 'show') + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'flavor') + flavor_dict = fixture['flavor'] + + for key in ['name', 'id', 'ram', 'disk']: + self.assertEqual(root.get(key), str(flavor_dict[key])) + + 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(" ", "")) - - expected = minidom.parseString(""" - - - - - - - - - - - """.replace(" ", "") % locals()) - - self.assertEqual(expected.toxml(), actual.toxml()) + 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] + + for key in ['name', 'id', 'ram', 'disk']: + self.assertEqual(flavor_elem.get(key), str(flavor_dict[key])) + + 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(" ", "")) - - expected = minidom.parseString(""" - - - - - - - - - - - """.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), 2) + for i, flavor_elem in enumerate(flavor_elems): + flavor_dict = fixture['flavors'][i] + + for key in ['name', 'id']: + self.assertEqual(flavor_elem.get(key), str(flavor_dict[key])) + + 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(""" - - """.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) -- cgit From 08981ee7228aa0e4b68ec8e9016ef68b987a3ac3 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Wed, 24 Aug 2011 16:41:26 -0400 Subject: Updated ImageXMLSerialization tests to use etree instead of minidom Fixed incorrect server entity ids in tests --- nova/tests/api/openstack/test_images.py | 347 +++++++++++++++----------------- 1 file changed, 162 insertions(+), 185 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index 2a7cfc382..145cf527c 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -23,6 +23,7 @@ and as a WSGI layer import copy import json import os +from lxml import etree import shutil import tempfile import xml.dom.minidom as minidom @@ -38,9 +39,14 @@ from nova import test from nova import utils import nova.api.openstack from nova.api.openstack import images +from nova.api.openstack import xmlutil from nova.tests.api.openstack import fakes +NS = "{http://docs.openstack.org/compute/api/v1.1}" +ATOMNS = "{http://www.w3.org/2005/Atom}" + + class _BaseImageServiceTests(test.TestCase): """Tasks to test for all image services""" @@ -1144,7 +1150,7 @@ class ImageXMLSerializationTest(test.TestCase): 'status': 'ACTIVE', 'progress': 80, 'server': { - 'id': 1, + 'id': '1', 'links': [ { 'href': self.SERVER_HREF, @@ -1173,37 +1179,35 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected_server_href = self.SERVER_HREF - expected_server_bookmark = self.SERVER_BOOKMARK - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - - - - - value1 - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'image') + image_dict = fixture['image'] + + for key in ['name', 'id', 'updated', 'created', 'status', 'progress']: + self.assertEqual(root.get(key), str(image_dict[key])) + + link_nodes = root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) + + metadata_root = root.find('{0}metadata'.format(NS)) + metadata_elems = metadata_root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 1) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = image_dict['metadata'].items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) + + server_root = root.find('{0}server'.format(NS)) + self.assertEqual(server_root.get('id'), image_dict['server']['id']) + link_nodes = server_root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['server']['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) def test_show_zero_metadata(self): serializer = images.ImageXMLSerializer() @@ -1216,7 +1220,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': 1, + 'id': '1', 'links': [ { 'href': self.SERVER_HREF, @@ -1243,31 +1247,31 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected_server_href = self.SERVER_HREF - expected_server_bookmark = self.SERVER_BOOKMARK - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'image') + image_dict = fixture['image'] + + for key in ['name', 'id', 'updated', 'created', 'status']: + self.assertEqual(root.get(key), str(image_dict[key])) + + link_nodes = root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) + + metadata_root = root.find('{0}metadata'.format(NS)) + meta_nodes = root.findall('{0}meta'.format(ATOMNS)) + self.assertEqual(len(meta_nodes), 0) + + server_root = root.find('{0}server'.format(NS)) + self.assertEqual(server_root.get('id'), image_dict['server']['id']) + link_nodes = server_root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['server']['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) def test_show_image_no_metadata_key(self): serializer = images.ImageXMLSerializer() @@ -1280,7 +1284,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': 1, + 'id': '1', 'links': [ { 'href': self.SERVER_HREF, @@ -1306,31 +1310,31 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected_server_href = self.SERVER_HREF - expected_server_bookmark = self.SERVER_BOOKMARK - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'image') + image_dict = fixture['image'] + + for key in ['name', 'id', 'updated', 'created', 'status']: + self.assertEqual(root.get(key), str(image_dict[key])) + + link_nodes = root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) + + metadata_root = root.find('{0}metadata'.format(NS)) + meta_nodes = root.findall('{0}meta'.format(ATOMNS)) + self.assertEqual(len(meta_nodes), 0) + + server_root = root.find('{0}server'.format(NS)) + self.assertEqual(server_root.get('id'), image_dict['server']['id']) + link_nodes = server_root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['server']['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) def test_show_no_server(self): serializer = images.ImageXMLSerializer() @@ -1359,30 +1363,30 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - value1 - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'image') + image_dict = fixture['image'] + + for key in ['name', 'id', 'updated', 'created', 'status']: + self.assertEqual(root.get(key), str(image_dict[key])) + + link_nodes = root.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) + + metadata_root = root.find('{0}metadata'.format(NS)) + metadata_elems = metadata_root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 1) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = image_dict['metadata'].items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) + + server_root = root.find('{0}server'.format(NS)) + self.assertEqual(server_root, None) def test_index(self): serializer = images.ImageXMLSerializer() @@ -1397,6 +1401,10 @@ class ImageXMLSerializationTest(test.TestCase): 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, + { + 'href': self.IMAGE_BOOKMARK % 1, + 'rel': 'bookmark', + }, ], }, { @@ -1407,35 +1415,32 @@ class ImageXMLSerializationTest(test.TestCase): 'href': self.IMAGE_HREF % 2, 'rel': 'self', }, + { + 'href': self.IMAGE_BOOKMARK % 2, + 'rel': 'bookmark', + }, ], }, ] } output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected_server_href = self.SERVER_HREF - expected_server_bookmark = self.SERVER_BOOKMARK - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_href_two = self.IMAGE_HREF % 2 - expected_bookmark_two = self.IMAGE_BOOKMARK % 2 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'images_index') + image_elems = root.findall('{0}image'.format(NS)) + self.assertEqual(len(image_elems), 2) + for i, image_elem in enumerate(image_elems): + image_dict = fixture['images'][i] + + for key in ['name', 'id']: + self.assertEqual(image_elem.get(key), str(image_dict[key])) + + link_nodes = image_elem.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) def test_index_zero_images(self): serializer = images.ImageXMLSerializer() @@ -1445,15 +1450,11 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixtures, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'images_index') + image_elems = root.findall('{0}image'.format(NS)) + self.assertEqual(len(image_elems), 0) def test_detail(self): serializer = images.ImageXMLSerializer() @@ -1467,7 +1468,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': 1, + 'id': '1', 'links': [ { 'href': self.SERVER_HREF, @@ -1491,7 +1492,7 @@ class ImageXMLSerializationTest(test.TestCase): ], }, { - 'id': 2, + 'id': '2', 'name': 'Image2', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, @@ -1515,46 +1516,22 @@ class ImageXMLSerializationTest(test.TestCase): } output = serializer.serialize(fixture, 'detail') - actual = minidom.parseString(output.replace(" ", "")) - - expected_server_href = self.SERVER_HREF - expected_server_bookmark = self.SERVER_BOOKMARK - expected_href = self.IMAGE_HREF % 1 - expected_bookmark = self.IMAGE_BOOKMARK % 1 - expected_href_two = self.IMAGE_HREF % 2 - expected_bookmark_two = self.IMAGE_BOOKMARK % 2 - expected_now = self.TIMESTAMP - expected = minidom.parseString(""" - - - - - - - - - - - - - value1 - - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'images') + image_elems = root.findall('{0}image'.format(NS)) + self.assertEqual(len(image_elems), 2) + for i, image_elem in enumerate(image_elems): + image_dict = fixture['images'][i] + + for key in ['name', 'id', 'updated', 'created', 'status']: + self.assertEqual(image_elem.get(key), str(image_dict[key])) + + link_nodes = image_elem.findall('{0}link'.format(ATOMNS)) + self.assertEqual(len(link_nodes), 2) + for i, link in enumerate(image_dict['links']): + for key, value in link.items(): + self.assertEqual(link_nodes[i].get(key), value) + + metadata_root = image_elem.find('{0}metadata'.format(NS)) + metadata_elems = metadata_root.findall('{0}meta'.format(NS)) -- cgit From 48b2de98d002a3f7dac03d29696e4c37ed4f983f Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 12:05:53 -0400 Subject: Added schemas Updated metadata tests to use etree instead of minidom --- nova/tests/api/openstack/test_common.py | 122 ++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 54 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_common.py b/nova/tests/api/openstack/test_common.py index b422bc4d1..2e5bf08fa 100644 --- a/nova/tests/api/openstack/test_common.py +++ b/nova/tests/api/openstack/test_common.py @@ -19,6 +19,7 @@ Test suites for 'common' code used throughout the OpenStack HTTP API. """ +from lxml import etree import webob.exc import xml.dom.minidom as minidom @@ -26,6 +27,11 @@ from webob import Request from nova import test from nova.api.openstack import common +from nova.api.openstack import xmlutil + + +NS = "{http://docs.openstack.org/compute/api/v1.1}" +ATOMNS = "{http://www.w3.org/2005/Atom}" class LimiterTest(test.TestCase): @@ -323,16 +329,16 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - four - two - - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'metadata') + metadata_dict = fixture['metadata'] + metadata_elems = root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 2) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = metadata_dict.items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) def test_index_null(self): serializer = common.MetadataXMLSerializer() @@ -342,15 +348,16 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - None - - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'metadata') + metadata_dict = fixture['metadata'] + metadata_elems = root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 1) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = metadata_dict.items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) def test_index_unicode(self): serializer = common.MetadataXMLSerializer() @@ -360,15 +367,16 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(u""" - - Jos\xe9 - - """.encode("UTF-8").replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'metadata') + metadata_dict = fixture['metadata'] + metadata_elems = root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 1) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = metadata_dict.items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(metadata_elem.text.strip(), meta_value) def test_show(self): serializer = common.MetadataXMLSerializer() @@ -378,14 +386,12 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - two - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + meta_dict = fixture['meta'] + (meta_key, meta_value) = meta_dict.items()[0] + self.assertEqual(str(root.get('key')), str(meta_key)) + self.assertEqual(root.text.strip(), meta_value) def test_update_all(self): serializer = common.MetadataXMLSerializer() @@ -396,16 +402,16 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'update_all') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - value6 - value4 - - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'metadata') + metadata_dict = fixture['metadata'] + metadata_elems = root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 2) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = metadata_dict.items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) def test_update_item(self): serializer = common.MetadataXMLSerializer() @@ -415,14 +421,12 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'update') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - two - """.replace(" ", "").replace("\n", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + meta_dict = fixture['meta'] + (meta_key, meta_value) = meta_dict.items()[0] + self.assertEqual(str(root.get('key')), str(meta_key)) + self.assertEqual(root.text.strip(), meta_value) def test_create(self): serializer = common.MetadataXMLSerializer() @@ -434,6 +438,16 @@ class MetadataXMLSerializationTest(test.TestCase): }, } output = serializer.serialize(fixture, 'create') + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'metadata') + metadata_dict = fixture['metadata'] + metadata_elems = root.findall('{0}meta'.format(NS)) + self.assertEqual(len(metadata_elems), 3) + for i, metadata_elem in enumerate(metadata_elems): + (meta_key, meta_value) = metadata_dict.items()[i] + self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) + self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) actual = minidom.parseString(output.replace(" ", "")) expected = minidom.parseString(""" -- cgit From bed249069e2770d1248ab9ef89751c15be8a13d6 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 15:12:02 -0400 Subject: updated addresses xml serialization tests to use etree instead of minidom --- nova/tests/api/openstack/test_servers.py | 54 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 3559e6de5..0c816f965 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -3171,17 +3171,17 @@ class TestAddressesXMLSerialization(test.TestCase): ], } output = self.serializer.serialize(fixture, 'show') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - - - - """.replace(" ", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + network = fixture['network_2'] + self.assertEqual(str(root.get('id')), 'network_2') + ip_elems = root.findall('{0}ip'.format(NS)) + for z, ip_elem in enumerate(ip_elems): + ip = network[z] + self.assertEqual(str(ip_elem.get('version')), + str(ip['version'])) + self.assertEqual(str(ip_elem.get('addr')), + str(ip['addr'])) def test_index(self): fixture = { @@ -3197,22 +3197,22 @@ class TestAddressesXMLSerialization(test.TestCase): }, } output = self.serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - - - - - - - - - - """.replace(" ", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'addresses') + addresses_dict = fixture['addresses'] + network_elems = root.findall('{0}network'.format(NS)) + self.assertEqual(len(network_elems), 2) + for i, network_elem in enumerate(network_elems): + network = addresses_dict.items()[i] + self.assertEqual(str(network_elem.get('id')), str(network[0])) + ip_elems = network_elem.findall('{0}ip'.format(NS)) + for z, ip_elem in enumerate(ip_elems): + ip = network[1][z] + self.assertEqual(str(ip_elem.get('version')), + str(ip['version'])) + self.assertEqual(str(ip_elem.get('addr')), + str(ip['addr'])) class TestServerInstanceCreation(test.TestCase): -- cgit From b02a5e4f581590c1bf31dae1c9c2bc1e448a6106 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 16:35:04 -0400 Subject: DRYed up code by moving _to_xml into XMLDictSerializer --- nova/tests/api/openstack/test_common.py | 14 +++++ nova/tests/api/openstack/test_flavors.py | 27 ++++++++++ nova/tests/api/openstack/test_images.py | 45 ++++++++++++++++ nova/tests/api/openstack/test_servers.py | 89 ++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_common.py b/nova/tests/api/openstack/test_common.py index 2e5bf08fa..867e9d446 100644 --- a/nova/tests/api/openstack/test_common.py +++ b/nova/tests/api/openstack/test_common.py @@ -320,6 +320,20 @@ class MetadataXMLDeserializationTest(test.TestCase): class MetadataXMLSerializationTest(test.TestCase): + def test_xml_declaration(self): + serializer = common.MetadataXMLSerializer() + fixture = { + 'metadata': { + 'one': 'two', + 'three': 'four', + }, + } + + output = serializer.serialize(fixture, 'index') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_index(self): serializer = common.MetadataXMLSerializer() fixture = { diff --git a/nova/tests/api/openstack/test_flavors.py b/nova/tests/api/openstack/test_flavors.py index ea5038b39..a3c5bd107 100644 --- a/nova/tests/api/openstack/test_flavors.py +++ b/nova/tests/api/openstack/test_flavors.py @@ -267,6 +267,33 @@ class FlavorsTest(test.TestCase): class FlavorsXMLSerializationTest(test.TestCase): + def test_xml_declaration(self): + serializer = flavors.FlavorXMLSerializer() + + fixture = { + "flavor": { + "id": "12", + "name": "asdf", + "ram": "256", + "disk": "10", + "links": [ + { + "rel": "self", + "href": "http://localhost/v1.1/fake/flavors/12", + }, + { + "rel": "bookmark", + "href": "http://localhost/fake/flavors/12", + }, + ], + }, + } + + output = serializer.serialize(fixture, 'show') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_show(self): serializer = flavors.FlavorXMLSerializer() diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index 145cf527c..97e940974 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -1138,6 +1138,51 @@ class ImageXMLSerializationTest(test.TestCase): IMAGE_HREF = 'http://localhost/v1.1/fake/images/%s' IMAGE_BOOKMARK = 'http://localhost/fake/images/%s' + def test_xml_declaration(self): + serializer = images.ImageXMLSerializer() + + fixture = { + 'image': { + 'id': 1, + 'name': 'Image1', + 'created': self.TIMESTAMP, + 'updated': self.TIMESTAMP, + 'status': 'ACTIVE', + 'progress': 80, + 'server': { + 'id': '1', + 'links': [ + { + 'href': self.SERVER_HREF, + 'rel': 'self', + }, + { + 'href': self.SERVER_BOOKMARK, + 'rel': 'bookmark', + }, + ], + }, + 'metadata': { + 'key1': 'value1', + }, + 'links': [ + { + 'href': self.IMAGE_HREF % 1, + 'rel': 'self', + }, + { + 'href': self.IMAGE_BOOKMARK % 1, + 'rel': 'bookmark', + }, + ], + }, + } + + output = serializer.serialize(fixture, 'show') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_show(self): serializer = images.ImageXMLSerializer() diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index 0c816f965..d80846747 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -3163,6 +3163,18 @@ class TestAddressesXMLSerialization(test.TestCase): serializer = nova.api.openstack.ips.IPXMLSerializer() + def test_xml_declaration(self): + fixture = { + 'network_2': [ + {'addr': '192.168.0.1', 'version': 4}, + {'addr': 'fe80::beef', 'version': 6}, + ], + } + output = self.serializer.serialize(fixture, 'show') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_show(self): fixture = { 'network_2': [ @@ -3918,6 +3930,83 @@ class ServerXMLSerializationTest(test.TestCase): self.maxDiff = None test.TestCase.setUp(self) + def test_xml_declaration(self): + serializer = servers.ServerXMLSerializer() + + fixture = { + "server": { + "id": 1, + "uuid": FAKE_UUID, + 'created': self.TIMESTAMP, + 'updated': self.TIMESTAMP, + "progress": 0, + "name": "test_server", + "status": "BUILD", + "hostId": 'e4d909c290d0fb1ca068ffaddf22cbd0', + "accessIPv4": "1.2.3.4", + "accessIPv6": "fead::1234", + "image": { + "id": "5", + "links": [ + { + "rel": "bookmark", + "href": self.IMAGE_BOOKMARK, + }, + ], + }, + "flavor": { + "id": "1", + "links": [ + { + "rel": "bookmark", + "href": self.FLAVOR_BOOKMARK, + }, + ], + }, + "addresses": { + "network_one": [ + { + "version": 4, + "addr": "67.23.10.138", + }, + { + "version": 6, + "addr": "::babe:67.23.10.138", + }, + ], + "network_two": [ + { + "version": 4, + "addr": "67.23.10.139", + }, + { + "version": 6, + "addr": "::babe:67.23.10.139", + }, + ], + }, + "metadata": { + "Open": "Stack", + "Number": "1", + }, + 'links': [ + { + 'href': self.SERVER_HREF, + 'rel': 'self', + }, + { + 'href': self.SERVER_BOOKMARK, + 'rel': 'bookmark', + }, + ], + } + } + + output = serializer.serialize(fixture, 'show') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_show(self): serializer = servers.ServerXMLSerializer() -- cgit From 86029908254850e9d04fcc2399eef54e7af58193 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 16:56:21 -0400 Subject: updated tests --- nova/tests/api/openstack/test_servers.py | 46 +++----------------------------- nova/tests/integrated/test_xml.py | 7 ++--- 2 files changed, 8 insertions(+), 45 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index d80846747..87ae19f56 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -428,48 +428,10 @@ class ServersTest(test.TestCase): req = webob.Request.blank('/v1.1/fake/servers/1') req.headers['Accept'] = 'application/xml' res = req.get_response(fakes.wsgi_app()) - actual = minidom.parseString(res.body.replace(' ', '')) - expected_uuid = FAKE_UUID - expected_updated = "2010-11-11T11:00:00Z" - expected_created = "2010-10-10T12:00:00Z" - expected = minidom.parseString(""" - - - - - - - - - - - - 1 - - - - - - - - - - - - """.replace(" ", "") % (locals())) - - self.assertEqual(expected.toxml(), actual.toxml()) + output = res.body + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'server') def test_get_server_with_active_status_by_id_v1_1(self): image_bookmark = "http://localhost/fake/images/10" diff --git a/nova/tests/integrated/test_xml.py b/nova/tests/integrated/test_xml.py index 74baaacc2..6b4f4c0ec 100644 --- a/nova/tests/integrated/test_xml.py +++ b/nova/tests/integrated/test_xml.py @@ -34,8 +34,8 @@ class XmlTests(integrated_helpers._IntegratedTestBase): response = self.api.api_request('/limits', headers=headers) data = response.read() LOG.debug("data: %s" % data) - - prefix = ' Date: Thu, 25 Aug 2011 17:10:35 -0400 Subject: Fixed integrated.test_xml to be more robust --- nova/tests/integrated/test_xml.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/integrated/test_xml.py b/nova/tests/integrated/test_xml.py index 6b4f4c0ec..cf013da1d 100644 --- a/nova/tests/integrated/test_xml.py +++ b/nova/tests/integrated/test_xml.py @@ -15,6 +15,8 @@ # License for the specific language governing permissions and limitations # under the License. +from lxml import etree + from nova.log import logging from nova.tests.integrated import integrated_helpers from nova.api.openstack import common @@ -34,9 +36,8 @@ class XmlTests(integrated_helpers._IntegratedTestBase): response = self.api.api_request('/limits', headers=headers) data = response.read() LOG.debug("data: %s" % data) - declaration = "" - prefix = '%s Date: Thu, 25 Aug 2011 18:01:35 -0400 Subject: Updated limits serialization tests to use etree and added limits schema --- nova/tests/api/openstack/test_limits.py | 56 ++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 26 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_limits.py b/nova/tests/api/openstack/test_limits.py index 801b06230..a3bcecc3a 100644 --- a/nova/tests/api/openstack/test_limits.py +++ b/nova/tests/api/openstack/test_limits.py @@ -19,6 +19,7 @@ Tests dealing with HTTP rate-limiting. import httplib import json +from lxml import etree import StringIO import stubout import time @@ -29,6 +30,7 @@ from xml.dom import minidom import nova.context from nova.api.openstack import limits from nova.api.openstack import views +from nova.api.openstack import xmlutil from nova import test @@ -39,6 +41,8 @@ TEST_LIMITS = [ limits.Limit("PUT", "*", "", 10, limits.PER_MINUTE), limits.Limit("PUT", "/servers", "^/servers", 5, limits.PER_MINUTE), ] +NS = "{http://docs.openstack.org/compute/api/v1.1}" +ATOMNS = "{http://www.w3.org/2005/Atom}" class BaseLimitTestSuite(unittest.TestCase): @@ -1006,32 +1010,32 @@ class LimitsXMLSerializationTest(test.TestCase): "maxPersonalitySize": 10240}}} output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) - - expected = minidom.parseString(""" - - - - - - - - - - - - - - - - - """.replace(" ", "")) - - self.assertEqual(expected.toxml(), actual.toxml()) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'limits') + + #verify absolute limits + absolute = root.find('{0}absolute'.format(NS)) + absolutes = absolute.findall('limit'.format(NS)) + for limit in absolutes: + name = limit.get('name') + value = limit.get('value') + self.assertEqual(value, str(fixture['limits']['absolute'][name])) + + #verify rate limits + rate_root = root.find('{0}rates'.format(NS)) + rates = rate_root.findall('{0}rate'.format(NS)) + for i in range(len(rates)): + rate = rates[i] + for key in ['uri', 'regex']: + self.assertEqual(rate.get(key), str(fixture['limits']['rate'][i][key])) + rate_limits = rate.findall('{0}limit'.format(NS)) + for z in range(len(rate_limits)): + limit = rate_limits[z] + for key in ['verb', 'value', 'remaining', 'unit', + 'next-available']: + self.assertEqual(limit.get(key), + str(fixture['limits']['rate'][i]['limit'][z][key])) def test_index_no_limits(self): serializer = limits.LimitsXMLSerializer() -- cgit From bebc02efbf4f049efeb4e1f72a21a8fdc825903a Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 18:08:46 -0400 Subject: pep8 --- nova/tests/api/openstack/test_limits.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_limits.py b/nova/tests/api/openstack/test_limits.py index a3bcecc3a..888f62697 100644 --- a/nova/tests/api/openstack/test_limits.py +++ b/nova/tests/api/openstack/test_limits.py @@ -1028,14 +1028,15 @@ class LimitsXMLSerializationTest(test.TestCase): for i in range(len(rates)): rate = rates[i] for key in ['uri', 'regex']: - self.assertEqual(rate.get(key), str(fixture['limits']['rate'][i][key])) + self.assertEqual(rate.get(key), + str(fixture['limits']['rate'][i][key])) rate_limits = rate.findall('{0}limit'.format(NS)) for z in range(len(rate_limits)): limit = rate_limits[z] for key in ['verb', 'value', 'remaining', 'unit', 'next-available']: self.assertEqual(limit.get(key), - str(fixture['limits']['rate'][i]['limit'][z][key])) + str(fixture['limits']['rate'][i]['limit'][z][key])) def test_index_no_limits(self): serializer = limits.LimitsXMLSerializer() -- cgit From bcdec7da59ade484d370fb4a605e4f6926038252 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 18:10:43 -0400 Subject: updated additional limits test --- nova/tests/api/openstack/test_limits.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_limits.py b/nova/tests/api/openstack/test_limits.py index 888f62697..250d97101 100644 --- a/nova/tests/api/openstack/test_limits.py +++ b/nova/tests/api/openstack/test_limits.py @@ -1046,13 +1046,16 @@ class LimitsXMLSerializationTest(test.TestCase): "absolute": {}}} output = serializer.serialize(fixture, 'index') - actual = minidom.parseString(output.replace(" ", "")) + print output + root = etree.XML(output) + xmlutil.validate_schema(root, 'limits') - expected = minidom.parseString(""" - - - - - """.replace(" ", "")) + #verify absolute limits + absolute = root.find('{0}absolute'.format(NS)) + absolutes = absolute.findall('limit'.format(NS)) + self.assertEqual(len(absolutes), 0) - self.assertEqual(expected.toxml(), actual.toxml()) + #verify rate limits + rate_root = root.find('{0}rates'.format(NS)) + rates = rate_root.findall('{0}rate'.format(NS)) + self.assertEqual(len(rates), 0) -- cgit From e4966cc21ca34380be98a9f24c76404ca43f663f Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 25 Aug 2011 18:31:41 -0400 Subject: updated LimitsXMLSerializer to use etree and supply the xml declaration --- nova/tests/api/openstack/test_limits.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_limits.py b/nova/tests/api/openstack/test_limits.py index 250d97101..f71d9c454 100644 --- a/nova/tests/api/openstack/test_limits.py +++ b/nova/tests/api/openstack/test_limits.py @@ -984,6 +984,18 @@ class LimitsXMLSerializationTest(test.TestCase): def tearDown(self): pass + def test_xml_declaration(self): + serializer = limits.LimitsXMLSerializer() + + fixture = {"limits": { + "rate": [], + "absolute": {}}} + + output = serializer.serialize(fixture, 'index') + print output + has_dec = output.startswith("") + self.assertTrue(has_dec) + def test_index(self): serializer = limits.LimitsXMLSerializer() fixture = {"limits": { -- cgit From e39ec75169ff3b7ac29212ca315ad213997a8cbc Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Sat, 27 Aug 2011 04:32:20 -0400 Subject: Updated VersionsXMLSerializer and corresponding tests to use lxml. --- nova/tests/api/openstack/test_versions.py | 176 ++++++++++++------------------ 1 file changed, 72 insertions(+), 104 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 1269f13c9..3b4396b1a 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -18,8 +18,7 @@ import json import stubout import webob -import xml.etree.ElementTree - +from lxml import etree from nova import context from nova import test @@ -28,6 +27,10 @@ from nova.api.openstack import versions from nova.api.openstack import views from nova.api.openstack import wsgi +NS = { + 'atom': 'http://www.w3.org/2005/Atom', + 'ns': 'http://docs.openstack.org/compute/api/v1.1' +} VERSIONS = { "v1.0": { "id": "v1.0", @@ -113,23 +116,23 @@ class VersionsTest(test.TestCase): versions = json.loads(res.body)["versions"] expected = [ { - "id": "v1.1", - "status": "CURRENT", + "id": "v1.0", + "status": "DEPRECATED", "updated": "2011-01-21T11:33:21Z", "links": [ { "rel": "self", - "href": "http://localhost/v1.1/", + "href": "http://localhost/v1.0/", }], }, { - "id": "v1.0", - "status": "DEPRECATED", + "id": "v1.1", + "status": "CURRENT", "updated": "2011-01-21T11:33:21Z", "links": [ { "rel": "self", - "href": "http://localhost/v1.0/", + "href": "http://localhost/v1.1/", }], }, ] @@ -233,48 +236,19 @@ class VersionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/xml") - root = xml.etree.ElementTree.XML(res.body) - self.assertEqual(root.tag.split('}')[1], "version") - self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) - children = list(root) - media_types = children[0] - media_type_nodes = list(media_types) - links = (children[1], children[2], children[3]) - - self.assertEqual(media_types.tag.split('}')[1], 'media-types') - for media_node in media_type_nodes: - self.assertEqual(media_node.tag.split('}')[1], 'media-type') - - expected = """ - - - - - - - - - - - - - """.replace(" ", "").replace("\n", "") % wsgi.XMLNS_V11 - - actual = res.body.replace(" ", "").replace("\n", "") - self.assertEqual(expected, actual) + version = etree.XML(res.body) + expected = VERSIONS['v1.0'] + self.assertTrue(version.xpath('/ns:version', namespaces=NS)) + media_types = version.xpath('ns:media-types/ns:media-type', + namespaces=NS) + self._compare_media_types(media_types, expected['media-types']) + for key in ['id', 'status', 'updated']: + self.assertEqual(version.get(key), expected[key]) + links = version.xpath('atom:link', namespaces=NS) + self._compare_links(links, + [{'rel': 'self', 'href': 'http://localhost/v1.0/'}] + + expected['links']) def test_get_version_1_1_detail_xml(self): req = webob.Request.blank('/v1.1/') @@ -282,35 +256,19 @@ class VersionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/xml") - expected = """ - - - - - - - - - - - - - """.replace(" ", "").replace("\n", "") % wsgi.XMLNS_V11 - actual = res.body.replace(" ", "").replace("\n", "") - self.assertEqual(expected, actual) + version = etree.XML(res.body) + expected = VERSIONS['v1.1'] + self.assertTrue(version.xpath('/ns:version', namespaces=NS)) + media_types = version.xpath('ns:media-types/ns:media-type', + namespaces=NS) + self._compare_media_types(media_types, expected['media-types']) + for key in ['id', 'status', 'updated']: + self.assertEqual(version.get(key), expected[key]) + links = version.xpath('atom:link', namespaces=NS) + self._compare_links(links, + [{'rel': 'self', 'href': 'http://localhost/v1.1/'}] + + expected['links']) def test_get_version_list_xml(self): req = webob.Request.blank('/') @@ -319,21 +277,19 @@ class VersionsTest(test.TestCase): self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/xml") - expected = """ - - - - - - - - """.replace(" ", "").replace("\n", "") % (wsgi.XMLNS_V11, - wsgi.XMLNS_ATOM) - - actual = res.body.replace(" ", "").replace("\n", "") + root = etree.XML(res.body) + self.assertTrue(root.xpath('/ns:versions', namespaces=NS)) + versions = root.xpath('ns:version', namespaces=NS) + self.assertEqual(len(versions), 2) - self.assertEqual(expected, actual) + for (i, v) in ((0, 'v1.0'), (1, 'v1.1')): + version = versions[i] + expected = VERSIONS[v] + for key in ['id', 'status', 'updated']: + self.assertEqual(version.get(key), expected[key]) + (link,) = version.xpath('atom:link', namespaces=NS) + self._compare_links(link, + [{'rel': 'self', 'href': 'http://localhost/%s/' % v}]) def test_get_version_1_0_detail_atom(self): req = webob.Request.blank('/v1.0/') @@ -427,21 +383,21 @@ class VersionsTest(test.TestCase): - http://localhost/v1.1/ - Version v1.1 + http://localhost/v1.0/ + Version v1.0 2011-01-21T11:33:21Z - + - Version v1.1 CURRENT (2011-01-21T11:33:21Z) + Version v1.0 DEPRECATED (2011-01-21T11:33:21Z) - http://localhost/v1.0/ - Version v1.0 + http://localhost/v1.1/ + Version v1.1 2011-01-21T11:33:21Z - + - Version v1.0 DEPRECATED (2011-01-21T11:33:21Z) + Version v1.1 CURRENT (2011-01-21T11:33:21Z) @@ -598,6 +554,18 @@ class VersionsTest(test.TestCase): self.assertDictMatch(expected, json.loads(res.body)) + def _compare_media_types(self, actual, expected): + for elem, data in zip(actual, expected): + self.assertEqual(elem.get('base'), data['base']) + self.assertEqual(elem.get('type'), data['type']) + + def _compare_links(self, actual, expected): + for elem, data in zip(actual, expected): + self.assertEqual(elem.get('rel'), data['rel']) + self.assertEqual(elem.get('href'), data['href']) + if 'type' in data: + self.assertEqual(elem.get('type'), data['type']) + class VersionsViewBuilderTests(test.TestCase): def test_view_builder(self): @@ -665,7 +633,7 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsXMLSerializer() response = serializer.index(versions_data) - root = xml.etree.ElementTree.XML(response) + root = etree.XML(response) self.assertEqual(root.tag.split('}')[1], "versions") self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) version = list(root)[0] @@ -703,7 +671,7 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsXMLSerializer() response = serializer.multi(versions_data) - root = xml.etree.ElementTree.XML(response) + root = etree.XML(response) self.assertEqual(root.tag.split('}')[1], "choices") self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) version = list(root)[0] @@ -770,7 +738,7 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsXMLSerializer() response = serializer.show(version_data) - root = xml.etree.ElementTree.XML(response) + root = etree.XML(response) self.assertEqual(root.tag.split('}')[1], "version") self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) @@ -812,7 +780,7 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsAtomSerializer() response = serializer.index(versions_data) - root = xml.etree.ElementTree.XML(response) + root = etree.XML(response) self.assertEqual(root.tag.split('}')[1], "feed") self.assertEqual(root.tag.split('}')[0].strip('{'), "http://www.w3.org/2005/Atom") @@ -905,7 +873,7 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsAtomSerializer() response = serializer.show(versions_data) - root = xml.etree.ElementTree.XML(response) + root = etree.XML(response) self.assertEqual(root.tag.split('}')[1], "feed") self.assertEqual(root.tag.split('}')[0].strip('{'), "http://www.w3.org/2005/Atom") -- cgit From 9e3fd76a2f7d55ef111631e6ffac5575a6dd4817 Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Wed, 31 Aug 2011 02:35:01 -0400 Subject: Use feedparser to parse the generated atom feeds in the tests for the versions resource. --- nova/tests/api/openstack/test_versions.py | 374 +++++++++++++----------------- 1 file changed, 167 insertions(+), 207 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 3b4396b1a..57a81738c 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -15,6 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. +import feedparser import json import stubout import webob @@ -297,36 +298,43 @@ class VersionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual("application/atom+xml", res.content_type) - expected = """ - - About This Version - 2011-01-21T11:33:21Z - http://localhost/v1.0/ - - Rackspace - http://www.rackspace.com/ - - - - http://localhost/v1.0/ - Version v1.0 - 2011-01-21T11:33:21Z - - - - - Version v1.0 DEPRECATED (2011-01-21T11:33:21Z) - - - """.replace(" ", "").replace("\n", "") - - actual = res.body.replace(" ", "").replace("\n", "") - self.assertEqual(expected, actual) + + f = feedparser.parse(res.body) + self.assertEqual(f.feed.title, 'About This Version') + self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') + self.assertEqual(f.feed.id, 'http://localhost/v1.0/') + self.assertEqual(f.feed.author, 'Rackspace') + self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.0/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + + self.assertEqual(len(f.entries), 1) + entry = f.entries[0] + self.assertEqual(entry.id, 'http://localhost/v1.0/') + self.assertEqual(entry.title, 'Version v1.0') + self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version v1.0 DEPRECATED (2011-01-21T11:33:21Z)') + self.assertEqual(entry.links, [ + { + 'href': 'http://localhost/v1.0/', + 'type': 'application/atom+xml', + 'rel': 'self' + }, + { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ + 'cs-devguide-20110125.pdf', + 'type': 'application/pdf', + 'rel': 'describedby' + }, + { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ + 'application.wadl', + 'type': 'application/vnd.sun.wadl+xml', + 'rel': 'describedby' + }]) def test_get_version_1_1_detail_atom(self): req = webob.Request.blank('/v1.1/') @@ -334,36 +342,43 @@ class VersionsTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) self.assertEqual("application/atom+xml", res.content_type) - expected = """ - - About This Version - 2011-01-21T11:33:21Z - http://localhost/v1.1/ - - Rackspace - http://www.rackspace.com/ - - - - http://localhost/v1.1/ - Version v1.1 - 2011-01-21T11:33:21Z - - - - - Version v1.1 CURRENT (2011-01-21T11:33:21Z) - - - """.replace(" ", "").replace("\n", "") - - actual = res.body.replace(" ", "").replace("\n", "") - self.assertEqual(expected, actual) + + f = feedparser.parse(res.body) + self.assertEqual(f.feed.title, 'About This Version') + self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') + self.assertEqual(f.feed.id, 'http://localhost/v1.1/') + self.assertEqual(f.feed.author, 'Rackspace') + self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + + self.assertEqual(len(f.entries), 1) + entry = f.entries[0] + self.assertEqual(entry.id, 'http://localhost/v1.1/') + self.assertEqual(entry.title, 'Version v1.1') + self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') + self.assertEqual(entry.links, [ + { + 'href': 'http://localhost/v1.1/', + 'type': 'application/atom+xml', + 'rel': 'self' + }, + { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ + 'cs-devguide-20110125.pdf', + 'type': 'application/pdf', + 'rel': 'describedby' + }, + { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ + 'application.wadl', + 'type': 'application/vnd.sun.wadl+xml', + 'rel': 'describedby' + }]) def test_get_version_list_atom(self): req = webob.Request.blank('/') @@ -372,40 +387,39 @@ class VersionsTest(test.TestCase): self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, "application/atom+xml") - expected = """ - - Available API Versions - 2011-01-21T11:33:21Z - http://localhost/ - - Rackspace - http://www.rackspace.com/ - - - - http://localhost/v1.0/ - Version v1.0 - 2011-01-21T11:33:21Z - - - Version v1.0 DEPRECATED (2011-01-21T11:33:21Z) - - - - http://localhost/v1.1/ - Version v1.1 - 2011-01-21T11:33:21Z - - - Version v1.1 CURRENT (2011-01-21T11:33:21Z) - - - - """.replace(" ", "").replace("\n", "") - - actual = res.body.replace(" ", "").replace("\n", "") - - self.assertEqual(expected, actual) + f = feedparser.parse(res.body) + self.assertEqual(f.feed.title, 'Available API Versions') + self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') + self.assertEqual(f.feed.id, 'http://localhost/') + self.assertEqual(f.feed.author, 'Rackspace') + self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.links, [{'href': 'http://localhost/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + + self.assertEqual(len(f.entries), 2) + entry = f.entries[0] + self.assertEqual(entry.id, 'http://localhost/v1.0/') + self.assertEqual(entry.title, 'Version v1.0') + self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version v1.0 DEPRECATED (2011-01-21T11:33:21Z)') + self.assertEqual(entry.links, [{ + 'href': 'http://localhost/v1.0/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + entry = f.entries[1] + self.assertEqual(entry.id, 'http://localhost/v1.1/') + self.assertEqual(entry.title, 'Version v1.1') + self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') + self.assertEqual(entry.links, [{ + 'href': 'http://localhost/v1.1/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) def test_multi_choice_image(self): req = webob.Request.blank('/images/1') @@ -779,59 +793,28 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsAtomSerializer() response = serializer.index(versions_data) - - root = etree.XML(response) - self.assertEqual(root.tag.split('}')[1], "feed") - self.assertEqual(root.tag.split('}')[0].strip('{'), - "http://www.w3.org/2005/Atom") - - children = list(root) - title = children[0] - updated = children[1] - id = children[2] - author = children[3] - link = children[4] - entry = children[5] - - self.assertEqual(title.tag.split('}')[1], 'title') - self.assertEqual(title.text, 'Available API Versions') - self.assertEqual(updated.tag.split('}')[1], 'updated') - self.assertEqual(updated.text, '2011-07-20T11:40:00Z') - self.assertEqual(id.tag.split('}')[1], 'id') - self.assertEqual(id.text, 'http://test/') - - self.assertEqual(author.tag.split('}')[1], 'author') - author_name = list(author)[0] - author_uri = list(author)[1] - self.assertEqual(author_name.tag.split('}')[1], 'name') - self.assertEqual(author_name.text, 'Rackspace') - self.assertEqual(author_uri.tag.split('}')[1], 'uri') - self.assertEqual(author_uri.text, 'http://www.rackspace.com/') - - self.assertEqual(link.get('href'), 'http://test/') - self.assertEqual(link.get('rel'), 'self') - - self.assertEqual(entry.tag.split('}')[1], 'entry') - entry_children = list(entry) - entry_id = entry_children[0] - entry_title = entry_children[1] - entry_updated = entry_children[2] - entry_link = entry_children[3] - entry_content = entry_children[4] - self.assertEqual(entry_id.tag.split('}')[1], "id") - self.assertEqual(entry_id.text, "http://test/2.9.8") - self.assertEqual(entry_title.tag.split('}')[1], "title") - self.assertEqual(entry_title.get('type'), "text") - self.assertEqual(entry_title.text, "Version 2.9.8") - self.assertEqual(entry_updated.tag.split('}')[1], "updated") - self.assertEqual(entry_updated.text, "2011-07-20T11:40:00Z") - self.assertEqual(entry_link.tag.split('}')[1], "link") - self.assertEqual(entry_link.get('href'), "http://test/2.9.8") - self.assertEqual(entry_link.get('rel'), "self") - self.assertEqual(entry_content.tag.split('}')[1], "content") - self.assertEqual(entry_content.get('type'), "text") - self.assertEqual(entry_content.text, - "Version 2.9.8 CURRENT (2011-07-20T11:40:00Z)") + f = feedparser.parse(response) + + self.assertEqual(f.feed.title, 'Available API Versions') + self.assertEqual(f.feed.updated, '2011-07-20T11:40:00Z') + self.assertEqual(f.feed.id, 'http://test/') + self.assertEqual(f.feed.author, 'Rackspace') + self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.links, [{'href': 'http://test/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + self.assertEqual(len(f.entries), 1) + entry = f.entries[0] + self.assertEqual(entry.id, 'http://test/2.9.8') + self.assertEqual(entry.title, 'Version 2.9.8') + self.assertEqual(entry.updated, '2011-07-20T11:40:00Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version 2.9.8 CURRENT (2011-07-20T11:40:00Z)') + self.assertEqual(entry.links, [{ + 'href': 'http://test/2.9.8', + 'type': 'application/atom+xml', + 'rel': 'self'}]) def test_version_detail_atom_serializer(self): versions_data = { @@ -872,63 +855,40 @@ class VersionsSerializerTests(test.TestCase): serializer = versions.VersionsAtomSerializer() response = serializer.show(versions_data) - - root = etree.XML(response) - self.assertEqual(root.tag.split('}')[1], "feed") - self.assertEqual(root.tag.split('}')[0].strip('{'), - "http://www.w3.org/2005/Atom") - - children = list(root) - title = children[0] - updated = children[1] - id = children[2] - author = children[3] - link = children[4] - entry = children[5] - - self.assertEqual(root.tag.split('}')[1], 'feed') - self.assertEqual(title.tag.split('}')[1], 'title') - self.assertEqual(title.text, 'About This Version') - self.assertEqual(updated.tag.split('}')[1], 'updated') - self.assertEqual(updated.text, '2011-01-21T11:33:21Z') - self.assertEqual(id.tag.split('}')[1], 'id') - self.assertEqual(id.text, 'http://localhost/v1.1/') - - self.assertEqual(author.tag.split('}')[1], 'author') - author_name = list(author)[0] - author_uri = list(author)[1] - self.assertEqual(author_name.tag.split('}')[1], 'name') - self.assertEqual(author_name.text, 'Rackspace') - self.assertEqual(author_uri.tag.split('}')[1], 'uri') - self.assertEqual(author_uri.text, 'http://www.rackspace.com/') - - self.assertEqual(link.get('href'), - 'http://localhost/v1.1/') - self.assertEqual(link.get('rel'), 'self') - - self.assertEqual(entry.tag.split('}')[1], 'entry') - entry_children = list(entry) - entry_id = entry_children[0] - entry_title = entry_children[1] - entry_updated = entry_children[2] - entry_links = (entry_children[3], entry_children[4], entry_children[5]) - entry_content = entry_children[6] - - self.assertEqual(entry_id.tag.split('}')[1], "id") - self.assertEqual(entry_id.text, - "http://localhost/v1.1/") - self.assertEqual(entry_title.tag.split('}')[1], "title") - self.assertEqual(entry_title.get('type'), "text") - self.assertEqual(entry_title.text, "Version v1.1") - self.assertEqual(entry_updated.tag.split('}')[1], "updated") - self.assertEqual(entry_updated.text, "2011-01-21T11:33:21Z") - - for i, link in enumerate(versions_data["version"]["links"]): - self.assertEqual(entry_links[i].tag.split('}')[1], "link") - for key, val in versions_data["version"]["links"][i].items(): - self.assertEqual(entry_links[i].get(key), val) - - self.assertEqual(entry_content.tag.split('}')[1], "content") - self.assertEqual(entry_content.get('type'), "text") - self.assertEqual(entry_content.text, - "Version v1.1 CURRENT (2011-01-21T11:33:21Z)") + f = feedparser.parse(response) + + self.assertEqual(f.feed.title, 'About This Version') + self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') + self.assertEqual(f.feed.id, 'http://localhost/v1.1/') + self.assertEqual(f.feed.author, 'Rackspace') + self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', + 'type': 'application/atom+xml', + 'rel': 'self'}]) + self.assertEqual(len(f.entries), 1) + entry = f.entries[0] + self.assertEqual(entry.id, 'http://localhost/v1.1/') + self.assertEqual(entry.title, 'Version v1.1') + self.assertEqual(entry.updated, '2011-01-21T11:33:21Z') + self.assertEqual(len(entry.content), 1) + self.assertEqual(entry.content[0].value, + 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') + self.assertEqual(entry.links, [ + { + 'rel': 'self', + 'type': 'application/atom+xml', + 'href': 'http://localhost/v1.1/', + }, + { + 'rel': 'describedby', + 'type': 'application/pdf', + 'href': 'http://docs.rackspacecloud.com/' + 'servers/api/v1.1/cs-devguide-20110125.pdf', + }, + { + 'rel': 'describedby', + 'type': 'application/vnd.sun.wadl+xml', + 'href': 'http://docs.rackspacecloud.com/' + 'servers/api/v1.1/application.wadl', + }, + ]) -- cgit From 13d68b59833f55d69497a0f5ac5ec8904af9ab0a Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Wed, 31 Aug 2011 03:40:42 -0400 Subject: Code cleanup. --- nova/tests/api/openstack/test_versions.py | 94 +++++++++++++++---------------- 1 file changed, 46 insertions(+), 48 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 57a81738c..129d1e69d 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -243,13 +243,14 @@ class VersionsTest(test.TestCase): self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self._compare_media_types(media_types, expected['media-types']) + self.assertTrue(_compare_media_types(media_types, + expected['media-types'])) for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) links = version.xpath('atom:link', namespaces=NS) - self._compare_links(links, + self.assertTrue(_compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.0/'}] - + expected['links']) + + expected['links'])) def test_get_version_1_1_detail_xml(self): req = webob.Request.blank('/v1.1/') @@ -263,13 +264,14 @@ class VersionsTest(test.TestCase): self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self._compare_media_types(media_types, expected['media-types']) + self.assertTrue(_compare_media_types(media_types, + expected['media-types'])) for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) links = version.xpath('atom:link', namespaces=NS) - self._compare_links(links, + self.assertTrue(_compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.1/'}] - + expected['links']) + + expected['links'])) def test_get_version_list_xml(self): req = webob.Request.blank('/') @@ -289,8 +291,8 @@ class VersionsTest(test.TestCase): for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) (link,) = version.xpath('atom:link', namespaces=NS) - self._compare_links(link, - [{'rel': 'self', 'href': 'http://localhost/%s/' % v}]) + self.assertTrue(_compare_links(link, + [{'rel': 'self', 'href': 'http://localhost/%s/' % v}])) def test_get_version_1_0_detail_atom(self): req = webob.Request.blank('/v1.0/') @@ -568,18 +570,6 @@ class VersionsTest(test.TestCase): self.assertDictMatch(expected, json.loads(res.body)) - def _compare_media_types(self, actual, expected): - for elem, data in zip(actual, expected): - self.assertEqual(elem.get('base'), data['base']) - self.assertEqual(elem.get('type'), data['type']) - - def _compare_links(self, actual, expected): - for elem, data in zip(actual, expected): - self.assertEqual(elem.get('rel'), data['rel']) - self.assertEqual(elem.get('href'), data['href']) - if 'type' in data: - self.assertEqual(elem.get('type'), data['type']) - class VersionsViewBuilderTests(test.TestCase): def test_view_builder(self): @@ -648,21 +638,19 @@ class VersionsSerializerTests(test.TestCase): response = serializer.index(versions_data) root = etree.XML(response) - self.assertEqual(root.tag.split('}')[1], "versions") - self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) - version = list(root)[0] - self.assertEqual(version.tag.split('}')[1], "version") - self.assertEqual(version.get('id'), - versions_data['versions'][0]['id']) + self.assertTrue(root.xpath('/ns:versions', namespaces=NS)) + version_elems = root.xpath('ns:version', namespaces=NS) + self.assertEqual(len(version_elems), 1) + version = version_elems[0] + self.assertEqual(version.get('id'), versions_data['versions'][0]['id']) self.assertEqual(version.get('status'), versions_data['versions'][0]['status']) - link = list(version)[0] - - self.assertEqual(link.tag.split('}')[1], "link") - self.assertEqual(link.tag.split('}')[0].strip('{'), wsgi.XMLNS_ATOM) - for key, val in versions_data['versions'][0]['links'][0].items(): - self.assertEqual(link.get(key), val) + (link,) = version.xpath('atom:link', namespaces=NS) + self.assertTrue(_compare_links(link, [{ + 'rel': 'self', + 'href': 'http://test/2.7.1', + 'type': 'application/atom+xml'}])) def test_versions_multi_xml_serializer(self): versions_data = { @@ -686,10 +674,8 @@ class VersionsSerializerTests(test.TestCase): response = serializer.multi(versions_data) root = etree.XML(response) - self.assertEqual(root.tag.split('}')[1], "choices") - self.assertEqual(root.tag.split('}')[0].strip('{'), wsgi.XMLNS_V11) - version = list(root)[0] - self.assertEqual(version.tag.split('}')[1], "version") + self.assertTrue(root.xpath('/ns:choices', namespaces=NS)) + (version,) = root.xpath('ns:version', namespaces=NS) self.assertEqual(version.get('id'), versions_data['choices'][0]['id']) self.assertEqual(version.get('status'), versions_data['choices'][0]['status']) @@ -698,19 +684,14 @@ class VersionsSerializerTests(test.TestCase): media_type_nodes = list(media_types) self.assertEqual(media_types.tag.split('}')[1], "media-types") - set_types = versions_data['choices'][0]['media-types'] - for i, type in enumerate(set_types): - node = media_type_nodes[i] - self.assertEqual(node.tag.split('}')[1], "media-type") - for key, val in set_types[i].items(): - self.assertEqual(node.get(key), val) - - link = list(version)[1] + media_types = version.xpath('ns:media-types/ns:media-type', + namespaces=NS) + self.assertTrue(_compare_media_types(media_types, + versions_data['choices'][0]['media-types'])) - self.assertEqual(link.tag.split('}')[1], "link") - self.assertEqual(link.tag.split('}')[0].strip('{'), wsgi.XMLNS_ATOM) - for key, val in versions_data['choices'][0]['links'][0].items(): - self.assertEqual(link.get(key), val) + (link,) = version.xpath('atom:link', namespaces=NS) + self.assertTrue(_compare_links(link, + versions_data['choices'][0]['links'])) def test_version_detail_xml_serializer(self): version_data = { @@ -892,3 +873,20 @@ class VersionsSerializerTests(test.TestCase): 'servers/api/v1.1/application.wadl', }, ]) + + +def _compare_links(actual, expected): + for elem, data in zip(actual, expected): + for key in ('rel', 'href', 'type'): + if elem.get(key) != data.get(key): + return False + return True + + +def _compare_media_types(actual, expected): + for elem, data in zip(actual, expected): + for key in ('base', 'type'): + if elem.get(key) != data.get(key): + return False + return True + -- cgit From 162f1750344a4e1812a15ec132fefd6e1fd47ca5 Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Wed, 31 Aug 2011 18:38:55 -0400 Subject: Updating test for xml to use lxml. --- nova/tests/api/openstack/test_versions.py | 61 ++++++++++++++++--------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 129d1e69d..03a47bf24 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -483,28 +483,32 @@ class VersionsTest(test.TestCase): self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/xml") - expected = """ - - - - - - - - - - - - - - - - """.replace(" ", "").replace("\n", "") % (wsgi.XMLNS_V11, - wsgi.XMLNS_ATOM) + root = etree.XML(res.body) + self.assertTrue(root.xpath('/ns:choices', namespaces=NS)) + versions = root.xpath('ns:version', namespaces=NS) + self.assertEqual(len(versions), 2) + + version = versions[0] + self.assertEqual(version.get('id'), 'v1.1'); + self.assertEqual(version.get('status'), 'CURRENT') + media_types = version.xpath('ns:media-types/ns:media-type', + namespaces=NS) + self.assertTrue(_compare_media_types(media_types, + VERSIONS['v1.1']['media-types'])) + links = version.xpath('atom:link', namespaces=NS) + self.assertTrue(_compare_links(links, + [{'rel': 'self', 'href': 'http://localhost/v1.1/images/1'}])) + + version = versions[1] + self.assertEqual(version.get('id'), 'v1.0'); + self.assertEqual(version.get('status'), 'DEPRECATED') + media_types = version.xpath('ns:media-types/ns:media-type', + namespaces=NS) + self.assertTrue(_compare_media_types(media_types, + VERSIONS['v1.0']['media-types'])) + links = version.xpath('atom:link', namespaces=NS) + self.assertTrue(_compare_links(links, + [{'rel': 'self', 'href': 'http://localhost/v1.0/images/1'}])) def test_multi_choice_server_atom(self): """ @@ -876,17 +880,16 @@ class VersionsSerializerTests(test.TestCase): def _compare_links(actual, expected): - for elem, data in zip(actual, expected): - for key in ('rel', 'href', 'type'): - if elem.get(key) != data.get(key): - return False - return True + return _compare_tree_to_dict(actual, expected, ('rel', 'href', 'type')) def _compare_media_types(actual, expected): + return _compare_tree_to_dict(actual, expected, ('base', 'type')) + + +def _compare_tree_to_dict(actual, expected, keys): for elem, data in zip(actual, expected): - for key in ('base', 'type'): + for key in keys: if elem.get(key) != data.get(key): return False return True - -- cgit From 0c737cb60980b8db74496e7914322f567950c2c3 Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Fri, 2 Sep 2011 01:07:30 -0400 Subject: Removing xml functions that are no longer called. --- nova/tests/api/openstack/test_versions.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 03a47bf24..a9308e31d 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -306,7 +306,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') self.assertEqual(f.feed.id, 'http://localhost/v1.0/') self.assertEqual(f.feed.author, 'Rackspace') - self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.author_detail.href, + 'http://www.rackspace.com/') self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.0/', 'type': 'application/atom+xml', 'rel': 'self'}]) @@ -350,7 +351,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') self.assertEqual(f.feed.id, 'http://localhost/v1.1/') self.assertEqual(f.feed.author, 'Rackspace') - self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.author_detail.href, + 'http://www.rackspace.com/') self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', 'type': 'application/atom+xml', 'rel': 'self'}]) @@ -394,7 +396,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') self.assertEqual(f.feed.id, 'http://localhost/') self.assertEqual(f.feed.author, 'Rackspace') - self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.author_detail.href, + 'http://www.rackspace.com/') self.assertEqual(f.feed.links, [{'href': 'http://localhost/', 'type': 'application/atom+xml', 'rel': 'self'}]) @@ -489,7 +492,7 @@ class VersionsTest(test.TestCase): self.assertEqual(len(versions), 2) version = versions[0] - self.assertEqual(version.get('id'), 'v1.1'); + self.assertEqual(version.get('id'), 'v1.1') self.assertEqual(version.get('status'), 'CURRENT') media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) @@ -500,7 +503,7 @@ class VersionsTest(test.TestCase): [{'rel': 'self', 'href': 'http://localhost/v1.1/images/1'}])) version = versions[1] - self.assertEqual(version.get('id'), 'v1.0'); + self.assertEqual(version.get('id'), 'v1.0') self.assertEqual(version.get('status'), 'DEPRECATED') media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) @@ -784,7 +787,8 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(f.feed.updated, '2011-07-20T11:40:00Z') self.assertEqual(f.feed.id, 'http://test/') self.assertEqual(f.feed.author, 'Rackspace') - self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.author_detail.href, + 'http://www.rackspace.com/') self.assertEqual(f.feed.links, [{'href': 'http://test/', 'type': 'application/atom+xml', 'rel': 'self'}]) @@ -846,7 +850,8 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') self.assertEqual(f.feed.id, 'http://localhost/v1.1/') self.assertEqual(f.feed.author, 'Rackspace') - self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') + self.assertEqual(f.feed.author_detail.href, + 'http://www.rackspace.com/') self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', 'type': 'application/atom+xml', 'rel': 'self'}]) -- cgit From 9b35957eef001ae1c3329e9197984d3aca0da787 Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Sun, 4 Sep 2011 05:39:21 -0400 Subject: Fixing xml serialization of limits resource. --- nova/tests/api/openstack/test_limits.py | 34 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_limits.py b/nova/tests/api/openstack/test_limits.py index f71d9c454..3db57ee86 100644 --- a/nova/tests/api/openstack/test_limits.py +++ b/nova/tests/api/openstack/test_limits.py @@ -41,8 +41,10 @@ TEST_LIMITS = [ limits.Limit("PUT", "*", "", 10, limits.PER_MINUTE), limits.Limit("PUT", "/servers", "^/servers", 5, limits.PER_MINUTE), ] -NS = "{http://docs.openstack.org/compute/api/v1.1}" -ATOMNS = "{http://www.w3.org/2005/Atom}" +NS = { + 'atom': 'http://www.w3.org/2005/Atom', + 'ns': 'http://docs.openstack.org/compute/api/v1.1' +} class BaseLimitTestSuite(unittest.TestCase): @@ -998,7 +1000,8 @@ class LimitsXMLSerializationTest(test.TestCase): def test_index(self): serializer = limits.LimitsXMLSerializer() - fixture = {"limits": { + fixture = { + "limits": { "rate": [{ "uri": "*", "regex": ".*", @@ -1027,28 +1030,27 @@ class LimitsXMLSerializationTest(test.TestCase): xmlutil.validate_schema(root, 'limits') #verify absolute limits - absolute = root.find('{0}absolute'.format(NS)) - absolutes = absolute.findall('limit'.format(NS)) + absolutes = root.xpath('ns:absolute/ns:limit', namespaces=NS) + self.assertEqual(len(absolutes), 4) for limit in absolutes: name = limit.get('name') value = limit.get('value') self.assertEqual(value, str(fixture['limits']['absolute'][name])) #verify rate limits - rate_root = root.find('{0}rates'.format(NS)) - rates = rate_root.findall('{0}rate'.format(NS)) - for i in range(len(rates)): - rate = rates[i] + rates = root.xpath('ns:rates/ns:rate', namespaces=NS) + self.assertEqual(len(rates), 2) + for i, rate in enumerate(rates): for key in ['uri', 'regex']: self.assertEqual(rate.get(key), str(fixture['limits']['rate'][i][key])) - rate_limits = rate.findall('{0}limit'.format(NS)) - for z in range(len(rate_limits)): - limit = rate_limits[z] + rate_limits = rate.xpath('ns:limit', namespaces=NS) + self.assertEqual(len(rate_limits), 1) + for j, limit in enumerate(rate_limits): for key in ['verb', 'value', 'remaining', 'unit', 'next-available']: self.assertEqual(limit.get(key), - str(fixture['limits']['rate'][i]['limit'][z][key])) + str(fixture['limits']['rate'][i]['limit'][j][key])) def test_index_no_limits(self): serializer = limits.LimitsXMLSerializer() @@ -1063,11 +1065,9 @@ class LimitsXMLSerializationTest(test.TestCase): xmlutil.validate_schema(root, 'limits') #verify absolute limits - absolute = root.find('{0}absolute'.format(NS)) - absolutes = absolute.findall('limit'.format(NS)) + absolutes = root.xpath('ns:absolute/ns:limit', namespaces=NS) self.assertEqual(len(absolutes), 0) #verify rate limits - rate_root = root.find('{0}rates'.format(NS)) - rates = rate_root.findall('{0}rate'.format(NS)) + rates = root.xpath('ns:rates/ns:rate', namespaces=NS) self.assertEqual(len(rates), 0) -- cgit From d155f4224cb97e43cacd4102ba01f0e1775dfbdf Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Sun, 4 Sep 2011 05:39:59 -0400 Subject: Minor cleanup. --- nova/tests/api/openstack/test_versions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index a9308e31d..8b60db71e 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -285,7 +285,7 @@ class VersionsTest(test.TestCase): versions = root.xpath('ns:version', namespaces=NS) self.assertEqual(len(versions), 2) - for (i, v) in ((0, 'v1.0'), (1, 'v1.1')): + for i, v in enumerate(['v1.0', 'v1.1']): version = versions[i] expected = VERSIONS[v] for key in ['id', 'status', 'updated']: -- cgit From b890b992f3013a1959e3c3cdf1f149cacf4e569b Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Thu, 8 Sep 2011 21:30:21 -0400 Subject: Fixing security groups stuff --- .../api/openstack/contrib/test_createserverext.py | 6 ++- nova/tests/api/openstack/test_versions.py | 43 +++++++--------------- 2 files changed, 19 insertions(+), 30 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/contrib/test_createserverext.py b/nova/tests/api/openstack/contrib/test_createserverext.py index 078b72d67..03c7d1ec5 100644 --- a/nova/tests/api/openstack/contrib/test_createserverext.py +++ b/nova/tests/api/openstack/contrib/test_createserverext.py @@ -49,9 +49,13 @@ INSTANCE = { "id": 1, "display_name": "test_server", "uuid": FAKE_UUID, + "user_id": 'fake_user_id', + "tenant_id": 'fake_tenant_id', "created_at": datetime.datetime(2010, 10, 10, 12, 0, 0), "updated_at": datetime.datetime(2010, 11, 11, 11, 0, 0), - "security_groups": [{"id": 1, "name": "test"}] + "security_groups": [{"id": 1, "name": "test"}], + "image_ref": 'http://foo.com/123', + "instance_type": {"flavorid": '124'}, } diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 8b60db71e..686752509 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -23,10 +23,11 @@ from lxml import etree from nova import context from nova import test -from nova.tests.api.openstack import fakes from nova.api.openstack import versions from nova.api.openstack import views from nova.api.openstack import wsgi +from nova.tests.api.openstack import common +from nova.tests.api.openstack import fakes NS = { 'atom': 'http://www.w3.org/2005/Atom', @@ -243,12 +244,12 @@ class VersionsTest(test.TestCase): self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self.assertTrue(_compare_media_types(media_types, + self.assertTrue(common.compare_media_types(media_types, expected['media-types'])) for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) links = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(links, + self.assertTrue(common.compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.0/'}] + expected['links'])) @@ -264,12 +265,12 @@ class VersionsTest(test.TestCase): self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self.assertTrue(_compare_media_types(media_types, + self.assertTrue(common.compare_media_types(media_types, expected['media-types'])) for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) links = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(links, + self.assertTrue(common.compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.1/'}] + expected['links'])) @@ -291,7 +292,7 @@ class VersionsTest(test.TestCase): for key in ['id', 'status', 'updated']: self.assertEqual(version.get(key), expected[key]) (link,) = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(link, + self.assertTrue(common.compare_links(link, [{'rel': 'self', 'href': 'http://localhost/%s/' % v}])) def test_get_version_1_0_detail_atom(self): @@ -496,10 +497,10 @@ class VersionsTest(test.TestCase): self.assertEqual(version.get('status'), 'CURRENT') media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self.assertTrue(_compare_media_types(media_types, + self.assertTrue(common.compare_media_types(media_types, VERSIONS['v1.1']['media-types'])) links = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(links, + self.assertTrue(common.compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.1/images/1'}])) version = versions[1] @@ -507,10 +508,10 @@ class VersionsTest(test.TestCase): self.assertEqual(version.get('status'), 'DEPRECATED') media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self.assertTrue(_compare_media_types(media_types, + self.assertTrue(common.compare_media_types(media_types, VERSIONS['v1.0']['media-types'])) links = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(links, + self.assertTrue(common.compare_links(links, [{'rel': 'self', 'href': 'http://localhost/v1.0/images/1'}])) def test_multi_choice_server_atom(self): @@ -654,7 +655,7 @@ class VersionsSerializerTests(test.TestCase): versions_data['versions'][0]['status']) (link,) = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(link, [{ + self.assertTrue(common.compare_links(link, [{ 'rel': 'self', 'href': 'http://test/2.7.1', 'type': 'application/atom+xml'}])) @@ -693,11 +694,11 @@ class VersionsSerializerTests(test.TestCase): media_types = version.xpath('ns:media-types/ns:media-type', namespaces=NS) - self.assertTrue(_compare_media_types(media_types, + self.assertTrue(common.compare_media_types(media_types, versions_data['choices'][0]['media-types'])) (link,) = version.xpath('atom:link', namespaces=NS) - self.assertTrue(_compare_links(link, + self.assertTrue(common.compare_links(link, versions_data['choices'][0]['links'])) def test_version_detail_xml_serializer(self): @@ -882,19 +883,3 @@ class VersionsSerializerTests(test.TestCase): 'servers/api/v1.1/application.wadl', }, ]) - - -def _compare_links(actual, expected): - return _compare_tree_to_dict(actual, expected, ('rel', 'href', 'type')) - - -def _compare_media_types(actual, expected): - return _compare_tree_to_dict(actual, expected, ('base', 'type')) - - -def _compare_tree_to_dict(actual, expected, keys): - for elem, data in zip(actual, expected): - for key in keys: - if elem.get(key) != data.get(key): - return False - return True -- cgit From 13ee200d8265175922b5747f9e00fc31db4803fd Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Thu, 8 Sep 2011 21:37:40 -0400 Subject: pep 8 --- nova/tests/api/openstack/test_servers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index c3203eb43..9d0f9c4f7 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -496,7 +496,8 @@ class ServersTest(test.TestCase): self.assertEqual(image.get('id'), str(expected['image']['id'])) links = root.xpath('ns:image/atom:link', namespaces=XPATH_NS) - self.assertTrue(common.compare_links(links, expected['image']['links'])) + self.assertTrue(common.compare_links(links, + expected['image']['links'])) (flavor,) = root.xpath('ns:flavor', namespaces=XPATH_NS) self.assertEqual(flavor.get('id'), str(expected['flavor']['id'])) @@ -3663,7 +3664,7 @@ class TestGetKernelRamdiskFromImage(test.TestCase): self.assertRaises(exception.NotFound, self._get_k_r, image_meta) def test_ami_no_ramdisk(self): - """If an ami is missing a ramdisk, return kernel ID and None for + """If an ami is missing a ramdisk, return kernel ID and None for ramdisk ID """ image_meta = {'id': 1, 'status': 'active', 'container_format': 'ami', -- cgit From c890890c7ccbc7df1060d59747089b5e39c5510a Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Sat, 10 Sep 2011 17:11:21 +0900 Subject: api/ec2: make get_metadata() return correct mappings The entries corresponding to volumes are in the form of ebs': --- nova/tests/test_cloud.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/test_cloud.py b/nova/tests/test_cloud.py index 7fe353b3d..7bdae0552 100644 --- a/nova/tests/test_cloud.py +++ b/nova/tests/test_cloud.py @@ -1540,7 +1540,9 @@ class CloudTestCase(test.TestCase): 'ephemeral0': '/dev/sdb', 'swap': '/dev/sdc', 'ephemeral1': '/dev/sdd', - 'ephemeral2': '/dev/sd3'} + 'ephemeral2': '/dev/sd3', + 'ebs0': '/dev/sdh', + 'ebs1': '/dev/sdi'} self.assertEqual(self.cloud._format_instance_mapping(ctxt, instance_ref0), -- cgit From f40955d419c886be29213f73f5ffdf2f38e00057 Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Mon, 12 Sep 2011 08:00:30 -0700 Subject: add test for method sig --- nova/tests/test_libvirt.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'nova/tests') diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 93967ceec..d776a386b 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -16,6 +16,7 @@ import copy import eventlet +import inspect import mox import os import re @@ -35,6 +36,7 @@ from nova import utils from nova.api.ec2 import cloud from nova.compute import power_state from nova.compute import vm_states +from nova.virt import driver from nova.virt.libvirt import connection from nova.virt.libvirt import firewall from nova.tests import fake_network @@ -840,6 +842,50 @@ class LibvirtConnTestCase(test.TestCase): _assert_volume_in_mapping('sdg', False) _assert_volume_in_mapping('sdh1', False) + def test_reboot_signature(self): + """Test that libvirt driver method sig matches interface""" + def fake_reboot_with_correct_sig(ignore, instance, + network_info, reboot_type): + pass + + def fake_destroy(instance, network_info, cleanup=False): + pass + + def fake_plug_vifs(instance, network_info): + pass + + def fake_create_new_domain(xml): + return + + def fake_none(self, instance): + return + + instance = db.instance_create(self.context, self.test_instance) + network_info = _fake_network_info(self.stubs, 1) + + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup + + conn = connection.LibvirtConnection(False) + self.stubs.Set(conn, 'destroy', fake_destroy) + self.stubs.Set(conn, 'plug_vifs', fake_plug_vifs) + self.stubs.Set(conn.firewall_driver, + 'setup_basic_filtering', + fake_none) + self.stubs.Set(conn.firewall_driver, + 'prepare_instance_filter', + fake_none) + self.stubs.Set(conn, '_create_new_domain', fake_create_new_domain) + self.stubs.Set(conn.firewall_driver, + 'apply_instance_filter', + fake_none) + + args = [instance, network_info, 'SOFT'] + conn.reboot(*args) + + compute_driver = driver.ComputeDriver() + self.assertRaises(NotImplementedError, compute_driver.reboot, *args) + class NWFilterFakes: def __init__(self): -- cgit From 4cbfc60b225d0386b6719e49fc9797fd72dc219b Mon Sep 17 00:00:00 2001 From: Anthony Young Date: Mon, 12 Sep 2011 09:15:31 -0700 Subject: remove unused dep --- nova/tests/test_libvirt.py | 1 - 1 file changed, 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index d776a386b..5346e089b 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -16,7 +16,6 @@ import copy import eventlet -import inspect import mox import os import re -- cgit From 837f611af4467c716a0585bb8f38345ceef32921 Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Mon, 12 Sep 2011 19:13:18 -0400 Subject: Made tests version version links more robust --- nova/tests/api/openstack/test_versions.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 686752509..0ad26135e 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -309,9 +309,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') - self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.0/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(f.feed.links[0]['href'], 'http://localhost/v1.0/') + self.assertEqual(f.feed.links[0]['rel'], 'self') self.assertEqual(len(f.entries), 1) entry = f.entries[0] @@ -354,9 +353,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') - self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(f.feed.links[0]['href'], 'http://localhost/v1.1/') + self.assertEqual(f.feed.links[0]['rel'], 'self') self.assertEqual(len(f.entries), 1) entry = f.entries[0] @@ -399,9 +397,8 @@ class VersionsTest(test.TestCase): self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') - self.assertEqual(f.feed.links, [{'href': 'http://localhost/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(f.feed.links[0]['href'], 'http://localhost/') + self.assertEqual(f.feed.links[0]['rel'], 'self') self.assertEqual(len(f.entries), 2) entry = f.entries[0] @@ -790,9 +787,9 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') - self.assertEqual(f.feed.links, [{'href': 'http://test/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(f.feed.links[0]['href'], 'http://test/') + self.assertEqual(f.feed.links[0]['rel'], 'self') + self.assertEqual(len(f.entries), 1) entry = f.entries[0] self.assertEqual(entry.id, 'http://test/2.9.8') @@ -853,9 +850,9 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(f.feed.author, 'Rackspace') self.assertEqual(f.feed.author_detail.href, 'http://www.rackspace.com/') - self.assertEqual(f.feed.links, [{'href': 'http://localhost/v1.1/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(f.feed.links[0]['href'], 'http://localhost/v1.1/') + self.assertEqual(f.feed.links[0]['rel'], 'self') + self.assertEqual(len(f.entries), 1) entry = f.entries[0] self.assertEqual(entry.id, 'http://localhost/v1.1/') -- cgit From 4c2f8b561390358cbd1c8dbfbc7e4d8370b2b84e Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Tue, 13 Sep 2011 02:45:11 -0400 Subject: Same as last time. --- nova/tests/api/openstack/test_versions.py | 109 +++++++++++++----------------- 1 file changed, 46 insertions(+), 63 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 0ad26135e..0077cbe79 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -320,24 +320,19 @@ class VersionsTest(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v1.0 DEPRECATED (2011-01-21T11:33:21Z)') - self.assertEqual(entry.links, [ - { - 'href': 'http://localhost/v1.0/', - 'type': 'application/atom+xml', - 'rel': 'self' - }, - { - 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ - 'cs-devguide-20110125.pdf', - 'type': 'application/pdf', - 'rel': 'describedby' - }, - { - 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ - 'application.wadl', - 'type': 'application/vnd.sun.wadl+xml', - 'rel': 'describedby' - }]) + self.assertEqual(len(entry.links), 3) + self.assertEqual(entry.links[0]['href'], 'http://localhost/v1.0/') + self.assertEqual(entry.links[0]['rel'], 'self') + self.assertEqual(entry.links[1], { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ + 'cs-devguide-20110125.pdf', + 'type': 'application/pdf', + 'rel': 'describedby'}) + self.assertEqual(entry.links[2], { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.0/'\ + 'application.wadl', + 'type': 'application/vnd.sun.wadl+xml', + 'rel': 'describedby'}) def test_get_version_1_1_detail_atom(self): req = webob.Request.blank('/v1.1/') @@ -364,24 +359,19 @@ class VersionsTest(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') - self.assertEqual(entry.links, [ - { - 'href': 'http://localhost/v1.1/', - 'type': 'application/atom+xml', - 'rel': 'self' - }, - { - 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ - 'cs-devguide-20110125.pdf', - 'type': 'application/pdf', - 'rel': 'describedby' - }, - { - 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ - 'application.wadl', - 'type': 'application/vnd.sun.wadl+xml', - 'rel': 'describedby' - }]) + self.assertEqual(len(entry.links), 3) + self.assertEqual(entry.links[0]['href'], 'http://localhost/v1.1/') + self.assertEqual(entry.links[0]['rel'], 'self') + self.assertEqual(entry.links[1], { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ + 'cs-devguide-20110125.pdf', + 'type': 'application/pdf', + 'rel': 'describedby'}) + self.assertEqual(entry.links[2], { + 'href': 'http://docs.rackspacecloud.com/servers/api/v1.1/'\ + 'application.wadl', + 'type': 'application/vnd.sun.wadl+xml', + 'rel': 'describedby'}) def test_get_version_list_atom(self): req = webob.Request.blank('/') @@ -408,10 +398,9 @@ class VersionsTest(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v1.0 DEPRECATED (2011-01-21T11:33:21Z)') - self.assertEqual(entry.links, [{ - 'href': 'http://localhost/v1.0/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(len(entry.links), 1) + self.assertEqual(entry.links[0]['href'], 'http://localhost/v1.0/') + self.assertEqual(entry.links[0]['rel'], 'self') entry = f.entries[1] self.assertEqual(entry.id, 'http://localhost/v1.1/') self.assertEqual(entry.title, 'Version v1.1') @@ -798,10 +787,9 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version 2.9.8 CURRENT (2011-07-20T11:40:00Z)') - self.assertEqual(entry.links, [{ - 'href': 'http://test/2.9.8', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(len(entry.links), 1) + self.assertEqual(entry.links[0]['href'], 'http://test/2.9.8') + self.assertEqual(entry.links[0]['rel'], 'self') def test_version_detail_atom_serializer(self): versions_data = { @@ -861,22 +849,17 @@ class VersionsSerializerTests(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') - self.assertEqual(entry.links, [ - { - 'rel': 'self', - 'type': 'application/atom+xml', - 'href': 'http://localhost/v1.1/', - }, - { - 'rel': 'describedby', - 'type': 'application/pdf', - 'href': 'http://docs.rackspacecloud.com/' - 'servers/api/v1.1/cs-devguide-20110125.pdf', - }, - { - 'rel': 'describedby', - 'type': 'application/vnd.sun.wadl+xml', - 'href': 'http://docs.rackspacecloud.com/' - 'servers/api/v1.1/application.wadl', - }, - ]) + self.assertEqual(len(entry.links), 3) + self.assertEqual(entry.links[0]['href'], 'http://localhost/v1.1/') + self.assertEqual(entry.links[0]['rel'], 'self') + self.assertEqual(entry.links[1], { + 'rel': 'describedby', + 'type': 'application/pdf', + 'href': 'http://docs.rackspacecloud.com/' + 'servers/api/v1.1/cs-devguide-20110125.pdf'}) + self.assertEqual(entry.links[2], { + 'rel': 'describedby', + 'type': 'application/vnd.sun.wadl+xml', + 'href': 'http://docs.rackspacecloud.com/' + 'servers/api/v1.1/application.wadl', + }) -- cgit From 613bcfc45865c9d7c7577e124b187920970d031e Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Tue, 13 Sep 2011 11:22:45 -0400 Subject: And again. --- nova/tests/api/openstack/test_versions.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'nova/tests') diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 0077cbe79..f69dbd316 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -408,10 +408,9 @@ class VersionsTest(test.TestCase): self.assertEqual(len(entry.content), 1) self.assertEqual(entry.content[0].value, 'Version v1.1 CURRENT (2011-01-21T11:33:21Z)') - self.assertEqual(entry.links, [{ - 'href': 'http://localhost/v1.1/', - 'type': 'application/atom+xml', - 'rel': 'self'}]) + self.assertEqual(len(entry.links), 1) + self.assertEqual(entry.links[0]['href'], 'http://localhost/v1.1/') + self.assertEqual(entry.links[0]['rel'], 'self') def test_multi_choice_image(self): req = webob.Request.blank('/images/1') -- cgit From 98e2fd764b33fa5a3af6ca982a171717a12ee206 Mon Sep 17 00:00:00 2001 From: Josh Kearney Date: Tue, 13 Sep 2011 15:33:34 -0500 Subject: Only allow up to 15 chars for a Windows hostname. --- nova/tests/test_xenapi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'nova/tests') diff --git a/nova/tests/test_xenapi.py b/nova/tests/test_xenapi.py index 4a83d139e..47c6a3c95 100644 --- a/nova/tests/test_xenapi.py +++ b/nova/tests/test_xenapi.py @@ -364,7 +364,7 @@ class XenAPIVMTestCase(test.TestCase): def _test_spawn(self, image_ref, kernel_id, ramdisk_id, instance_type_id="3", os_type="linux", - architecture="x86-64", instance_id=1, + hostname="test", architecture="x86-64", instance_id=1, check_injection=False, create_record=True, empty_dns=False): stubs.stubout_loopingcall_start(self.stubs) @@ -377,6 +377,7 @@ class XenAPIVMTestCase(test.TestCase): 'ramdisk_id': ramdisk_id, 'instance_type_id': instance_type_id, 'os_type': os_type, + 'hostname': hostname, 'architecture': architecture} instance = db.instance_create(self.context, values) else: -- cgit