Merge "Publish rally logs to ease debugging."
[functest.git] / functest / opnfv_tests / openstack / rally / rally.py
index 872e75b..4a63629 100644 (file)
@@ -168,14 +168,6 @@ class RallyBase(singlevm.VmReady1):
 
         return False
 
-    @staticmethod
-    def get_cmd_output(proc):
-        """Get command stdout."""
-        result = ""
-        for line in proc.stdout:
-            result += line
-        return result
-
     @staticmethod
     def excl_scenario():
         """Exclude scenario."""
@@ -297,23 +289,17 @@ class RallyBase(singlevm.VmReady1):
         # put detailed result to log
         cmd = (["rally", "task", "detailed", "--uuid", task_id])
         LOGGER.debug('running command: %s', cmd)
-        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                                stderr=subprocess.STDOUT)
-        json_detailed = self.get_cmd_output(proc)
-        LOGGER.info('%s', json_detailed)
+        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+        LOGGER.info("%s\n%s", " ".join(cmd), output)
 
         # save report as JSON
-        cmd = (["rally", "task", "report", "--json", "--uuid", task_id])
-        LOGGER.debug('running command: %s', cmd)
-        with open(os.devnull, 'w') as devnull:
-            proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                                    stderr=devnull)
-        json_results = self.get_cmd_output(proc)
         report_json_name = 'opnfv-{}.json'.format(test_name)
         report_json_dir = os.path.join(self.RESULTS_DIR, report_json_name)
-        with open(report_json_dir, 'w') as r_file:
-            LOGGER.debug('saving json file')
-            r_file.write(json_results)
+        cmd = (["rally", "task", "report", "--json", "--uuid", task_id,
+                "--out", report_json_dir])
+        LOGGER.debug('running command: %s', cmd)
+        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+        LOGGER.info("%s\n%s", " ".join(cmd), output)
 
         # save report as HTML
         report_html_name = 'opnfv-{}.html'.format(test_name)
@@ -321,9 +307,10 @@ class RallyBase(singlevm.VmReady1):
         cmd = (["rally", "task", "report", "--html", "--uuid", task_id,
                 "--out", report_html_dir])
         LOGGER.debug('running command: %s', cmd)
-        with open(os.devnull, 'w') as devnull:
-            subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=devnull)
+        output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
+        LOGGER.info("%s\n%s", " ".join(cmd), output)
 
+        json_results = open(report_json_dir).read()
         self._append_summary(json_results, test_name)
 
         # parse JSON operation result
@@ -350,23 +337,15 @@ class RallyBase(singlevm.VmReady1):
                 task_file, "--task-args",
                 str(self._build_task_args(test_name))])
         LOGGER.debug('running command: %s', cmd)
-
         proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                 stderr=subprocess.STDOUT)
-        output = self.get_cmd_output(proc)
-        task_id = self.get_task_id(output)
+        output = proc.communicate()[0]
 
+        task_id = self.get_task_id(output)
         LOGGER.debug('task_id : %s', task_id)
-
         if task_id is None:
-            LOGGER.error('Failed to retrieve task_id, validating task...')
-            cmd = (["rally", "task", "validate", "--task", task_file,
-                    "--task-args", str(self._build_task_args(test_name))])
-            LOGGER.debug('running command: %s', cmd)
-            proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                                    stderr=subprocess.STDOUT)
-            output = self.get_cmd_output(proc)
-            LOGGER.error("Task validation result:" + "\n" + output)
+            LOGGER.error("Failed to retrieve task_id")
+            LOGGER.error("Result:\n%s", output)
             raise Exception("Failed to retrieve task id")
 
         self._save_results(test_name, task_id)
@@ -478,8 +457,9 @@ class RallyBase(singlevm.VmReady1):
 
     def clean(self):
         """Cleanup of OpenStack resources. Should be called on completion."""
+        if self.flavor_alt:
+            self.orig_cloud.delete_flavor(self.flavor_alt.id)
         super(RallyBase, self).clean()
-        self.orig_cloud.delete_flavor(self.flavor_alt.id)
 
     def is_successful(self):
         """The overall result of the test."""
@@ -494,7 +474,8 @@ class RallyBase(singlevm.VmReady1):
         """Run testcase."""
         self.start_time = time.time()
         try:
-            super(RallyBase, self).run(**kwargs)
+            assert super(RallyBase, self).run(
+                **kwargs) == testcase.TestCase.EX_OK
             conf_utils.create_rally_deployment()
             self._prepare_env()
             self._run_tests()
@@ -502,6 +483,7 @@ class RallyBase(singlevm.VmReady1):
             res = testcase.TestCase.EX_OK
         except Exception as exc:   # pylint: disable=broad-except
             LOGGER.error('Error with run: %s', exc)
+            self.result = 0
             res = testcase.TestCase.EX_RUN_ERROR
         self.stop_time = time.time()
         return res