Example #1
0
def deal_LoopController(root):
    step = importOfflineCase_step()
    step.set_stepname(root.get("testname"))
    step.set_stepdes(root.get("testname"))
    steps = [step.get_step()]

    sub_elements = root.get_sub_elements()
    for sub_element in sub_elements:
        if sub_element.tag == "BeanShellPreProcessor":
            script = sub_element.element.find(
                ".//stringProp[@name='script']").text
            csv_file = re.findall(
                r'new FileInputStream\(vars.get\("(.*?)"\)\);', script)[0]
            csv_file_path = arguments_local.get(csv_file)
            csv_file_name = os.path.split(csv_file_path)[-1]
            logger.debug(csv_file_name)
            csv_file = open(CSV_FILE_DIR / csv_file_name, encoding="gbk")
            lines = csv_file.readlines()
            for line in lines:
                line_list = line.split(",")
                after_sqls = line_list[-2]
                results = line_list[-1]
                after_sqls_list = after_sqls.split("union ALL")
                result_list = results.split("|")
                for i in range(len(after_sqls_list)):
                    check_string = "count(1)={};".format(result_list[i])
                    step.add_checkcontent(check_string,
                                          data_sources["bupps_107_orcl"],
                                          after_sqls_list[i])
    return steps
Example #2
0
def deal_ssh_command(root):
    step = importOfflineCase_step()
    step.set_stepname(root.get("testname"))
    step.set_stepdes(root.get("testname"))
    steps = [step.get_step()]

    # 添加脚本
    script_name = "ssh_jmeter_smart_route"
    ssh_connect = "smart_route_jmeter_ssh"
    ds = dealScriptData(base_name)
    ds.set_ssh_with_default(script_name, ssh_connect, root.get("testname"))
    resp, script_id = post_blade.dealScriptData(ds)
    # 添加数据
    data_content = list()
    temp = dict()
    temp["sheet0"] = list()
    data_chose_row = random_uuid(32)
    one = [random_uuid(32), "序号", "期望", "command"]
    two = [random_uuid(32), "参数说明", "", "脚本文件需要事先放到redis的bin目录下"]
    three = [
        data_chose_row, "", "keyword=OK", "EXEC,sh {};".format(
            root.element.find(".//stringProp[@name='command']").text)
    ]
    temp["sheet0"].append(one)
    temp["sheet0"].append(two)
    temp["sheet0"].append(three)
    data_content.append(temp)

    step.set_scriptcontent(script_id)
    step.set_dataarrcontent(data_chose_row, data_content)

    return steps
Example #3
0
def deal_JDBCSample(root):
    step = importOfflineCase_step()
    step.set_stepname(root.get("testname"))
    step.set_stepdes(root.get("testname"))
    steps = [step.get_step()]

    sql_text = replace_argument(
        root.element.find(".//stringProp[@name='query']").text)
    sqls_list = re.findall(
        r"((delete|insert|update|select|DELETE|INSERT|UPDATE|SELECT|Update|Insert|Select|Delete|Truncate|truncate|TRUNCATE).+[\s\S].+;)",
        sql_text)
    sqls = [sql_list[0] for sql_list in sqls_list]
    logger.debug(sqls)
    data_source = root.element.find(".//stringProp[@name='dataSource']").text
    for sql in sqls:
        if "%" in sql:
            sql = sql.replace("%", "%25")
        logger.debug(sql)
        step.add_presqlcontent(data_sources[data_source], sql)

    sub_elements = root.get_sub_elements()
    # 没有子组件, 纯粹是执行一条sql
    if not sub_elements:
        logger.debug("no sub element")
        return steps

    for sub_element in sub_elements:
        if not sub_element.isEnabled():
            continue

        if sub_element.tag == "BeanShellPostProcessor":
            if "redis" in sub_element.get("testname"):
                steps.append(deal_redis_beanshellsampler(sub_element))

    return steps
