From 090f7ee98d6aee3b9f337e0df2bd0283fa7ebc23 Mon Sep 17 00:00:00 2001 From: tomasfratrik Date: Mon, 4 Mar 2024 09:10:02 +0100 Subject: [PATCH] Add unit tests for actor udevamdinfo * Move actor's process to its library * Add check for run in process * Create file with short output of 'udevamd info -e' for testing purposes * Add unit tests for actor Jira: OAMG-1277 --- .../common/actors/udev/udevadminfo/actor.py | 5 +- .../udev/udevadminfo/libraries/udevadminfo.py | 19 +++ .../udevadminfo/tests/files/udevadm_database | 134 ++++++++++++++++++ .../udevadminfo/tests/test_udevadminfo.py | 40 ++++++ 4 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 repos/system_upgrade/common/actors/udev/udevadminfo/libraries/udevadminfo.py create mode 100644 repos/system_upgrade/common/actors/udev/udevadminfo/tests/files/udevadm_database create mode 100644 repos/system_upgrade/common/actors/udev/udevadminfo/tests/test_udevadminfo.py diff --git a/repos/system_upgrade/common/actors/udev/udevadminfo/actor.py b/repos/system_upgrade/common/actors/udev/udevadminfo/actor.py index b674e56c5d..ac702914eb 100644 --- a/repos/system_upgrade/common/actors/udev/udevadminfo/actor.py +++ b/repos/system_upgrade/common/actors/udev/udevadminfo/actor.py @@ -1,5 +1,5 @@ from leapp.actors import Actor -from leapp.libraries.stdlib import run +from leapp.libraries.actor import udevadminfo from leapp.models import UdevAdmInfoData from leapp.tags import FactsPhaseTag, IPUWorkflowTag @@ -15,5 +15,4 @@ class UdevAdmInfo(Actor): tags = (IPUWorkflowTag, FactsPhaseTag,) def process(self): - out = run(['udevadm', 'info', '-e'])['stdout'] - self.produce(UdevAdmInfoData(db=out)) + udevadminfo.process() diff --git a/repos/system_upgrade/common/actors/udev/udevadminfo/libraries/udevadminfo.py b/repos/system_upgrade/common/actors/udev/udevadminfo/libraries/udevadminfo.py new file mode 100644 index 0000000000..dabe49e0f0 --- /dev/null +++ b/repos/system_upgrade/common/actors/udev/udevadminfo/libraries/udevadminfo.py @@ -0,0 +1,19 @@ +from leapp.exceptions import StopActorExecutionError +from leapp.libraries.stdlib import api, CalledProcessError, run +from leapp.models import UdevAdmInfoData + + +def process(): + try: + out = run(['udevadm', 'info', '-e'])['stdout'] + except (CalledProcessError, OSError) as err: + raise StopActorExecutionError( + message=( + "Unable to gather information about the system devices" + ), + details={ + 'details': 'Failed to execute `udevadm info -e` command.', + 'error': str(err) + } + ) + api.produce(UdevAdmInfoData(db=out)) diff --git a/repos/system_upgrade/common/actors/udev/udevadminfo/tests/files/udevadm_database b/repos/system_upgrade/common/actors/udev/udevadminfo/tests/files/udevadm_database new file mode 100644 index 0000000000..219fb57482 --- /dev/null +++ b/repos/system_upgrade/common/actors/udev/udevadminfo/tests/files/udevadm_database @@ -0,0 +1,134 @@ +P: /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXCPU: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698543 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:01 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:01 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXCPU: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698839 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0103:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0103:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0103: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1697906 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0A03: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698109 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/PNP0A06:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/PNP0A06:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0A06: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1702939 +P: /devices/LNXSYSTM:00 +E: DEVPATH=/devices/LNXSYSTM:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXSYSTM: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1694509 + +P: /devices/LNXSYSTM:00/LNXPWRBN:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXPWRBN:00 +E: DRIVER=button +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXPWRBN: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1695034 + +P: /devices/LNXSYSTM:00/LNXPWRBN:00/input/input0 +E: DEVPATH=/devices/LNXSYSTM:00/LNXPWRBN:00/input/input0 +E: EV=3 +E: ID_FOR_SEAT=input-acpi-LNXPWRBN_00 +E: ID_INPUT=1 +E: ID_INPUT_KEY=1 +E: ID_PATH=acpi-LNXPWRBN:00 +E: ID_PATH_TAG=acpi-LNXPWRBN_00 +E: KEY=10000000000000 0 +E: MODALIAS=input:b0019v0000p0001e0000-e0,1,k74,ramlsfw +E: NAME="Power Button" +E: PHYS="LNXPWRBN/button/input0" +E: PRODUCT=19/0/1/0 +E: PROP=0 +E: SUBSYSTEM=input +E: TAGS=:seat: +E: USEC_INITIALIZED=1697068 + +P: /devices/LNXSYSTM:00/LNXPWRBN:00/input/input0/event0 +N: input/event0 +E: DEVNAME=/dev/input/event0 +E: DEVPATH=/devices/LNXSYSTM:00/LNXPWRBN:00/input/input0/event0 +E: ID_INPUT=1 +E: ID_INPUT_KEY=1 +E: ID_PATH=acpi-LNXPWRBN:00 +E: ID_PATH_TAG=acpi-LNXPWRBN_00 +E: MAJOR=13 +E: MINOR=64 +E: SUBSYSTEM=input +E: TAGS=:power-switch: +E: USEC_INITIALIZED=1744996 + +P: /devices/LNXSYSTM:00/LNXPWRBN:00/wakeup/wakeup10 +E: DEVPATH=/devices/LNXSYSTM:00/LNXPWRBN:00/wakeup/wakeup10 +E: SUBSYSTEM=wakeup + +P: /devices/LNXSYSTM:00/LNXSYBUS:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXSYBUS: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1695925 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:ACPI0010:PNP0A05: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698058 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXCPU: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698543 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:01 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0010:00/LNXCPU:01 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:LNXCPU: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698839 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0103:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0103:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0103: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1697906 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0A03: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1698109 + +P: /devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/PNP0A06:00 +E: DEVPATH=/devices/LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/PNP0A06:00 +E: ID_VENDOR_FROM_DATABASE=The Linux Foundation +E: MODALIAS=acpi:PNP0A06: +E: SUBSYSTEM=acpi +E: USEC_INITIALIZED=1702939 + diff --git a/repos/system_upgrade/common/actors/udev/udevadminfo/tests/test_udevadminfo.py b/repos/system_upgrade/common/actors/udev/udevadminfo/tests/test_udevadminfo.py new file mode 100644 index 0000000000..f465d6f6aa --- /dev/null +++ b/repos/system_upgrade/common/actors/udev/udevadminfo/tests/test_udevadminfo.py @@ -0,0 +1,40 @@ +import os + +import pytest + +from leapp.exceptions import StopActorExecutionError +from leapp.libraries.actor import udevadminfo +from leapp.libraries.common import testutils +from leapp.libraries.stdlib import api, CalledProcessError +from leapp.models import UdevAdmInfoData + +CUR_DIR = os.path.dirname(os.path.abspath(__file__)) + + +def _raise_call_error(*args): + raise CalledProcessError( + message='A Leapp Command Error occurred.', + command=args, + result={'signal': None, 'exit_code': 1, 'pid': 0, 'stdout': 'fake', 'stderr': 'fake'} + ) + + +def test_failed_run(monkeypatch): + monkeypatch.setattr(api, 'produce', testutils.produce_mocked()) + monkeypatch.setattr(udevadminfo, 'run', _raise_call_error) + + with pytest.raises(StopActorExecutionError): + udevadminfo.process() + + +def test_udevadminfo(monkeypatch): + + with open(os.path.join(CUR_DIR, 'files', 'udevadm_database'), 'r') as fp: + mocked_data = fp.read() + monkeypatch.setattr(api, 'produce', testutils.produce_mocked()) + monkeypatch.setattr(udevadminfo, 'run', lambda *args: {'stdout': mocked_data}) + udevadminfo.process() + + assert api.produce.called == 1 + assert isinstance(api.produce.model_instances[0], UdevAdmInfoData) + assert api.produce.model_instances[0].db == mocked_data