1 ##############################################################################
\r
2 # Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
\r
4 # All rights reserved. This program and the accompanying materials
\r
5 # are made available under the terms of the Apache License, Version 2.0
\r
6 # which accompanies this distribution, and is available at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
8 ##############################################################################
\r
10 from flask import Flask
\r
11 from flask import jsonify
\r
12 from flask import request
\r
13 from flask_cors import CORS
\r
21 import src.test_parser as test_parser
\r
24 BASE_DIR = os.path.abspath(os.path.dirname(__file__))
\r
25 TESTSUITE_DIR = os.path.join(BASE_DIR, "..", "..", "test", "test_case")
\r
26 SERVICE_DIR = os.path.join(BASE_DIR, "..", "env", "service")
\r
27 CONTEXT_FILE_DIR = os.path.join(BASE_DIR, "..", "env", "context",
\r
29 app = Flask(__name__)
\r
35 ###########################################################################
\r
38 return "Hello, World! This is a greet from parser." + SERVICE_DIR
\r
41 @app.route("/execute/testcase", methods=['POST'])
\r
43 suiteName = request.values.get('suiteName')
\r
44 caseName = request.values.get('caseName')
\r
46 casePath = os.path.join(TESTSUITE_DIR, suiteName, caseName)
\r
47 if os.path.exists(casePath):
\r
48 workflowId = test_parser.parse(casePath)
\r
49 if workflowId is None or workflowId == '':
\r
50 return jsonify({"code": 500, "error": "Server Error."})
\r
51 return jsonify({"code": 200, "result": {"workflowId": workflowId}})
\r
53 return jsonify({"code": 300, "error": "no such test case: %s" %
\r
54 (os.path.join(suiteName, caseName))})
\r
55 except BaseException, e:
\r
56 return returnServerError(e)
\r
59 @app.route("/story-content")
\r
60 def getStoryContent():
\r
62 story_name = request.args['story']
\r
63 service_name = request.args['service']
\r
64 storyFileDir = os.path.join("/tmp", "generate_workflow.json")
\r
65 with open(storyFileDir, "r") as f:
\r
66 storyContent = f.read()
\r
67 except BaseException, e:
\r
68 return returnServerError(e)
\r
70 result = {"code": 200, "result":
\r
71 {"service": service_name, "story": story_name,
\r
72 "content": storyContent}}
\r
73 return jsonify(result)
\r
78 ###########################################################################
\r
79 @app.route("/testsuite/list")
\r
84 for fileName in os.listdir(TESTSUITE_DIR):
\r
86 suiteInfo["id"] = id
\r
87 suiteInfo["testsuite"] = fileName
\r
88 res.append(suiteInfo)
\r
90 except BaseException, e:
\r
92 app.logger.error(traceback.format_exc())
\r
93 return jsonify({"code": 500, "error": "Server error"})
\r
95 return jsonify({"code": 200, "result": res})
\r
98 @app.route("/testsuite/content")
\r
99 def getSuiteContent():
\r
103 suiteName = request.values.get("suiteName")
\r
104 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
\r
105 if os.path.exists(exSuitePath):
\r
106 for fileName in os.listdir(exSuitePath):
\r
109 tcInfo["testcase"] = fileName
\r
113 return jsonify({"code": 300, "error": "no such test suite!"})
\r
114 except BaseException, e:
\r
116 app.logger.error(traceback.format_exc())
\r
117 return jsonify({"code": 500, "error": "Server error"})
\r
119 return jsonify({"code": 200, "result": res})
\r
122 @app.route("/testcase/content")
\r
123 def getTCContent():
\r
127 suiteName = request.values.get("suiteName")
\r
128 caseName = request.values.get("caseName")
\r
129 casePath = os.path.join(suiteName, caseName)
\r
130 casePath = os.path.join(TESTSUITE_DIR, casePath)
\r
131 if os.path.exists(casePath):
\r
132 with open(casePath, "r") as f:
\r
133 fileContent = f.read()
\r
135 editorRes = test_parser.getWebTestcase(yaml.load(res))
\r
137 return jsonify({"code": 300, "error": "no such file!"})
\r
138 except BaseException, e:
\r
140 app.logger.error(traceback.format_exc())
\r
141 return jsonify({"code": 500, "error": "Server error"})
\r
143 return jsonify({"code": 200, "result":
\r
144 {"content": res, "editorContent": editorRes}})
\r
147 @app.route("/testsuite/new", methods=['POST'])
\r
150 suiteName = request.values.get("suiteName")
\r
151 for fileName in os.listdir(TESTSUITE_DIR):
\r
152 if fileName == suiteName:
\r
153 return jsonify({"code": 300,
\r
154 "error": "testsuite already exists!"})
\r
155 testSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
\r
156 os.mkdir(testSuitePath)
\r
157 except BaseException, e:
\r
158 return returnServerError(e)
\r
160 return jsonify({"code": 200, "result": "ok"})
\r
163 @app.route("/testsuite/delete", methods=['POST'])
\r
166 suiteName = request.values.get("suiteName")
\r
167 for fileName in os.listdir(TESTSUITE_DIR):
\r
168 if fileName == suiteName:
\r
169 testSuitePath = os.path.join(TESTSUITE_DIR, fileName)
\r
170 del_file(testSuitePath)
\r
171 os.rmdir(testSuitePath)
\r
172 return jsonify({"code": 200, "result": "ok"})
\r
173 except BaseException, e:
\r
174 return returnServerError(e)
\r
176 return jsonify({"code": 300, "error": "no such testsuite!"})
\r
179 def del_file(path):
\r
180 for i in os.listdir(path):
\r
181 path_file = os.path.join(path, i)
\r
182 if os.path.isfile(path_file):
\r
183 os.remove(path_file)
\r
185 del_file(path_file)
\r
188 @app.route("/testcase/new", methods=['POST'])
\r
189 def createTestcase():
\r
191 suiteName = request.values.get("suiteName")
\r
192 caseName = request.values.get("caseName")
\r
193 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
\r
194 if os.path.exists(exSuitePath):
\r
195 for fileName in os.listdir(exSuitePath):
\r
196 if fileName == caseName:
\r
197 return jsonify({"code": 301,
\r
198 "error": "testcase already exists!"})
\r
199 casePath = os.path.join(exSuitePath, caseName)
\r
200 with open(casePath, "w") as f:
\r
201 # the next line is a placeholder.
\r
204 return jsonify({"code": 300, "error": "no such test suite!"})
\r
205 except BaseException, e:
\r
206 return returnServerError(e)
\r
208 return jsonify({"code": 200, "result": "ok"})
\r
211 @app.route("/testcase/delete", methods=['POST'])
\r
212 def deleteTestcase():
\r
214 suiteName = request.values.get("suiteName")
\r
215 caseName = request.values.get("caseName")
\r
216 exSuitePath = os.path.join(TESTSUITE_DIR, suiteName)
\r
217 if os.path.exists(exSuitePath):
\r
218 for fileName in os.listdir(exSuitePath):
\r
219 if fileName == caseName:
\r
220 casePath = os.path.join(exSuitePath, caseName)
\r
221 os.remove(casePath)
\r
222 return jsonify({"code": 200, "result": "ok"})
\r
223 return jsonify({"code": 301, "error": "no such test case!"})
\r
225 return jsonify({"code": 300, "error": "no such test suite!"})
\r
226 except BaseException, e:
\r
227 return returnServerError(e)
\r
230 @app.route("/testcase/save", methods=["POST"])
\r
231 def saveTCContent():
\r
233 suiteName = request.values.get("suiteName")
\r
234 caseName = request.values.get("caseName")
\r
235 stepList = json.loads(request.values.get("stepList"))
\r
236 subflowList = json.loads(request.values.get("subflowList"))
\r
237 mainOrdersList = json.loads(request.values.get("mainOrdersList"))
\r
238 jsonObj = {"stepList": stepList, "subflowList": subflowList,
\r
239 "mainOrdersList": mainOrdersList}
\r
240 parseData = test_parser.parseWebTestcase(jsonObj)
\r
242 casePath = os.path.join(suiteName, caseName)
\r
243 casePath = os.path.join(TESTSUITE_DIR, casePath)
\r
244 if os.path.exists(casePath):
\r
245 with open(casePath, "w") as f:
\r
246 pyaml.dump(parseData, f, safe=True)
\r
248 return jsonify({"code": 300, "error": "no such file!"})
\r
249 except BaseException, e:
\r
250 return returnServerError(e)
\r
252 return jsonify({"code": 200, "result": "save success"})
\r
256 # 3.1 API FOR SERVICE
\r
257 ############################################################
\r
258 @app.route("/service/list")
\r
259 def getAllServices():
\r
262 for fileName in os.listdir(SERVICE_DIR):
\r
263 serviceName = os.path.splitext(fileName)[0]
\r
264 res.append(serviceName)
\r
265 except BaseException, e:
\r
266 return returnServerError(e)
\r
268 return jsonify({"code": 200, "result": res})
\r
271 @app.route("/service/content")
\r
272 def getServiceContent():
\r
275 serviceName = request.values.get("serviceName")
\r
276 for fileName in os.listdir(SERVICE_DIR):
\r
277 if serviceName == os.path.splitext(fileName)[0]:
\r
278 res["actions"] = []
\r
279 filePath = os.path.join(SERVICE_DIR, fileName)
\r
280 with open(filePath, "r") as f:
\r
281 content = yaml.load(f)
\r
282 apisArr = content[serviceName]['apis']
\r
283 for i in range(len(apisArr)):
\r
284 apisArr[i].pop("method")
\r
285 apisArr[i].pop("baseuri")
\r
286 res["actions"] = apisArr
\r
287 except BaseException, e:
\r
288 return returnServerError(e)
\r
291 return jsonify({"code": 300, "error": "no such service!"})
\r
293 return jsonify({"code": 200, "result": res})
\r
296 def paramTransform(paramDict):
\r
298 for (key, value) in paramDict.items():
\r
300 paramJson["name"] = key
\r
301 paramJson["description"] = value["help"]
\r
302 if "params" in value:
\r
303 paramJson["params"] = paramTransform(value["params"])
\r
304 res.append(paramJson)
\r
308 @app.route("/service/action_response")
\r
309 def actionResponse():
\r
312 serviceName = request.values.get("serviceName")
\r
313 actionName = request.values.get("actionName")
\r
314 for fileName in os.listdir(SERVICE_DIR):
\r
315 if serviceName == os.path.splitext(fileName)[0]:
\r
316 res["responseParams"] = []
\r
317 filePath = os.path.join(SERVICE_DIR, fileName)
\r
318 with open(filePath, "r") as f:
\r
319 content = yaml.load(f)
\r
320 apisArr = content[serviceName]['apis']
\r
321 for i in range(len(apisArr)):
\r
322 if actionName == apisArr[i]['name'] and (
\r
323 "response" in apisArr[i]):
\r
324 res["responseParams"] = apisArr[i]["response"]
\r
325 except BaseException, e:
\r
326 return returnServerError(e)
\r
328 return jsonify({"code": 300, "error": "no such service!"})
\r
329 return jsonify({"code": 200, "result": res})
\r
333 # 3.2 API FOR ENVIRONMENT SERVICE AND CONTEXT
\r
334 ###########################################################################
\r
335 @app.route('/env/getAllServices')
\r
336 def getAllService():
\r
340 for fileName in os.listdir(SERVICE_DIR):
\r
343 item['name'] = os.path.splitext(fileName)[0]
\r
344 filePath = os.path.join(SERVICE_DIR, fileName)
\r
345 filemt = time.localtime(os.stat(filePath).st_mtime)
\r
346 item['time'] = time.strftime("%Y-%m-%d", filemt)
\r
349 except BaseException, e:
\r
350 return returnServerError(e)
\r
351 return jsonify({"code": 200, "result": res})
\r
354 @app.route('/env/getService')
\r
357 serviceName = request.values.get('serviceName')
\r
358 serviceFile = serviceName + '.yaml'
\r
359 servicePath = os.path.join(SERVICE_DIR, serviceFile)
\r
360 if os.path.exists(servicePath):
\r
361 with open(servicePath, "r") as f:
\r
362 serviceDict = yaml.load(f)
\r
363 serviceDict = serviceDict[serviceName]
\r
364 return jsonify({"code": 200, "result": serviceDict})
\r
366 return jsonify({"code": 300, "error": "no such service!"})
\r
367 except BaseException, e:
\r
368 return returnServerError(e)
\r
371 @app.route('/env/createService', methods=['POST'])
\r
372 def createService():
\r
374 name = str(request.values.get('name'))
\r
375 ip = str(request.values.get('ip'))
\r
376 port = int(request.values.get('port'))
\r
377 apis = json.loads(request.values.get('apis'))
\r
385 serviceJson = json.dumps(service, indent=True)
\r
387 app.logger.debug(service)
\r
389 serviceFile = name + '.yaml'
\r
390 servicePath = os.path.join(SERVICE_DIR, serviceFile)
\r
391 with open(servicePath, 'w') as f:
\r
392 pyaml.dump(service, f, safe=True)
\r
393 except BaseException, e:
\r
394 return returnServerError(e)
\r
395 return jsonify({"code": 200, "result": "create success!"})
\r
398 @app.route('/env/editService', methods=['POST'])
\r
401 oldName = str(request.values.get('oldName'))
\r
402 name = str(request.values.get('newName'))
\r
403 ip = str(request.values.get('ip'))
\r
404 port = int(request.values.get('port'))
\r
405 apis = json.loads(request.values.get('apis'))
\r
406 app.logger.debug(apis)
\r
414 serviceJson = json.dumps(service, indent=True)
\r
416 app.logger.debug(service)
\r
418 for fileName in os.listdir(SERVICE_DIR):
\r
419 serviceName = os.path.splitext(fileName)[0]
\r
420 if serviceName == oldName:
\r
421 filePath = os.path.join(SERVICE_DIR, fileName)
\r
422 os.remove(filePath)
\r
424 serviceFile = name + '.yaml'
\r
425 servicePath = os.path.join(SERVICE_DIR, serviceFile)
\r
426 with open(servicePath, 'w') as f:
\r
427 pyaml.dump(service, f, safe=True)
\r
428 except BaseException, e:
\r
429 return returnServerError(e)
\r
430 return jsonify({"code": 200, "result": "edit success!"})
\r
433 @app.route('/env/deleteService', methods=['POST'])
\r
434 def deleteService():
\r
436 name = str(request.values.get('serviceName'))
\r
438 for fileName in os.listdir(SERVICE_DIR):
\r
439 serviceName = os.path.splitext(fileName)[0]
\r
440 if serviceName == name:
\r
441 filePath = os.path.join(SERVICE_DIR, fileName)
\r
442 os.remove(filePath)
\r
443 except BaseException, e:
\r
444 return returnServerError(e)
\r
445 return jsonify({"code": 200, "result": "delete success!"})
\r
448 @app.route('/env/getContext')
\r
451 with open(CONTEXT_FILE_DIR, "r") as f:
\r
452 fileContent = f.read()
\r
454 except BaseException, e:
\r
455 return returnServerError(e)
\r
456 return jsonify({"code": 200, "result": {"context": res}})
\r
459 @app.route('/env/editContext', methods=['POST'])
\r
462 context = request.values.get("context")
\r
463 test = yaml.load(context)
\r
465 with open(CONTEXT_FILE_DIR, "w") as f:
\r
467 except yaml.constructor.ConstructorError, e:
\r
468 app.logger.error(traceback.format_exc())
\r
469 return jsonify({"code": 500, "error":
\r
470 "context content error: not a .yaml file!"})
\r
471 except BaseException, e:
\r
472 return returnServerError(e)
\r
474 return jsonify({"code": 200, "result": "edit context success!"})
\r
475 ###########################################################################
\r
478 def returnServerError(e, msg="Server Error"):
\r
480 app.logger.error(traceback.format_exc())
\r
481 return jsonify({"code": 500, "error": msg})
\r
484 if __name__ == "__main__":
\r
485 app.run(host='0.0.0.0', port=5310)
\r