Best Python code snippet using lisa_python
nvme.py
Source:nvme.py
...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 )258 # 4. Remount command should fail after run blkdiscard command.259 mount_result = node.execute(260 f"mount {namespace}p1 {mount_point}", shell=True, sudo=True261 )262 mount_result.assert_exit_code(expected_exit_code=32)263 @TestCaseMetadata(264 description="""265 This test case will run commands 2-5, the commands are expected fail or not266 based on the capabilities of the device.267 1. Use `nvme id-ctrl device` command list the capabilities of the device.268 1.1 When 'Format NVM Supported' shown up in output of 'nvme id-ctrl device',269 then nvme disk can be format, otherwise, it can't be format.270 1.2 When 'NS Management and Attachment Supported' shown up in output of271 'nvme id-ctrl device', nvme namespace can be created, deleted and detached,272 otherwise it can't be managed.273 2. `nvme format namespace` - format a namespace.274 3. `nvme create-ns namespace` - create a namespace.275 4. `nvme delete-ns -n 1 namespace` - delete a namespace.276 5. `nvme detach-ns -n 1 namespace` - detach a namespace.277 """,278 priority=3,279 requirement=simple_requirement(280 supported_features=[Nvme],281 ),282 )283 def nvme_manage_ns_validation(self, node: Node) -> None:284 nvme = node.features[Nvme]285 nvme_namespaces = nvme.get_namespaces()286 nvme_devices = nvme.get_devices()287 nvme_cli = node.tools[Nvmecli]288 device_format_exit_code = 0289 ns_management_exit_code = 0290 # 1. Use `nvme id-ctrl device` command list the capabilities of the device.291 # 1.1 When 'Format NVM Supported' shown up in output of 'nvme id-ctrl device',292 # then nvme disk can be format, otherwise, it can't be format.293 if not nvme_cli.support_device_format(nvme_devices[0]):294 device_format_exit_code = 1295 # 1.2 When 'NS Management and Attachment Supported' shown up in output of296 # 'nvme id-ctrl device', nvme namespace can be created, deleted and detached,297 # otherwise it can't be managed.298 if not nvme_cli.support_ns_manage_attach(nvme_devices[0]):299 # NVMe Status:INVALID_OPCODE(1)300 ns_management_exit_code = 1301 for namespace in nvme_namespaces:302 # 2. `nvme format namespace` - format a namespace.303 format_namespace = nvme_cli.format_namespace(namespace)304 format_namespace.assert_exit_code(device_format_exit_code)305 # 3. `nvme create-ns namespace` - create a namespace.306 create_namespace = nvme_cli.create_namespace(namespace)307 # NVMe Status:INVALID_OPCODE: The associated command opcode field is not308 # valid(1) => exit code 1309 # NVMe status: INVALID_OPCODE: The associated command opcode field is not310 # valid(0x1) => exit code 22311 if "(0x1)" in create_namespace.stdout:312 ns_management_exit_code = 22313 create_namespace.assert_exit_code(ns_management_exit_code)314 # 4. `nvme delete-ns -n 1 namespace` - delete a namespace.315 delete_namespace = nvme_cli.delete_namespace(namespace, 1)316 delete_namespace.assert_exit_code(ns_management_exit_code)317 # 5. `nvme detach-ns -n 1 namespace` - detach a namespace.318 detach_namespace = nvme_cli.detach_namespace(namespace, 1)319 detach_namespace.assert_exit_code(ns_management_exit_code)320 @TestCaseMetadata(321 description="""322 This test case will323 1. Disable NVME devices.324 2. Enable NVME device.325 """,326 priority=2,327 requirement=simple_requirement(328 supported_features=[Nvme],329 ),330 )331 def nvme_rescind_validation(self, node: Node) -> None:332 lspci = node.tools[Lspci]333 # 1. Disable NVME devices.334 lspci.disable_devices_by_type(device_type=constants.DEVICE_TYPE_NVME)335 # 2. Enable NVME device.336 lspci.enable_devices()337 @TestCaseMetadata(338 description="""339 This test case does following steps to verify VM working normally during340 disable and enable nvme and sriov devices.341 1. Disable PCI devices.342 2. Enable PCI devices.343 3. Get PCI devices slots.344 4. Check PCI devices are back after rescan.345 """,346 priority=2,347 requirement=simple_requirement(348 network_interface=Sriov(),349 supported_features=[Nvme],350 ),351 )352 def nvme_sriov_rescind_validation(self, node: Node) -> None:353 lspci = node.tools[Lspci]354 device_types = [constants.DEVICE_TYPE_NVME, constants.DEVICE_TYPE_SRIOV]355 for device_type in device_types:356 # 1. Disable PCI devices.357 before_pci_count = lspci.disable_devices_by_type(device_type)358 # 2. Enable PCI devices.359 lspci.enable_devices()360 # 3. Get PCI devices slots.361 after_devices_slots = lspci.get_device_names_by_type(device_type, True)362 # 4. Check PCI devices are back after rescan.363 assert_that(364 after_devices_slots,365 "After rescan, the disabled PCI devices should be back.",366 ).is_length(before_pci_count)367 def _validate_nvme_disk(self, environment: Environment, node: Node) -> None:368 # 1. Get nvme devices and nvme namespaces from /dev/ folder,369 # compare the count of nvme namespaces and nvme devices.370 nvme = node.features[Nvme]371 nvme_device = nvme.get_devices()372 nvme_namespace = nvme.get_namespaces()373 assert_that(nvme_device).described_as(374 "nvme devices count should be equal to namespace count by listing devices "375 "under folder /dev."376 ).is_length(len(nvme_namespace))377 # 2. Compare the count of nvme namespaces return from `nvme list`378 # and list nvme namespaces under /dev/.379 nvme_namespace_cli = nvme.get_namespaces_from_cli()380 assert_that(nvme_namespace_cli).described_as(381 "nvme namespace count should be consistent between listed devides under "...
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!!