summaryrefslogtreecommitdiffstats
path: root/test/test-defs.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-16 11:03:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-06-16 11:03:18 +0000
commit347467d3fa6fb239f917c05c4cf7f6c3fe7f9b30 (patch)
tree44ae9f59984c8a36b93f29a729f10473653f9f19 /test/test-defs.py
parentAdding upstream version 2.2.2. (diff)
downloadnvme-stas-347467d3fa6fb239f917c05c4cf7f6c3fe7f9b30.tar.xz
nvme-stas-347467d3fa6fb239f917c05c4cf7f6c3fe7f9b30.zip
Adding upstream version 2.3~rc1.upstream/2.3_rc1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'test/test-defs.py')
-rwxr-xr-xtest/test-defs.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/test-defs.py b/test/test-defs.py
new file mode 100755
index 0000000..3f8b02b
--- /dev/null
+++ b/test/test-defs.py
@@ -0,0 +1,59 @@
+#!/usr/bin/python3
+import os
+import sys
+import unittest
+from unittest import mock
+
+
+class MockLibnvmeTestCase(unittest.TestCase):
+ '''Testing defs.py by mocking the libnvme package'''
+
+ def test_libnvme_version(self):
+ # For unknown reasons, this test does
+ # not work when run from GitHub Actions.
+ if not os.getenv('GITHUB_ACTIONS'):
+ from staslib import defs
+
+ libnvme_ver = defs.LIBNVME_VERSION
+ self.assertEqual(libnvme_ver, '?.?')
+
+ @classmethod
+ def setUpClass(cls): # called once before all the tests
+ # define what to patch sys.modules with
+ cls._modules_patcher = mock.patch.dict(sys.modules, {'libnvme': mock.Mock()})
+
+ # actually patch it
+ cls._modules_patcher.start()
+
+ # make the package globally visible and import it,
+ # just like if you have imported it in a usual way
+ # placing import statement at the top of the file,
+ # but relying on a patched dependency
+ global libnvme
+ import libnvme
+
+ @classmethod # called once after all tests
+ def tearDownClass(cls):
+ # restore initial sys.modules state back
+ cls._modules_patcher.stop()
+
+
+class RealLibnvmeUnitTest(unittest.TestCase):
+ '''Testing defs.py with the real libnvme package'''
+
+ def test_libnvme_version(self):
+ try:
+ # We can't proceed with this test if the
+ # module libnvme is not installed.
+ import libnvme
+ except ModuleNotFoundError:
+ return
+
+ from staslib import defs
+
+ libnvme_ver = defs.LIBNVME_VERSION
+ self.assertNotEqual(libnvme_ver, '?.?')
+
+
+if __name__ == '__main__':
+ unittest.main()