Example #4
0
def deal_redis_beanshellsampler(root):
    # 添加脚本
    script_name = "redis_jmeter_smart_route"
    ssh_connect = "smart_route_jmeter_redis"
    ds = dealScriptData(base_name)
    ds.set_ssh_with_default(script_name, ssh_connect, root.get("testname"))
    resp, script_id = post_blade.dealScriptData(ds)
    # 添加数据
    data_content = list()
    temp = dict()
    temp["sheet0"] = list()
    data_chose_row = random_uuid(32)
    one = [random_uuid(32), "序号", "期望", "command"]
    two = [random_uuid(32), "参数说明", "", "脚本文件需要事先放到redis的bin目录下"]
    three = [
        data_chose_row, "", "keyword=OK", "EXEC,sh clear_redis_smart_route.sh;"
    ]
    temp["sheet0"].append(one)
    temp["sheet0"].append(two)
    temp["sheet0"].append(three)
    data_content.append(temp)

    step = importOfflineCase_step()
    step.set_stepname(root.tag + root.get("testname"))
    step.set_stepdes(root.get("testname"))
    step.set_scriptcontent(script_id)
    step.set_dataarrcontent(data_chose_row, data_content)
    return step.get_step()
Example #5
0
def deal_HTTPSampler(root, step_name, script_content="", request_body=""):
    check_string = ""
    # 初始化 step 数据
    step = importOfflineCase_step()
    step.set_stepname(step_name)
    step.set_stepdes(root.get("testname"))

    if script_content:
        step.set_scriptcontent(script_content)

    # 没有传入报文内容, 自行获取当前报文内容, 否则使用传入内容,request 特殊处理, 将其中使用的变量替换成blade变量
    if not request_body:
        request_body = root.element.find(".//stringProp[@name='Argument.value']").text
    request_body = replace_argument(request_body)

    # HTTP 的子组件处理
    sub_elements = root.get_sub_elements()
    for sub_element in sub_elements:
        # 组件禁用, 不读取
        if not sub_element.isEnabled():
            continue

        # 前置提取
        if sub_element.tag == "JDBCPreProcessor":
            data_source = sub_element.element.find(".//stringProp[@name='dataSource']").text
            sql_text = sub_element.element.find(".//stringProp[@name='query']").text
            variable_name = sub_element.element.find(".//stringProp[@name='variableNames']").text
            # 对sql做提取
            sqls = re.findall(
                r"([delete|insert|update|select|DELETE|INSERT|UPDATE|SELECT|Update|Insert|Select|Delete].*?'.*;)",
                sql_text)
            logger.debug("提取的sql:%s" % str(sqls))
            for sql in sqls:
                sql = replace_argument(sql)
                if "%" in sql:
                    sql = sql.replace("%", "%25")
                if variable_name:
                    sql = variable_name + "|" + sql
                step.add_presqlcontent(data_sources[data_source], sql)
        # 后置提取
        # 验证提取
        elif sub_element.tag in ("ResponseAssertion", "BeanShellAssertion"):
            check_elements = sub_element.element.findall("collectionProp/stringProp")
            check_string = ""
            for check_element in check_elements:
                if check_element.text:
                    messages = check_element.text.replace('"', '').split(":")
                    if len(messages) == 1:
                        check_string += check_element.text
                    else:
                        check_string += check_string_root + messages[0] + "=" + messages[1] + ";"

            logger.debug(check_string)
    logger.debug(request_body)
    data_chose_row, data_arr_content = Josn2Blade(eval(request_body), [], 0, check_string)
    step.set_dataarrcontent(data_chose_row, data_arr_content)

    logger.debug(step)
    return step.get_step()
Example #6
0
def deal_user_parameters(root):
    step = importOfflineCase_step()
    step.set_stepdes(root.get("testname"))
    params_str = ""
    names = root.element.findall("collectionProp[@name='UserParameters.names']/stringProp")
    values = root.element.findall("collectionProp[@name='UserParameters.thread_values']//stringProp")
    for i in range(len(names)):
        params_str += check_argument(names[i].text) + "|" + values[i].text + ";"
    logger.debug(params_str)
    step.add_presqlcontent(data_sources["default"], params_str)
    return step.get_step()
