Conform ODL to TestCasesBase
authorCédric Ollivier <cedric.ollivier@orange.com>
Tue, 11 Oct 2016 13:42:56 +0000 (15:42 +0200)
committerCédric Ollivier <cedric.ollivier@orange.com>
Mon, 17 Oct 2016 07:14:01 +0000 (09:14 +0200)
Now ODLTestCases inherits from TestCasesBase what induces several
modifications of function definitions.
It also renames functest_run to run and run to main and they now
return the code status defined in TestCasesBase instead of True and
False (run_tests.py has been adapted as well).

It checks if errno is equal to EEXIST when makedir raises an OSError
exception.

JIRA: FUNCTEST-353

Change-Id: I88240a9ce8e491dab4c7e6c604f3ecc62cf9edce
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
ci/run_tests.py
testcases/Controllers/ODL/OpenDaylightTesting.py

index 638a6ed..834a29b 100755 (executable)
@@ -17,12 +17,14 @@ import argparse
 
 import functest.ci.generate_report as generate_report
 import functest.ci.tier_builder as tb
+import functest.core.TestCasesBase as TestCasesBase
 import functest.utils.functest_logger as ft_logger
 import functest.utils.functest_utils as ft_utils
 import functest.utils.openstack_clean as os_clean
 import functest.utils.openstack_snapshot as os_snapshot
 import functest.utils.openstack_utils as os_utils
-from functest.testcases.Controllers.ODL.OpenDaylightTesting import ODLTestCases
+import functest.testcases.Controllers.ODL.OpenDaylightTesting as odl_test
+
 
 parser = argparse.ArgumentParser()
 parser.add_argument("-t", "--test", dest="test", action='store',
@@ -101,10 +103,10 @@ def run_test(test, tier_name):
         flags += " -r"
 
     if test_name == 'odl':
-        result = ODLTestCases.functest_run()
-        if result and REPORT_FLAG:
-            result = ODLTestCases.push_to_db()
-        result = not result
+        odl = odl_test.ODLTestCases()
+        result = odl.run()
+        if result == TestCasesBase.TestCasesBase.EX_OK and REPORT_FLAG:
+            result = odl.push_to_db()
     else:
         cmd = ("%s%s" % (EXEC_SCRIPT, flags))
         logger.debug("Executing command '%s'" % cmd)
index 35a2ee1..0ee37b6 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/python
 
 import argparse
+import errno
 import fileinput
 import os
 import re
@@ -13,8 +14,8 @@ from robot.api import ExecutionResult, ResultVisitor
 from robot.errors import RobotError
 from robot.utils.robottime import timestamp_to_secs
 
+import functest.core.TestCasesBase as TestCasesBase
 import functest.utils.functest_logger as ft_logger
-import functest.utils.functest_utils as ft_utils
 import functest.utils.openstack_utils as op_utils
 
 
@@ -39,7 +40,7 @@ class ODLResultVisitor(ResultVisitor):
         return self._data
 
 
-class ODLTestCases:
+class ODLTestCases(TestCasesBase.TestCasesBase):
 
     repos = "/home/opnfv/repos/"
     odl_test_repo = repos + "odl_test/"
@@ -48,6 +49,9 @@ class ODLTestCases:
     res_dir = '/home/opnfv/functest/results/odl/'
     logger = ft_logger.Logger("opendaylight").getLogger()
 
+    def __init__(self):
+        self.case_name = "odl"
+
     @classmethod
     def copy_opnf_testcases(cls):
         opnfv_testcases_dir = (os.path.dirname(os.path.abspath(__file__)) +
@@ -76,9 +80,19 @@ class ODLTestCases:
             cls.logger.error("Cannot set ODL creds: %s" % str(e))
             return False
 
-    @classmethod
-    def run(cls, **kwargs):
-        dirs = [cls.basic_suite_dir, cls.neutron_suite_dir]
+    def parse_results(self):
+        result = ExecutionResult(self.res_dir + 'output.xml')
+        visitor = ODLResultVisitor()
+        result.visit(visitor)
+        self.criteria = result.suite.status
+        self.start_time = timestamp_to_secs(result.suite.starttime)
+        self.stop_time = timestamp_to_secs(result.suite.endtime)
+        self.details = {}
+        self.details['description'] = result.suite.name
+        self.details['tests'] = visitor.get_data()
+
+    def main(self, **kwargs):
+        dirs = [self.basic_suite_dir, self.neutron_suite_dir]
         try:
             odlusername = kwargs['odlusername']
             odlpassword = kwargs['odlpassword']
@@ -91,35 +105,44 @@ class ODLTestCases:
                          'PORT:' + kwargs['odlwebport'],
                          'RESTCONFPORT:' + kwargs['odlrestconfport']]
         except KeyError as e:
-            cls.logger.error("Cannot run ODL testcases. Please check "
-                             "%s" % str(e))
+            self.logger.error("Cannot run ODL testcases. Please check "
+                              "%s" % str(e))
             return False
-        if (cls.copy_opnf_testcases() and
-                cls.set_robotframework_vars(odlusername, odlpassword)):
+        if (self.copy_opnf_testcases() and
+                self.set_robotframework_vars(odlusername, odlpassword)):
             try:
-                os.makedirs(cls.res_dir)
-            except OSError:
-                pass
-            stdout_file = cls.res_dir + 'stdout.txt'
+                os.makedirs(self.res_dir)
+            except OSError as e:
+                if e.errno != errno.EEXIST:
+                    self.logger.exception(
+                        "Cannot create {}".format(self.res_dir))
+                    return self.EX_RUN_ERROR
+            stdout_file = self.res_dir + 'stdout.txt'
             with open(stdout_file, 'w+') as stdout:
                 run(*dirs, variable=variables,
-                    output=cls.res_dir + 'output.xml',
+                    output=self.res_dir + 'output.xml',
                     log='NONE',
                     report='NONE',
                     stdout=stdout)
                 stdout.seek(0, 0)
-                cls.logger.info("\n" + stdout.read())
-            cls.logger.info("ODL results were successfully generated")
+                self.logger.info("\n" + stdout.read())
+            self.logger.info("ODL results were successfully generated")
+            try:
+                self.parse_results()
+                self.logger.info("ODL results were successfully parsed")
+            except RobotError as e:
+                self.logger.error("Run tests before publishing: %s" %
+                                  e.message)
+                return self.EX_RUN_ERROR
             try:
                 os.remove(stdout_file)
             except OSError:
                 pass
-            return True
+            return self.EX_OK
         else:
-            return False
+            return self.EX_RUN_ERROR
 
-    @classmethod
-    def functest_run(cls):
+    def run(self):
         kclient = op_utils.get_keystone_client()
         keystone_url = kclient.service_catalog.url_for(
             service_type='identity', endpoint_type='publicURL')
@@ -149,34 +172,12 @@ class ODLTestCases:
             else:
                 kwargs['odlip'] = os.environ['SDN_CONTROLLER_IP']
         except KeyError as e:
-            cls.logger.error("Cannot run ODL testcases. Please check env var: "
-                             "%s" % str(e))
-            return False
-
-        return cls.run(**kwargs)
-
-    @classmethod
-    def push_to_db(cls):
-        try:
-            result = ExecutionResult(cls.res_dir + 'output.xml')
-            visitor = ODLResultVisitor()
-            result.visit(visitor)
-            start_time = timestamp_to_secs(result.suite.starttime)
-            stop_time = timestamp_to_secs(result.suite.endtime)
-            details = {}
-            details['description'] = result.suite.name
-            details['tests'] = visitor.get_data()
-            if not ft_utils.push_results_to_db(
-                    "functest", "odl", start_time, stop_time,
-                    result.suite.status, details):
-                cls.logger.error("Cannot push ODL results to DB")
-                return False
-            else:
-                cls.logger.info("ODL results were successfully pushed to DB")
-                return True
-        except RobotError as e:
-            cls.logger.error("Run tests before publishing: %s" % e.message)
+            self.logger.error("Cannot run ODL testcases. "
+                              "Please check env var: "
+                              "%s" % str(e))
+            return self.EX_RUN_ERROR
 
+        return self.main(**kwargs)
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser()
@@ -215,8 +216,9 @@ if __name__ == '__main__':
                         action='store_true')
 
     args = vars(parser.parse_args())
-    if not ODLTestCases.run(**args):
-        sys.exit(os.EX_SOFTWARE)
+    odl = ODLTestCases()
+    result = odl.main(**args)
+    if result != TestCasesBase.TestCasesBase.EX_OK:
+        sys.exit(result)
     if args['pushtodb']:
-        sys.exit(not ODLTestCases.push_to_db())
-    sys.exit(os.EX_OK)
+        sys.exit(odl.push_to_db())