From c4ec528269def17d9e9d409f2e50a6ac8079d571 Mon Sep 17 00:00:00 2001 From: Archit Modi Date: Wed, 5 Feb 2020 11:02:38 -0500 Subject: [PATCH] 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 --- .../api/compute/test_cpu_pinning.py | 2 ++ whitebox_tempest_plugin/tests/test_utils.py | 32 +++++++++++++++++++ whitebox_tempest_plugin/utils.py | 31 ++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 whitebox_tempest_plugin/tests/test_utils.py create mode 100644 whitebox_tempest_plugin/utils.py diff --git a/whitebox_tempest_plugin/api/compute/test_cpu_pinning.py b/whitebox_tempest_plugin/api/compute/test_cpu_pinning.py index 58acd1a5..a8487af3 100644 --- a/whitebox_tempest_plugin/api/compute/test_cpu_pinning.py +++ b/whitebox_tempest_plugin/api/compute/test_cpu_pinning.py @@ -37,6 +37,7 @@ from tempest import config from whitebox_tempest_plugin.api.compute import base from whitebox_tempest_plugin import exceptions from whitebox_tempest_plugin.services import clients +from whitebox_tempest_plugin import utils as whitebox_utils from oslo_log import log as logging @@ -173,6 +174,7 @@ class BasePinningTest(base.BaseWhiteboxComputeTest): 'WHERE instance_uuid = "%s"' % instance_uuid) numa_topology = jsonutils.loads( cursor.fetchone()['numa_topology']) + numa_topology = whitebox_utils.normalize_json(numa_topology) return numa_topology diff --git a/whitebox_tempest_plugin/tests/test_utils.py b/whitebox_tempest_plugin/tests/test_utils.py new file mode 100644 index 00000000..34ba1e80 --- /dev/null +++ b/whitebox_tempest_plugin/tests/test_utils.py @@ -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)) diff --git a/whitebox_tempest_plugin/utils.py b/whitebox_tempest_plugin/utils.py new file mode 100644 index 00000000..74540689 --- /dev/null +++ b/whitebox_tempest_plugin/utils.py @@ -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