Example #7
0
def deal_JDBCSample(root):
    step = importOfflineCase_step()
    step.set_stepname(root.get("testname"))
    step.set_stepdes(root.get("testname"))
    steps = [step.get_step()]

    sql_text = replace_argument(
        root.element.find(".//stringProp[@name='query']").text)
    sqls_list = re.findall(
        r"((delete|insert|update|select|DELETE|INSERT|UPDATE|SELECT|Update|Insert|Select|Delete|Truncate|truncate|TRUNCATE).+[\s\S])",
        sql_text)
    sqls = [sql_list[0] for sql_list in sqls_list]
    logger.debug(sqls)
    data_source = root.element.find(".//stringProp[@name='dataSource']").text
    for sql in sqls:
        if "%" in sql:
            sql = sql.replace("%", "%25")
        logger.debug(sql)
        step.add_presqlcontent(data_sources[data_source], sql)

    sub_elements = root.get_sub_elements()
    # 没有子组件, 纯粹是执行一条sql
    if not sub_elements:
        logger.debug("no sub element")
        return steps

    for sub_element in sub_elements:
        if not sub_element.isEnabled():
            continue

        if sub_element.tag == "BeanShellPostProcessor":
            steps.append(deal_redis_beanshellsampler(sub_element))
        elif sub_element.tag == "BeanShellAssertion":
            check_results = list(
                sub_element.element.find(
                    ".//stringProp[@name='BeanShellAssertion.parameters']").
                text)
            # 清空presqlcontent, 存在这个子组件,说明JDBC里存放的是验证SQL
            step.set_presqlcontent(list())
            for i in range(len(sqls)):
                check_string = "count(1)={};".format(check_results[i])
                sql = sqls[i]
                logger.debug("check sql:{}, check:{}".format(
                    sql, check_string))
                step.add_checkcontent(check_string, data_sources[data_source],
                                      sql)

    return steps
Example #8
0
def deal_redis_beanshellsampler(root, step_name, script_content):
    data_content = list()
    temp = dict()
    temp["sheet0"] = list()
    data_chose_row = random_uuid(32)
    one = [random_uuid(32), "序号", "期望", "command"]
    two = [random_uuid(32), "参数说明", "", "脚本文件需要事先放到redis的bin目录下"]
    three = [data_chose_row, "", "keyword=OK", "EXEC,sh clear_redis_ibps.sh;"]
    temp["sheet0"].append(one)
    temp["sheet0"].append(two)
    temp["sheet0"].append(three)
    data_content.append(temp)

    step = importOfflineCase_step()
    step.set_stepname(step_name)
    step.set_stepdes(root.get("testname"))
    step.set_scriptcontent(script_content)
    step.set_dataarrcontent(data_chose_row, data_content)
    return step.get_step()
Example #9
0
def deal_msg_beanshellsampler(root, step_name):
    step = importOfflineCase_step()
    step.set_stepname(step_name)
    step.set_stepdes(root.get("testname"))

    sub_elements = root.get_sub_elements()
    for sub_element in sub_elements:
        if not sub_element.isEnabled():
            continue

        if sub_element.tag == "JDBCPreProcessor":
            data_source = sub_element.element.find(".//stringProp[@name='dataSource']").text
            sql_text = sub_element.element.find(".//stringProp[@name='query']").text
            # 对sql做提取
            sqls = re.findall(
                r"([delete|insert|update|select|DELETE|INSERT|UPDATE|SELECT|Update|Insert|Select|Delete].*?'.*;)",
                sql_text)
            logger.debug("提取的sql:%s" % str(sqls))
            for sql in sqls:
                sql = replace_argument(sql)
                if "%" in sql:
                    sql = sql.replace("%", "%25")
                step.add_presqlcontent(data_sources[data_source], sql)

    script_text = root.element.find(".//stringProp[@name='BeanShellSampler.query']").text
    body = replace_argument(re.findall(r'String msg_body=[\r\n]?(.*?);', script_text)[0])
    body = body.replace("\\r\\n", "\r\n")
    logger.debug(body)

    data_content = list()
    temp = dict()
    temp["sheet0"] = list()
    data_chose_row = random_uuid(32)
    one = [random_uuid(32), "序号", "期望", "body"]
    two = [random_uuid(32), "参数说明", "", ""]
    three = [data_chose_row, "", "", body]
    temp["sheet0"].append(one)
    temp["sheet0"].append(two)
    temp["sheet0"].append(three)
    data_content.append(temp)
    step.set_dataarrcontent(data_chose_row, data_content)
    return step
