summaryrefslogtreecommitdiffstats
path: root/vnftest/cmd/cli.py
diff options
context:
space:
mode:
authorMoshe <moshehoa@amdocs.com>2018-02-26 13:39:57 +0200
committerMoshe <moshehoa@amdocs.com>2018-03-04 14:24:35 +0200
commit0bb532c41e89568966ca2bfae259737e8830249b (patch)
treedaad9e5debb95f292de880cfad18d2d0dce29239 /vnftest/cmd/cli.py
parentc1731afc800b3f7aaefd7c06dbe887ff057fa0f8 (diff)
Initial VNFTEST fw
Issue-ID: VNFSDK-195 Change-Id: I5abf0dd033e76e5225bb8271c0afaea325d741d9 Signed-off-by: Moshe <moshehoa@amdocs.com> docker build Issue-ID: VNFSDK-195 Change-Id: I25eb933504c0201e6c26477b540626fd515d2887 Signed-off-by: Moshe <moshehoa@amdocs.com> fix requirements Issue-ID: VNFSDK-195 Change-Id: I5907fa102bfbf9cb81d42e491c133b4fdbb0d6fd Signed-off-by: Moshe <moshehoa@amdocs.com> rm netifaces Issue-ID: VNFSDK-195 Change-Id: I349d0c738442edfef256c90b06cbaeb446c1db13 Signed-off-by: Moshe <moshehoa@amdocs.com> fix tox config IssueID: VNFTEST-195 Change-Id: I5c0b0e0ab96cad1bdc56ab63860d794bfd15b5eb Signed-off-by: Moshe <moshehoa@amdocs.com> Add unit test IssueID: VNFTEST-195 Change-Id: I08c9ba53721306aff4b74720181f8c853c4ccabe Signed-off-by: Moshe <moshehoa@amdocs.com> fix setup.py Issue-ID: VNFSDK-195 Change-Id: I72bd93e4977edf5ef0b46c72fe47165b805aab7b Signed-off-by: Moshe <moshehoa@amdocs.com> fix test execution Issue-ID: VNFSDK-195 Change-Id: I488a6226d2562229f0e7fa6c1d20f0c43882bc3b Signed-off-by: Moshe <moshehoa@amdocs.com>
Diffstat (limited to 'vnftest/cmd/cli.py')
-rw-r--r--vnftest/cmd/cli.py184
1 files changed, 184 insertions, 0 deletions
diff --git a/vnftest/cmd/cli.py b/vnftest/cmd/cli.py
new file mode 100644
index 0000000..9bffe56
--- /dev/null
+++ b/vnftest/cmd/cli.py
@@ -0,0 +1,184 @@
+#!/usr/bin/env python
+##############################################################################
+# Copyright 2018 EuropeanSoftwareMarketingLtd.
+# ===================================================================
+# Licensed under the ApacheLicense, Version2.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
+#
+# 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
+##############################################################################
+# vnftest comment: this is a modified copy of
+# yardstick/cmd/cli.py
+"""
+Command-line interface to vnftest
+"""
+
+from __future__ import absolute_import
+import logging
+import os
+import sys
+
+from pkg_resources import get_distribution
+from argparse import RawDescriptionHelpFormatter
+from oslo_config import cfg
+
+from vnftest import _init_logging, _LOG_STREAM_HDLR
+from vnftest.cmd.commands import task
+from vnftest.cmd.commands import runner
+from vnftest.cmd.commands import step
+from vnftest.cmd.commands import testcase
+from vnftest.cmd.commands import plugin
+from vnftest.cmd.commands import env
+from vnftest.cmd.commands import report
+
+CONF = cfg.CONF
+cli_opts = [
+ cfg.BoolOpt('debug',
+ short='d',
+ default=False,
+ help='increase output verbosity to debug')
+]
+CONF.register_cli_opts(cli_opts)
+
+CONFIG_SEARCH_PATHS = [sys.prefix + "/etc/vnftest",
+ "~/.vnftest",
+ "/etc/vnftest"]
+
+
+def find_config_files(path_list):
+ for path in path_list:
+ abspath = os.path.abspath(os.path.expanduser(path))
+ confname = abspath + "/vnftest.conf"
+ if os.path.isfile(confname):
+ return [confname]
+
+ return None
+
+
+class VnftestCLI(): # pragma: no cover
+ """Command-line interface to vnftest"""
+
+ # Command categories
+ categories = {
+ 'task': task.TaskCommands,
+ 'runner': runner.RunnerCommands,
+ 'step': step.StepCommands,
+ 'testcase': testcase.TestcaseCommands,
+ 'plugin': plugin.PluginCommands,
+ 'env': env.EnvCommand,
+ 'report': report.ReportCommands
+ }
+
+ def __init__(self):
+ self.opts = []
+ self._version = 'vnftest version %s ' % \
+ get_distribution('vnftest').version
+
+ def _find_actions(self, subparsers, actions_module):
+ """find action methods"""
+ # Find action methods inside actions_module and
+ # add them to the command parser.
+ # The 'actions_module' argument may be a class
+ # or module. Action methods start with 'do_'
+ for attr in (a for a in dir(actions_module) if a.startswith('do_')):
+ command = attr[3:].replace('_', '-')
+ callback = getattr(actions_module, attr)
+ desc = callback.__doc__ or ''
+ arguments = getattr(callback, 'arguments', [])
+ subparser = subparsers.add_parser(
+ command,
+ description=desc
+ )
+ for (args, kwargs) in arguments:
+ subparser.add_argument(*args, **kwargs)
+ subparser.set_defaults(func=callback)
+
+ def _add_command_parsers(self, categories, subparsers):
+ """add commands to command-line parser"""
+ for category in categories:
+ command_object = categories[category]()
+ desc = command_object.__doc__ or ''
+ subparser = subparsers.add_parser(
+ category, description=desc,
+ formatter_class=RawDescriptionHelpFormatter
+ )
+ subparser.set_defaults(command_object=command_object)
+ cmd_subparsers = subparser.add_subparsers(title='subcommands')
+ self._find_actions(cmd_subparsers, command_object)
+
+ def _register_cli_opt(self):
+
+ # register subcommands to parse additional command line arguments
+ def parser(subparsers):
+ self._add_command_parsers(VnftestCLI.categories, subparsers)
+
+ category_opt = cfg.SubCommandOpt("category",
+ title="Command categories",
+ help="Available categories",
+ handler=parser)
+ self._register_opt(category_opt)
+
+ def _register_opt(self, opt):
+
+ CONF.register_cli_opt(opt)
+ self.opts.append(opt)
+
+ def _load_cli_config(self, argv):
+
+ # load CLI args and config files
+ CONF(argv, project="vnftest", version=self._version,
+ default_config_files=find_config_files(CONFIG_SEARCH_PATHS))
+
+ def _handle_global_opts(self):
+
+ _init_logging()
+ if CONF.debug:
+ _LOG_STREAM_HDLR.setLevel(logging.DEBUG)
+
+ def _dispatch_func_notask(self):
+
+ # dispatch to category parser
+ func = CONF.category.func
+ func(CONF.category)
+
+ def _dispatch_func_task(self, task_id):
+
+ # dispatch to category parser
+ func = CONF.category.func
+ func(CONF.category, task_id=task_id)
+
+ def _clear_config_opts(self):
+
+ CONF.clear()
+ CONF.unregister_opts(self.opts)
+
+ def main(self, argv): # pragma: no cover
+ """run the command line interface"""
+ try:
+ self._register_cli_opt()
+
+ self._load_cli_config(argv)
+
+ self._handle_global_opts()
+
+ self._dispatch_func_notask()
+ finally:
+ self._clear_config_opts()
+
+ def api(self, argv, task_id): # pragma: no cover
+ """run the api interface"""
+ try:
+ self._register_cli_opt()
+
+ self._load_cli_config(argv)
+
+ self._handle_global_opts()
+
+ self._dispatch_func_task(task_id)
+ finally:
+ self._clear_config_opts()