diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 16:04:21 +0000 |
commit | 8a754e0858d922e955e71b253c139e071ecec432 (patch) | |
tree | 527d16e74bfd1840c85efd675fdecad056c54107 /test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py | |
parent | Initial commit. (diff) | |
download | ansible-core-upstream/2.14.3.tar.xz ansible-core-upstream/2.14.3.zip |
Adding upstream version 2.14.3.upstream/2.14.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py b/test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py new file mode 100644 index 0000000..aea8694 --- /dev/null +++ b/test/units/module_utils/facts/hardware/test_linux_get_cpu_info.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2019 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +from ansible.module_utils.facts.hardware import linux + +from . linux_data import CPU_INFO_TEST_SCENARIOS + + +def test_get_cpu_info(mocker): + module = mocker.Mock() + inst = linux.LinuxHardware(module) + + mocker.patch('os.path.exists', return_value=False) + mocker.patch('os.access', return_value=True) + for test in CPU_INFO_TEST_SCENARIOS: + mocker.patch('ansible.module_utils.facts.hardware.linux.get_file_lines', side_effect=[[], test['cpuinfo']]) + mocker.patch('os.sched_getaffinity', create=True, return_value=test['sched_getaffinity']) + module.run_command.return_value = (0, test['nproc_out'], '') + collected_facts = {'ansible_architecture': test['architecture']} + + assert test['expected_result'] == inst.get_cpu_facts(collected_facts=collected_facts) + + +def test_get_cpu_info_nproc(mocker): + module = mocker.Mock() + inst = linux.LinuxHardware(module) + + mocker.patch('os.path.exists', return_value=False) + mocker.patch('os.access', return_value=True) + for test in CPU_INFO_TEST_SCENARIOS: + mocker.patch('ansible.module_utils.facts.hardware.linux.get_file_lines', side_effect=[[], test['cpuinfo']]) + mocker.patch('os.sched_getaffinity', create=True, side_effect=AttributeError) + mocker.patch('ansible.module_utils.facts.hardware.linux.get_bin_path', return_value='/usr/bin/nproc') + module.run_command.return_value = (0, test['nproc_out'], '') + collected_facts = {'ansible_architecture': test['architecture']} + + assert test['expected_result'] == inst.get_cpu_facts(collected_facts=collected_facts) + + +def test_get_cpu_info_missing_arch(mocker): + module = mocker.Mock() + inst = linux.LinuxHardware(module) + + # ARM and Power will report incorrect processor count if architecture is not available + mocker.patch('os.path.exists', return_value=False) + mocker.patch('os.access', return_value=True) + for test in CPU_INFO_TEST_SCENARIOS: + mocker.patch('ansible.module_utils.facts.hardware.linux.get_file_lines', side_effect=[[], test['cpuinfo']]) + mocker.patch('os.sched_getaffinity', create=True, return_value=test['sched_getaffinity']) + + module.run_command.return_value = (0, test['nproc_out'], '') + + test_result = inst.get_cpu_facts() + + if test['architecture'].startswith(('armv', 'aarch', 'ppc')): + assert test['expected_result'] != test_result + else: + assert test['expected_result'] == test_result |