def uploadfile(): #上传文件 if 'file' not in request.files or 'xmdz' not in request.form: abort(400) xmdz = request.form.get('xmdz') file = request.files.get('file') sql_xm_bh = "SELECT c_bh, c_code_team AS code, c_test_team AS test FROM db_apitesting.t_at_xmxx WHERE c_url = '%s';" % ( xmdz) try: xm_info = all_dbc.pg_select_operator(sql_xm_bh) if len(xm_info) == 0: return jsonify({'result': 'fail', 'msg': '项目不存在'}) else: xmbh = xm_info[0]['c_bh'] except Exception as eee: logger.error('根据项目地址查询项目编号失败' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) all_name = os.path.splitext(file.filename) file_name = all_name[-2] Suffix = all_name[-1] if Suffix in app.config['ALLOWED_EXTENSIONS']: file_uuid = base_tool.next_id() newpath = os.path.join(app.config['UPLOAD_FOLDER'], xmdz) if not os.path.isdir(newpath): os.makedirs(newpath) newpath_all = os.path.join(newpath, str(file_uuid + '.xlsx')) try: file.save(newpath_all) except Exception as eee: logger.error('文件保存失败: ' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 开始解析参数,如果解析失败,不进行用例保存 try: cs_res = panda_for_web.read_keylist(newpath_all) except Exception as eee: return jsonify({'result': 'fail', 'msg': '解析参数失败,请检查参数sheet页'}) ylbh = base_tool.next_id() sql_addyl = "INSERT INTO db_apitesting.t_at_ylxx(c_bh, c_bh_xm, c_ylmc, dt_scsj, dt_zxzxsj, n_zxcs, c_bclj, c_edit_key, c_sfbj, dt_gxsj, c_code_team, c_test_team) "\ "VALUES ('%s', '%s', '%s', now(), NULL, 0, '%s', '%s', 2, now(), '%s', '%s');" % (ylbh, xmbh, file_name, newpath_all, ylbh, xm_info[0]['code'], xm_info[0]['test']) try: all_dbc.pg_insert_operator(sql_addyl) except Exception as eee: logger.error('插入用例信息错误:' + str(eee)) return jsonify({'result': 'fail'}) intnum = 1 for num in cs_res: sql_addcs = "INSERT INTO db_apitesting.t_at_zxcs(c_bh, c_bh_yl, c_key, c_value, n_xh) VALUES "\ "('%s', '%s', '%s', '%s', '%s')" % (base_tool.next_id(), ylbh, num, cs_res[num], intnum) try: all_dbc.pg_insert_operator(sql_addcs) except Exception as eee: logger.error('插入参数报错:' + str(eee)) return jsonify({'result': 'fail', 'msg': '参数插入失败'}) intnum = intnum + 1 return jsonify({'result': 'success'}) else: return jsonify({'result': 'fail', 'msg': '文件类型错误,仅支持.xlsx格式'})
def add_ci(): if not request.json or 'xmurl' not in request.json or 'ylinfo' not in request.json: abort(400) xmurl = request.json['xmurl'] ylinfo = request.json['ylinfo'] if len(ylinfo) == 0: return jsonify({'result': 'fail'}) xm_id_sql = "SELECT c_bh AS xm_id FROM db_apitesting.t_at_xmxx WHERE c_url = '%s';" % xmurl try: xm_id_res = all_dbc.pg_select_operator(xm_id_sql) except Exception as eee: logger.error('CI项目编号查询失败:' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail'}) ci_id = base_tool.next_id() api_url = '/apitest/ci_control/' + ci_id ci_sql = "INSERT INTO db_apitesting.t_at_ci(c_bh, c_api, c_yl_list, dt_zxdysj, c_zxcs, c_bh_xm, c_ylsl, dt_cjsj) VALUES"\ " ('%s', '%s', '%s', NULL, '0', '%s', '%s', now());" % (ci_id, api_url, ",".join(ylinfo), xm_id_res[0]['xm_id'], len(ylinfo)) try: all_dbc.pg_insert_operator(ci_sql) except Exception as eee: logger.error('CI项目创建失败:' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail'}) return jsonify({'result': 'success'})
def smd2sql(): #上传文件 if 'file' not in request.files: abort(400) file = request.files.get('file') all_name = os.path.splitext(file.filename) file_name = all_name[-2] Suffix = all_name[-1] if Suffix in app.config['ALLOWED_EXTENSIONS']: file_uuid = base_tool.next_id() newpath = os.path.join(app.config['UPLOAD_FOLDER'], 'smd2sql', file_uuid) if not os.path.isdir(newpath): os.makedirs(newpath) newpath_all = os.path.join(newpath, str(file_name + '.xlsx')) try: file.save(newpath_all) ''' 文件已经保存成功了,开始调用工具方法 ''' m_queues = smdtosql.read_folder(newpath) if m_queues: while not m_queues.empty(): smd = m_queues.get() for table in smd: if table not in ('file_name', 'schema_name'): smdtosql.write_file(table, smd[table], newpath + '/' + file_name, smd['schema_name']) ''' 开始压缩文件 ''' base_tool.ZipFile(newpath + '/' + file_name, newpath + '/' + 'smd2sql.zip') ''' 返回压缩包 ''' p, f = os.path.split(newpath + '/' + 'smd2sql.zip') logger.info('···smd2sql运行完毕···✿✿ヽ(°▽°)ノ✿') response = make_response( send_from_directory(p, f, as_attachment=True)) response.headers["content-type"] = "application/x-zip-compressed" return response except Exception as eee: logger.error('文件保存失败: ' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 开始解析参数,如果解析失败,不进行用例保存 return jsonify({'result': 'fail', 'msg': '文件格式不正确'})
def add_tc(): if not request.json or 'tc_text' not in request.json: abort(400) username = login_tool.login_tools().get_username().decode('utf-8') if username == 'guest': return jsonify({'result': 'guest', 'msg': '游客就别吐槽了呗!_(ÒωÓ๑ゝ∠)_'}) tc_text = request.json['tc_text'] tc_id = base_tool.next_id() name = cache_tool.res_cache(username)[1] add_tc_sql = "INSERT INTO db_apitesting.c_at_tc (c_id, c_name, c_text, dt_tjsj, c_fkxx, c_fkzt, dt_fksj) VALUES ('%s', '%s', '%s', now(), NULL, '0', NULL);" % ( tc_id, name, tc_text) try: all_dbc.pg_insert_operator(add_tc_sql) except Exception as eee: logger.error('提交吐槽信息失败:' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail', 'msg': '吐槽失败!(,,#゚Д゚) 请联系管理员'}) return jsonify({'result': 'success', 'msg': '提交成功'})
def run_case(): if not request.json or 'ylbh' not in request.json or 'list' not in request.json: abort(400) # 根据ylbh查询出用例对应的用例文件 ylbh = request.json['ylbh'] cs_res = request.json['list'] sfjs = request.json['sfjs'] api_docs = request.json['api_docs'] if sfjs: if str(api_docs) != '': if str(api_docs[:4]).upper() == 'HTTP': pass else: try: api_docs = int(api_docs) except: return jsonify({'result': 'fail', 'msg': '接口地址错误或数量书写不规范'}) else: return jsonify({'result': 'fail', 'msg': '接口地址错误或数量书写不规范'}) # 删除之前的参数 cs_del_sql = "DELETE FROM db_apitesting.t_at_zxcs WHERE c_bh_yl = '%s'" % ylbh try: all_dbc.pg_delete_operator(cs_del_sql) except Exception as eee: logger.error('删除参数信息报错:' + str(eee)) return jsonify({'result': 'fail', 'msg': '删除参数失败'}) intnum = 1 new_kv = {} for num in range(len(cs_res)): sql_addcs = "INSERT INTO db_apitesting.t_at_zxcs(c_bh, c_bh_yl, c_key, c_value, n_xh) VALUES "\ "('%s', '%s', '%s', '%s', '%s')" % (base_tool.next_id(), ylbh, cs_res[num]['zxcs_key'], cs_res[num]['zxcs_value'], intnum) try: all_dbc.pg_insert_operator(sql_addcs) new_kv[cs_res[num]['zxcs_key']] = cs_res[num]['zxcs_value'] except Exception as eee: logger.error('插入参数报错:' + str(eee)) return jsonify({'result': 'fail', 'msg': '参数插入失败'}) intnum = intnum + 1 lj_sql = "SELECT c_bclj as yllj FROM db_apitesting.t_at_ylxx WHERE c_bh = '%s';" % ylbh try: yl_path = all_dbc.pg_select_operator(lj_sql) if len(yl_path) == 0: return jsonify({'result': 'fail', 'msg': '用例文件不存在,请重新上传'}) except Exception as eee: logger.error('查询用例路径失败:' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 读取文件,返回格式[参数dict, [总条数, 用例信息], 数据库数组] try: case_info_all = panda_for_web.read_case(yl_path[0]['yllj']) # logger.info(case_info_all) except Exception as eee: logger.error('用例读取失败:' + str(eee)) return jsonify({'result': 'fail', 'msg': '用例文件不存在或用例内容不正确,请重新上传'}) if case_info_all == False: return jsonify({'result': 'fail', 'msg': '用例文件不存在或用例内容不正确,请重新上传'}) # 将参数放入用例信息的首位 case_info_all.insert(0, new_kv) # logger.info(case_info_all) # 初始化用例中使用的数据库连接 try: db_case = db_clints_for_web.db_clints(case_info_all[2]) except Exception as eee: return jsonify({'result': 'fail', 'msg': str(eee)}) if db_case == False: return jsonify({'result': 'fail', 'msg': '数据库连接创建失败,请检查用例中的数据库配置'}) logger.info("开始关闭数据链接") db_clints_for_web.db_tools().db_close() logger.info("完成关闭数据链接") # 更新执行次数 zx_num = "UPDATE db_apitesting.t_at_ylxx SET n_zxcs = n_zxcs + 1, c_api_count = '%s' WHERE c_bh = '%s';" % ( api_docs, ylbh) try: all_dbc.pg_update_operator(zx_num) except Exception as eee: logger.error('更新执行次数失败' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) ''' # 创建子进程 ~~~~~此处由于进程创建时完整复制当前进程的内容,包括数据库连接,因此会导致在主进程(不知原因,可能是因为程序报错)关闭时,导致子进程的数据库连接发生异常~~~~~ ~~~~~由此修改为调用线程池中的预留线程,通过vthread提供的线程池装饰器来实现,在主进程中直接调用对应的被装饰函数即可~~~~~ p = Process(target=run_caselist_new.run_caselist, args=(zxid, ylbh, case_info_all, )) logger.info('创建子进程,主进程id:' + str(os.getpid())) try: p.start() except Exception as eaa: logger.error('子进程启动失败:' + str(eaa)) return jsonify({'result': 'fail', 'msg': str(eaa)}) logger.info('子进程状态' + str(p.is_alive())) ''' # 插入用例执行记录 zxid = base_tool.next_id() zxjl_sql = "INSERT INTO db_apitesting.t_at_zxxx(c_bh, c_bh_yl, dt_zxsj, c_fgl, c_cgl, c_tgl, n_zt, c_cg, c_wcg, c_tg, c_wtg, c_sfci) VALUES"\ " ('%s', '%s', now(), 0, 0, 0, 0, 0, 0, 0, 0, 1);" % (zxid, ylbh) try: all_dbc.pg_insert_operator(zxjl_sql) except Exception as eee: logger.error('插入执行信息失败' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 开始执行用例 logger.info('···开始调用线程池···') run_caselist_new.run_caselist(zxid, ylbh, case_info_all, api_docs, sfjs) logger.info('···调用线程池完毕···') return jsonify({'result': 'success', 'msg': '成功', 'zxid': zxid})
def office_update(): # logger.info(request.json) if request.json['status'] == 2: logger.info('编辑结束,开始保存用例···') file_id = request.json['key'] file = requests.get(request.json['url'], stream=False) sql_yl_bh = "SELECT c_bh as ylbh, c_bclj as yllj FROM db_apitesting.t_at_ylxx WHERE c_edit_key = '%s'" % ( file_id) try: yl_old = all_dbc.pg_select_operator(sql_yl_bh) yllj_old = yl_old[0]['yllj'] yl_bh = yl_old[0]['ylbh'] except Exception as eee: logger.error('更新用例时,查询失败:' + str(eee)) return jsonify({'result': 'fail'}) # 开始保存新的用例文件 # 获取原来的保存路径 p_old, f = os.path.split(yllj_old) # 删除之前的用例文件 if (os.path.exists(yllj_old)): try: os.remove(yllj_old) except Exception as eee: logger.error('删除原用例失败:' + str(eee)) file_uuid = base_tool.next_id() newpath = p_old if not os.path.isdir(newpath): os.makedirs(newpath) newpath_all = os.path.join(newpath, str(file_uuid + '.xlsx')) try: with open(newpath_all, "wb") as f: f.write(file.content) except Exception as eee: logger.error('文件保存失败: ' + str(eee)) # 开始解析参数,如果解析失败,不进行用例保存 cs_res = panda_for_web.read_keylist(newpath_all) # 删除之前的参数 cs_del_sql = "DELETE FROM db_apitesting.t_at_zxcs WHERE c_bh_yl = '%s'" % yl_bh try: all_dbc.pg_delete_operator(cs_del_sql) except Exception as eee: logger.error('删除参数出错:' + str(eee)) intnum = 1 ''' 待优化,将sql放到外层组装,最终一起执行 ''' for num in cs_res: sql_addcs = "INSERT INTO db_apitesting.t_at_zxcs(c_bh, c_bh_yl, c_key, c_value, n_xh) VALUES "\ "('%s', '%s', '%s', '%s', '%s')" % (base_tool.next_id(), yl_bh, num, cs_res[num], intnum) # logger.info(sql_addcs) try: all_dbc.pg_insert_operator(sql_addcs) except Exception as eee: logger.error('插入参数报错:' + str(eee)) intnum = intnum + 1 try: sql_addyl = "UPDATE db_apitesting.t_at_ylxx SET dt_gxsj = now(), c_bclj = '%s', c_sfbj = 2, c_edit_key = '%s' WHERE c_bh = '%s'" % ( newpath_all, str(base_tool.next_id())[:20], yl_old[0]['ylbh']) # logger.info(str(sql_addyl)) all_dbc.pg_update_operator(sql_addyl) except Exception as eee: logger.error('插入用例信息错误:' + str(eee)) logger.info('保存结束') return jsonify({"error": 0}) elif request.json['status'] == 4: file_id = request.json['key'] try: sql_upyl = "UPDATE db_apitesting.t_at_ylxx SET c_sfbj = 2 WHERE c_edit_key = '%s'" % ( file_id) all_dbc.pg_update_operator(sql_upyl) return jsonify({"error": 0}) except Exception as eee: logger.error('插入用例信息错误:' + str(eee)) return jsonify({'result': 'fail'}) else: return jsonify({"error": 0})
def ci_run(ci_id): if not request.json: logger.error('CI调用失败,请求参数' + str(request)) abort(400) cs_info = request.json yl_list_sql = "SELECT c_yl_list AS list FROM db_apitesting.t_at_ci WHERE c_bh = '%s';" % ci_id try: yl_list_res = all_dbc.pg_select_operator(yl_list_sql) except Exception as eee: logger.error('CI执行用例查询失败:' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail'}) if len(yl_list_res) == 0 or len(yl_list_res[0]['list']) == 0: return jsonify({'result': 'fail', 'msg': 'CI设置不存在或绑定用例为空'}) else: yl_list = re.split(',', yl_list_res[0]['list']) for yl_id in yl_list: ylinfo_sql = "SELECT c_bclj as yllj, c_api_count AS api_docs FROM db_apitesting.t_at_ylxx WHERE c_bh = '%s';" % yl_id yl_cs_sql = "SELECT c_key AS key, c_value AS value FROM db_apitesting.t_at_zxcs WHERE c_bh_yl = '%s';" % yl_id try: ylinfo_res = all_dbc.pg_select_operator(ylinfo_sql) yl_cs_res = all_dbc.pg_select_operator(yl_cs_sql) except Exception as eee: logger.error('CI执行查询用例路径失败:' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail'}) new_kv = {} logger.info(str(yl_cs_res)) for cs in yl_cs_res: new_kv[cs['key']] = cs['value'] if len(ylinfo_res) != 1: return jsonify({'result': 'fail', 'msg': '用例可能已经不存在了'}) else: yl_path = ylinfo_res[0]['yllj'] api_docs = ylinfo_res[0]['api_docs'] if str(api_docs) == 'null' or api_docs == '': api_docs = '' try: sfjs = cs_info['sfjs'] if str(sfjs) == '0': sfjs = True else: sfjs = False except: sfjs = False try: case_info_all = panda_for_web.read_case(yl_path) except Exception as eee: logger.error('用例读取失败:' + str(eee)) return jsonify({ 'result': 'fail', 'msg': '用例文件不存在或用例内容不正确,请重新上传' }) if case_info_all == False: return jsonify({ 'result': 'fail', 'msg': '用例文件不存在或用例内容不正确,请重新上传' }) case_info_all.insert(0, new_kv) try: db_case = db_clints_for_web.db_clints(case_info_all[2]) except Exception as eee: return jsonify({'result': 'fail', 'msg': str(eee)}) if db_case == False: return jsonify({ 'result': 'fail', 'msg': '数据库连接创建失败,请检查用例中的数据库配置' }) logger.info("开始关闭数据链接") db_clints_for_web.db_tools().db_close() logger.info("完成关闭数据链接") zx_num = "UPDATE db_apitesting.t_at_ylxx SET n_zxcs = n_zxcs + 1 WHERE c_bh = '%s';" % yl_id try: all_dbc.pg_update_operator(zx_num) except Exception as eee: logger.error('更新执行次数失败' + str(eee)) return jsonify({'result': 'fail'}) zxid = base_tool.next_id() zxjl_sql = "INSERT INTO db_apitesting.t_at_zxxx(c_bh, c_bh_yl, dt_zxsj, c_fgl, c_cgl, c_tgl, n_zt, c_cg, c_wcg, c_tg, c_wtg, c_sfci) VALUES"\ " ('%s', '%s', now(), '0%', '0%', '0%', 0, 0, 0, 0, 0, 0);" % (zxid, yl_id) try: all_dbc.pg_insert_operator(zxjl_sql) except Exception as eee: logger.error('新增执行信息失败' + str(eee)) return jsonify({'result': 'fail'}) logger.info('···开始调用线程池···') logger.info(str(sfjs) + str(zxid) + str(api_docs)) run_caselist_new.run_caselist(zxid, yl_id, case_info_all, api_docs, sfjs, cs_info) logger.info('···调用线程池完毕···') ci_up_sql = "UPDATE db_apitesting.t_at_ci SET c_zxcs = c_zxcs + 1, dt_zxdysj = now() WHERE c_bh = '%s';" % ci_id try: all_dbc.pg_update_operator(ci_up_sql) except Exception as eee: logger.error('更新CI信息失败' + str(eee)) logger.exception(eee) return jsonify({'result': 'fail'}) return jsonify({'result': 'success', 'msg': 'CI执行成功'})
def updatefile(): #上传文件 if 'file' not in request.files or 'ylbh' not in request.form: abort(400) ylbh = request.form.get('ylbh') file = request.files.get('file') sql_yl_bh = "SELECT c_bclj as yllj FROM db_apitesting.t_at_ylxx WHERE c_bh = '%s'" % ( ylbh) try: yl_old = all_dbc.pg_select_operator(sql_yl_bh) if len(yl_old) == 0: return jsonify({'result': 'fail', 'msg': '用例信息已不存在,请刷新页面'}) else: yllj_old = yl_old[0]['yllj'] except Exception as eee: logger.error('更新用例时,查询失败:' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 开始保存新的用例文件 # 获取原来的保存路径 p_old, f = os.path.split(yllj_old) all_name = os.path.splitext(file.filename) # file_name = all_name[-2] Suffix = all_name[-1] if Suffix in app.config['ALLOWED_EXTENSIONS']: # 删除之前的用例文件 if (os.path.exists(yllj_old)): try: os.remove(yllj_old) except Exception as eee: logger.error('删除原用例失败:' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) file_uuid = base_tool.next_id() newpath = p_old if not os.path.isdir(newpath): os.makedirs(newpath) newpath_all = os.path.join(newpath, str(file_uuid + '.xlsx')) try: file.save(newpath_all) except Exception as eee: logger.error('文件保存失败: ' + str(eee)) return jsonify({'result': 'fail', 'msg': str(eee)}) # 开始解析参数,如果解析失败,不进行用例保存 try: cs_res = panda_for_web.read_keylist(newpath_all) except Exception as eee: return jsonify({'result': 'fail', 'msg': '解析参数失败,请检查参数sheet页'}) # 删除之前的参数 cs_del_sql = "DELETE FROM db_apitesting.t_at_zxcs WHERE c_bh_yl = '%s'" % ylbh try: all_dbc.pg_delete_operator(cs_del_sql) except Exception as eee: logger.error('删除参数出错:' + str(eee)) return jsonify({'result': 'fail', 'msg': '删除参数报错'}) intnum = 1 for num in cs_res: sql_addcs = "INSERT INTO db_apitesting.t_at_zxcs(c_bh, c_bh_yl, c_key, c_value, n_xh) VALUES "\ "('%s', '%s', '%s', '%s', '%s')" % (base_tool.next_id(), ylbh, num, cs_res[num], intnum) try: all_dbc.pg_insert_operator(sql_addcs) except Exception as eee: logger.error('插入参数报错:' + str(eee)) return jsonify({'result': 'fail', 'msg': '参数插入失败'}) intnum = intnum + 1 try: new_edit_key = base_tool.next_id() sql_addyl = "UPDATE db_apitesting.t_at_ylxx SET c_bclj = '%s', c_edit_key = '%s', dt_gxsj = now() WHERE c_bh = '%s'" % ( newpath_all, new_edit_key, ylbh, ) all_dbc.pg_insert_operator(sql_addyl) except Exception as eee: logger.error('插入用例信息错误:' + str(eee)) return jsonify({'result': 'fail'}) return jsonify({'result': 'success'}) else: return jsonify({'result': 'fail', 'msg': '文件类型错误,仅支持.xlsx格式'})