Normalize numa_topology for consistent equality tests

To ensure comparing numa_topology JSON dicts is always consistent,
this patch normalizes them by recursively sorting the keys, and
sorting any values that are lists. To make for easier unit testing,
the normalization method lives in a new utils.py file.

Change-Id: I0f56f037ea387ecc028c4a342c7b71759fdcf7da
This commit is contained in:
Archit Modi 2020-02-05 11:02:38 -05:00 committed by Artom Lifshitz
parent 09d56749ea
commit c4ec528269
3 changed files with 65 additions and 0 deletions

View File

@ -37,6 +37,7 @@ from tempest import config
from whitebox_tempest_plugin.api.compute import base from whitebox_tempest_plugin.api.compute import base
from whitebox_tempest_plugin import exceptions from whitebox_tempest_plugin import exceptions
from whitebox_tempest_plugin.services import clients from whitebox_tempest_plugin.services import clients
from whitebox_tempest_plugin import utils as whitebox_utils
from oslo_log import log as logging from oslo_log import log as logging
@ -173,6 +174,7 @@ class BasePinningTest(base.BaseWhiteboxComputeTest):
'WHERE instance_uuid = "%s"' % instance_uuid) 'WHERE instance_uuid = "%s"' % instance_uuid)
numa_topology = jsonutils.loads( numa_topology = jsonutils.loads(
cursor.fetchone()['numa_topology']) cursor.fetchone()['numa_topology'])
numa_topology = whitebox_utils.normalize_json(numa_topology)
return numa_topology return numa_topology

View File

@ -0,0 +1,32 @@
# Copyright 2020 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from whitebox_tempest_plugin.tests import base
from whitebox_tempest_plugin import utils
class UtilsTestCase(base.WhiteboxPluginTestCase):
def test_normalize_json(self):
json = {'2': [2, 3, 1],
'1': True,
'4': {'b': [2, 1],
'a': [3, 0]},
'3': ['b', 'a', 'z']}
self.assertEqual({'1': True,
'2': [1, 2, 3],
'3': ['a', 'b', 'z'],
'4': {'a': [0, 3],
'b': [1, 2]}},
utils.normalize_json(json))

View File

@ -0,0 +1,31 @@
# Copyright 2020 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo_serialization import jsonutils
def normalize_json(json):
"""Normalizes a JSON dict for consistent equality tests. Sorts the keys,
and sorts any values that are lists.
"""
def sort_list_values(json):
for k, v in json.items():
if isinstance(v, list):
v.sort()
elif isinstance(v, dict):
sort_list_values(v)
json = jsonutils.loads(jsonutils.dumps(json, sort_keys=True))
sort_list_values(json)
return json