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
This commit is contained in:
Sabari Kumar Murugesan 2015-01-20 16:03:18 -08:00
parent dc19b11770
commit e278d14838
2 changed files with 16 additions and 4 deletions

View File

@ -25,10 +25,16 @@ def sort_url_by_qs_keys(url):
# non-deterministic ordering of the query string causing problems with unit # non-deterministic ordering of the query string causing problems with unit
# tests. # tests.
parsed = urlparse.urlparse(url) 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]) sorted_query = sorted(queries, key=lambda x: x[0])
encoded_sorted_query = urllib.urlencode(sorted_query, True) 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.params, encoded_sorted_query,
parsed.fragment) parsed.fragment)
return urlparse.urlunparse(url_parts) return urlparse.urlunparse(url_parts)

View File

@ -58,9 +58,9 @@ def format_location(host_ip, folder_name,
the component pieces. the component pieces.
""" """
scheme = 'vsphere' 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, % (scheme, host_ip, folder_name,
image_id, datastore_name, datacenter_path)) image_id, datacenter_path, datastore_name))
class FakeHTTPConnection(object): class FakeHTTPConnection(object):
@ -384,3 +384,9 @@ class TestStore(base.StoreBaseTest,
self.assertRaises(exceptions.BackendException, self.assertRaises(exceptions.BackendException,
self.store.add, self.store.add,
expected_image_id, image, expected_size) 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))