Blame SOURCES/0009-nvmetcli-Allow-different-devices-for-make-test.patch

76c28c
From 35e92a1bfedf28868112165892b3eb2cd7f750ce Mon Sep 17 00:00:00 2001
76c28c
From: Tony Asleson <tasleson@redhat.com>
76c28c
Date: Thu, 2 Apr 2020 10:54:43 -0500
76c28c
Subject: [PATCH 09/12] nvmetcli: Allow different devices for make test
76c28c
76c28c
The test_nvmet.py by default uses /dev/ram0 and /dev/ram1 for 2 of the
76c28c
unit tests.  Add env. variable to allow user to specify different devices
76c28c
or files.  Additionally, skip these unit tests that require devices/files
76c28c
if they are not present.  Update README too.
76c28c
76c28c
$ sudo make test
76c28c
......s...s.
76c28c
----------------------------------------------------------------------
76c28c
Ran 12 tests in 0.043s
76c28c
76c28c
OK (skipped=2)
76c28c
Name                  Stmts   Miss  Cover
76c28c
-----------------------------------------
76c28c
nvmet/__init__.py         1      0   100%
76c28c
nvmet/nvme.py           517    237    54%
76c28c
nvmet/test_nvmet.py     276     63    77%
76c28c
-----------------------------------------
76c28c
TOTAL                   794    300    62%
76c28c
76c28c
$ sudo NVMET_TEST_DEVICES="/dev/sdc,/dev/sdd" make test
76c28c
............
76c28c
----------------------------------------------------------------------
76c28c
Ran 12 tests in 0.124s
76c28c
76c28c
OK
76c28c
Name                  Stmts   Miss  Cover
76c28c
-----------------------------------------
76c28c
nvmet/__init__.py         1      0   100%
76c28c
nvmet/nvme.py           517    100    81%
76c28c
nvmet/test_nvmet.py     276      4    99%
76c28c
-----------------------------------------
76c28c
TOTAL                   794    104    87%
76c28c
76c28c
Signed-off-by: Tony Asleson <tasleson@redhat.com>
76c28c
Signed-off-by: Christoph Hellwig <hch@lst.de>
76c28c
---
76c28c
 README              |  5 ++++-
76c28c
 nvmet/test_nvmet.py | 26 ++++++++++++++++++++++----
76c28c
 2 files changed, 26 insertions(+), 5 deletions(-)
76c28c
76c28c
diff --git a/README b/README
76c28c
index 5a4ecd1..44f1c33 100644
76c28c
--- a/README
76c28c
+++ b/README
76c28c
@@ -47,7 +47,10 @@ Testing
76c28c
 -------
76c28c
 nvmetcli comes with a testsuite that tests itself and the kernel configfs
76c28c
 interface for the NVMe target.  To run it make sure you have nose2 and
76c28c
-the coverage plugin for it installed and simple run 'make test'.
76c28c
+the coverage plugin for it installed and simple run 'make test'.  To run all
76c28c
+the tests you also need some test block devices or files.  Default is to
76c28c
+use /dev/ram0 and /dev/ram1.  You can override default with environmental
76c28c
+variable eg. NVMET_TEST_DEVICES="/dev/sdk,/dev/sdj" make test .
76c28c
 
76c28c
 Development
76c28c
 -----------------
76c28c
diff --git a/nvmet/test_nvmet.py b/nvmet/test_nvmet.py
76c28c
index aae4a86..f8ec232 100644
76c28c
--- a/nvmet/test_nvmet.py
76c28c
+++ b/nvmet/test_nvmet.py
76c28c
@@ -1,9 +1,22 @@
76c28c
 
76c28c
+import os
76c28c
 import random
76c28c
+import stat
76c28c
 import string
76c28c
 import unittest
76c28c
 import nvmet.nvme as nvme
76c28c
 
