diff options
| author | Naveed Massjouni <naveedm9@gmail.com> | 2011-09-23 15:03:35 -0400 |
|---|---|---|
| committer | Naveed Massjouni <naveedm9@gmail.com> | 2011-09-23 15:06:37 -0400 |
| commit | 4e94ec1a0a566b66f09b734e6ffe964b4b3b4bee (patch) | |
| tree | 2cda3576e8a4af637809058b16eb8d843fb9fd68 | |
| parent | e15258def26bd3b4781486a7a85ca599cbdb07f6 (diff) | |
Adding xml schema validation for /versions resource.
Change-Id: I8e0b7695743844614b3e7c5a08bb88d3a7500b5d
| -rw-r--r-- | nova/api/openstack/schemas/v1.1/version.rng | 17 | ||||
| -rw-r--r-- | nova/api/openstack/schemas/v1.1/versions.rng | 11 | ||||
| -rw-r--r-- | nova/api/openstack/xmlutil.py | 5 | ||||
| -rw-r--r-- | nova/tests/api/openstack/test_versions.py | 14 |
4 files changed, 46 insertions, 1 deletions
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 @@ +<element name="version" ns="http://docs.openstack.org/compute/api/v1.1" + xmlns="http://relaxng.org/ns/structure/1.0"> + <attribute name="id"/> + <attribute name="status"/> + <attribute name="updated"/> + <element name="media-types"> + <oneOrMore> + <element name="media-type"> + <attribute name="base"/> + <attribute name="type"/> + </element> + </oneOrMore> + </element> + <zeroOrMore> + <externalRef href="../atom-link.rng"/> + </zeroOrMore> +</element> 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 @@ +<element name="versions" xmlns="http://relaxng.org/ns/structure/1.0" + ns="http://docs.openstack.org/compute/api/v1.1"> + <oneOrMore> + <element name="version"> + <attribute name="id"/> + <attribute name="status"/> + <attribute name="updated"/> + <externalRef href="../atom-link.rng"/> + </element> + </oneOrMore> +</element> 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) |
