From 77c4788975869ccc4a4cb0f71fc9ca23bad088dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Albert?= Date: Thu, 3 Dec 2015 11:53:47 +0100 Subject: [PATCH] Fixed random failures in storage tests A bug related to list order in samples or result resulted in tests sometimes failing. This is due to python3 not ordering dict indexes. Change-Id: I276a2e9c896d94eaabb9206bd0123c5704b72fcb --- cloudkitty/tests/samples.py | 5 ++++- cloudkitty/tests/storage/test_storage.py | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cloudkitty/tests/samples.py b/cloudkitty/tests/samples.py index 5bc40a8a..3e58932c 100644 --- a/cloudkitty/tests/samples.py +++ b/cloudkitty/tests/samples.py @@ -18,6 +18,8 @@ import copy import decimal +import six + from cloudkitty import utils as ck_utils TENANT = 'f266f30b11f246b589fd266f85eeec39' @@ -107,7 +109,8 @@ def split_storage_data(raw_data): frame['period']['begin'] = ck_utils.ts2iso(frame['period']['begin']) frame['period']['end'] = ck_utils.ts2iso(frame['period']['end']) usage_buffer = frame.pop('usage') - for service, data in usage_buffer.items(): + # Sort to have a consistent result as we are converting it to a list + for service, data in sorted(six.iteritems(usage_buffer)): new_frame = copy.deepcopy(frame) new_frame['usage'] = {service: data} new_frame['usage'][service][0]['tenant_id'] = TENANT diff --git a/cloudkitty/tests/storage/test_storage.py b/cloudkitty/tests/storage/test_storage.py index 66951bde..d0ef0c86 100644 --- a/cloudkitty/tests/storage/test_storage.py +++ b/cloudkitty/tests/storage/test_storage.py @@ -18,6 +18,7 @@ import copy import mock +import six import sqlalchemy import testscenarios @@ -118,7 +119,7 @@ class StorageTest(tests.TestCase): def test_send_nodata_between_data(self): working_data = copy.deepcopy(samples.RATED_DATA) for period in working_data: - for service, data in sorted(period['usage'].items()): + for service, data in sorted(six.iteritems(period['usage'])): sub_data = [{ 'period': period['period'], 'usage': { @@ -148,6 +149,11 @@ class StorageTest(tests.TestCase): # We only stored the first timeframe, the second one is waiting for a # commit or an append with the next timeframe. del expected_data[2] + # NOTE(sheeprine): Quick and dirty sort (ensure result consistency, + # order is not significant to the test result) + if 'image' in stored_data[0]['usage']: + stored_data[0]['usage'], stored_data[1]['usage'] = ( + stored_data[1]['usage'], stored_data[0]['usage']) self.assertEqual( expected_data, stored_data)