From f231f2166963c86b5193ace905bf6dbfd50fb61f Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Fri, 19 Jul 2019 11:33:05 -0400 Subject: [PATCH] Handle situation where ansible_architecure may not be defined when gathering facts (#55466) --- .../facts-linux-cpu-arm-architecture.yml | 2 ++ lib/ansible/module_utils/facts/hardware/linux.py | 2 +- .../facts/hardware/test_linux_get_cpu_info.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/facts-linux-cpu-arm-architecture.yml diff --git a/changelogs/fragments/facts-linux-cpu-arm-architecture.yml b/changelogs/fragments/facts-linux-cpu-arm-architecture.yml new file mode 100644 index 00000000000..d5e61fbe286 --- /dev/null +++ b/changelogs/fragments/facts-linux-cpu-arm-architecture.yml @@ -0,0 +1,2 @@ +bugfixes: + - facts - handle situation where ``ansible_architecture`` may not be defined (https://github.com/ansible/ansible/issues/55400) diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py index 5f37f8e3704..19ca6e47999 100644 --- a/lib/ansible/module_utils/facts/hardware/linux.py +++ b/lib/ansible/module_utils/facts/hardware/linux.py @@ -240,7 +240,7 @@ class LinuxHardware(Hardware): # The fields for ARM CPUs do not always include 'vendor_id' or 'model name', # and sometimes includes both 'processor' and 'Processor'. # Always use 'processor' count for ARM systems - if collected_facts.get('ansible_architecture').startswith(('armv', 'aarch')): + if collected_facts.get('ansible_architecture', '').startswith(('armv', 'aarch')): i = processor_occurence # FIXME 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 index bf34c62302e..542ed4bce2d 100644 --- 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 @@ -20,3 +20,19 @@ def test_get_cpu_info(mocker): mocker.patch('ansible.module_utils.facts.hardware.linux.get_file_lines', side_effect=[[], test['cpuinfo']]) 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 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']]) + test_result = inst.get_cpu_facts() + if test['architecture'].startswith(('armv', 'aarch')): + assert test['expected_result'] != test_result + else: + assert test['expected_result'] == test_result