Best Python code snippet using lisa_python
nvme.py
Source:nvme.py  
...15from lisa.features import Nvme, NvmeSettings, Sriov16from lisa.sut_orchestrator.azure.platform_ import AzurePlatform17from lisa.tools import Cat, Echo, Fdisk, Lscpu, Lspci, Mount, Nvmecli18from lisa.tools.fdisk import FileSystem19def _format_mount_disk(20    node: Node,21    namespace: str,22    file_system: FileSystem,23) -> None:24    mount_point = namespace.rpartition("/")[-1]25    fdisk = node.tools[Fdisk]26    mount = node.tools[Mount]27    mount.umount(namespace, mount_point)28    fdisk.make_partition(namespace, file_system)29    mount.mount(f"{namespace}p1", mount_point)30@TestSuiteMetadata(31    area="nvme",32    category="functional",33    name="Nvme",34    description="""35    This test suite is to validate NVMe disk on Linux VM.36    """,37)38class NvmeTestSuite(TestSuite):39    TIME_OUT = 30040    @TestCaseMetadata(41        description="""42        This test case will43        1. Get nvme devices and nvme namespaces from /dev/ folder,44         compare the count of nvme namespaces and nvme devices.45        2. Compare the count of nvme namespaces return from `nvme list`46          and list nvme namespaces under /dev/.47        3. Compare nvme devices count return from `lspci`48          and list nvme devices under /dev/.49        4. Azure platform only, nvme devices count should equal to50          actual vCPU count / 8.51        """,52        priority=1,53        requirement=simple_requirement(54            supported_features=[Nvme],55        ),56    )57    def nvme_basic_validation(self, environment: Environment, node: Node) -> None:58        self._validate_nvme_disk(environment, node)59    @TestCaseMetadata(60        description="""61        This case runs nvme_basic_validation test against 10 NVMe disks.62        The test steps are same as `nvme_basic_validation`.63        """,64        priority=2,65        requirement=simple_requirement(66            supported_features=[NvmeSettings(disk_count=10)],67        ),68    )69    def nvme_max_disk_validation(self, environment: Environment, node: Node) -> None:70        self._validate_nvme_disk(environment, node)71    @TestCaseMetadata(72        description="""73        This test case will do following things for each NVMe device.74        1. Get the number of errors from nvme-cli before operations.75        2. Create a partition, filesystem and mount it.76        3. Create a txt file on the partition, content is 'TestContent'.77        4. Create a file 'data' on the partition, get the md5sum value.78        5. Umount and remount the partition.79        6. Get the txt file content, compare the value.80        7. Compare the number of errors from nvme-cli after operations.81        """,82        priority=2,83        requirement=simple_requirement(84            supported_features=[Nvme],85        ),86    )87    def nvme_function_validation(self, node: Node) -> None:88        nvme = node.features[Nvme]89        nvme_namespaces = nvme.get_namespaces()90        nvme_cli = node.tools[Nvmecli]91        cat = node.tools[Cat]92        mount = node.tools[Mount]93        for namespace in nvme_namespaces:94            # 1. Get the number of errors from nvme-cli before operations.95            error_count_before_operations = nvme_cli.get_error_count(namespace)96            # 2. Create a partition, filesystem and mount it.97            _format_mount_disk(node, namespace, FileSystem.ext4)98            # 3. Create a txt file on the partition, content is 'TestContent'.99            mount_point = namespace.rpartition("/")[-1]100            cmd_result = node.execute(101                f"echo TestContent > {mount_point}/testfile.txt", shell=True, sudo=True102            )103            cmd_result.assert_exit_code(104                message=f"{mount_point}/testfile.txt may not exist."105            )106            # 4. Create a file 'data' on the partition, get the md5sum value.107            cmd_result = node.execute(108                f"dd if=/dev/zero of={mount_point}/data bs=10M count=100",109                shell=True,110                sudo=True,111            )112            cmd_result.assert_exit_code(113                message=f"{mount_point}/data is not created successfully, "114                "please check the disk space."115            )116            initial_md5 = node.execute(117                f"md5sum {mount_point}/data", shell=True, sudo=True118            )119            initial_md5.assert_exit_code(120                message=f"{mount_point}/data not exist or md5sum command enounter"121                " unexpected error."122            )123            # 5. Umount and remount the partition.124            mount.umount(namespace, mount_point, erase=False)125            mount.mount(f"{namespace}p1", mount_point)126            # 6. Get the txt file content, compare the value.127            file_content = cat.run(f"{mount_point}/testfile.txt", shell=True, sudo=True)128            assert_that(129                file_content.stdout,130                f"content of {mount_point}/testfile.txt should keep consistent "131                "after umount and re-mount.",132            ).is_equal_to("TestContent")133            # 6. Get md5sum value of file 'data', compare with initial value.134            final_md5 = node.execute(135                f"md5sum {mount_point}/data", shell=True, sudo=True136            )137            assert_that(138                initial_md5.stdout,139                f"md5sum of {mount_point}/data should keep consistent "140                "after umount and re-mount.",141            ).is_equal_to(final_md5.stdout)142            # 7. Compare the number of errors from nvme-cli after operations.143            error_count_after_operations = nvme_cli.get_error_count(namespace)144            assert_that(145                error_count_before_operations,146                "error-log should not increase after operations.",147            ).is_equal_to(error_count_after_operations)148            mount.umount(disk_name=namespace, point=mount_point)149    @TestCaseMetadata(150        description="""151        This test case will152        1. Create a partition, xfs filesystem and mount it.153        2. Check how much the mountpoint is trimmed before operations.154        3. Create a 300 gb file 'data' using dd command in the partition.155        4. Check how much the mountpoint is trimmed after creating the file.156        5. Delete the file 'data'.157        6. Check how much the mountpoint is trimmed after deleting the file,158         and compare the final fstrim status with initial fstrim status.159        """,160        priority=3,161        requirement=simple_requirement(162            supported_features=[Nvme],163        ),164    )165    def nvme_fstrim_validation(self, node: Node) -> None:166        nvme = node.features[Nvme]167        nvme_namespaces = nvme.get_namespaces()168        mount = node.tools[Mount]169        for namespace in nvme_namespaces:170            mount_point = namespace.rpartition("/")[-1]171            mount.umount(disk_name=namespace, point=mount_point)172            # 1. Create a partition, xfs filesystem and mount it.173            _format_mount_disk(node, namespace, FileSystem.xfs)174            # 2. Check how much the mountpoint is trimmed before operations.175            initial_fstrim = node.execute(176                f"fstrim {mount_point} -v", shell=True, sudo=True177            )178            initial_fstrim.assert_exit_code(179                message=f"{mount_point} not exist or fstrim command enounter "180                "unexpected error."181            )182            # 3. Create a 300 gb file 'data' using dd command in the partition.183            cmd_result = node.execute(184                f"dd if=/dev/zero of={mount_point}/data bs=1G count=300",185                shell=True,186                sudo=True,187            )188            cmd_result.assert_exit_code(189                message=f"{mount_point}/data is not created successfully, "190                "please check the disk space."191            )192            # 4. Check how much the mountpoint is trimmed after creating the file.193            intermediate_fstrim = node.execute(194                f"fstrim {mount_point} -v", shell=True, sudo=True195            )196            intermediate_fstrim.assert_exit_code(197                message=f"{mount_point} not exist or fstrim command enounter "198                "unexpected error."199            )200            # 5. Delete the file 'data'.201            node.execute(f"rm {mount_point}/data", shell=True, sudo=True)202            # 6. Check how much the mountpoint is trimmed after deleting the file,203            #  and compare the final fstrim status with initial fstrim status.204            node.tools[Echo].write_to_file(205                "2", node.get_pure_path("/proc/sys/vm/drop_caches"), sudo=True206            )207            final_fstrim = node.execute(208                f"fstrim {mount_point} -v", shell=True, sudo=True209            )210            mount.umount(disk_name=namespace, point=mount_point)211            assert_that(212                final_fstrim.stdout,213                "initial_fstrim should equal to final_fstrim after operations "214                "after umount and re-mount.",215            ).is_equal_to(initial_fstrim.stdout)216    @TestCaseMetadata(217        description="""218        This test case will219        1. Create a partition, xfs filesystem and mount it.220        2. Umount the mountpoint.221        3. Run blkdiscard command on the partition.222        4. Remount command should fail after run blkdiscard command.223        """,224        priority=3,225        requirement=simple_requirement(226            supported_features=[Nvme],227        ),228    )229    def nvme_blkdiscard_validation(self, node: Node) -> None:230        os_information = node.os.information231        if "Ubuntu" == os_information.vendor and "14.04" == os_information.release:232            raise SkippedException(233                f"blkdiscard is not supported with distro {os_information.vendor} and "234                f"version {os_information.release}"235            )236        nvme = node.features[Nvme]237        nvme_namespaces = nvme.get_namespaces()238        mount = node.tools[Mount]239        for namespace in nvme_namespaces:240            mount_point = namespace.rpartition("/")[-1]241            mount.umount(disk_name=namespace, point=mount_point)242            # 1. Create a partition, xfs filesystem and mount it.243            _format_mount_disk(node, namespace, FileSystem.xfs)244            # 2. Umount the mountpoint.245            mount.umount(disk_name=namespace, point=mount_point, erase=False)246            # 3. Run blkdiscard command on the partition.247            blkdiscard = node.execute(248                f"blkdiscard -v {namespace}p1", shell=True, sudo=True249            )250            if 0 != blkdiscard.exit_code:251                blkdiscard = node.execute(252                    f"blkdiscard -f -v {namespace}p1", shell=True, sudo=True253                )254            blkdiscard.assert_exit_code(255                message=f"{namespace}p1 not exist or blkdiscard command enounter "256                "unexpected error."257            )...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!!
