From fcf8e1d0b91ec28191f96783c367eaedfbd85a61 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Sun, 13 Oct 2013 09:47:08 -0400 Subject: [PATCH] python3: align the order of parameters for urlencode() In Python 3.3, hash randomization is enabled by default. It causes the iteration order of dicts and sets to be unpredictable and differ across Python runs. In the test case, the fixed expecting string will not match the test result, it is relying on the dict order. This change transforms the input dict to a sequence of two-element list, with fixed order, and update the related expecitng string in test case. Change-Id: I60d7bb3c4f940b76460ad5c417a1807915e0418e Signed-off-by: Chuck Short --- cinderclient/v1/volume_snapshots.py | 8 +++++++- cinderclient/v2/volume_snapshots.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cinderclient/v1/volume_snapshots.py b/cinderclient/v1/volume_snapshots.py index 0aa6495..ea0ed43 100644 --- a/cinderclient/v1/volume_snapshots.py +++ b/cinderclient/v1/volume_snapshots.py @@ -108,7 +108,13 @@ class SnapshotManager(base.ManagerWithFind): if val: qparams[opt] = val - query_string = "?%s" % urlencode(qparams) if qparams else "" + # Transform the dict to a sequence of two-element tuples in fixed + # order, then the encoded string will be consistent in Python 2&3. + if qparams: + new_qparams = sorted(qparams.items(), key=lambda x: x[0]) + query_string = "?%s" % urlencode(new_qparams) + else: + query_string = "" detail = "" if detailed: diff --git a/cinderclient/v2/volume_snapshots.py b/cinderclient/v2/volume_snapshots.py index 4e16ba8..a904f95 100644 --- a/cinderclient/v2/volume_snapshots.py +++ b/cinderclient/v2/volume_snapshots.py @@ -95,7 +95,13 @@ class SnapshotManager(base.ManagerWithFind): if val: qparams[opt] = val - query_string = "?%s" % urlencode(qparams) if qparams else "" + # Transform the dict to a sequence of two-element tuples in fixed + # order, then the encoded string will be consistent in Python 2&3. + if qparams: + new_qparams = sorted(qparams.items(), key=lambda x: x[0]) + query_string = "?%s" % urlencode(new_qparams) + else: + query_string = "" detail = "" if detailed: