Unified command execution and unit tests
- All arguments inits moved to own clases
- Added unified way to execute commands
- Unit test structure and very basic tests
- Command line script to test coverage
Change-Id: I10bc973776595779b563b84548d46367bcd0886f
Related-PROD: PROD-28199
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/test_base.py b/tests/test_base.py
new file mode 100644
index 0000000..c4a627f
--- /dev/null
+++ b/tests/test_base.py
@@ -0,0 +1,29 @@
+import contextlib
+import io
+import sys
+import unittest
+
+
+class CfgCheckerTestBase(unittest.TestCase):
+ dummy_base_var = 0
+
+ def _safe_import_module(self, _str):
+ _import_msg = ""
+ _module = None
+
+ try:
+ _module = __import__(_str)
+ except ImportError as e:
+ _import_msg = e.message
+
+ return _import_msg, _module
+
+ @contextlib.contextmanager
+ def redirect_output(self):
+ save_stdout = sys.stdout
+ save_stderr = sys.stderr
+ sys.stdout = io.BytesIO()
+ sys.stderr = io.BytesIO()
+ yield
+ sys.stdout = save_stdout
+ sys.stderr = save_stderr
diff --git a/tests/test_entrypoints.py b/tests/test_entrypoints.py
new file mode 100644
index 0000000..0b87e6d
--- /dev/null
+++ b/tests/test_entrypoints.py
@@ -0,0 +1,99 @@
+from test_base import CfgCheckerTestBase
+
+
+class TestEntrypoints(CfgCheckerTestBase):
+ def test_entry_mcp_checker(self):
+ _module_name = 'cfg_checker.cfg_check'
+ with self.redirect_output():
+ _msg, _m = self._safe_import_module(_module_name)
+
+ self.assertEqual(
+ len(_msg),
+ 0,
+ "Error importing '{}': {}".format(
+ _module_name,
+ _msg
+ )
+ )
+
+ with self.redirect_output():
+ with self.assertRaises(SystemExit) as ep:
+ _m.cfg_check.config_check_entrypoint()
+
+ self.assertEqual(
+ ep.exception.code,
+ 0,
+ "mcp-checker has non-zero exit-code"
+ )
+
+ def test_entry_packages(self):
+ _module_name = 'cfg_checker.cli.package'
+ with self.redirect_output():
+ _msg, _m = self._safe_import_module(_module_name)
+
+ self.assertEqual(
+ len(_msg),
+ 0,
+ "Error importing '{}': {}".format(
+ _module_name,
+ _msg
+ )
+ )
+
+ with self.redirect_output():
+ with self.assertRaises(SystemExit) as ep:
+ _m.cli.package.cli_package()
+
+ self.assertEqual(
+ ep.exception.code,
+ 0,
+ "packages has non-zero exit code"
+ )
+
+ def test_entry_network(self):
+ _module_name = 'cfg_checker.cli.network'
+ with self.redirect_output():
+ _msg, _m = self._safe_import_module(_module_name)
+
+ self.assertEqual(
+ len(_msg),
+ 0,
+ "Error importing '{}': {}".format(
+ _module_name,
+ _msg
+ )
+ )
+
+ with self.redirect_output():
+ with self.assertRaises(SystemExit) as ep:
+ _m.cli.network.cli_network()
+
+ self.assertEqual(
+ ep.exception.code,
+ 0,
+ "network has non-zero exit-code"
+ )
+
+ def test_entry_reclass(self):
+ _module_name = 'cfg_checker.cli.reclass'
+ with self.redirect_output():
+ _msg, _m = self._safe_import_module(_module_name)
+
+ self.assertEqual(
+ len(_msg),
+ 0,
+ "Error importing '{}': {}".format(
+ _module_name,
+ _msg
+ )
+ )
+
+ with self.redirect_output():
+ with self.assertRaises(SystemExit) as ep:
+ _m.cli.reclass.cli_reclass()
+
+ self.assertEqual(
+ ep.exception.code,
+ 0,
+ "reclass has non-zero exit-code"
+ )