1 ##############################################################################
2 # Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from flask import Flask
11 from flask import jsonify
12 from flask import request
13 from flask_cors import CORS
21 import src.test_parser as test_parser
23 BASE_DIR = unicode(os.path.abspath(os.path.dirname(__file__)), 'utf-8')
24 TESTSUITE_DIR = os.path.join(BASE_DIR, "..", "..", "test", "test_case")
25 SERVICE_DIR = os.path.join(BASE_DIR, "..", "env", "service")
26 CONTEXT_FILE_DIR = os.path.join(BASE_DIR, "..", "env", "context",
28 CONFIG_DIR = os.path.join(BASE_DIR, "..", "env", "config")
35 ###########################################################################
38 return "Hello, World! This is a greet from parser." + SERVICE_DIR
41 @app.route("/execute/testcase", methods=['POST'])
43 suiteName = request.values.get('suiteName')
44 caseName = request.values.get('caseName')
46 casePath = os.path.join(TESTSUITE_DIR, suiteName, caseName)
47 if os.path.exists(casePath):
48 workflowId = test_parser.parse(casePath)
49 if workflowId is None or workflowId == '':
50 return jsonify({"code": 500, "error": "Server Error."})
51 return jsonify({"code": 200, "result": {"workflowId": workflowId}})
53 return jsonify({"code": 300, "error": "no such test case: %s" %
54 (os.path.join(suiteName, caseName))})
55 except BaseException, e:
56 return returnServerError(e)
59 @app.route("/story-content")
60 def getStoryContent():
62 story_name = request.args['story']
63 service_name = request.args['service']
64 storyFileDir = os.path.join("/tmp", "generate_workflow.json")
65 with open(storyFileDir, "r") as f:
66 storyContent = f.read()
67 except BaseException, e:
68 return returnServerError(e)
70 result = {"code": 200, "result":
71 {"service": service_name, "story": story_name,
72 "content": storyContent}}
73 return jsonify(result)
78 ###########################################################################
79 @app.route("/testsuite/list")
84 for fileName in os.listdir(TESTSUITE_DIR):
87 suiteInfo["testsuite"] = fileName
90 except BaseException, e:
92 app.logger.error(traceback.format_exc())
93 return jsonify({"code": 500, "error": "Server error"})
95 return jsonify({"code": 200, "result": res})
98 @app.route("/testsuite/content")
99 def getSuiteContent():
103 suiteName = request.values.get("suiteName")
104 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
105 if os.path.exists(exSuitePath):
106 for fileName in os.listdir(exSuitePath):
109 tcInfo["testcase"] = fileName
113 return jsonify({"code": 300, "error": "no such test suite!"})
114 except BaseException, e:
116 app.logger.error(traceback.format_exc())
117 return jsonify({"code": 500, "error": "Server error"})
119 return jsonify({"code": 200, "result": res})
122 @app.route("/testcase/content")
127 suiteName = request.values.get("suiteName")
128 caseName = request.values.get("caseName")
129 casePath = os.path.join(suiteName, caseName)
130 casePath = os.path.join(TESTSUITE_DIR, casePath)
131 if os.path.exists(casePath):
132 with open(casePath, "r") as f:
133 fileContent = f.read()
135 editorRes = test_parser.getWebTestcase(yaml.load(res))
137 return jsonify({"code": 300, "error": "no such file!"})
138 except BaseException, e:
140 app.logger.error(traceback.format_exc())
141 return jsonify({"code": 500, "error": "Server error"})
143 return jsonify({"code": 200, "result":
144 {"content": res, "editorContent": editorRes}})
147 @app.route("/testsuite/new", methods=['POST'])
150 suiteName = request.values.get("suiteName")
151 for fileName in os.listdir(TESTSUITE_DIR):
152 if fileName == suiteName:
153 return jsonify({"code": 300,
154 "error": "testsuite already exists!"})
155 testSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
156 os.mkdir(testSuitePath)
157 except BaseException, e:
158 return returnServerError(e)
160 return jsonify({"code": 200, "result": "ok"})
163 @app.route("/testsuite/delete", methods=['POST'])
166 suiteName = request.values.get("suiteName")
167 for fileName in os.listdir(TESTSUITE_DIR):
168 if fileName == suiteName:
169 testSuitePath = os.path.join(TESTSUITE_DIR, fileName)
170 del_file(testSuitePath)
171 os.rmdir(testSuitePath)
172 return jsonify({"code": 200, "result": "ok"})
173 except BaseException, e:
174 return returnServerError(e)
176 return jsonify({"code": 300, "error": "no such testsuite!"})
180 for i in os.listdir(path):
181 path_file = os.path.join(path, i)
182 if os.path.isfile(path_file):
188 @app.route("/testcase/new", methods=['POST'])
189 def createTestcase():
191 suiteName = request.values.get("suiteName")
192 caseName = request.values.get("caseName")
193 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
194 if os.path.exists(exSuitePath):
195 for fileName in os.listdir(exSuitePath):
196 if fileName == caseName:
197 return jsonify({"code": 301,
198 "error": "testcase already exists!"})
199 casePath = os.path.join(exSuitePath, caseName)
200 with open(casePath, "w") as f:
201 license_header = getLicense()
202 f.write(license_header)
204 return jsonify({"code": 300, "error": "no such test suite!"})
205 except BaseException, e:
206 return returnServerError(e)
208 return jsonify({"code": 200, "result": "ok"})
211 @app.route("/testcase/delete", methods=['POST'])
212 def deleteTestcase():
214 suiteName = request.values.get("suiteName")
215 caseName = request.values.get("caseName")
216 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
217 if os.path.exists(exSuitePath):
218 for fileName in os.listdir(exSuitePath):
219 if fileName == caseName:
220 casePath = os.path.join(exSuitePath, caseName)
222 return jsonify({"code": 200, "result": "ok"})
223 return jsonify({"code": 301, "error": "no such test case!"})
225 return jsonify({"code": 300, "error": "no such test suite!"})
226 except BaseException, e:
227 return returnServerError(e)
230 @app.route("/testcase/save", methods=["POST"])
233 suiteName = request.values.get("suiteName")
234 caseName = request.values.get("caseName")
235 stepList = json.loads(request.values.get("stepList"))
236 subflowList = json.loads(request.values.get("subflowList"))
237 mainOrdersList = json.loads(request.values.get("mainOrdersList"))
238 jsonObj = {"stepList": stepList, "subflowList": subflowList,
239 "mainOrdersList": mainOrdersList}
240 parseData = test_parser.parseWebTestcase(jsonObj)
242 casePath = os.path.join(suiteName, caseName)
243 casePath = os.path.join(TESTSUITE_DIR, casePath)
244 if os.path.exists(casePath):
245 with open(casePath, "w") as f:
246 license_header = getLicense()
247 f.write(license_header)
248 pyaml.dump(parseData, f, safe=True)
250 return jsonify({"code": 300, "error": "no such file!"})
251 except BaseException, e:
252 return returnServerError(e)
254 return jsonify({"code": 200, "result": "save success"})
258 # 3.1 API FOR SERVICE
259 ############################################################
260 @app.route("/service/list")
261 def getAllServices():
264 for fileName in os.listdir(SERVICE_DIR):
265 serviceName = os.path.splitext(fileName)[0]
266 res.append(serviceName)
267 except BaseException, e:
268 return returnServerError(e)
270 return jsonify({"code": 200, "result": res})
273 @app.route("/service/content")
274 def getServiceContent():
277 serviceName = request.values.get("serviceName")
278 for fileName in os.listdir(SERVICE_DIR):
279 if serviceName == os.path.splitext(fileName)[0]:
281 filePath = os.path.join(SERVICE_DIR, fileName)
282 with open(filePath, "r") as f:
283 content = yaml.load(f)
284 apisArr = content[serviceName]['apis']
285 for i in range(len(apisArr)):
286 apisArr[i].pop("method")
287 apisArr[i].pop("baseuri")
288 res["actions"] = apisArr
289 except BaseException, e:
290 return returnServerError(e)
293 return jsonify({"code": 300, "error": "no such service!"})
295 return jsonify({"code": 200, "result": res})
298 def paramTransform(paramDict):
300 for (key, value) in paramDict.items():
302 paramJson["name"] = key
303 paramJson["description"] = value["help"]
304 if "params" in value:
305 paramJson["params"] = paramTransform(value["params"])
306 res.append(paramJson)
310 @app.route("/service/action_response")
311 def actionResponse():
314 serviceName = request.values.get("serviceName")
315 actionName = request.values.get("actionName")
316 for fileName in os.listdir(SERVICE_DIR):
317 if serviceName == os.path.splitext(fileName)[0]:
318 res["responseParams"] = []
319 filePath = os.path.join(SERVICE_DIR, fileName)
320 with open(filePath, "r") as f:
321 content = yaml.load(f)
322 apisArr = content[serviceName]['apis']
323 for i in range(len(apisArr)):
324 if actionName == apisArr[i]['name'] and (
325 "response" in apisArr[i]):
326 res["responseParams"] = apisArr[i]["response"]
327 except BaseException, e:
328 return returnServerError(e)
330 return jsonify({"code": 300, "error": "no such service!"})
331 return jsonify({"code": 200, "result": res})
335 # 3.2 API FOR ENVIRONMENT SERVICE AND CONTEXT
336 ###########################################################################
337 @app.route('/env/getAllServices')
342 for fileName in os.listdir(SERVICE_DIR):
345 item['name'] = os.path.splitext(fileName)[0]
346 filePath = os.path.join(SERVICE_DIR, fileName)
347 filemt = time.localtime(os.stat(filePath).st_mtime)
348 item['time'] = time.strftime("%Y-%m-%d", filemt)
351 except BaseException, e:
352 return returnServerError(e)
353 return jsonify({"code": 200, "result": res})
356 @app.route('/env/getService')
359 serviceName = request.values.get('serviceName')
360 serviceFile = serviceName + '.yaml'
361 servicePath = os.path.join(SERVICE_DIR, serviceFile)
362 if os.path.exists(servicePath):
363 with open(servicePath, "r") as f:
364 serviceDict = yaml.load(f)
365 serviceDict = serviceDict[serviceName]
366 return jsonify({"code": 200, "result": serviceDict})
368 return jsonify({"code": 300, "error": "no such service!"})
369 except BaseException, e:
370 return returnServerError(e)
373 @app.route('/env/createService', methods=['POST'])
376 name = str(request.values.get('name'))
377 ip = str(request.values.get('ip'))
378 port = int(request.values.get('port'))
379 apis = json.loads(request.values.get('apis'))
387 serviceJson = json.dumps(service, indent=True)
389 app.logger.debug(service)
391 serviceFile = name + '.yaml'
392 servicePath = os.path.join(SERVICE_DIR, serviceFile)
393 with open(servicePath, 'w') as f:
394 license_header = getLicense()
395 f.write(license_header)
396 pyaml.dump(service, f, safe=True)
397 except BaseException, e:
398 return returnServerError(e)
399 return jsonify({"code": 200, "result": "create success!"})
402 @app.route('/env/editService', methods=['POST'])
405 oldName = str(request.values.get('oldName'))
406 name = str(request.values.get('newName'))
407 ip = str(request.values.get('ip'))
408 port = int(request.values.get('port'))
409 apis = json.loads(request.values.get('apis'))
410 app.logger.debug(apis)
418 serviceJson = json.dumps(service, indent=True)
420 app.logger.debug(service)
422 for fileName in os.listdir(SERVICE_DIR):
423 serviceName = os.path.splitext(fileName)[0]
424 if serviceName == oldName:
425 filePath = os.path.join(SERVICE_DIR, fileName)
428 serviceFile = name + '.yaml'
429 servicePath = os.path.join(SERVICE_DIR, serviceFile)
430 with open(servicePath, 'w') as f:
431 license_header = getLicense()
432 f.write(license_header)
433 pyaml.dump(service, f, safe=True)
434 except BaseException, e:
435 return returnServerError(e)
436 return jsonify({"code": 200, "result": "edit success!"})
439 @app.route('/env/deleteService', methods=['POST'])
442 name = str(request.values.get('serviceName'))
444 for fileName in os.listdir(SERVICE_DIR):
445 serviceName = os.path.splitext(fileName)[0]
446 if serviceName == name:
447 filePath = os.path.join(SERVICE_DIR, fileName)
449 except BaseException, e:
450 return returnServerError(e)
451 return jsonify({"code": 200, "result": "delete success!"})
454 @app.route('/env/getContext')
457 with open(CONTEXT_FILE_DIR, "r") as f:
458 fileContent = f.read()
460 except BaseException, e:
461 return returnServerError(e)
462 return jsonify({"code": 200, "result": {"context": res}})
465 @app.route('/env/editContext', methods=['POST'])
468 context = request.values.get("context")
469 test = yaml.load(context)
471 with open(CONTEXT_FILE_DIR, "w") as f:
473 except yaml.constructor.ConstructorError, e:
474 app.logger.error(traceback.format_exc())
475 return jsonify({"code": 500, "error":
476 "context content error: not a .yaml file!"})
477 except BaseException, e:
478 return returnServerError(e)
480 return jsonify({"code": 200, "result": "edit context success!"})
484 licenseDir = os.path.join(CONFIG_DIR, "license")
485 with open(licenseDir, 'r') as f:
489 return content + '\n---\n\n'
491 ###########################################################################
494 def returnServerError(e, msg="Server Error"):
496 app.logger.error(traceback.format_exc())
497 return jsonify({"code": 500, "error": msg})
500 if __name__ == "__main__":
501 app.run(host='0.0.0.0', port=5310)