Update API to python3 and add some addtional APIs 69/68669/3
authorxudan <xudan16@huawei.com>
Thu, 24 Oct 2019 08:04:10 +0000 (04:04 -0400)
committerxudan <xudan16@huawei.com>
Fri, 25 Oct 2019 07:46:11 +0000 (03:46 -0400)
Update the unit test case to fix the py27 failure.

Change-Id: Ic75b8fc037c320ec5599eb007dcee43788e32807
Signed-off-by: xudan <xudan16@huawei.com>
dovetail/api/app/routes.py
dovetail/api/app/server.py
dovetail/tests/unit/utils/test_dovetail_utils.py

index b1557b6..e60f10a 100644 (file)
@@ -9,7 +9,7 @@ import uuid
 from flask import Flask, jsonify, request
 from flask_cors import CORS
 
-import server
+import app.server as server
 
 app = Flask(__name__)
 CORS(app)
@@ -51,9 +51,10 @@ def run_testcases():
                                os.pardir, os.pardir))
     run_script = os.path.join(repo_dir, 'run.py')
 
-    cmd = 'python {} {}'.format(run_script, input_str)
+    cmd = 'python3 {} {}'.format(run_script, input_str)
     api_home = os.path.join(dovetail_home, str(requestId))
-    subprocess.Popen(cmd, shell=True, env={'DOVETAIL_HOME': api_home})
+    subprocess.Popen(cmd, shell=True, env={'DOVETAIL_HOME': api_home,
+                     'LC_ALL': 'C.UTF-8', 'LANG': 'C.UTF-8'})
 
     testcases_file = os.path.join(dovetail_home, str(requestId),
                                   'results', 'testcases.json')
@@ -72,7 +73,7 @@ def run_testcases():
         testsuite = data['testsuite']
 
     result = server.get_execution_status(dovetail_home, testsuite,
-                                         testcases, requestId)
+                                         testcases, testcases, requestId)
 
     return jsonify({'result': result}), 200
 
index e6b1df4..312657d 100644 (file)
@@ -2,8 +2,8 @@ import json
 import os
 import shutil
 
-import constants
-import utils
+import app.constants as constants
+import app.utils as utils
 
 from dovetail.testcase import Testsuite, Testcase
 
@@ -134,10 +134,30 @@ def get_execution_status(dovetail_home, testsuite, request_testcases,
                    'timestart': None, 'endTime': None}
             results.append(res)
         if tc.startswith('bottlenecks'):
-            pass
+            status, result = get_bottlenecks_status(results_dir, tc)
+            res = {'testCaseName': tc, 'testSuiteName': testsuite,
+                   'scenario': 'nfvi', 'executionId': requestId,
+                   'results': result, 'status': status,
+                   'timestart': None, 'endTime': None}
+            results.append(res)
     return results
 
 
+def get_status_from_total_file(total_file, testcase):
+    with open(total_file, 'r') as f:
+        for jsonfile in f:
+            try:
+                data = json.loads(jsonfile)
+                for item in data['testcases_list']:
+                    if item['name'] == testcase:
+                        return item['result'], item['sub_testcase']
+            except KeyError as e:
+                return 'FAILED', None
+            except ValueError:
+                continue
+    return 'FAILED', None
+
+
 def get_functest_status(results_dir, testcase):
     functest_file = os.path.join(results_dir, 'functest_results.txt')
     total_file = os.path.join(results_dir, 'results.json')
@@ -152,21 +172,10 @@ def get_functest_status(results_dir, testcase):
 
     # get criteria and sub_testcase from results.json when all tests completed
     if os.path.isfile(total_file):
-        with open(total_file, 'r') as f:
-            for jsonfile in f:
-                try:
-                    data = json.loads(jsonfile)
-                    for item in data['testcases_list']:
-                        if item['name'] == testcase:
-                            criteria = item['result']
-                            sub_testcase = item['sub_testcase']
-                            break
-                    else:
-                        return 'FAILED', None
-                except KeyError:
-                    return 'FAILED', None
-                except ValueError:
-                    continue
+        criteria, sub_testcase = get_status_from_total_file(total_file,
+                                                            testcase)
+        if criteria == 'FAILED':
+            return 'FAILED', None
 
     # get detailed results from functest_results.txt
     with open(functest_file, 'r') as f:
@@ -200,11 +209,21 @@ def get_yardstick_status(results_dir, testcase):
         if not os.path.isfile(total_file):
             return 'IN_PROGRESS', None
         return 'FAILED', None
+
+    criteria = None
+
+    # get criteria and sub_testcase from results.json when all tests completed
+    if os.path.isfile(total_file):
+        criteria, _ = get_status_from_total_file(total_file, testcase)
+        if criteria == 'FAILED':
+            return 'FAILED', None
+
     with open(yardstick_file, 'r') as f:
         for jsonfile in f:
             data = json.loads(jsonfile)
             try:
-                criteria = data['result']['criteria']
+                if not criteria:
+                    criteria = data['result']['criteria']
                 if criteria == 'PASS':
                     details = data['result']['testcases']
                     for key, value in details.items():
@@ -217,3 +236,34 @@ def get_yardstick_status(results_dir, testcase):
     status = 'COMPLETED' if criteria == 'PASS' else 'FAILED'
     results = {'criteria': criteria, 'timestart': None, 'timestop': None}
     return status, results
+
+
+def get_bottlenecks_status(results_dir, testcase):
+    bottlenecks_file = os.path.join(results_dir, 'stress_logs',
+                                    '{}.out'.format(testcase))
+    total_file = os.path.join(results_dir, 'results.json')
+    if not os.path.isfile(bottlenecks_file):
+        if not os.path.isfile(total_file):
+            return 'IN_PROGRESS', None
+        return 'FAILED', None
+
+    criteria = None
+
+    # get criteria and sub_testcase from results.json when all tests completed
+    if os.path.isfile(total_file):
+        criteria, _ = get_status_from_total_file(total_file, testcase)
+        if criteria == 'FAILED':
+            return 'FAILED', None
+
+    with open(bottlenecks_file, 'r') as f:
+        for jsonfile in f:
+            data = json.loads(jsonfile)
+            try:
+                if not criteria:
+                    criteria = data['data_body']['result']
+            except KeyError:
+                return 'FAILED', None
+
+    status = 'COMPLETED' if criteria == 'PASS' else 'FAILED'
+    results = {'criteria': criteria, 'timestart': None, 'timestop': None}
+    return status, results
index 2d2cdb0..7ec177d 100644 (file)
@@ -511,7 +511,7 @@ class DovetailUtilsTesting(unittest.TestCase):
         hosts_obj.write.assert_called_once()
 
     def test_get_obj_by_path(self):
-        obj = {'name': 'name', 'validate': {'testcase': 'testcase'}}
+        obj = {'list': ['a', 'b'], 'name': 'name'}
         dst_path = ('name',)
 
         expected = 'name'