From 4e94ec1a0a566b66f09b734e6ffe964b4b3b4bee Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Fri, 23 Sep 2011 15:03:35 -0400 Subject: Adding xml schema validation for /versions resource. Change-Id: I8e0b7695743844614b3e7c5a08bb88d3a7500b5d --- nova/api/openstack/schemas/v1.1/version.rng | 17 +++++++++++++++++ nova/api/openstack/schemas/v1.1/versions.rng | 11 +++++++++++ nova/api/openstack/xmlutil.py | 5 ++++- nova/tests/api/openstack/test_versions.py | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 nova/api/openstack/schemas/v1.1/version.rng create mode 100644 nova/api/openstack/schemas/v1.1/versions.rng diff --git a/nova/api/openstack/schemas/v1.1/version.rng b/nova/api/openstack/schemas/v1.1/version.rng new file mode 100644 index 000000000..ae76270ba --- /dev/null +++ b/nova/api/openstack/schemas/v1.1/version.rng @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + diff --git a/nova/api/openstack/schemas/v1.1/versions.rng b/nova/api/openstack/schemas/v1.1/versions.rng new file mode 100644 index 000000000..8b2cc7f71 --- /dev/null +++ b/nova/api/openstack/schemas/v1.1/versions.rng @@ -0,0 +1,11 @@ + + + + + + + + + + diff --git a/nova/api/openstack/xmlutil.py b/nova/api/openstack/xmlutil.py index 97ad90ada..d5eb88a57 100644 --- a/nova/api/openstack/xmlutil.py +++ b/nova/api/openstack/xmlutil.py @@ -30,8 +30,11 @@ XMLNS_ATOM = 'http://www.w3.org/2005/Atom' def validate_schema(xml, schema_name): if type(xml) is str: xml = etree.fromstring(xml) + base_path = 'nova/api/openstack/schemas/v1.1/' + if schema_name in ('atom', 'atom-link'): + base_path = 'nova/api/openstack/schemas/' schema_path = os.path.join(utils.novadir(), - 'nova/api/openstack/schemas/v1.1/%s.rng' % schema_name) + '%s%s.rng' % (base_path, schema_name)) schema_doc = etree.parse(schema_path) relaxng = etree.RelaxNG(schema_doc) relaxng.assertValid(xml) diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index f69dbd316..1ae3789d9 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -26,6 +26,7 @@ from nova import test from nova.api.openstack import versions from nova.api.openstack import views from nova.api.openstack import wsgi +from nova.api.openstack import xmlutil from nova.tests.api.openstack import common from nova.tests.api.openstack import fakes @@ -240,6 +241,8 @@ class VersionsTest(test.TestCase): self.assertEqual(res.content_type, "application/xml") version = etree.XML(res.body) + xmlutil.validate_schema(version, 'version') + expected = VERSIONS['v1.0'] self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', @@ -261,6 +264,8 @@ class VersionsTest(test.TestCase): self.assertEqual(res.content_type, "application/xml") version = etree.XML(res.body) + xmlutil.validate_schema(version, 'version') + expected = VERSIONS['v1.1'] self.assertTrue(version.xpath('/ns:version', namespaces=NS)) media_types = version.xpath('ns:media-types/ns:media-type', @@ -282,6 +287,9 @@ class VersionsTest(test.TestCase): self.assertEqual(res.content_type, "application/xml") root = etree.XML(res.body) + print res.body + xmlutil.validate_schema(root, 'versions') + self.assertTrue(root.xpath('/ns:versions', namespaces=NS)) versions = root.xpath('ns:version', namespaces=NS) self.assertEqual(len(versions), 2) @@ -302,6 +310,8 @@ class VersionsTest(test.TestCase): self.assertEqual(res.status_int, 200) self.assertEqual("application/atom+xml", res.content_type) + xmlutil.validate_schema(etree.XML(res.body), 'atom') + f = feedparser.parse(res.body) self.assertEqual(f.feed.title, 'About This Version') self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') @@ -341,6 +351,8 @@ class VersionsTest(test.TestCase): self.assertEqual(res.status_int, 200) self.assertEqual("application/atom+xml", res.content_type) + xmlutil.validate_schema(etree.XML(res.body), 'atom') + f = feedparser.parse(res.body) self.assertEqual(f.feed.title, 'About This Version') self.assertEqual(f.feed.updated, '2011-01-21T11:33:21Z') @@ -631,6 +643,8 @@ class VersionsSerializerTests(test.TestCase): response = serializer.index(versions_data) root = etree.XML(response) + xmlutil.validate_schema(root, 'versions') + self.assertTrue(root.xpath('/ns:versions', namespaces=NS)) version_elems = root.xpath('ns:version', namespaces=NS) self.assertEqual(len(version_elems), 1) -- cgit