]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
tests: Fix nvme_copy_test python traceback fails
authorDennis Maisenbacher <dennis.maisenbacher@wdc.com>
Tue, 15 Oct 2024 12:08:37 +0000 (12:08 +0000)
committerDaniel Wagner <wagi@monom.org>
Tue, 29 Oct 2024 06:37:41 +0000 (07:37 +0100)
Reworking the `get_ocfs` function to correctly parse the ocfs field.
At the same time refactor `nvme id-ctrl` that extracts the ocfs
field into separate functions. This is in preparation for following
commits that reuse the same functionality.

Furthermore nvme_copy_test fails on devices that do not support any
copy formats. For this to pass we need to declare
`self.host_behavior_data` in any case.

Signed-off-by: Dennis Maisenbacher <dennis.maisenbacher@wdc.com>
tests/nvme_copy_test.py
tests/nvme_test.py

index a5472312fa2fac3829e2a1730492e777e036bda7..5d16b00ddab005e4ed7302a7db1a567f033143c5 100644 (file)
@@ -36,6 +36,7 @@ class TestNVMeCopy(TestNVMe):
         super().setUp()
         print("\nSetting up test...")
         self.ocfs = self.get_ocfs()
+        self.host_behavior_data = None
         cross_namespace_copy = self.ocfs & 0xc
         if cross_namespace_copy:
             # get host behavior support data
index 5292ed13254f6803a306869e75f6cbc2ea6178fa..235c855a8f24567756e753d54828d815d3d1f222 100644 (file)
@@ -260,6 +260,25 @@ class TestNVMe(unittest.TestCase):
         print(ncap)
         return int(ncap)
 
+    def get_id_ctrl_field_value(self, field):
+        """ Wrapper for extracting id-ctrl field values
+            - Args:
+                - None
+            - Returns:
+                - Filed value of the given field
+        """
+        id_ctrl_cmd = f"nvme id-ctrl {self.ctrl} --output-format=json"
+        proc = subprocess.Popen(id_ctrl_cmd,
+                                shell=True,
+                                stdout=subprocess.PIPE,
+                                encoding='utf-8')
+        err = proc.wait()
+        self.assertEqual(err, 0, "ERROR : reading id-ctrl failed")
+        json_output = json.loads(proc.stdout.read())
+        self.assertTrue(field in json_output,
+                        f"ERROR : reading field '{field}' failed")
+        return str(json_output[field])
+
     def get_ocfs(self):
         """ Wrapper for extracting optional copy formats supported
             - Args:
@@ -267,11 +286,7 @@ class TestNVMe(unittest.TestCase):
             - Returns:
                 - Optional Copy Formats Supported
         """
-        pattern = re.compile(r'^ocfs\s*: 0x[0-9a-fA-F]+$')
-        output = subprocess.check_output(["nvme", "id-ctrl", self.ctrl], encoding='utf-8')
-        ocfs_line = next(line for line in output.splitlines() if pattern.match(line))
-        ocfs = ocfs_line.split(":")[1].strip()
-        return int(ocfs, 16)
+        return int(self.get_id_ctrl_field_value("ocfs"), 16)
 
     def get_format(self):
         """ Wrapper for extracting format.