diff --git a/.github/workflows/codespell.yml b/.github/workflows/codespell.yml index 24add3fb14..4921bc907e 100644 --- a/.github/workflows/codespell.yml +++ b/.github/workflows/codespell.yml @@ -23,7 +23,7 @@ jobs: ./repos/system_upgrade/el8toel9/actors/xorgdrvfact/tests/files/journalctl-xorg-intel,\ ./repos/system_upgrade/el8toel9/actors/xorgdrvfact/tests/files/journalctl-xorg-qxl,\ ./repos/system_upgrade/el8toel9/actors/xorgdrvfact/tests/files/journalctl-xorg-without-qxl,\ - ./repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_s390x,\ + ./repos/system_upgrade/common/actors/scancpu/tests/files,\ ./etc/leapp/files/device_driver_deprecation_data.json,\ ./etc/leapp/files/pes-events.json,\ ./etc/leapp/files/repomap.json,\ diff --git a/repos/system_upgrade/common/actors/scancpu/libraries/scancpu.py b/repos/system_upgrade/common/actors/scancpu/libraries/scancpu.py index 9de50fae04..7451066a5b 100644 --- a/repos/system_upgrade/common/actors/scancpu/libraries/scancpu.py +++ b/repos/system_upgrade/common/actors/scancpu/libraries/scancpu.py @@ -1,22 +1,41 @@ +import json import re from leapp.libraries.common.config import architecture +from leapp.libraries.common.config.version import get_source_major_version from leapp.libraries.stdlib import api, CalledProcessError, run from leapp.models import CPUInfo, DetectedDeviceOrDriver, DeviceDriverDeprecationData -LSCPU_NAME_VALUE = re.compile(r'(?P[^:]+):\s+(?P.+)\n?') +LSCPU_NAME_VALUE = re.compile(r'^(?P[^:]+):[^\S\n]+(?P.+)\n?', flags=re.MULTILINE) PPC64LE_MODEL = re.compile(r'\d+\.\d+ \(pvr (?P[0-9a-fA-F]+) 0*[0-9a-fA-F]+\)') -def _get_lscpu_output(): +def _get_lscpu_output(output_json=False): try: - result = run(['lscpu']) + result = run(['lscpu', '-J' if output_json else '']) return result.get('stdout', '') except (OSError, CalledProcessError): api.current_logger().debug('Executing `lscpu` failed', exc_info=True) return '' +def _parse_lscpu_output(): + if get_source_major_version() == '7': + return dict(LSCPU_NAME_VALUE.findall(_get_lscpu_output())) + + lscpu = _get_lscpu_output(output_json=True) + try: + parsed_json = json.loads(lscpu) + # The json contains one entry "lscpu" which is a list of dictionaries + # with 2 keys "field" (name of the field from lscpu) and "data" (value + # of the field). + return dict((entry['field'].rstrip(':'), entry['data']) for entry in parsed_json['lscpu']) + except ValueError: + api.current_logger().debug('Failed to parse json output from `lscpu`. Got:\n{}'.format(lscpu)) + + return dict() + + def _get_cpu_flags(lscpu): flags = lscpu.get('Flags', '') return flags.split() @@ -128,24 +147,16 @@ def _find_deprecation_data_entries(lscpu): arch_prefix, is_detected = architecture.ARCH_ARM64, _is_detected_aarch64 if arch_prefix and is_detected: - return [ - _to_detected_device(entry) for entry in _get_cpu_entries_for(arch_prefix) - if is_detected(lscpu, entry) - ] + return [_to_detected_device(entry) for entry in _get_cpu_entries_for(arch_prefix) if is_detected(lscpu, entry)] api.current_logger().warning('Unsupported platform could not detect relevant CPU information') return [] def process(): - lscpu = dict(LSCPU_NAME_VALUE.findall(_get_lscpu_output())) + lscpu = _parse_lscpu_output() api.produce(*_find_deprecation_data_entries(lscpu)) # Backwards compatibility machine_type = lscpu.get('Machine type') flags = _get_cpu_flags(lscpu) - api.produce( - CPUInfo( - machine_type=int(machine_type) if machine_type else None, - flags=flags - ) - ) + api.produce(CPUInfo(machine_type=int(machine_type) if machine_type else None, flags=flags)) diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/json/invalid b/repos/system_upgrade/common/actors/scancpu/tests/files/json/invalid new file mode 100644 index 0000000000..422c2b7ab3 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/json/invalid @@ -0,0 +1,2 @@ +a +b diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_aarch64 b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_aarch64 new file mode 100644 index 0000000000..7918669553 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_aarch64 @@ -0,0 +1,29 @@ +{ + "lscpu": [ + {"field": "Architecture:", "data": "aarch64"}, + {"field": "Byte Order:", "data": "Little Endian"}, + {"field": "CPU(s):", "data": "160"}, + {"field": "On-line CPU(s) list:", "data": "0-159"}, + {"field": "Thread(s) per core:", "data": "1"}, + {"field": "Core(s) per socket:", "data": "80"}, + {"field": "Socket(s):", "data": "2"}, + {"field": "NUMA node(s):", "data": "4"}, + {"field": "Vendor ID:", "data": "ARM"}, + {"field": "BIOS Vendor ID:", "data": "Ampere(R)"}, + {"field": "Model:", "data": "1"}, + {"field": "Model name:", "data": "Neoverse-N1"}, + {"field": "BIOS Model name:", "data": "Ampere(R) Altra(R) Processor"}, + {"field": "Stepping:", "data": "r3p1"}, + {"field": "CPU max MHz:", "data": "3000.0000"}, + {"field": "CPU min MHz:", "data": "1000.0000"}, + {"field": "BogoMIPS:", "data": "50.00"}, + {"field": "L1d cache:", "data": "64K"}, + {"field": "L1i cache:", "data": "64K"}, + {"field": "L2 cache:", "data": "1024K"}, + {"field": "NUMA node0 CPU(s):", "data": "0-79"}, + {"field": "NUMA node1 CPU(s):", "data": "80-159"}, + {"field": "NUMA node2 CPU(s):", "data": null}, + {"field": "NUMA node3 CPU(s):", "data": null}, + {"field": "Flags:", "data": "fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs"} + ] +} diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_ppc64le b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_ppc64le new file mode 100644 index 0000000000..cc51c4ac51 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_ppc64le @@ -0,0 +1,19 @@ +{ + "lscpu": [ + {"field": "Architecture:", "data": "ppc64le"}, + {"field": "Byte Order:", "data": "Little Endian"}, + {"field": "CPU(s):", "data": "8"}, + {"field": "On-line CPU(s) list:", "data": "0-7"}, + {"field": "Thread(s) per core:", "data": "1"}, + {"field": "Core(s) per socket:", "data": "1"}, + {"field": "Socket(s):", "data": "8"}, + {"field": "NUMA node(s):", "data": "1"}, + {"field": "Model:", "data": "2.1 (pvr 004b 0201)"}, + {"field": "Model name:", "data": "POWER8E (raw), altivec supported"}, + {"field": "Hypervisor vendor:", "data": "KVM"}, + {"field": "Virtualization type:", "data": "para"}, + {"field": "L1d cache:", "data": "64K"}, + {"field": "L1i cache:", "data": "32K"}, + {"field": "NUMA node0 CPU(s):", "data": "0-7"} + ] +} diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_s390x b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_s390x new file mode 100644 index 0000000000..950da2debf --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_s390x @@ -0,0 +1,30 @@ +{ + "lscpu": [ + {"field": "Architecture:", "data": "s390x"}, + {"field": "CPU op-mode(s):", "data": "32-bit, 64-bit"}, + {"field": "Byte Order:", "data": "Big Endian"}, + {"field": "CPU(s):", "data": "4"}, + {"field": "On-line CPU(s) list:", "data": "0-3"}, + {"field": "Thread(s) per core:", "data": "1"}, + {"field": "Core(s) per socket:", "data": "1"}, + {"field": "Socket(s) per book:", "data": "1"}, + {"field": "Book(s) per drawer:", "data": "1"}, + {"field": "Drawer(s):", "data": "4"}, + {"field": "NUMA node(s):", "data": "1"}, + {"field": "Vendor ID:", "data": "IBM/S390"}, + {"field": "Machine type:", "data": "3931"}, + {"field": "CPU dynamic MHz:", "data": "5200"}, + {"field": "CPU static MHz:", "data": "5200"}, + {"field": "BogoMIPS:", "data": "3331.00"}, + {"field": "Hypervisor:", "data": "KVM/Linux"}, + {"field": "Hypervisor vendor:", "data": "KVM"}, + {"field": "Virtualization type:", "data": "full"}, + {"field": "Dispatching mode:", "data": "horizontal"}, + {"field": "L1d cache:", "data": "128K"}, + {"field": "L1i cache:", "data": "128K"}, + {"field": "L2 cache:", "data": "32768K"}, + {"field": "L3 cache:", "data": "262144K"}, + {"field": "NUMA node0 CPU(s):", "data": "0-3"}, + {"field": "Flags:", "data": "esan3 zarch stfle msa ldisp eimm dfp edat etf3eh highgprs te vx vxd vxe gs vxe2 vxp sort dflt vxp2 nnpa sie"} + ] +} diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_x86_64 b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_x86_64 new file mode 100644 index 0000000000..da75a3faa2 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/json/lscpu_x86_64 @@ -0,0 +1,31 @@ +{ + "lscpu": [ + {"field": "Architecture:", "data": "x86_64"}, + {"field": "CPU op-mode(s):", "data": "32-bit, 64-bit"}, + {"field": "Byte Order:", "data": "Little Endian"}, + {"field": "CPU(s):", "data": "2"}, + {"field": "On-line CPU(s) list:", "data": "0,1"}, + {"field": "Thread(s) per core:", "data": "1"}, + {"field": "Core(s) per socket:", "data": "1"}, + {"field": "Socket(s):", "data": "2"}, + {"field": "NUMA node(s):", "data": "1"}, + {"field": "Vendor ID:", "data": "GenuineIntel"}, + {"field": "BIOS Vendor ID:", "data": "QEMU"}, + {"field": "CPU family:", "data": "6"}, + {"field": "Model:", "data": "165"}, + {"field": "Model name:", "data": "Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz"}, + {"field": "BIOS Model name:", "data": "pc-i440fx-7.2"}, + {"field": "Stepping:", "data": "2"}, + {"field": "CPU MHz:", "data": "2712.006"}, + {"field": "BogoMIPS:", "data": "5424.01"}, + {"field": "Virtualization:", "data": "VT-x"}, + {"field": "Hypervisor vendor:", "data": "KVM"}, + {"field": "Virtualization type:", "data": "full"}, + {"field": "L1d cache:", "data": "32K"}, + {"field": "L1i cache:", "data": "32K"}, + {"field": "L2 cache:", "data": "4096K"}, + {"field": "L3 cache:", "data": "16384K"}, + {"field": "NUMA node0 CPU(s):", "data": "0,1"}, + {"field": "Flags:", "data": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm ida arat pln pts md_clear flush_l1d"} + ] +} diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_aarch64 b/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_aarch64 deleted file mode 100644 index 5b6c3470a7..0000000000 --- a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_aarch64 +++ /dev/null @@ -1,26 +0,0 @@ -Architecture: aarch64 -CPU op-mode(s): 32-bit, 64-bit -Byte Order: Little Endian -CPU(s): 5 -On-line CPU(s) list: 0-4 -Vendor ID: APM -Model name: - -Model: 2 -Thread(s) per core: 1 -Core(s) per cluster: 5 -Socket(s): - -Cluster(s): 1 -Stepping: 0x3 -BogoMIPS: 80.00 -Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid -NUMA node(s): 1 -NUMA node0 CPU(s): 0-4 -Vulnerability Itlb multihit: Not affected -Vulnerability L1tf: Not affected -Vulnerability Mds: Not affected -Vulnerability Meltdown: Mitigation; PTI -Vulnerability Spec store bypass: Vulnerable -Vulnerability Spectre v1: Mitigation; __user pointer sanitization -Vulnerability Spectre v2: Vulnerable -Vulnerability Srbds: Not affected -Vulnerability Tsx async abort: Not affected diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_ppc64le b/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_ppc64le deleted file mode 100644 index 259dd19d5b..0000000000 --- a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_ppc64le +++ /dev/null @@ -1,24 +0,0 @@ -Architecture: ppc64le -Byte Order: Little Endian -CPU(s): 8 -On-line CPU(s) list: 0-7 -Model name: POWER9 (architected), altivec supported -Model: 2.2 (pvr 004e 1202) -Thread(s) per core: 1 -Core(s) per socket: 1 -Socket(s): 8 -Hypervisor vendor: KVM -Virtualization type: para -L1d cache: 256 KiB (8 instances) -L1i cache: 256 KiB (8 instances) -NUMA node(s): 1 -NUMA node0 CPU(s): 0-7 -Vulnerability Itlb multihit: Not affected -Vulnerability L1tf: Mitigation; RFI Flush, L1D private per thread -Vulnerability Mds: Not affected -Vulnerability Meltdown: Mitigation; RFI Flush, L1D private per thread -Vulnerability Spec store bypass: Mitigation; Kernel entry/exit barrier (eieio) -Vulnerability Spectre v1: Mitigation; __user pointer sanitization, ori31 speculation barrier enabled -Vulnerability Spectre v2: Mitigation; Software count cache flush (hardware accelerated), Software link stack flush -Vulnerability Srbds: Not affected -Vulnerability Tsx async abort: Not affected diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_s390x b/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_s390x deleted file mode 100644 index 3c0a0ac390..0000000000 --- a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_s390x +++ /dev/null @@ -1,38 +0,0 @@ -Architecture: s390x -CPU op-mode(s): 32-bit, 64-bit -Byte Order: Big Endian -CPU(s): 2 -On-line CPU(s) list: 0,1 -Vendor ID: IBM/S390 -Model name: - -Machine type: 2827 -Thread(s) per core: 1 -Core(s) per socket: 1 -Socket(s) per book: 1 -Book(s) per drawer: 1 -Drawer(s): 2 -CPU dynamic MHz: 5200 -CPU static MHz: 5200 -BogoMIPS: 3241.00 -Dispatching mode: horizontal -Flags: esan3 zarch stfle msa ldisp eimm dfp edat etf3eh highgprs te vx vxd vxe gs vxe2 vxp sort dflt sie -Hypervisor: z/VM 7.2.0 -Hypervisor vendor: IBM -Virtualization type: full -L1d cache: 256 KiB (2 instances) -L1i cache: 256 KiB (2 instances) -L2d cache: 8 MiB (2 instances) -L2i cache: 8 MiB (2 instances) -L3 cache: 256 MiB -L4 cache: 960 MiB -NUMA node(s): 1 -NUMA node0 CPU(s): 0,1 -Vulnerability Itlb multihit: Not affected -Vulnerability L1tf: Not affected -Vulnerability Mds: Not affected -Vulnerability Meltdown: Not affected -Vulnerability Spec store bypass: Not affected -Vulnerability Spectre v1: Mitigation; __user pointer sanitization -Vulnerability Spectre v2: Mitigation; etokens -Vulnerability Srbds: Not affected -Vulnerability Tsx async abort: Not affected diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_aarch64 b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_aarch64 new file mode 100644 index 0000000000..3b9619ef33 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_aarch64 @@ -0,0 +1,25 @@ +Architecture: aarch64 +Byte Order: Little Endian +CPU(s): 160 +On-line CPU(s) list: 0-159 +Thread(s) per core: 1 +Core(s) per socket: 80 +Socket(s): 2 +NUMA node(s): 4 +Vendor ID: ARM +BIOS Vendor ID: Ampere(R) +Model: 1 +Model name: Neoverse-N1 +BIOS Model name: Ampere(R) Altra(R) Processor +Stepping: r3p1 +CPU max MHz: 3000.0000 +CPU min MHz: 1000.0000 +BogoMIPS: 50.00 +L1d cache: 64K +L1i cache: 64K +L2 cache: 1024K +NUMA node0 CPU(s): 0-79 +NUMA node1 CPU(s): 80-159 +NUMA node2 CPU(s): +NUMA node3 CPU(s): +Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_empty_field b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_empty_field new file mode 100644 index 0000000000..f830b7feff --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_empty_field @@ -0,0 +1,4 @@ +Empyt 1: +Empyt 2: +Empyt 3: +Flags: flag diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_ppc64le b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_ppc64le new file mode 100644 index 0000000000..07d2ed6510 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_ppc64le @@ -0,0 +1,15 @@ +Architecture: ppc64le +Byte Order: Little Endian +CPU(s): 8 +On-line CPU(s) list: 0-7 +Thread(s) per core: 1 +Core(s) per socket: 1 +Socket(s): 8 +NUMA node(s): 1 +Model: 2.1 (pvr 004b 0201) +Model name: POWER8E (raw), altivec supported +Hypervisor vendor: KVM +Virtualization type: para +L1d cache: 64K +L1i cache: 32K +NUMA node0 CPU(s): 0-7 diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_s390x b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_s390x new file mode 100644 index 0000000000..2c0de9f979 --- /dev/null +++ b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_s390x @@ -0,0 +1,26 @@ +Architecture: s390x +CPU op-mode(s): 32-bit, 64-bit +Byte Order: Big Endian +CPU(s): 4 +On-line CPU(s) list: 0-3 +Thread(s) per core: 1 +Core(s) per socket: 1 +Socket(s) per book: 1 +Book(s) per drawer: 1 +Drawer(s): 4 +NUMA node(s): 1 +Vendor ID: IBM/S390 +Machine type: 3931 +CPU dynamic MHz: 5200 +CPU static MHz: 5200 +BogoMIPS: 3331.00 +Hypervisor: KVM/Linux +Hypervisor vendor: KVM +Virtualization type: full +Dispatching mode: horizontal +L1d cache: 128K +L1i cache: 128K +L2 cache: 32768K +L3 cache: 262144K +NUMA node0 CPU(s): 0-3 +Flags: esan3 zarch stfle msa ldisp eimm dfp edat etf3eh highgprs te vx vxd vxe gs vxe2 vxp sort dflt vxp2 nnpa sie diff --git a/repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_x86_64 b/repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_x86_64 similarity index 100% rename from repos/system_upgrade/common/actors/scancpu/tests/files/lscpu_x86_64 rename to repos/system_upgrade/common/actors/scancpu/tests/files/txt/lscpu_x86_64 diff --git a/repos/system_upgrade/common/actors/scancpu/tests/test_scancpu.py b/repos/system_upgrade/common/actors/scancpu/tests/test_scancpu.py index 894fae08cc..dc9d1ffcc0 100644 --- a/repos/system_upgrade/common/actors/scancpu/tests/test_scancpu.py +++ b/repos/system_upgrade/common/actors/scancpu/tests/test_scancpu.py @@ -3,7 +3,6 @@ import pytest from leapp.libraries.actor import scancpu -from leapp.libraries.common import testutils from leapp.libraries.common.config.architecture import ( ARCH_ARM64, ARCH_PPC64LE, @@ -11,6 +10,7 @@ ARCH_SUPPORTED, ARCH_X86_64 ) +from leapp.libraries.common.testutils import CurrentActorMocked, logger_mocked, produce_mocked from leapp.libraries.stdlib import api from leapp.models import CPUInfo @@ -18,8 +18,12 @@ LSCPU = { ARCH_ARM64: { - "machine_type": None, - "flags": ['fp', 'asimd', 'evtstrm', 'aes', 'pmull', 'sha1', 'sha2', 'crc32', 'cpuid'], + "machine_type": + None, + "flags": [ + 'fp', 'asimd', 'evtstrm', 'aes', 'pmull', 'sha1', 'sha2', 'crc32', 'atomics', 'fphp', 'asimdhp', 'cpuid', + 'asimdrdm', 'lrcpc', 'dcpop', 'asimddp', 'ssbs' + ] }, ARCH_PPC64LE: { "machine_type": None, @@ -27,10 +31,10 @@ }, ARCH_S390X: { "machine_type": - 2827, + 3931, "flags": [ 'esan3', 'zarch', 'stfle', 'msa', 'ldisp', 'eimm', 'dfp', 'edat', 'etf3eh', 'highgprs', 'te', 'vx', 'vxd', - 'vxe', 'gs', 'vxe2', 'vxp', 'sort', 'dflt', 'sie' + 'vxe', 'gs', 'vxe2', 'vxp', 'sort', 'dflt', 'vxp2', 'nnpa', 'sie' ] }, ARCH_X86_64: { @@ -57,23 +61,34 @@ class mocked_get_cpuinfo(object): def __init__(self, filename): self.filename = filename - def __call__(self): + def __call__(self, output_json=False): """ Return lines of the self.filename test file located in the files directory. Those files contain /proc/cpuinfo content from several machines. """ - with open(os.path.join(CUR_DIR, 'files', self.filename), 'r') as fp: + + filename = self.filename + if output_json: + filename = os.path.join('json', filename) + else: + filename = os.path.join('txt', filename) + filename = os.path.join(CUR_DIR, 'files', filename) + + with open(filename, 'r') as fp: return '\n'.join(fp.read().splitlines()) @pytest.mark.parametrize("arch", ARCH_SUPPORTED) -def test_scancpu(monkeypatch, arch): +@pytest.mark.parametrize("version", ['7', '8']) +def test_scancpu(monkeypatch, arch, version): + + monkeypatch.setattr('leapp.libraries.actor.scancpu.get_source_major_version', lambda: version) mocked_cpuinfo = mocked_get_cpuinfo('lscpu_' + arch) monkeypatch.setattr(scancpu, '_get_lscpu_output', mocked_cpuinfo) - monkeypatch.setattr(api, 'produce', testutils.produce_mocked()) - current_actor = testutils.CurrentActorMocked(arch=arch) + monkeypatch.setattr(api, 'produce', produce_mocked()) + current_actor = CurrentActorMocked(arch=arch) monkeypatch.setattr(api, 'current_actor', current_actor) scancpu.process() @@ -89,3 +104,50 @@ def test_scancpu(monkeypatch, arch): # Did not produce anything extra assert expected == produced + + +def test_lscpu_with_empty_field(monkeypatch): + + def mocked_cpuinfo(*args, **kwargs): + return mocked_get_cpuinfo('lscpu_empty_field')(output_json=False) + + monkeypatch.setattr(scancpu, '_get_lscpu_output', mocked_cpuinfo) + monkeypatch.setattr(api, 'produce', produce_mocked()) + current_actor = CurrentActorMocked() + monkeypatch.setattr(api, 'current_actor', current_actor) + + scancpu.process() + + expected = CPUInfo(machine_type=None, flags=['flag']) + produced = api.produce.model_instances[0] + + assert api.produce.called == 1 + + assert expected.machine_type == produced.machine_type + assert sorted(expected.flags) == sorted(produced.flags) + + +def test_parse_invalid_json(monkeypatch): + + monkeypatch.setattr('leapp.libraries.actor.scancpu.get_source_major_version', lambda: '8') + + def mocked_cpuinfo(*args, **kwargs): + return mocked_get_cpuinfo('invalid')(output_json=True) + + monkeypatch.setattr(scancpu, '_get_lscpu_output', mocked_cpuinfo) + monkeypatch.setattr(api, 'produce', produce_mocked()) + monkeypatch.setattr(api, 'current_logger', logger_mocked()) + current_actor = CurrentActorMocked() + monkeypatch.setattr(api, 'current_actor', current_actor) + + scancpu.process() + + assert api.produce.called == 1 + + assert any('Failed to parse json output' in msg for msg in api.current_logger().dbgmsg) + + expected = CPUInfo(machine_type=None, flags=[]) + produced = api.produce.model_instances[0] + + assert expected.machine_type == produced.machine_type + assert sorted(expected.flags) == sorted(produced.flags)