Define ODL console_script
[functest-xtesting.git] / functest / opnfv_tests / sdn / odl / odl.py
old mode 100755 (executable)
new mode 100644 (file)
index 45ed7cc..67bf66e
@@ -30,9 +30,11 @@ import robot.api
 from robot.errors import RobotError
 import robot.run
 from robot.utils.robottime import timestamp_to_secs
+from six import StringIO
 from six.moves import urllib
 
 from functest.core import testcase
+from functest.utils import constants
 import functest.utils.openstack_utils as op_utils
 
 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
@@ -64,14 +66,15 @@ class ODLResultVisitor(robot.api.ResultVisitor):
 class ODLTests(testcase.TestCase):
     """ODL test runner."""
 
-    repos = "/home/opnfv/repos/"
-    odl_test_repo = os.path.join(repos, "odl_test")
+    odl_test_repo = os.path.join(
+        constants.CONST.__getattribute__('dir_repos'), 'odl_test')
     neutron_suite_dir = os.path.join(odl_test_repo,
                                      "csit/suites/openstack/neutron")
     basic_suite_dir = os.path.join(odl_test_repo,
                                    "csit/suites/integration/basic")
     default_suites = [basic_suite_dir, neutron_suite_dir]
-    res_dir = '/home/opnfv/functest/results/odl/'
+    res_dir = os.path.join(
+        constants.CONST.__getattribute__('dir_results'), 'odl')
     __logger = logging.getLogger(__name__)
 
     @classmethod
@@ -114,7 +117,7 @@ class ODLTests(testcase.TestCase):
         self.details['description'] = result.suite.name
         self.details['tests'] = visitor.get_data()
 
-    def main(self, suites=None, **kwargs):
+    def run_suites(self, suites=None, **kwargs):
         """Run the test suites
 
         It has been designed to be called in any context.
@@ -172,16 +175,11 @@ class ODLTests(testcase.TestCase):
                     self.__logger.exception(
                         "Cannot create %s", self.res_dir)
                     return self.EX_RUN_ERROR
-            stdout_file = os.path.join(self.res_dir, 'stdout.txt')
             output_dir = os.path.join(self.res_dir, 'output.xml')
-            with open(stdout_file, 'w+') as stdout:
-                robot.run(*suites, variable=variables,
-                          output=output_dir,
-                          log='NONE',
-                          report='NONE',
-                          stdout=stdout)
-                stdout.seek(0, 0)
-                self.__logger.info("\n" + stdout.read())
+            stream = StringIO()
+            robot.run(*suites, variable=variables, output=output_dir,
+                      log='NONE', report='NONE', stdout=stream)
+            self.__logger.info("\n" + stream.getvalue())
             self.__logger.info("ODL results were successfully generated")
             try:
                 self.parse_results()
@@ -190,10 +188,6 @@ class ODLTests(testcase.TestCase):
                 self.__logger.error("Run tests before publishing: %s",
                                     ex.message)
                 return self.EX_RUN_ERROR
-            try:
-                os.remove(stdout_file)
-            except OSError:
-                self.__logger.warning("Cannot remove %s", stdout_file)
             return self.EX_OK
         else:
             return self.EX_RUN_ERROR
@@ -252,7 +246,7 @@ class ODLTests(testcase.TestCase):
             self.__logger.exception("Cannot run ODL testcases.")
             return self.EX_RUN_ERROR
 
-        return self.main(suites, **kwargs)
+        return self.run_suites(suites, **kwargs)
 
 
 class ODLParser(object):  # pylint: disable=too-few-public-methods
@@ -307,16 +301,19 @@ class ODLParser(object):  # pylint: disable=too-few-public-methods
         return vars(self.parser.parse_args(argv))
 
 
-if __name__ == '__main__':
+def main():
+    """Entry point"""
     logging.basicConfig()
-    ODL = ODLTests()
-    PARSER = ODLParser()
-    ARGS = PARSER.parse_args(sys.argv[1:])
+    odl = ODLTests()
+    parser = ODLParser()
+    args = parser.parse_args(sys.argv[1:])
     try:
-        RESULT = ODL.main(ODLTests.default_suites, **ARGS)
-        if RESULT != testcase.TestCase.EX_OK:
-            sys.exit(RESULT)
-        if ARGS['pushtodb']:
-            sys.exit(ODL.push_to_db())
+        result = odl.run_suites(ODLTests.default_suites, **args)
+        if result != testcase.TestCase.EX_OK:
+            return result
+        if args['pushtodb']:
+            return odl.push_to_db()
+        else:
+            return result
     except Exception:  # pylint: disable=broad-except
-        sys.exit(testcase.TestCase.EX_RUN_ERROR)
+        return testcase.TestCase.EX_RUN_ERROR