diff options
Diffstat (limited to 'conductor')
-rw-r--r-- | conductor/conductor/data/plugins/inventory_provider/hpa_utils.py | 48 | ||||
-rw-r--r-- | conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py | 65 |
2 files changed, 113 insertions, 0 deletions
diff --git a/conductor/conductor/data/plugins/inventory_provider/hpa_utils.py b/conductor/conductor/data/plugins/inventory_provider/hpa_utils.py new file mode 100644 index 0000000..6ed622c --- /dev/null +++ b/conductor/conductor/data/plugins/inventory_provider/hpa_utils.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# +# ------------------------------------------------------------------------- +# Copyright (c) 2018 Intel Corporation Intellectual Property +# +# 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. +# +# ------------------------------------------------------------------------- +# + +'''Utility functions for + Hardware Platform Awareness (HPA) constraint plugin''' + +# python imports + +# Conductor imports + +# Third-party library imports +from oslo_log import log + +LOG = log.getLogger(__name__) + + +def match_all_operator(big_list, small_list): + ''' + Match ALL operator for HPA + Check if smaller list is a subset of bigger list + :param big_list: bigger list + :param small_list: smaller list + :return: True or False + ''' + if not big_list or not small_list: + return False + + big_set = set(big_list) + small_set = set(small_list) + + return small_set.issubset(big_set) diff --git a/conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py b/conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py new file mode 100644 index 0000000..f5e36c9 --- /dev/null +++ b/conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py @@ -0,0 +1,65 @@ +# +# ------------------------------------------------------------------------- +# Copyright (c) 2018 Intel Corporation Intellectual Property +# +# 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. +# +# ------------------------------------------------------------------------- +# +import unittest + +from conductor.data.plugins.inventory_provider import hpa_utils + + +class TestHPAUtils(unittest.TestCase): + + def test_match_all_operator(self): + self.assertEqual(False, hpa_utils.match_all_operator(None, None)) + self.assertEqual(False, hpa_utils.match_all_operator([], None)) + self.assertEqual(False, hpa_utils.match_all_operator(None, [])) + self.assertEqual(False, hpa_utils.match_all_operator([], [])) + + big_list = ['aes', 'sse', 'avx', 'smt'] + small_list = ['avx', 'aes'] + self.assertEqual(True, + hpa_utils.match_all_operator(big_list, small_list)) + + big_list = ['aes', 'sse', 'avx', 'smt'] + small_list = ['avx', 'pcmulqdq'] + self.assertEqual(False, + hpa_utils.match_all_operator(big_list, small_list)) + + big_list = ['aes', 'sse', 'avx', 'smt'] + small_list = ['aes', 'sse', 'avx', 'smt'] + self.assertEqual(True, + hpa_utils.match_all_operator(big_list, small_list)) + + big_list = ['aes', 'sse', 'avx', 'smt'] + small_list = ['smt'] + self.assertEqual(True, + hpa_utils.match_all_operator(big_list, small_list)) + + big_list = ['aes'] + small_list = ['smt'] + self.assertEqual(False, + hpa_utils.match_all_operator(big_list, small_list)) + + # check the order of elements + big_list = ['aes', 'sse', 'avx', 'smt'] + small_list = sorted(big_list) + self.assertEqual(True, + hpa_utils.match_all_operator(big_list, small_list)) + + +if __name__ == "__main__": + unittest.main() |