76c28c
+# Default test devices are ram disks, but allow user to specify different
76c28c
+# block devices or files.
76c28c
+NVMET_TEST_DEVICES = os.getenv("NVMET_TEST_DEVICES",
76c28c
+                               "/dev/ram0,/dev/ram1").split(',')
76c28c
+
76c28c
+
76c28c
+def test_devices_present():
76c28c
+    return len([x for x in NVMET_TEST_DEVICES
76c28c
+                if os.path.exists(x) and
76c28c
+                (stat.S_ISBLK(os.stat(x).st_mode) or os.path.isfile(x))]) >= 2
76c28c
+
76c28c
 
76c28c
 class TestNvmet(unittest.TestCase):
76c28c
     def test_subsystem(self):
76c28c
@@ -101,6 +114,8 @@ class TestNvmet(unittest.TestCase):
76c28c
             n.delete()
76c28c
         self.assertEqual(len(list(s.namespaces)), 0)
76c28c
 
76c28c
+    @unittest.skipUnless(test_devices_present(),
76c28c
+                         "Devices %s not available or suitable" % ','.join(NVMET_TEST_DEVICES))
76c28c
     def test_namespace_attrs(self):
76c28c
         root = nvme.Root()
76c28c
         root.clear_existing()
76c28c
@@ -116,7 +131,7 @@ class TestNvmet(unittest.TestCase):
76c28c
         self.assertRaises(nvme.CFSError, n.set_enable, 1)
76c28c
 
76c28c
         # now set a path and enable
76c28c
-        n.set_attr('device', 'path', '/dev/ram0')
76c28c
+        n.set_attr('device', 'path', NVMET_TEST_DEVICES[0])
76c28c
         n.set_enable(1)
76c28c
         self.assertTrue(n.get_enable())
76c28c
 
76c28c
@@ -125,7 +140,7 @@ class TestNvmet(unittest.TestCase):
76c28c
 
76c28c
         # test that we can't write to attrs while enabled
76c28c
         self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'path',
76c28c
-                          '/dev/ram1')
76c28c
+                          NVMET_TEST_DEVICES[1])
76c28c
         self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'nguid',
76c28c
                           '15f7767b-50e7-4441-949c-75b99153dea7')
76c28c
 
76c28c
@@ -403,6 +418,9 @@ class TestNvmet(unittest.TestCase):
76c28c
         self.assertRaises(nvme.CFSError, nvme.Port,
76c28c
                           portid=1 << 17, mode='create')
76c28c
 
76c28c
+    @unittest.skipUnless(test_devices_present(),
76c28c
+                         "Devices %s not available or suitable" % ','.join(
76c28c
+                             NVMET_TEST_DEVICES))
76c28c
     def test_save_restore(self):
76c28c
         root = nvme.Root()
76c28c
         root.clear_existing()
76c28c
@@ -416,7 +434,7 @@ class TestNvmet(unittest.TestCase):
76c28c
         s2.set_attr('attr', 'allow_any_host', 1)
76c28c
 
76c28c
         n = nvme.Namespace(s, nsid=42, mode='create')
76c28c
-        n.set_attr('device', 'path', '/dev/ram0')
76c28c
+        n.set_attr('device', 'path', NVMET_TEST_DEVICES[0])
76c28c
         n.set_enable(1)
76c28c
 
76c28c
         nguid = n.get_attr('device', 'nguid')
76c28c
@@ -454,7 +472,7 @@ class TestNvmet(unittest.TestCase):
76c28c
 
76c28c
         # and check everything is still the same
76c28c
         self.assertTrue(n.get_enable())
76c28c
-        self.assertEqual(n.get_attr('device', 'path'), '/dev/ram0')
76c28c
+        self.assertEqual(n.get_attr('device', 'path'), NVMET_TEST_DEVICES[0])
76c28c
         self.assertEqual(n.get_attr('device', 'nguid'), nguid)
76c28c
 
76c28c
         self.assertEqual(p.get_attr('addr', 'trtype'), 'loop')
76c28c
-- 
76c28c
2.29.2
76c28c