Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Fix GitHub Actions (failing mock libnvme test) #348

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions coverage.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ run_unit_test test-nvme_options.py # ... and with regular user
run_unit_test test-service.py
run_unit_test test-timeparse.py
run_unit_test test-transport_id.py
run_unit_test test-defs.py
run_unit_test test-udev.py
run_unit_test test-version.py

Expand Down
23 changes: 11 additions & 12 deletions test/test-defs.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
#!/usr/bin/python3
import gc
import os
import sys
import copy
import unittest

from unittest import mock


class MockLibnvmeTestCase(unittest.TestCase):
'''Testing defs.py by mocking the libnvme package'''

def test_libnvme_version(self):
# implement your testing logic, for example:
from staslib import defs
# 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, '?.?')
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()},
)
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,
Expand All @@ -42,7 +41,7 @@ def tearDownClass(cls):
class RealLibnvmeUnitTest(unittest.TestCase):
'''Testing defs.py with the real libnvme package'''

def test_LIBNVME_VERSION(self):
def test_libnvme_version(self):
try:
# We can't proceed with this test if the
# module libnvme is not installed.
Expand Down