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