fix:thrift/sql改为可选

This commit is contained in:
duanchao.bill
2022-05-07 11:07:17 +08:00
parent 998b76bdbf
commit 60f447da04
3 changed files with 70 additions and 24 deletions
+29 -9
View File
@@ -1,21 +1,41 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys
import time import time
from typing import Text from typing import Text
from loguru import logger from loguru import logger
from httprunner import utils from httprunner import utils
from httprunner.exceptions import SqlMethodNotSupport
from httprunner.exceptions import ValidationFailure from httprunner.exceptions import ValidationFailure
from httprunner.models import IStep, StepResult, TStep from httprunner.models import IStep, StepResult, TStep
from httprunner.models import TSqlRequest, SqlMethodEnum from httprunner.models import SqlMethodEnum, TSqlRequest
from httprunner.response import SqlResponseObject from httprunner.response import SqlResponseObject
from httprunner.runner import HttpRunner from httprunner.runner import HttpRunner
from httprunner.step_request import ( from httprunner.step_request import (StepRequestExtraction, StepRequestValidation, call_hooks)
call_hooks,
StepRequestExtraction, try:
StepRequestValidation, import sqlalchemy
) import pymysql
from httprunner.exceptions import SqlMethodNotSupport
SQL_READY = True
except ModuleNotFoundError:
SQL_READY = False
def ensure_sql_ready():
if SQL_READY:
return
msg = """
uploader extension dependencies uninstalled, install first and try again.
install with pip:
$ pip install sqlalchemy pymysql
or you can install httprunner with optional upload dependencies:
$ pip install "httprunner[sql]"
"""
logger.error(msg)
sys.exit(1)
def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult: def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult:
@@ -51,8 +71,8 @@ def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult:
) )
if not runner.db_engine: if not runner.db_engine:
ensure_sql_ready()
from httprunner.database.engine import DBEngine from httprunner.database.engine import DBEngine
runner.db_engine = DBEngine( runner.db_engine = DBEngine(
f'mysql+pymysql://{parsed_request_dict["db_config"]["user"]}:' f'mysql+pymysql://{parsed_request_dict["db_config"]["user"]}:'
f'{parsed_request_dict["db_config"]["password"]}@{parsed_request_dict["db_config"]["ip"]}:' f'{parsed_request_dict["db_config"]["password"]}@{parsed_request_dict["db_config"]["ip"]}:'
+40 -13
View File
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import platform
import sys
import time import time
from typing import Text, Union from typing import Text, Union
@@ -22,6 +24,31 @@ from httprunner.step_request import (
call_hooks, call_hooks,
) )
try:
import thriftpy2
from thrift.Thrift import TType
THRIFT_READY = True
except ModuleNotFoundError:
THRIFT_READY = False
def ensure_thrift_ready():
assert platform.system() != "Windows", "Sorry,thrift not support Windows for now"
if THRIFT_READY:
return
msg = """
uploader extension dependencies uninstalled, install first and try again.
install with pip:
$ pip install cython thriftpy2 thrift
or you can install httprunner with optional upload dependencies:
$ pip install "httprunner[thrift]"
"""
logger.error(msg)
sys.exit(1)
def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult: def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult:
"""run teststep:thrift request""" """run teststep:thrift request"""
@@ -39,30 +66,30 @@ def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult:
parsed_request_dict["psm"] = parsed_request_dict["psm"] or config.thrift.psm parsed_request_dict["psm"] = parsed_request_dict["psm"] or config.thrift.psm
parsed_request_dict["env"] = parsed_request_dict["env"] or config.thrift.env parsed_request_dict["env"] = parsed_request_dict["env"] or config.thrift.env
parsed_request_dict["cluster"] = ( parsed_request_dict["cluster"] = (
parsed_request_dict["cluster"] or config.thrift.cluster parsed_request_dict["cluster"] or config.thrift.cluster
) )
parsed_request_dict["idl_path"] = ( parsed_request_dict["idl_path"] = (
parsed_request_dict["idl_path"] or config.thrift.idl_path parsed_request_dict["idl_path"] or config.thrift.idl_path
) )
parsed_request_dict["include_dirs"] = ( parsed_request_dict["include_dirs"] = (
parsed_request_dict["include_dirs"] or config.thrift.include_dirs parsed_request_dict["include_dirs"] or config.thrift.include_dirs
) )
parsed_request_dict["method"] = ( parsed_request_dict["method"] = (
parsed_request_dict["method"] or config.thrift.method parsed_request_dict["method"] or config.thrift.method
) )
parsed_request_dict["service_name"] = ( parsed_request_dict["service_name"] = (
parsed_request_dict["service_name"] or config.thrift.service_name parsed_request_dict["service_name"] or config.thrift.service_name
) )
parsed_request_dict["ip"] = parsed_request_dict["ip"] or config.thrift.ip parsed_request_dict["ip"] = parsed_request_dict["ip"] or config.thrift.ip
parsed_request_dict["port"] = parsed_request_dict["port"] or config.thrift.port parsed_request_dict["port"] = parsed_request_dict["port"] or config.thrift.port
parsed_request_dict["proto_type"] = ( parsed_request_dict["proto_type"] = (
parsed_request_dict["proto_type"] or config.thrift.proto_type parsed_request_dict["proto_type"] or config.thrift.proto_type
) )
parsed_request_dict["trans_port"] = ( parsed_request_dict["trans_port"] = (
parsed_request_dict["trans_type"] or config.thrift.trans_type parsed_request_dict["trans_type"] or config.thrift.trans_type
) )
parsed_request_dict["timeout"] = ( parsed_request_dict["timeout"] = (
parsed_request_dict["timeout"] or config.thrift.timeout parsed_request_dict["timeout"] or config.thrift.timeout
) )
parsed_request_dict["thrift_client"] = parsed_request_dict["thrift_client"] parsed_request_dict["thrift_client"] = parsed_request_dict["thrift_client"]
@@ -76,8 +103,8 @@ def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult:
if not runner.thrift_client: if not runner.thrift_client:
runner.thrift_client = parsed_request_dict["thrift_client"] runner.thrift_client = parsed_request_dict["thrift_client"]
if not runner.thrift_client: if not runner.thrift_client:
ensure_thrift_ready()
from httprunner.thrift.thrift_client import ThriftClient from httprunner.thrift.thrift_client import ThriftClient
runner.thrift_client = ThriftClient( runner.thrift_client = ThriftClient(
thrift_file=parsed_request_dict["idl_path"], thrift_file=parsed_request_dict["idl_path"],
service_name=parsed_request_dict["service_name"], service_name=parsed_request_dict["service_name"],
@@ -187,7 +214,7 @@ class RunThriftRequest(IStep):
return self return self
def teardown_hook( def teardown_hook(
self, hook: Text, assign_var_name: Text = None self, hook: Text, assign_var_name: Text = None
) -> "RunThriftRequest": ) -> "RunThriftRequest":
if assign_var_name: if assign_var_name:
self.__step.teardown_hooks.append({assign_var_name: hook}) self.__step.teardown_hooks.append({assign_var_name: hook})
@@ -197,7 +224,7 @@ class RunThriftRequest(IStep):
return self return self
def setup_hook( def setup_hook(
self, hook: Text, assign_var_name: Text = None self, hook: Text, assign_var_name: Text = None
) -> "RunThriftRequest": ) -> "RunThriftRequest":
if assign_var_name: if assign_var_name:
self.__step.setup_hooks.append({assign_var_name: hook}) self.__step.setup_hooks.append({assign_var_name: hook})
@@ -220,7 +247,7 @@ class RunThriftRequest(IStep):
return self return self
def with_thrift_client( def with_thrift_client(
self, thrift_client: Union["ThriftClient", str] self, thrift_client: Union["ThriftClient", str]
) -> "RunThriftRequest": ) -> "RunThriftRequest":
self.__step.thrift_request.thrift_client = thrift_client self.__step.thrift_request.thrift_client = thrift_client
return self return self
@@ -260,7 +287,7 @@ class RunThriftRequest(IStep):
return StepThriftRequestValidation(self.__step) return StepThriftRequestValidation(self.__step)
def with_jmespath( def with_jmespath(
self, jmes_path: Text, var_name: Text self, jmes_path: Text, var_name: Text
) -> "StepThriftRequestExtraction": ) -> "StepThriftRequestExtraction":
self.__step.extract[var_name] = jmes_path self.__step.extract[var_name] = jmes_path
return StepThriftRequestExtraction(self.__step) return StepThriftRequestExtraction(self.__step)
+1 -2
View File
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import absolute_import from __future__ import absolute_import
import platform
import enum import enum
import json import json
@@ -84,7 +84,6 @@ class ThriftClient(object):
self.timeout = timeout self.timeout = timeout
self.proto_type = proto_type self.proto_type = proto_type
self.trans_type = trans_type self.trans_type = trans_type
assert platform.system() != "Windows", "thrift not support Windows for now"
try: try:
logger.debug( logger.debug(
"init thrift module: thrift_file=%s, module_name=%s", "init thrift module: thrift_file=%s, module_name=%s",