From ecdd79b04aa65b5e8740c5a2a5fdca2530f2d652 Mon Sep 17 00:00:00 2001 From: Kui Shi Date: Sun, 29 Sep 2013 21:45:36 +0800 Subject: [PATCH] py33: align the order of parameters for urlencode() In Python 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. Close-Bug #1231871 Change-Id: Ia998cdb6978fc024dd0d3c9bd161fbdebe68638a --- novaclient/tests/v1_1/contrib/test_migrations.py | 4 ++-- novaclient/tests/v1_1/test_shell.py | 4 ++-- novaclient/v1_1/contrib/migrations.py | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/novaclient/tests/v1_1/contrib/test_migrations.py b/novaclient/tests/v1_1/contrib/test_migrations.py index 7b49c4cc7..2b4d4cb03 100644 --- a/novaclient/tests/v1_1/contrib/test_migrations.py +++ b/novaclient/tests/v1_1/contrib/test_migrations.py @@ -36,7 +36,7 @@ class MigrationsTest(utils.TestCase): ml = cs.migrations.list('host1', 'finished', 'child1') cs.assert_called('GET', - '/os-migrations?status=finished&host=host1' - '&cell_name=child1') + '/os-migrations?cell_name=child1&host=host1' + '&status=finished') for m in ml: self.assertTrue(isinstance(m, migrations.Migration)) diff --git a/novaclient/tests/v1_1/test_shell.py b/novaclient/tests/v1_1/test_shell.py index 48b08d9b1..dec56b218 100644 --- a/novaclient/tests/v1_1/test_shell.py +++ b/novaclient/tests/v1_1/test_shell.py @@ -1799,5 +1799,5 @@ class ShellTest(utils.TestCase): self.run_command('migration-list --host host1 --cell_name child1 ' '--status finished') self.assert_called('GET', - '/os-migrations?status=finished&host=host1' - '&cell_name=child1') + '/os-migrations?cell_name=child1&host=host1' + '&status=finished') diff --git a/novaclient/v1_1/contrib/migrations.py b/novaclient/v1_1/contrib/migrations.py index fc7c89213..9322849aa 100644 --- a/novaclient/v1_1/contrib/migrations.py +++ b/novaclient/v1_1/contrib/migrations.py @@ -44,7 +44,11 @@ class MigrationManager(base.ManagerWithFind): if cell_name: opts['cell_name'] = cell_name - query_string = "?%s" % urlutils.urlencode(opts) if opts 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. + new_opts = sorted(opts.items(), key=lambda x: x[0]) + + query_string = "?%s" % urlutils.urlencode(new_opts) if new_opts else "" return self._list("/os-migrations%s" % query_string, "migrations")