Best Python code snippet using lisa_python
lissuite.py
Source:lissuite.py  
...47    def verify_lis_driver_version(self, node: Node, log: Logger) -> None:48        cat = node.tools[Cat]49        modinfo_tool = node.tools[Modinfo]50        wget_tool = node.tools[Wget]51        self._check_lis_installable(node)52        # Checking for RPM Package Manager53        rpm_qa = node.execute("rpm -qa").stdout54        if ("kmod-microsoft-hyper-v" not in rpm_qa) or (55            "microsoft-hyper-v" not in rpm_qa56        ):57            raise SkippedException("No LIS RPM's are detected. Skipping test.")58        # Capturing LIS version from the node59        version = modinfo_tool.get_version(mod_name='"hv_vmbus"').strip()60        for i in range(5, 8):61            node.execute("rm -rf hv_compat.h")62            wget_tool.get(63                "https://raw.githubusercontent.com/LIS/lis-next/"64                f"{version}/hv-rhel{i}.x/hv/include/linux/hv_compat.h",65                filename="hv_compat.h",66                file_path="./",67            )68            # Capturing the LIS version from the source code69            source_version = cat.read_with_filter(70                "hv_compat.h", "define HV_DRV_VERSION"71            )72            source_version = get_matched_str(source_version, self.version_pattern)73            # Capturing the LIS version in hex from the source code74            source_version_hex = source_version = cat.read_with_filter(75                "hv_compat.h", "define _HV_DRV_VERSION"76            )77            source_version_hex = get_matched_str(78                source_version_hex, self.hex_version_pattern79            )80            self._check_lis_version(node, version, source_version, log)81            self._check_lis_version_hex(node, version, source_version_hex, log)82    @TestCaseMetadata(83        description="""84            This test case is to verify LIS RPM installation script to check the disk85             space before proceeding to LIS install.86            This avoids half / corrupted installations.87            Steps:88            1. Test leaves "bare minimum" size available for LIS install and checks if89             LIS installation is successful.90        """,91        priority=2,92    )93    def verify_lis_preinstall_disk_size_positive(self, node: Node, log: Logger) -> None:94        self._verify_lis_preinstall_disk_size(node, log)95    @TestCaseMetadata(96        description="""97            This test case is to verify LIS RPM installation script to check the disk98             space before proceeding to LIS install.99            This avoids half / corrupted installations.100            Steps:101            1. Test leaves "non installable" size on disk and checks if ./install.sh102             script skips the installation of not.103        """,104        priority=2,105    )106    def verify_lis_preinstall_disk_size_negative(self, node: Node, log: Logger) -> None:107        self._verify_lis_preinstall_disk_size(node, log, test_type="negative")108    def _verify_lis_preinstall_disk_size(109        self, node: Node, log: Logger, test_type: str = "positive"110    ) -> None:111        lisdriver = self._check_lis_installable(node)112        fallocate = node.tools[Fallocate]113        stat = node.tools[Stat]114        df = node.tools[Df]115        rpm = node.tools[Rpm]116        cmd_result = lisdriver.uninstall_from_iso()117        cmd_result.assert_exit_code(0, "failed to uninstall lis")118        self._clean_up_files(node)119        lib_module_folder = "/lib/modules"120        boot_folder = "/boot"121        # fetched from spec122        min_space_for_ramfs_creation = 157286400123        # 9MB to hit rpm limit124        min_sz_root_partition_not_root = 9437184125        # 10MB for log file creation126        root_partition_buffer_space = 10485760127        # allowed limit of +- 1MB128        boot_partition_buffer_space = 1048576129        root_partition = df.get_partition_by_mountpoint("/")130        boot_partition = df.get_partition_by_mountpoint("/boot")131        assert root_partition, "fail to get root partition"132        assert boot_partition, "fail to get boot partition"133        ramdisk_size_factor = 1134        if root_partition.name != boot_partition.name:135            ramdisk_size_factor = 2136        lis_path = lisdriver.download()137        os_version = node.os.information.release.split(".")138        version = os_version[0] + os_version[1]139        lib_module_required_space = rpm.get_file_size(140            f"{lis_path}/RPMS{version}/kmod-microsoft-hyper*x86_64.rpm"141        )142        uname = node.tools[Uname]143        kernel_version = uname.get_linux_information().kernel_version_raw144        ramdisk_required_space = stat.get_total_size(145            f"/boot/initramfs-{kernel_version}.img"146        )147        boot_part_required_space = ramdisk_required_space + boot_partition_buffer_space148        root_part_required_space = (149            min_space_for_ramfs_creation150            + ramdisk_size_factor * ramdisk_required_space151            + lib_module_required_space152            + root_partition_buffer_space153        )154        node.execute("sync")155        fblock_non_root = stat.get_fs_available_size(lib_module_folder)156        fblock_root = stat.get_fs_free_blocks(lib_module_folder)157        boot_part_avail_space = stat.get_fs_block_size(158            boot_folder159        ) * stat.get_fs_available_size(boot_folder)160        root_part_avail_space = (161            stat.get_fs_block_size(lib_module_folder) * fblock_non_root162        )163        if test_type == "negative":164            boot_part_required_space = int(boot_part_required_space / 2)165            root_part_required_space = int(root_part_required_space / 2)166            # 6.X distro RPM does not use root user space free space.167            # Hence setting the min limit.168            if fblock_non_root != fblock_root:169                root_part_required_space = min_sz_root_partition_not_root170            single_partition_file_size = (171                root_part_avail_space - root_part_required_space172            )173            fallocate.create_file(single_partition_file_size, "/lib/modules/file.out")174        if boot_partition != root_partition:175            single_partition_file_size_boot = (176                boot_part_avail_space - boot_part_required_space177            )178            fallocate.create_file(single_partition_file_size_boot, "/boot/file.out")179        result = lisdriver.install_from_iso()180        if test_type == "negative":181            if result.exit_code == 0:182                raise LisaException(183                    "lis should fail to be installed for insufficient space"184                )185            else:186                log.debug(f"fail to install lis for reason {result.stdout}")187        self._clean_up_files(node)188        if test_type == "positive":189            lisdriver.uninstall_from_iso()190    # Returns true if version and source_version are the same191    def _check_lis_version(192        self, node: Node, version: str, source_version: str, log: Logger193    ) -> None:194        log.debug("Detected modinfo version is {version}")195        log.debug("Version found in source code is {source_version}")196        assert_that(version).described_as(197            "Detected version and Source version are different. Expected LIS version:"198            f" {source_version}, Actual LIS version: {version}"199        )200    # Returns true if version and source_version_hex are the same201    def _check_lis_version_hex(202        self, node: Node, version: str, source_version_hex: str, log: Logger203    ) -> None:204        log.debug("Detected modinfo version is {version}")205        log.debug("Version found in source code is {source_version}")206        # The below two lines are converting the inputted LIS version to hex207        version_hex = version.replace(".", "")208        version_hex = str(hex(int(version_hex))).lower()209        # Converting to lower for consistency210        source_version_hex = source_version_hex.lower()211        assert_that(version).described_as(212            "Detected version and Source version are different for hex value. Expected"213            f" LIS version: {source_version_hex}, Actual LIS version: {version_hex}"214        )215    def _check_lis_installable(self, node: Node) -> LisDriver:216        try:217            lisdriver = node.tools[LisDriver]218            return lisdriver219        except UnsupportedDistroException as err:220            raise SkippedException(err)221    def _clean_up_files(self, node: Node) -> None:222        node.execute("rm -f /lib/modules/file.out", sudo=True)223        node.execute("rm -f /boot/file.out", sudo=True)224    def after_case(self, log: Logger, **kwargs: Any) -> None:225        node: Node = kwargs.pop("node")...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