Example #10
0
def deal_JDBCSample(root):
    step = importOfflineCase_step()
    step.set_stepname(root.get("testname"))
    step.set_stepdes(root.get("testname"))

    sql = replace_argument(root.element.find(".//stringProp[@name='query']").text)
    if "%" in sql:
        sql = sql.replace("%", "%25")
    logger.debug(sql)

    data_source = root.element.find(".//stringProp[@name='dataSource']").text

    sub_elements = root.get_sub_elements()

    # 没有子组件, 纯粹是执行一条sql
    if not sub_elements:
        logger.debug("no sub element")
        step.add_presqlcontent(data_sources[data_source], sql)
        return step.get_step()

    for sub_element in sub_elements:
        if not sub_element.isEnabled():
            continue

        if sub_element.tag == "ResponseAssertion":
            check_string = ""
            checks_element = sub_element.element.findall("collectionProp//stringProp")
            if checks_element is None:
                checks = ""
            elif len(checks_element) == 1:
                checks = checks_element[0].text
            else:
                check_keys = re.findall(r'(?:select|SELECT|Select)(.*)(?:FROM|from|From)', sql)[0].replace(' ', '').replace(',', '\t')
                check_value = ""
                for check_element_index in range(len(checks_element)):
                    check_value += checks_element[check_element_index].text
                    if check_element_index != len(checks_element) - 1:
                        check_value += "\t"
                checks = check_keys + "\n" + check_value

            if checks:
                checks_list = checks.split("\n")
                logger.debug(checks_list)
                check_values = []
                for i in range(len(checks_list)):
                    if not checks_list[i]:
                        continue

                    if i == 0:
                        check_keys = checks_list[i].split("\t")
                    else:
                        check_values.append(checks_list[i].split("\t"))
            else:
                check_keys = re.findall(r'select(.*?)from', sql)[0].split(",")
                check_values = [[''] * len(check_keys)]

            for value_index in range(len(check_values)):
                for key_index in range(len(check_keys)):
                    if key_index >= len(check_values[value_index]):
                        check_string += check_keys[key_index].strip() + "=null"
                    else:
                        check_string += check_keys[key_index].strip() + "=" + check_values[value_index][key_index]
                    if value_index == len(check_values) - 1 and key_index == len(check_keys) - 1:
                        check_string += ";"
                    else:
                        check_string += "|"
            step.add_checkcontent(check_string, data_sources[data_source], sql)
        elif sub_element.tag == "JDBCPreProcessor":
            pre_sql = replace_argument(sub_element.element.find(".//stringProp[@name='query']").text)
            logger.debug(pre_sql)
            data_source = sub_element.element.find(".//stringProp[@name='dataSource']").text
            variable_name = sub_element.element.find(".//stringProp[@name='variableNames']").text
            if variable_name:
                pre_sql = variable_name + "|" + pre_sql
            step.add_presqlcontent(data_sources[data_source], pre_sql)

    return step.get_step()
Example #11
0
def deal_constanttimer(root, step_name, script_content):
    step = importOfflineCase_step()
    step.set_stepname(step_name)
    step.set_stepdes(root.get("testname"))
    step.set_scriptcontent(script_content)
    return step.get_step()