Skip to content

Commit

Permalink
Merge pull request #1183 from vojtechtrefny/3.9-devel_nvme-test
Browse files Browse the repository at this point in the history
tests: Add a simple unit test for the NVMe module
  • Loading branch information
vojtechtrefny authored Dec 6, 2023
2 parents 4957d84 + c807e23 commit 851b5bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .devicetree_test import *
from .events_test import *
from .misc_test import *
from .nvme_test import *
from .parentlist_test import *
from .populator_test import *
from .size_test import *
Expand Down
38 changes: 38 additions & 0 deletions tests/unit_tests/nvme_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest

try:
from unittest.mock import patch
except ImportError:
from mock import patch

from blivet.nvme import nvme


class NVMeModuleTestCase(unittest.TestCase):

host_nqn = "nqn.2014-08.org.nvmexpress:uuid:01234567-8900-abcd-efff-abcdabcdabcd"

@patch("blivet.nvme.os")
@patch("blivet.nvme.blockdev")
def test_nvme_module(self, bd, os):
self.assertIsNotNone(nvme)
bd.nvme_get_host_nqn.return_value = self.host_nqn
bd.nvme_get_host_id.return_value = None # None = generate from host_nqn
os.path.isdir.return_value = False

# startup
with patch.object(nvme, "write") as write:
nvme.startup()
write.assert_called_once_with("/", overwrite=False)

self.assertTrue(nvme.started)
self.assertEqual(nvme._hostnqn, self.host_nqn)
self.assertEqual(nvme._hostid, "01234567-8900-abcd-efff-abcdabcdabcd")

# write
with patch("blivet.nvme.open") as op:
nvme.write("/test")

os.makedirs.assert_called_with("/test/etc/nvme/", 0o755)
op.assert_any_call("/test/etc/nvme/hostnqn", "w")
op.assert_any_call("/test/etc/nvme/hostid", "w")

0 comments on commit 851b5bb

Please sign in to comment.