From a3aa198016c248ddec23819d0362b8ca0a7c5b99 Mon Sep 17 00:00:00 2001 From: tommyzhou Date: Wed, 21 Sep 2022 18:25:52 +0800 Subject: [PATCH 1/2] feat: upload testcases to SQLAuto server --- tests/common/nebula_test_suite.py | 11 +++++- tests/common/utils.py | 64 +++++++++++++++++++++++++++++++ tests/tck/conftest.py | 8 +++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/tests/common/nebula_test_suite.py b/tests/common/nebula_test_suite.py index ce7c217ca25..111627745df 100644 --- a/tests/common/nebula_test_suite.py +++ b/tests/common/nebula_test_suite.py @@ -15,6 +15,8 @@ # from nebula3.gclient.net import ConnectionPool # from nebula3.Config import Config from nebula3.graph import ttypes + +from tests.common import utils from tests.common.configs import get_delay_time from tests.common.utils import ( compare_value, @@ -152,11 +154,16 @@ def release_nebula_client(self, client): # self.client.release() # self.close_nebula_clients() + @classmethod - def execute(self, ngql, profile=True): - return self.client.execute( + def execute(self, ngql, profile=True, space_name=None): + resp = self.client.execute( 'PROFILE {{{}}}'.format(ngql) if profile else ngql) + utils.upload_2_sqlauto(ngql, resp, space_name) + return resp + + @classmethod def check_rows_with_header(cls, stmt: str, expected: dict): resp = cls.execute(stmt) diff --git a/tests/common/utils.py b/tests/common/utils.py index 631eacbfad7..9c16aa45721 100644 --- a/tests/common/utils.py +++ b/tests/common/utils.py @@ -10,6 +10,8 @@ import random import string import time + +import requests import yaml from typing import Pattern @@ -472,3 +474,65 @@ def get_ssl_config(is_graph_ssl: bool, ca_signed: bool): ssl_config.certfile = os.path.join(NEBULA_HOME, 'tests/cert/test.derive.crt') ssl_config.keyfile = os.path.join(NEBULA_HOME, 'tests/cert/test.derive.key') return ssl_config + + +sqlauto_cookies = None + + +def upload_2_sqlauto(ngql, resp, space_name=None): + try: # Upload testcases and their responses to SQLAuto Server + key_code = "code" + host = "http://apijson.cn:8080" + code_success = 200 + + global sqlauto_cookies + + if sqlauto_cookies is None: + login_resp = requests.post(host + "/login", json={ + "phone": "13000082005", + "password": "123456", + "remember": True + }) + sqlauto_cookies = login_resp.cookies + + exist_resp = requests.post(host + "/get/Document", json={ + "url": "/" + ("" if space_name is None else space_name), + "sqlauto": ngql, + }, cookies=sqlauto_cookies) + + exist_resp = {key_code: exist_resp.status_code} if exist_resp.status_code != code_success else exist_resp.json() + + is_doc = exist_resp.get(key_code) != code_success or exist_resp.get("Document") is None + upload_resp = requests.post(host + "/post", json={ + "format": False, + "Document": { + "testAccountId": None, + "name": ngql if len(ngql) <= 20 else (ngql[:16] + "..."), + "type": "JSON", + "url": "/" + space_name, + "request": "{}", + "sqlauto": ngql, + "standard": None, + "header": "" + } if is_doc else None, + "TestRecord": { + "randomId": 0, + "host": "jdbc:nebula://localhost:9669", + "testAccountId": None, + "response": ('{"code":200,msg:"success","list":' + json.dumps(resp.rows) + '}') + if resp.is_succeeded else ('{"code":' + resp.error_code + ',msg:' + resp.error_msg + '}'), + "standard": None + }, + "tag": "Document" if is_doc else "TestRecord" + }, cookies=sqlauto_cookies) + + result = {key_code: upload_resp.status_code} if upload_resp.status_code != code_success else upload_resp.json() + if result.get(key_code) != code_success: + print("\n\nupload case to SQLAuto server failed: statusCode: " + upload_resp.status_code + + ", responseText: " + upload_resp.text + "\n\n") + except Exception as e: + print("\n\nupload case to SQLAuto server failed: {}".format(e)) + + +if __name__ == "__main__": + upload_2_sqlauto("show hosts;", requests.Response(), "testSpace") diff --git a/tests/tck/conftest.py b/tests/tck/conftest.py index 140064f7cc0..f8c859c1d69 100644 --- a/tests/tck/conftest.py +++ b/tests/tck/conftest.py @@ -17,6 +17,7 @@ from nebula3.Exception import AuthFailedException from pytest_bdd import given, parsers, then, when +from tests.common import utils from tests.common.dataset_printer import DataSetPrinter from tests.common.comparator import DataSetComparator, CmpType from tests.common.plan_differ import PlanDiffer @@ -271,9 +272,14 @@ def exec_query(request, ngql, exec_ctx, sess=None, need_try: bool = False): ngql = normalize_outline_scenario(request, ngql) if sess is None: sess = exec_ctx.get('current_session') - exec_ctx['result_set'] = response(sess, ngql, need_try) + + resp = response(sess, ngql, need_try) + exec_ctx['result_set'] = resp exec_ctx['ngql'] = ngql + space_desc = exec_ctx.get("space_desc", None) + utils.upload_2_sqlauto(ngql, resp, None if space_desc is None else space_desc.name) + @given( parse( From f294aabb5a0773d8e404174bceda673fae8c895c Mon Sep 17 00:00:00 2001 From: tommyzhou Date: Wed, 21 Sep 2022 18:37:33 +0800 Subject: [PATCH 2/2] fix: optimize for uploading testcases to SQLAuto server --- tests/common/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/common/utils.py b/tests/common/utils.py index 9c16aa45721..e9c8a1e21bc 100644 --- a/tests/common/utils.py +++ b/tests/common/utils.py @@ -509,7 +509,7 @@ def upload_2_sqlauto(ngql, resp, space_name=None): "testAccountId": None, "name": ngql if len(ngql) <= 20 else (ngql[:16] + "..."), "type": "JSON", - "url": "/" + space_name, + "url": "/" + ("" if space_name is None else space_name), "request": "{}", "sqlauto": ngql, "standard": None, @@ -519,8 +519,8 @@ def upload_2_sqlauto(ngql, resp, space_name=None): "randomId": 0, "host": "jdbc:nebula://localhost:9669", "testAccountId": None, - "response": ('{"code":200,msg:"success","list":' + json.dumps(resp.rows) + '}') - if resp.is_succeeded else ('{"code":' + resp.error_code + ',msg:' + resp.error_msg + '}'), + "response": ('{"code":200,"msg":"success","list":' + json.dumps(resp.rows) + '}') + if resp.is_succeeded else ('{"code":' + resp.error_code + ',"msg":' + resp.error_msg + '}'), "standard": None }, "tag": "Document" if is_doc else "TestRecord"