]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
tests: fix nvme_attach_detach_ns_test
authorDennis Maisenbacher <dennis.maisenbacher@wdc.com>
Fri, 11 Oct 2024 14:17:02 +0000 (14:17 +0000)
committerDaniel Wagner <wagi@monom.org>
Tue, 29 Oct 2024 06:37:41 +0000 (07:37 +0100)
Calculate ncap with configured flbas instead of setting a hard coded
value that might be to big for the test device.

Also, for this test to not fail the `nvme list-ctrl` outputs a summary
of ctrls present which must be skipped when parsing for the ctrl-id.

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

index 075f211a63ea59213a50376f3f5f7af2be5deded..a0e6129d5daf83c09dbec6fda002995482d47233 100644 (file)
@@ -52,8 +52,10 @@ class TestNVMeAttachDetachNSCmd(TestNVMe):
         super().setUp()
         self.dps = 0
         self.flbas = 0
-        self.nsze = 0x1400000
-        self.ncap = 0x1400000
+        (ds, ms) = self.get_lba_format_size()
+        ncap = int(self.get_ncap() / (ds+ms))
+        self.nsze = ncap
+        self.ncap = ncap
         self.setup_log_dir(self.__class__.__name__)
         self.ctrl_id = self.get_ctrl_id()
         self.delete_all_ns()
index 3888e6954dc32d87540f2626dc7d6dd184eb3718..5292ed13254f6803a306869e75f6cbc2ea6178fa 100644 (file)
@@ -147,22 +147,23 @@ class TestNVMe(unittest.TestCase):
         self.assertEqual(proc.wait(), 0, "ERROR : pci rescan failed")
 
     def get_ctrl_id(self):
-        """ Wrapper for extracting the controller id.
+        """ Wrapper for extracting the first controller id.
             - Args:
                 - None
             - Returns:
                 - controller id.
         """
-        get_ctrl_id = "nvme list-ctrl " + self.ctrl
+        get_ctrl_id = f"nvme list-ctrl {self.ctrl} --output-format=json"
         proc = subprocess.Popen(get_ctrl_id,
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 encoding='utf-8')
         err = proc.wait()
         self.assertEqual(err, 0, "ERROR : nvme list-ctrl failed")
-        line = proc.stdout.readline()
-        ctrl_id = line.split(":")[1].strip()
-        return ctrl_id
+        json_output = json.loads(proc.stdout.read())
+        self.assertTrue(len(json_output['ctrl_list']) > 0,
+                        "ERROR : nvme list-ctrl could not find ctrl")
+        return str(json_output['ctrl_list'][0]['ctrl_id'])
 
     def get_ns_list(self):
         """ Wrapper for extracting the namespace list.
@@ -207,6 +208,34 @@ class TestNVMe(unittest.TestCase):
         print(max_ns)
         return int(max_ns)
 
+    def get_lba_format_size(self):
+        """ Wrapper for extracting lba format size of the given flbas
+            - Args:
+                - None
+            - Returns:
+                - lba format size as a tuple of (data_size, metadata_size) in bytes.
+        """
+        nvme_id_ns_cmd = f"nvme id-ns {self.ns1} --output-format=json"
+        proc = subprocess.Popen(nvme_id_ns_cmd,
+                                shell=True,
+                                stdout=subprocess.PIPE,
+                                encoding='utf-8')
+        err = proc.wait()
+        self.assertEqual(err, 0, "ERROR : reading id-ns")
+        json_output = json.loads(proc.stdout.read())
+        self.assertTrue(len(json_output['lbafs']) > self.flbas,
+                        "Error : could not match the given flbas to an existing lbaf")
+        lbaf_json = json_output['lbafs'][int(self.flbas)]
+        ms_expo = int(lbaf_json['ms'])
+        ds_expo = int(lbaf_json['ds'])
+        ds = 0
+        ms = 0
+        if ds_expo > 0:
+            ds = (1 << ds_expo)
+        if ms_expo > 0:
+            ms = (1 << ms_expo)
+        return (ds, ms)
+
     def get_ncap(self):
         """ Wrapper for extracting capacity.
             - Args: