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/cfg_checker/cli/command.py b/cfg_checker/cli/command.py
new file mode 100644
index 0000000..0a9c2a2
--- /dev/null
+++ b/cfg_checker/cli/command.py
@@ -0,0 +1,59 @@
+import sys
+import traceback
+
+from cfg_checker.common import logger_cli
+from cfg_checker.common.exception import CheckerException
+
+# TODO: auto-create types for each command
+package_command_types = ['report']
+network_command_types = ['check', 'report']
+reclass_command_types = ['list', 'diff']
+
+commands = {
+ 'packages': package_command_types,
+ 'network': network_command_types,
+ 'reclass': reclass_command_types
+}
+
+
+def execute_command(args, command):
+ # Validate the commands
+ # check command
+ if command not in commands:
+ logger_cli.info("\n# Please, type a command listed above")
+ return 0
+ elif args.type not in commands[command]:
+ # check type
+ logger_cli.info(
+ "\n# Please, select '{}' command type listed above".format(
+ command
+ )
+ )
+ return 0
+ else:
+ # form function name to call
+ _method_name = "do_" + args.type
+ _target_module = __import__(
+ "cfg_checker.modules."+command,
+ fromlist=[""]
+ )
+ _method = getattr(_target_module, _method_name)
+
+ # Execute the command
+ try:
+ _method(args)
+ return 0
+ except CheckerException as e:
+ logger_cli.error("\nERROR: {}".format(
+ e.message
+ ))
+
+ exc_type, exc_value, exc_traceback = sys.exc_info()
+ logger_cli.debug("\n{}".format(
+ "".join(traceback.format_exception(
+ exc_type,
+ exc_value,
+ exc_traceback
+ ))
+ ))
+ return 1
diff --git a/cfg_checker/cli/network.py b/cfg_checker/cli/network.py
index 4d6d0c4..b53311d 100644
--- a/cfg_checker/cli/network.py
+++ b/cfg_checker/cli/network.py
@@ -1,14 +1,59 @@
-from cfg_checker.modules.network.checker import NetworkChecker
+import sys
+
+from cfg_checker.common import config, logger, logger_cli
+from cfg_checker.helpers.args_utils import MyParser
+
+from command import execute_command
+
+
+def init_network_parser(_parser):
+ # network subparser
+ net_subparsers = _parser.add_subparsers(dest='type')
+
+ net_check_parser = net_subparsers.add_parser(
+ 'check',
+ help="Do network check and print the result"
+ )
+
+ net_check_parser.add_argument(
+ '--detailed',
+ action="store_true", default=False,
+ help="Print error details after summary"
+ )
+
+ net_report_parser = net_subparsers.add_parser(
+ 'report',
+ help="Generate network check report"
+ )
+
+ net_report_parser.add_argument(
+ '--html',
+ metavar='network_html_filename',
+ help="HTML filename to save report"
+ )
+
+ return _parser
+
+
+def cli_network():
+ net_parser = MyParser("# Mirantis Cloud Network checker")
+ init_network_parser(net_parser)
+
+ # parse arguments
+ try:
+ args = net_parser.parse_args()
+ except TypeError:
+ logger_cli.info("\n# Please, check arguments")
+ sys.exit(0)
+
+ # force use of sudo
+ config.ssh_uses_sudo = True
+
+ # Execute the command
+ result = execute_command(args, 'network')
+ logger.debug(result)
+ sys.exit(result)
+
if __name__ == '__main__':
- # init connection to salt and collect minion data
- cl = NetworkChecker()
-
- # collect data on installed packages
- cl.collect_network_info()
-
- # diff installed and candidates
- # cl.collect_packages()
-
- # report it
- cl.create_html_report("./pkg_versions.html")
+ cli_network()
diff --git a/cfg_checker/cli/package.py b/cfg_checker/cli/package.py
index 74166be..5fe7e0b 100644
--- a/cfg_checker/cli/package.py
+++ b/cfg_checker/cli/package.py
@@ -1,15 +1,57 @@
-from cfg_checker.modules.packages.checker import CloudPackageChecker
+import sys
+
+from cfg_checker.common import config, logger, logger_cli
+from cfg_checker.helpers.args_utils import MyParser
+
+from command import execute_command
+
+
+def init_package_parser(_parser):
+ # packages subparser
+ pkg_subparsers = _parser.add_subparsers(dest='type')
+
+ pkg_report_parser = pkg_subparsers.add_parser(
+ 'report',
+ help="Report package versions to HTML file"
+ )
+ pkg_report_parser.add_argument(
+ '--full',
+ action="store_true", default=False,
+ help="HTML report will have all of the packages, not just errors"
+ )
+ pkg_report_parser.add_argument(
+ '--html',
+ metavar='packages_html_filename',
+ help="HTML filename to save report"
+ )
+ pkg_report_parser.add_argument(
+ '--csv',
+ metavar='packages_csv_filename',
+ help="CSV filename to save report"
+ )
+
+ return _parser
+
+
+def cli_package():
+ pkg_parser = MyParser("# Mirantis Cloud Package checker")
+ init_package_parser(pkg_parser)
+
+ # parse arguments
+ try:
+ args = pkg_parser.parse_args()
+ except TypeError:
+ logger_cli.info("\n# Please, check arguments")
+ sys.exit(0)
+
+ # force use of sudo
+ config.ssh_uses_sudo = True
+
+ # Execute the command
+ result = execute_command(args, 'packages')
+ logger.debug(result)
+ sys.exit(result)
if __name__ == '__main__':
- # init connection to salt and collect minion data
- cl = CloudPackageChecker()
-
- # collect data on installed packages
- cl.collect_installed_packages()
-
- # diff installed and candidates
- # cl.collect_packages()
-
- # report it
- cl.create_html_report("./pkg_versions.html")
+ cli_package()
diff --git a/cfg_checker/cli/reclass.py b/cfg_checker/cli/reclass.py
index 2959bf3..fc5961a 100644
--- a/cfg_checker/cli/reclass.py
+++ b/cfg_checker/cli/reclass.py
@@ -1,6 +1,67 @@
-from cfg_checker.modules.reclass.comparer import ModelComparer
+import sys
+
+from cfg_checker.common import config, logger, logger_cli
+from cfg_checker.helpers.args_utils import MyParser
+
+from command import execute_command
+
+
+def init_reclass_parser(_parser):
+ # reclass subparsers
+ reclass_subparsers = _parser.add_subparsers(dest='type')
+ reclass_list_parser = reclass_subparsers.add_parser(
+ 'list',
+ help="List models available to compare"
+ )
+ reclass_list_parser.add_argument(
+ "-p",
+ "--models-path",
+ default="/srv/salt/",
+ help="Global path to search models in"
+ )
+
+ reclass_diff_parser = reclass_subparsers.add_parser(
+ 'diff',
+ help="List models available to compare"
+ )
+ reclass_diff_parser.add_argument(
+ "--model1",
+ required=True,
+ help="Model A <path>. Model name is the folder name"
+ )
+ reclass_diff_parser.add_argument(
+ "--model2",
+ required=True,
+ help="Model B <path>. Model name is the folder name"
+ )
+ reclass_diff_parser.add_argument(
+ '--html',
+ metavar='reclass_html_filename',
+ help="HTML filename to save report"
+ )
+
+ return _parser
+
+
+def cli_reclass():
+ net_parser = MyParser("# Mirantis Cloud Reclass comparer")
+ init_reclass_parser(net_parser)
+
+ # parse arguments
+ try:
+ args = net_parser.parse_args()
+ except TypeError:
+ logger_cli.info("\n# Please, check arguments")
+ sys.exit(0)
+
+ # force use of sudo
+ config.ssh_uses_sudo = True
+
+ # Execute the command
+ result = execute_command(args, 'reclass')
+ logger.debug(result)
+ sys.exit(result)
+
if __name__ == "__main__":
- # Execute the comparison using argv params
-
- pass
+ cli_reclass()