Add run_stress to cliff-based cli framework

Also disable ability to run script in stand-alone mode. So users need to
use 'tempest run-stress' instead.

Change-Id: I3effd1b71b2375f75a11f5924205741be0903361
Implements: blueprint tempest-cli-improvements
diff --git a/setup.cfg b/setup.cfg
index ee61788..4415063 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -35,6 +35,7 @@
 tempest.cm =
     init = tempest.cmd.init:TempestInit
     cleanup = tempest.cmd.cleanup:TempestCleanup
+    run-stress = tempest.cmd.run_stress:TempestRunStress
 oslo.config.opts =
     tempest.config = tempest.config:list_opts
 
diff --git a/tempest/cmd/run_stress.py b/tempest/cmd/run_stress.py
old mode 100755
new mode 100644
index 80f1b85..f99e5d9
--- a/tempest/cmd/run_stress.py
+++ b/tempest/cmd/run_stress.py
@@ -23,6 +23,7 @@
     # unittest in python 2.6 does not contain loader, so uses unittest2
     from unittest2 import loader
 
+from cliff import command
 from oslo_log import log as logging
 from oslo_serialization import jsonutils as json
 from testtools import testsuite
@@ -70,29 +71,42 @@
     return tests
 
 
-parser = argparse.ArgumentParser(description='Run stress tests')
-parser.add_argument('-d', '--duration', default=300, type=int,
-                    help="Duration of test in secs")
-parser.add_argument('-s', '--serial', action='store_true',
-                    help="Trigger running tests serially")
-parser.add_argument('-S', '--stop', action='store_true',
-                    default=False, help="Stop on first error")
-parser.add_argument('-n', '--number', type=int,
-                    help="How often an action is executed for each process")
-group = parser.add_mutually_exclusive_group(required=True)
-group.add_argument('-a', '--all', action='store_true',
-                   help="Execute all stress tests")
-parser.add_argument('-T', '--type',
-                    help="Filters tests of a certain type (e.g. gate)")
-parser.add_argument('-i', '--call-inherited', action='store_true',
-                    default=False,
-                    help="Call also inherited function with stress attribute")
-group.add_argument('-t', "--tests", nargs='?',
-                   help="Name of the file with test description")
+class TempestRunStress(command.Command):
+
+    def get_parser(self, prog_name):
+        pa = super(TempestRunStress, self).get_parser(prog_name)
+        pa = add_arguments(pa)
+        return pa
+
+    def take_action(self, pa):
+        return action(pa)
 
 
-def main():
-    ns = parser.parse_args()
+def add_arguments(parser):
+    parser.add_argument('-d', '--duration', default=300, type=int,
+                        help="Duration of test in secs")
+    parser.add_argument('-s', '--serial', action='store_true',
+                        help="Trigger running tests serially")
+    parser.add_argument('-S', '--stop', action='store_true',
+                        default=False, help="Stop on first error")
+    parser.add_argument('-n', '--number', type=int,
+                        help="How often an action is executed for each "
+                        "process")
+    group = parser.add_mutually_exclusive_group(required=True)
+    group.add_argument('-a', '--all', action='store_true',
+                       help="Execute all stress tests")
+    parser.add_argument('-T', '--type',
+                        help="Filters tests of a certain type (e.g. gate)")
+    parser.add_argument('-i', '--call-inherited', action='store_true',
+                        default=False,
+                        help="Call also inherited function with stress "
+                        "attribute")
+    group.add_argument('-t', "--tests", nargs='?',
+                       help="Name of the file with test description")
+    return parser
+
+
+def action(ns):
     result = 0
     if not ns.all:
         tests = json.load(open(ns.tests, 'r'))
@@ -121,6 +135,15 @@
     return result
 
 
+def main():
+    LOG.warning("Deprecated: Use 'tempest run-stress' instead. "
+                "The old entrypoint will be removed in a future release.")
+    parser = argparse.ArgumentParser(description='Run stress tests')
+    pa = add_arguments(parser)
+    ns = pa.parse_args()
+    return action(ns)
+
+
 if __name__ == "__main__":
     try:
         sys.exit(main())
diff --git a/tempest/stress/README.rst b/tempest/stress/README.rst
index 4f1f56c..33842fd 100644
--- a/tempest/stress/README.rst
+++ b/tempest/stress/README.rst
@@ -33,17 +33,17 @@
 
 The stress test framework can automatically discover test inside the tempest
 test suite. All test flag with the `@stresstest` decorator will be executed.
-In order to use this discovery you have to be in the tempest root directory
-and execute the following:
+In order to use this discovery you have to install tempest CLI, be in the
+tempest root directory and execute the following:
 
-	run-tempest-stress -a -d 30
+	tempest run-stress -a -d 30
 
 Running the sample test
 -----------------------
 
 To test installation, do the following:
 
-	run-tempest-stress -t tempest/stress/etc/server-create-destroy-test.json -d 30
+	tempest run-stress -t tempest/stress/etc/server-create-destroy-test.json -d 30
 
 This sample test tries to create a few VMs and kill a few VMs.