Repository: stratis-storage/stratis-cli (language: python)

Issue:
Maybe we can move monkey-patching tests to unittest framework
The unittest implementation of mock may be able to do all we need a bit more cleanly.


Discussion / hints:

Fix patch:
diff --git a/Makefile b/Makefile
index d410193af..1c3b4a4bc 100644
--- a/Makefile
+++ b/Makefile
@@ -61,25 +61,14 @@ coverage-no-html:
 	python3 -m coverage --version
 	python3 -m coverage run --timid --branch -m unittest discover --quiet --top-level-directory ./tests/whitebox --start-directory ./tests/whitebox/integration >& /dev/null
 	python3 -m coverage run --timid --branch -a -m unittest discover --quiet --start-directory ./tests/whitebox/unittest
-	python3 -m coverage run --timid --branch -a -m unittest --quiet tests.whitebox.monkey_patching.test_keyboard_interrupt.KeyboardInterruptTestCase
-	python3 -m coverage run --timid --branch -a -m unittest --quiet tests.whitebox.monkey_patching.test_stratisd_version.StratisdVersionTestCase
 	python3 -m coverage report -m --fail-under=100 --show-missing --include="./src/*"
 
 .PHONY: coverage
 coverage: coverage-no-html
 	python3 -m coverage html --include="./src/*"
 
-keyboard-interrupt-test:
-	python3 -m unittest ${UNITTEST_OPTS} tests.whitebox.monkey_patching.test_keyboard_interrupt.KeyboardInterruptTestCase
-
-stratisd-version-test:
-	python3 -m unittest ${UNITTEST_OPTS} tests.whitebox.monkey_patching.test_stratisd_version.StratisdVersionTestCase
-
-.PHONY: sim-tests
-sim-tests: dbus-tests keyboard-interrupt-test stratisd-version-test
-
 .PHONY: all-tests
-all-tests: unittest-tests sim-tests
+all-tests: unittest-tests dbus-tests
 
 .PHONY: yamllint
 yamllint:


Test patch:
diff --git a/tests/whitebox/integration/test_stratis.py b/tests/whitebox/integration/test_stratis.py
index 2924a824d..0d128374d 100644
--- a/tests/whitebox/integration/test_stratis.py
+++ b/tests/whitebox/integration/test_stratis.py
@@ -15,11 +15,15 @@
 Test 'stratisd'.
 """
 
+# isort: STDLIB
+from unittest.mock import patch
+
 # isort: THIRDPARTY
 import dbus
 
 # isort: LOCAL
-from stratis_cli import StratisCliErrorCodes
+from stratis_cli import StratisCliErrorCodes, run
+from stratis_cli._errors import StratisCliStratisdVersionError
 
 from ._misc import RUNNER, TEST_RUNNER, RunTestCase, SimTestCase
 
@@ -60,3 +64,55 @@ def test_not_propagate(self):
         command_line = ["daemon", "version"]
         with self.assertRaises(SystemExit):
             RUNNER(command_line)
+
+
+class StratisdVersionTestCase(SimTestCase):
+    """
+    Test behavior of stratis on incompatible versions of stratisd.
+    """
+
+    def test_outdated_stratisd_version(self):
+        """
+        Verify that an outdated version of stratisd will produce a
+        StratisCliStratisdVersionError.
+        """
+        # pylint: disable=import-outside-toplevel
+        # isort: LOCAL
+        from stratis_cli._actions import _data
+
+        command_line = ["--propagate", "daemon", "version"]
+
+        # pylint: disable=protected-access
+        with patch.object(
+            _data.Manager0.Properties.Version,
+            "Get",
+            return_value="1.0.0",
+        ):
+            self.check_error(StratisCliStratisdVersionError, command_line, _ERROR)
+
+
+class KeyboardInterruptTestCase(SimTestCase):
+    """
+    Test behavior of stratis on KeyboardInterrupt.
+    """
+
+    def test_catch_keyboard_exception(self):
+        """
+        Verify that the KeyboardInterrupt is propagated by the run() method.
+        ./bin/stratis contains a try block at the outermost level which
+        then catches the KeyboardInterrupt and exits with an error message.
+        The KeyboardInterrupt is most likely raised in the dbus-python
+        method which is actually communicating on the D-Bus, but it is
+        fairly difficult to get at that method. Instead settle for getting
+        at the calling method generated by dbus-python-client-gen.
+        """
+
+        # pylint: disable=import-outside-toplevel
+        # isort: LOCAL
+        from stratis_cli._actions import _data
+
+        with patch.object(
+            _data.Manager0.Properties.Version, "Get", side_effect=KeyboardInterrupt()
+        ):
+            with self.assertRaises(KeyboardInterrupt):
+                run()(["daemon", "version"])
diff --git a/tests/whitebox/monkey_patching/README.txt b/tests/whitebox/monkey_patching/README.txt
deleted file mode 100644
index 078d4dfc9..000000000
--- a/tests/whitebox/monkey_patching/README.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-A directory for tests that require monkey-patching. Each test should
-be run separately, so that the monkey-patched attributes do not cause other
-tests to fail.
diff --git a/tests/whitebox/monkey_patching/__init__.py b/tests/whitebox/monkey_patching/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/tests/whitebox/monkey_patching/test_keyboard_interrupt.py b/tests/whitebox/monkey_patching/test_keyboard_interrupt.py
deleted file mode 100644
index 58432de97..000000000
--- a/tests/whitebox/monkey_patching/test_keyboard_interrupt.py
+++ /dev/null
@@ -1,56 +0,0 @@
-# Copyright 2016 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-"""
-Test management of KeyboardInterrupt in stratisd.
-"""
-
-# isort: LOCAL
-import stratis_cli
-
-from .._misc import SimTestCase
-
-
-class KeyboardInterruptTestCase(SimTestCase):
-    """
-    Test behavior of stratis on KeyboardInterrupt.
-    """
-
-    def test_catch_keyboard_exception(self):
-        """
-        Verify that the KeyboardInterrupt is propagated by the run() method.
-        ./bin/stratis contains a try block at the outermost level which
-        then catches the KeyboardInterrupt and exits with an error message.
-        The KeyboardInterrupt is most likely raised in the dbus-python
-        method which is actually communicating on the D-Bus, but it is
-        fairly difficult to get at that method. Instead settle for getting
-        at the calling method generated by dbus-python-client-gen.
-        """
-
-        def raise_keyboard_interrupt(_):
-            """
-            Just raise the interrupt.
-            """
-            raise KeyboardInterrupt()
-
-        # pylint: disable=import-outside-toplevel
-        # isort: LOCAL
-        from stratis_cli._actions import _data
-
-        # pylint: disable=protected-access
-        stratis_cli._actions._data.Manager.Properties.Version.Get = (
-            raise_keyboard_interrupt
-        )
-
-        with self.assertRaises(KeyboardInterrupt):
-            stratis_cli.run()(["daemon", "version"])
diff --git a/tests/whitebox/monkey_patching/test_stratisd_version.py b/tests/whitebox/monkey_patching/test_stratisd_version.py
deleted file mode 100644
index c6b22f292..000000000
--- a/tests/whitebox/monkey_patching/test_stratisd_version.py
+++ /dev/null
@@ -1,54 +0,0 @@
-# Copyright 2020 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-"""
-Test handling of incompatible stratisd version
-"""
-
-# isort: LOCAL
-import stratis_cli
-from stratis_cli import StratisCliErrorCodes
-from stratis_cli._errors import StratisCliStratisdVersionError
-
-from .._misc import SimTestCase
-
-_ERROR = StratisCliErrorCodes.ERROR
-
-
-class StratisdVersionTestCase(SimTestCase):
-    """
-    Test behavior of stratis on incompatible versions of stratisd.
-    """
-
-    def test_outdated_stratisd_version(self):
-        """
-        Verify that an outdated version of stratisd will produce a StratisCliStratisdVersionError.
-        """
-
-        def outdated_stratisd_version(_):
-            """
-            Return an outdated stratisd version.
-            """
-            return "1.0.0"
-
-        # pylint: disable=import-outside-toplevel
-        # isort: LOCAL
-        from stratis_cli._actions import _data
-
-        # pylint: disable=protected-access
-        stratis_cli._actions._data.Manager0.Properties.Version.Get = (
-            outdated_stratisd_version
-        )
-
-        command_line = ["--propagate", "daemon", "version"]
-        self.check_error(StratisCliStratisdVersionError, command_line, _ERROR)