summaryrefslogtreecommitdiffstats
path: root/dovetail/api/app
diff options
context:
space:
mode:
Diffstat (limited to 'dovetail/api/app')
-rw-r--r--dovetail/api/app/constants.py13
-rw-r--r--dovetail/api/app/routes.py79
-rw-r--r--dovetail/api/app/server.py195
-rw-r--r--dovetail/api/app/utils.py21
4 files changed, 307 insertions, 1 deletions
diff --git a/dovetail/api/app/constants.py b/dovetail/api/app/constants.py
index 14d9145e..f6ffd1ba 100644
--- a/dovetail/api/app/constants.py
+++ b/dovetail/api/app/constants.py
@@ -1,2 +1,15 @@
NFVI_PROJECT = ['bottlenecks', 'functest', 'yardstick']
VNF_PROJECT = ['onap-vtp', 'onap-vvp']
+RUN_TEST_ITEMS = {
+ 'arguments': {
+ 'no_multiple': ['testsuite', 'deploy_scenario'],
+ 'multiple': ['testarea', 'testcase']
+ },
+ 'options': ['mandatory', 'no_api_validation', 'no_clean', 'stop', 'debug',
+ 'opnfv_ci', 'report', 'offline', 'optional']
+}
+CONFIG_YAML_FILES = {
+ 'hosts': 'hosts.yaml',
+ 'pods': 'pod.yaml',
+ 'tempest_conf': 'tempest_conf.yaml'
+}
diff --git a/dovetail/api/app/routes.py b/dovetail/api/app/routes.py
index 6c327323..b1557b67 100644
--- a/dovetail/api/app/routes.py
+++ b/dovetail/api/app/routes.py
@@ -1,6 +1,12 @@
#!flask/bin/python
-from flask import Flask, jsonify
+import json
+import os
+import subprocess
+import time
+import uuid
+
+from flask import Flask, jsonify, request
from flask_cors import CORS
import server
@@ -19,3 +25,74 @@ def get_all_testsuites():
def get_testcases():
testcases = server.list_testcases()
return jsonify({'testcases': testcases}), 200
+
+
+@app.route('/api/v1/scenario/nfvi/execution', methods=['POST'])
+def run_testcases():
+ requestId = request.args.get('requestId')
+ if not requestId:
+ requestId = uuid.uuid1()
+ if os.getenv('DOVETAIL_HOME'):
+ dovetail_home = os.getenv('DOVETAIL_HOME')
+ else:
+ return 'No DOVETAIL_HOME found in env.\n', 500
+
+ msg, ret = server.set_conf_files(request.json, dovetail_home, requestId)
+ if not ret:
+ return msg, 500
+
+ msg, ret = server.set_vm_images(request.json, dovetail_home, requestId)
+ if not ret:
+ return msg, 500
+
+ input_str = server.parse_request(request.json)
+
+ repo_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
+ os.pardir, os.pardir))
+ run_script = os.path.join(repo_dir, 'run.py')
+
+ cmd = 'python {} {}'.format(run_script, input_str)
+ api_home = os.path.join(dovetail_home, str(requestId))
+ subprocess.Popen(cmd, shell=True, env={'DOVETAIL_HOME': api_home})
+
+ testcases_file = os.path.join(dovetail_home, str(requestId),
+ 'results', 'testcases.json')
+ for loop in range(60):
+ if not os.path.isfile(testcases_file):
+ time.sleep(1)
+ else:
+ break
+ else:
+ return 'Can not get file testcases.json.\n', 500
+
+ with open(testcases_file, "r") as f:
+ for jsonfile in f:
+ data = json.loads(jsonfile)
+ testcases = data['testcases']
+ testsuite = data['testsuite']
+
+ result = server.get_execution_status(dovetail_home, testsuite,
+ testcases, requestId)
+
+ return jsonify({'result': result}), 200
+
+
+@app.route('/api/v1/scenario/nfvi/execution/status/<exec_id>',
+ methods=['POST'])
+def get_testcases_status(exec_id):
+ if 'testcase' not in request.json:
+ return 'Need testcases list as input.\n', 400
+
+ testcases = request.json['testcase']
+ dovetail_home = os.getenv('DOVETAIL_HOME')
+
+ testcases_file = os.path.join(dovetail_home, str(exec_id),
+ 'results', 'testcases.json')
+ with open(testcases_file, "r") as f:
+ for jsonfile in f:
+ data = json.loads(jsonfile)
+ testsuite = data['testsuite']
+
+ result = server.get_execution_status(dovetail_home, testsuite,
+ testcases, data['testcases'], exec_id)
+ return jsonify({'result': result}), 200
diff --git a/dovetail/api/app/server.py b/dovetail/api/app/server.py
index 4428c251..e6b1df46 100644
--- a/dovetail/api/app/server.py
+++ b/dovetail/api/app/server.py
@@ -1,4 +1,9 @@
+import json
+import os
+import shutil
+
import constants
+import utils
from dovetail.testcase import Testsuite, Testcase
@@ -22,3 +27,193 @@ def list_testcases():
testcase['scenario'] = 'unknown'
testcase_list.append(testcase)
return testcase_list
+
+
+def set_vm_images(data, dovetail_home, requestId):
+ image_path = os.path.join(dovetail_home, str(requestId), 'images')
+ try:
+ origin_image_path = data['conf']['vm_images']
+ except KeyError:
+ origin_image_path = os.path.join(dovetail_home, 'images')
+ if os.path.exists(origin_image_path):
+ try:
+ shutil.copytree(origin_image_path, image_path)
+ except Exception as e:
+ return str(e), False
+ return "Success to set vm images.\n", True
+ else:
+ return "Could not find vm images.\n", False
+
+
+def set_conf_files(data, dovetail_home, requestId):
+ config_path = os.path.join(dovetail_home, str(requestId), 'pre_config')
+ origin_config_path = os.path.join(dovetail_home, 'pre_config')
+ if os.path.exists(origin_config_path):
+ try:
+ shutil.copytree(origin_config_path, config_path)
+ except Exception as e:
+ return str(e), False
+
+ # check and prepare mandatory env_config.sh file
+ # if there are envs in request body, use it
+ # otherwise, use the file in pre_config
+ # if don't have this file, return False with error message
+ env_file = os.path.join(config_path, 'env_config.sh')
+ try:
+ utils.write_env_file(data['conf']['envs'], env_file)
+ except KeyError:
+ if not os.path.isfile(env_file):
+ return "No 'envs' found in the request body.\n", False
+ else:
+ pass
+ except Exception as e:
+ return str(e), False
+
+ # check and prepare other optional yaml files
+ for key, value in constants.CONFIG_YAML_FILES.items():
+ config_file = os.path.join(config_path, value)
+ try:
+ utils.write_yaml_file(data['conf'][key], config_file)
+ except KeyError:
+ pass
+ except Exception as e:
+ return str(e), False
+
+ return 'Success to prepare all config files.\n', True
+
+
+def parse_request(request_json):
+ output = ''
+ default_args = constants.RUN_TEST_ITEMS['arguments']
+ default_options = constants.RUN_TEST_ITEMS['options']
+
+ for arg in default_args['no_multiple']:
+ if arg in request_json.keys():
+ output = output + ' --{} {}'.format(arg, request_json[arg])
+ for arg in default_args['multiple']:
+ if arg in request_json.keys() and request_json[arg]:
+ for item in request_json[arg]:
+ output = output + ' --{} {}'.format(arg, item)
+
+ if 'options' not in request_json.keys():
+ return output
+
+ for option in default_options:
+ if option in request_json['options']:
+ output = output + ' --{}'.format(option)
+
+ return output
+
+
+def get_execution_status(dovetail_home, testsuite, request_testcases,
+ exec_testcases, requestId):
+ results_dir = os.path.join(dovetail_home, str(requestId), 'results')
+ results = []
+ for tc in request_testcases:
+ if tc not in exec_testcases:
+ res = {'testCaseName': tc, 'status': 'NOT_EXECUTED'}
+ results.append(res)
+ continue
+ if tc.startswith('functest'):
+ status, result = get_functest_status(results_dir, tc)
+ res = {'testCaseName': tc, 'testSuiteName': testsuite,
+ 'scenario': 'nfvi', 'executionId': requestId,
+ 'results': result, 'status': status}
+ if not result:
+ res['timestart'] = None
+ res['endTime'] = None
+ else:
+ res['timestart'] = result['timestart']
+ res['endTime'] = result['timestop']
+ results.append(res)
+ if tc.startswith('yardstick'):
+ status, result = get_yardstick_status(results_dir, tc)
+ res = {'testCaseName': tc, 'testSuiteName': testsuite,
+ 'scenario': 'nfvi', 'executionId': requestId,
+ 'results': result, 'status': status,
+ 'timestart': None, 'endTime': None}
+ results.append(res)
+ if tc.startswith('bottlenecks'):
+ pass
+ return results
+
+
+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')
+ if not os.path.isfile(functest_file):
+ if not os.path.isfile(total_file):
+ return 'IN_PROGRESS', None
+ return 'FAILED', None
+ criteria = None
+ sub_testcase = []
+ timestart = None
+ timestop = None
+
+ # 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
+
+ # get detailed results from functest_results.txt
+ with open(functest_file, 'r') as f:
+ for jsonfile in f:
+ try:
+ data = json.loads(jsonfile)
+ if data['build_tag'].endswith(testcase):
+ criteria = data['criteria'] if not criteria else criteria
+ timestart = data['start_date']
+ timestop = data['stop_date']
+ break
+ except KeyError:
+ return 'FAILED', None
+ except ValueError:
+ continue
+ else:
+ if not criteria:
+ return 'IN_PROGRESS', None
+
+ status = 'COMPLETED' if criteria == 'PASS' else 'FAILED'
+ results = {'criteria': criteria, 'sub_testcase': sub_testcase,
+ 'timestart': timestart, 'timestop': timestop}
+ return status, results
+
+
+def get_yardstick_status(results_dir, testcase):
+ yardstick_file = os.path.join(results_dir, 'ha_logs',
+ '{}.out'.format(testcase))
+ total_file = os.path.join(results_dir, 'results.json')
+ if not os.path.isfile(yardstick_file):
+ if not os.path.isfile(total_file):
+ return 'IN_PROGRESS', None
+ return 'FAILED', None
+ with open(yardstick_file, 'r') as f:
+ for jsonfile in f:
+ data = json.loads(jsonfile)
+ try:
+ criteria = data['result']['criteria']
+ if criteria == 'PASS':
+ details = data['result']['testcases']
+ for key, value in details.items():
+ sla_pass = value['tc_data'][0]['data']['sla_pass']
+ if not 1 == sla_pass:
+ criteria = 'FAIL'
+ except KeyError:
+ return 'FAILED', None
+
+ status = 'COMPLETED' if criteria == 'PASS' else 'FAILED'
+ results = {'criteria': criteria, 'timestart': None, 'timestop': None}
+ return status, results
diff --git a/dovetail/api/app/utils.py b/dovetail/api/app/utils.py
new file mode 100644
index 00000000..1708dfb7
--- /dev/null
+++ b/dovetail/api/app/utils.py
@@ -0,0 +1,21 @@
+import json
+import os
+
+
+def write_env_file(envs, file_path):
+ file_dir = os.path.dirname(file_path)
+ if not os.path.exists(file_dir):
+ os.makedirs(file_dir)
+ with open(file_path, "w") as f:
+ for key, value in envs.items():
+ f.write("export {}={}\n".format(key, value))
+ return True
+
+
+def write_yaml_file(data, file_path):
+ file_dir = os.path.dirname(file_path)
+ if not os.path.exists(file_dir):
+ os.makedirs(file_dir)
+ with open(file_path, "w") as f:
+ f.write(json.dumps(data) + '\n')
+ return True