]> www.infradead.org Git - users/hch/nvmetcli.git/commitdiff
nvmetcli: Allow different devices for make test
authorTony Asleson <tasleson@redhat.com>
Thu, 2 Apr 2020 15:54:43 +0000 (10:54 -0500)
committerChristoph Hellwig <hch@lst.de>
Fri, 3 Apr 2020 06:48:29 +0000 (08:48 +0200)
The test_nvmet.py by default uses /dev/ram0 and /dev/ram1 for 2 of the
unit tests.  Add env. variable to allow user to specify different devices
or files.  Additionally, skip these unit tests that require devices/files
if they are not present.  Update README too.

$ sudo make test
......s...s.
----------------------------------------------------------------------
Ran 12 tests in 0.043s

OK (skipped=2)
Name                  Stmts   Miss  Cover
-----------------------------------------
nvmet/__init__.py         1      0   100%
nvmet/nvme.py           517    237    54%
nvmet/test_nvmet.py     276     63    77%
-----------------------------------------
TOTAL                   794    300    62%

$ sudo NVMET_TEST_DEVICES="/dev/sdc,/dev/sdd" make test
............
----------------------------------------------------------------------
Ran 12 tests in 0.124s

OK
Name                  Stmts   Miss  Cover
-----------------------------------------
nvmet/__init__.py         1      0   100%
nvmet/nvme.py           517    100    81%
nvmet/test_nvmet.py     276      4    99%
-----------------------------------------
TOTAL                   794    104    87%

Signed-off-by: Tony Asleson <tasleson@redhat.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
README
nvmet/test_nvmet.py

diff --git a/README b/README
index 5a4ecd15febb9ecc5b2638788638a88f299e9329..44f1c33cbeb6bc5622b31b2ff6eaf9c18741ea06 100644 (file)
--- a/README
+++ b/README
@@ -47,7 +47,10 @@ Testing
 -------
 nvmetcli comes with a testsuite that tests itself and the kernel configfs
 interface for the NVMe target.  To run it make sure you have nose2 and
-the coverage plugin for it installed and simple run 'make test'.
+the coverage plugin for it installed and simple run 'make test'.  To run all
+the tests you also need some test block devices or files.  Default is to
+use /dev/ram0 and /dev/ram1.  You can override default with environmental
+variable eg. NVMET_TEST_DEVICES="/dev/sdk,/dev/sdj" make test .
 
 Development
 -----------------
index aae4a8631107e734b20afcd320f8c3f60ecb3b18..f8ec232da9592a1a1b727a32b35dc3f6a59cd61c 100644 (file)
@@ -1,9 +1,22 @@
 
+import os
 import random
+import stat
 import string
 import unittest
 import nvmet.nvme as nvme
 
+# Default test devices are ram disks, but allow user to specify different
+# block devices or files.
+NVMET_TEST_DEVICES = os.getenv("NVMET_TEST_DEVICES",
+                               "/dev/ram0,/dev/ram1").split(',')
+
+
+def test_devices_present():
+    return len([x for x in NVMET_TEST_DEVICES
+                if os.path.exists(x) and
+                (stat.S_ISBLK(os.stat(x).st_mode) or os.path.isfile(x))]) >= 2
+
 
 class TestNvmet(unittest.TestCase):
     def test_subsystem(self):
@@ -101,6 +114,8 @@ class TestNvmet(unittest.TestCase):
             n.delete()
         self.assertEqual(len(list(s.namespaces)), 0)
 
+    @unittest.skipUnless(test_devices_present(),
+                         "Devices %s not available or suitable" % ','.join(NVMET_TEST_DEVICES))
     def test_namespace_attrs(self):
         root = nvme.Root()
         root.clear_existing()
@@ -116,7 +131,7 @@ class TestNvmet(unittest.TestCase):
         self.assertRaises(nvme.CFSError, n.set_enable, 1)
 
         # now set a path and enable
-        n.set_attr('device', 'path', '/dev/ram0')
+        n.set_attr('device', 'path', NVMET_TEST_DEVICES[0])
         n.set_enable(1)
         self.assertTrue(n.get_enable())
 
@@ -125,7 +140,7 @@ class TestNvmet(unittest.TestCase):
 
         # test that we can't write to attrs while enabled
         self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'path',
-                          '/dev/ram1')
+                          NVMET_TEST_DEVICES[1])
         self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'nguid',
                           '15f7767b-50e7-4441-949c-75b99153dea7')
 
@@ -403,6 +418,9 @@ class TestNvmet(unittest.TestCase):
         self.assertRaises(nvme.CFSError, nvme.Port,
                           portid=1 << 17, mode='create')
 
+    @unittest.skipUnless(test_devices_present(),
+                         "Devices %s not available or suitable" % ','.join(
+                             NVMET_TEST_DEVICES))
     def test_save_restore(self):
         root = nvme.Root()
         root.clear_existing()
@@ -416,7 +434,7 @@ class TestNvmet(unittest.TestCase):
         s2.set_attr('attr', 'allow_any_host', 1)
 
         n = nvme.Namespace(s, nsid=42, mode='create')
-        n.set_attr('device', 'path', '/dev/ram0')
+        n.set_attr('device', 'path', NVMET_TEST_DEVICES[0])
         n.set_enable(1)
 
         nguid = n.get_attr('device', 'nguid')
@@ -454,7 +472,7 @@ class TestNvmet(unittest.TestCase):
 
         # and check everything is still the same
         self.assertTrue(n.get_enable())
-        self.assertEqual(n.get_attr('device', 'path'), '/dev/ram0')
+        self.assertEqual(n.get_attr('device', 'path'), NVMET_TEST_DEVICES[0])
         self.assertEqual(n.get_attr('device', 'nguid'), nguid)
 
         self.assertEqual(p.get_attr('addr', 'trtype'), 'loop')