From e278d148387ec37c8bb2d9465fb4439b3fb4cf19 Mon Sep 17 00:00:00 2001 From: Sabari Kumar Murugesan Date: Tue, 20 Jan 2015 16:03:18 -0800 Subject: [PATCH] Fix sorting query string keys for arbitrary url schemes In Python2.6, urlparse module does not parse the query string from from the url for arbitrary schemes. http://bugs.python.org/issue9374 Thus, our test utility method does not sort the qs keys for vmware store url scheme (which is vsphere). This causes random failures in py26 unit tests. Change-Id: I1f5da01bfb346b547c1f70b7de9b18585c197f29 --- glance_store/tests/utils.py | 10 ++++++++-- tests/unit/test_vmware_store.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/glance_store/tests/utils.py b/glance_store/tests/utils.py index 52b08080..d73470e7 100644 --- a/glance_store/tests/utils.py +++ b/glance_store/tests/utils.py @@ -25,10 +25,16 @@ def sort_url_by_qs_keys(url): # non-deterministic ordering of the query string causing problems with unit # tests. parsed = urlparse.urlparse(url) - queries = urlparse.parse_qsl(parsed.query, True) + # In python2.6, for arbitrary url schemes, query string + # is not parsed from url. http://bugs.python.org/issue9374 + path = parsed.path + query = parsed.query + if not query: + path, query = parsed.path.split('?', 1) + queries = urlparse.parse_qsl(query, True) sorted_query = sorted(queries, key=lambda x: x[0]) encoded_sorted_query = urllib.urlencode(sorted_query, True) - url_parts = (parsed.scheme, parsed.netloc, parsed.path, + url_parts = (parsed.scheme, parsed.netloc, path, parsed.params, encoded_sorted_query, parsed.fragment) return urlparse.urlunparse(url_parts) diff --git a/tests/unit/test_vmware_store.py b/tests/unit/test_vmware_store.py index b69a67c6..773e135f 100644 --- a/tests/unit/test_vmware_store.py +++ b/tests/unit/test_vmware_store.py @@ -58,9 +58,9 @@ def format_location(host_ip, folder_name, the component pieces. """ scheme = 'vsphere' - return ("%s://%s/folder%s/%s?dsName=%s&dcPath=%s" + return ("%s://%s/folder%s/%s?dcPath=%s&dsName=%s" % (scheme, host_ip, folder_name, - image_id, datastore_name, datacenter_path)) + image_id, datacenter_path, datastore_name)) class FakeHTTPConnection(object): @@ -384,3 +384,9 @@ class TestStore(base.StoreBaseTest, self.assertRaises(exceptions.BackendException, self.store.add, expected_image_id, image, expected_size) + + def test_qs_sort_with_literal_question_mark(self): + url = 'scheme://example.com/path?key2=val2&key1=val1?sort=true' + exp_url = 'scheme://example.com/path?key1=val1%3Fsort%3Dtrue&key2=val2' + self.assertEqual(exp_url, + utils.sort_url_by_qs_keys(url))