diff options
| author | dongxiaoyan <[email protected]> | 2024-11-28 11:23:41 +0800 |
|---|---|---|
| committer | dongxiaoyan <[email protected]> | 2024-11-28 11:23:41 +0800 |
| commit | 4302bb47d0f13d129c07ecc00a493ab6e7b70af1 (patch) | |
| tree | 86cbafded7e4997f10f9d6b552a4f02b2284aceb | |
| parent | eecd484389657f9745df767363108b9b4e27e923 (diff) | |
测试数据适配重构
68 files changed, 23085 insertions, 11539 deletions
diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app.py index ae21c4e3e..9849640d6 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app.py @@ -1,201 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": ["app"], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -204,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app_appcategories.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app_appcategories.py index 3bad04ecf..ad40af06e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app_appcategories.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_app_appcategories.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Application, Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_appcategories.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_appcategories.py index 276da9d80..5930e5d2b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_appcategories.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_appcategories.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "app_category" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_link_id.py index cede88712..4038491cf 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_link_id.py @@ -1,201 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": ["s2c_link_id"], - "order_by": "all flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client-to-Server Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -204,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_link_id.py index 0dea223da..30b344b86 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_link_id.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client-to-Server Link ID, Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_ttl.py index 2c745d32d..d8049db91 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_s2c_ttl.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "stats_http_table_all_flows_dim_c2s_s2c_ttl", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client-to-Server TTL, Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_ttl.py index a177631a7..27ab8eb40 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c2s_ttl.py @@ -1,201 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": ["c2s_ttl"], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client-to-Server TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -204,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_country.py index c55573f90..9047c531b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_country.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip.py index c55573f90..2c47828e8 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip_object.py index 2b29b79fa..6faa07309 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_ip_object.py @@ -1,205 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.6", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "new c2s flows", - "row_limit": "10", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) - if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -208,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_port.py index 05c2a9fb1..2c47828e8 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_port.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:RATE:new c2s flows", - "new_s2c_flows_for_this_rule:RATE:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:RATE:all flows" - ], - "dimensions": [ - "client_port" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country.py index 394c1fa49..b46800872 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Country, Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country_app.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country_app.py index 6d7b4a78e..ec3ad0ad3 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_country_app.py @@ -1,205 +1,354 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_country", - "server_country", - "app" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Country, Server Country, Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -208,4 +357,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip.py index 307700fa8..b3680afb7 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client IP, Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip_object.py index 436e9f1f1..42f922b0f 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_ip_object.py @@ -1,206 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.6", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client IP Object List, Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) - if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -209,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_port.py index 9e30391d9..b26bcca43 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_c_s_port.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Client Port, Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_decoded_as.py index 9871f18a0..eafc3d656 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_decoded_as.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Decoded AS", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "decoded_as" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Decoded AS", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_device_group.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_device_group.py index 2de729f54..633e2e028 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_device_group.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_device_group.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "device_group" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Device Group", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "device_group" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Device Group", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_domain.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_domain.py index 41a5fa927..692e7c4fe 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_domain.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_flags.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_flags.py index f916f46fd..b1f1f7217 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_flags.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_flags.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "flags" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Flags", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "flags" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Flags", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn.py index e070bc5a5..e288de633 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn_domain.py index f522fe3ed..cd0a4411e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_fqdn_domain.py @@ -1,204 +1,340 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server FQDN, Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -207,4 +343,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_http_host.py index 572e2211f..b29563a9c 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_http_host.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "http_host" - ], - "order_by": "new s2c flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by HTTP Host", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "http_host" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "HTTP Host", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_link_id.py index cede88712..6fa62a5d1 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_link_id.py @@ -1,201 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": ["s2c_link_id"], - "order_by": "all flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -204,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_ttl.py index b1a5fc5dd..6ba97fd20 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s2c_ttl.py @@ -1,201 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": ["s2c_ttl"], - "order_by": "all flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -204,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_country.py index c55573f90..492a47431 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_country.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip.py index f5c388136..323c727bf 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip_object.py index 2d2d629dc..6b9bbb9fb 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_ip_object.py @@ -1,205 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.6", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "new c2s flows", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) - if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -208,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_port.py index 12024a364..5c22fb3ba 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_s_port.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "server_port" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sled_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sled_ip.py index dcb4f4cfc..f5c1dc24b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sled_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sled_ip.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Sled IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "sled_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Sled IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sub_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sub_id.py index 449e838c6..84a081587 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sub_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_flows_dim_sub_id.py @@ -1,203 +1,326 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", - }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.6", - }, - ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_c2s_flows_for_this_rule:SUM:new c2s flows", - "new_s2c_flows_for_this_rule:SUM:new s2c flows", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:all flows" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "new c2s flows", - "row_limit": "1000", - "min_in_size": "1", - "min_out_size": "1" + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.6", - "clients_end_ip": "10.64.224.6", - "servers_start_ip": "2.1.1.6", - "servers_end_ip": "2.1.1.6", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(New Client-to-Server Flows, New Server-to-Client Flows), SUM(New Client-to-Server Flows) and SUM(New Server-to-Client Flows) Distributed by Subscriber ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "subscriber_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Subscriber ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "new_c2s_flows_for_this_rule", + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_c2s_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(New Client-to-Server Flows)", + "unit": "flows" + }, + { + "source_fields": [ + "new_s2c_flows_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(New Server-to-Client Flows)", + "unit": "flows" + } + ], + "order_by": "SUM(New Client-to-Server Flows, New Server-to-Client Flows)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -206,4 +329,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app.py index 1e42bd26a..3ea77c9e1 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app" - ], - "order_by": "Packets", - "row_limit": "5", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_appcategory.py index 6d1676c35..26b59210d 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_appcategory.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application, Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application, Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "Packets Received", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_c_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_c_s_country.py index 0e83026ac..d85ad7f2a 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_c_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_c_s_country.py @@ -1,226 +1,472 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application, Client Country, Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application, Client Country, Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app", - "client_country", - "server_country" - ], - "order_by": "Packets", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app", - "client_country", - "server_country" - ], - "order_by": "Packets Sent", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -229,4 +475,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_fqdn.py index 5afe2a2e7..8715f526e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_app_fqdn.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application, Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application, Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app", - "server_fqdn" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app", - "server_fqdn" - ], - "order_by": "Packets Received", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_appcategory.py index 896c8b3a9..5ae1b58cf 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_appcategory.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app_category" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app_category" - ], - "order_by": "Packets", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_link_id.py index 31095eae7..cb510e20f 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_link_id.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client-to-Server Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client-to-Server Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "Packets Received", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_link_id": "702","s2c_link_id": "702", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_link_id.py index 6fb8c32d2..3244ce3ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_link_id.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client-to-Server Link ID, Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client-to-Server Link ID, Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server Link ID", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_link_id": "702","s2c_link_id": "702", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_ttl.py index 602151613..7c1a928c6 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_s2c_ttl.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client-to-Server TTL, Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client-to-Server TTL, Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + }, + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_ttl": "60","s2c_ttl": "20", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_ttl.py index b65f57bfe..1d2bbfcc3 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c2s_ttl.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client-to-Server TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client-to-Server TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "c2s_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client-to-Server TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_ttl": "60","s2c_ttl": "20", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_country.py index 65effc4c7..146d29ae0 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_country.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_country" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_country" - ], - "order_by": "Packets", - "row_limit": "5", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip.py index 839376bb7..3f9f0bb42 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "Packets", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip_object.py index dbd65126d..43d5849c4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_ip_object.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "Packets", - "row_limit": "5", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_port.py index e50349c9d..85d6fd1f4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_port.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_port" - ], - "order_by": "Packets", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_port" - ], - "order_by": "Packets", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_country_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_country_app_appcategory.py index 95768fac3..a9885f3b8 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_country_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_country_app_appcategory.py @@ -1,228 +1,500 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client Country, Server Country, Application, Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client Country, Server Country, Application, Application Category", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app_category" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application Category", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app", - "app_category", - "client_country", - "server_country" - ], - "order_by": "Packets", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app", - "app_category", - "client_country", - "server_country" - ], - "order_by": "Packets Sent", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -231,4 +503,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip.py index 7e4ff608b..cd13e1ac9 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client IP, Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client IP, Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip": "1.1.1.1","server_ip": "2.2.2.2", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip_objects.py index 69c479ccb..60bb702df 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_ip_objects.py @@ -1,226 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client IP Object List, Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "statistics_option": "Brief", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client IP Object List, Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client IP Object List", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip_object_list": "1573552","server_ip_object_list": "1573552", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -229,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_port.py index 257a6f902..f73d784fa 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_c_s_port.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Client Port, Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Client Port, Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "client_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Client Port", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "Packets", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "Packets", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"server_port": "80", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_decoded_as.py index f13c4db68..0d637a8a5 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_decoded_as.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Decoded AS", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "decoded_as" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Decoded AS", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Decoded AS", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "decoded_as" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Decoded AS", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "Packets", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_ttl": "60","s2c_ttl": "20", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group.py index 1856466a3..22850e1e4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Device Group", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "device_group" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Device Group", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.25", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Device Group", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "device_group" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Device Group", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "device_group" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "device_group" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.25", - "clients_end_ip": "10.64.224.25", - "servers_start_ip": "2.1.1.25", - "servers_end_ip": "2.1.1.25", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"device_group": "tsgx", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group_decoded_as.py index 1791895b2..c667ea316 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_device_group_decoded_as.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Device Group, Decoded AS", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "device_group" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Device Group", + "is_drill_down": 0 + }, + { + "source_fields": [ + "decoded_as" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Decoded AS", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Device Group, Decoded AS", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "device_group" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Device Group", + "is_drill_down": 0 + }, + { + "source_fields": [ + "decoded_as" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Decoded AS", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "decoded_as", - "device_group" - ], - "order_by": "Packets", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "decoded_as", - "device_group" - ], - "order_by": "Packets", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_ttl": "60","s2c_ttl": "20", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_direction.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_direction.py index d1d3193f2..fa896e237 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_direction.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_direction.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Direction", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "direction" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Direction", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Direction", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "direction" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Direction", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "direction" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "direction" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"server_port": "80", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_domain.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_domain.py index c0c3a822f..991f968a1 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_domain.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"server_port": "80", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_flags.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_flags.py index 2d2a2ebbc..3e7955dac 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_flags.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_flags.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Flags", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "flags" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Flags", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Flags", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "flags" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Flags", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "flags" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "flags" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"server_port": "80", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn.py index f950ba8cc..c894e256c 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn_domain.py index 606102b98..4695adc81 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_fqdn_domain.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server FQDN, Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server FQDN, Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_domain" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Domain", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "Packets Received", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"server_fqdm": "www.yumi.com", "server_domain": "yumi.com", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_http_host.py index f1b13210f..866048cce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_http_host.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by HTTP Host", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "http_host" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "HTTP Host", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by HTTP Host", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "http_host" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "HTTP Host", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "http_host" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "http_host" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_link_id.py index 01cea3914..f418c8bd4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_link_id.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server-to-Client Link ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_link_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client Link ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_link_id": "702","s2c_link_id": "702", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_ttl.py index 49c03ff7c..b70eb88da 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s2c_ttl.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server-to-Client TTL", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "s2c_ttl" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server-to-Client TTL", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_ttl": "60","s2c_ttl": "20", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_country.py index e89ca41f0..15b0ed3ed 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_country.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server Country", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_country" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Country", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_country" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_country" - ], - "order_by": "Packets", - "row_limit": "5", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip.py index 48c338cf7..f37c2c94b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip": "1.1.1.1","server_ip": "2.2.2.2", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_app.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_app.py index 4285f7e97..e2c911433 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_app.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server IP, Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server IP, Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "app", - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "app", - "server_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip": "1.1.1.1","server_ip": "2.2.2.2", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_fqdn.py index 2308f9c86..931dc5592 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_fqdn.py @@ -1,224 +1,444 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server IP, Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server IP, Server FQDN", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + }, + { + "source_fields": [ + "server_fqdn" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server FQDN", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_ip", - "server_fqdn" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_ip", - "server_fqdn" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip": "1.1.1.1","server_ip": "2.2.2.2", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -227,4 +447,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_object.py index 82452af74..9764c1abe 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_ip_object.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server IP Object List", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_ip_object_list" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP Object List", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_ip": "1.1.1.1","server_ip": "2.2.2.2", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_port.py index c97dc6e0c..55728514e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_s_port.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Server Port", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "server_port" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server Port", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "server_port" - ], - "order_by": "Packets", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "server_port" - ], - "order_by": "Packets", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sled_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sled_ip.py index 70e1e2703..7fe80ea48 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sled_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sled_ip.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Sled IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "sled_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Sled IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Sled IP", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "sled_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Sled IP", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "Packets", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "Packets", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sub_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sub_id.py index e2766bb8e..703cb7723 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sub_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_pkts_dim_sub_id.py @@ -1,222 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Subscriber ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "subscriber_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Subscriber ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Subscriber ID", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "subscriber_id" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Subscriber ID", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:SUM:Packets Sent", - "received_pkts:SUM:Packets Received", - "sent_pkts+received_pkts:SUM:Packets" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "Packets", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "sent_pkts:RATE:Packets Sent", - "received_pkts:RATE:Packets Received", - "sent_pkts+received_pkts:RATE:Packets" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "Packets", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"app": "123","app_category": "234", "in_pkts": 19, "out_pkts": 64, "pkts": 83} - ], - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -225,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app.py index 076bfddc9..3ea77c9e1 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app.py @@ -1,215 +1,416 @@ # -*- coding: UTF-8 -*- -import time import os import sys +import time +import pytz +from support.organize_config import OrganizeConfig from support.ui_utils.element_position.map_element_position_library import replace_paras - -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -import traceback -from datetime import datetime -from support.common_utils.create_policy import CreatePolicy +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.ui_client import UIClient +from support.api_utils.api_client import APIClient +from support.packet_generator.traffic_generator import * from support.report_update import ReportUpdate - def run(parameter): try: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Begin to run test case: " + parameter["test_case_name"], flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Begin to run test case: " + parameter["test_case_name"], flush=True) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "statistics", - "rule_name": parameter["test_case_name"], - "rule_action": "statistics", - "rule_type": "create", - "debug_flag": "local", - "test_subcriber_id": "test6489", - "script_type": "ui", - "condition": { - "source_ip": [ - { - "name": parameter["test_case_name"].replace("_dstip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ + policy_configuration = { + "name": parameter["test_case_name"], + "type": "statistics", + "action": "statistics", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_dstip", "", 1), + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "10.64.224.31", + "interval": "1-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "name": parameter["test_case_name"].replace("_srcip", "", 1), + "attribute_name": "ATTR_DESTINATION_IP", + "type": "ip", + "sub_type": "ip", + "statistics_option": "none", + "member_type": "item", + "items": [ + { + "op": "add", + "ip": "2.1.1.9", + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] + } + ] + } + ], + "action_parameter": { + "template_profile":{ + "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), + "uuid": "", + "dataview": { + "charts": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "10.64.224.31", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Packets Sent, Packets Received)", + "unit": "packets" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Packets Sent)", + "unit": "packets" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Packets Received)", + "unit": "packets" + } + ], + "order_by": "SUM(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } }, - ] - } - ], - "source_port": [], - "destination_ip": [ - { - "name": parameter["test_case_name"].replace("_srcip", "", 1), - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ { - "item_operation": "add", - "item_type": "ipv4", - "item_value": "2.1.1.31", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "20", + "metrics": [ + { + "source_fields": [ + "sent_pkts", + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Packets Sent, Packets Received)", + "unit": "pps" + }, + { + "source_fields": [ + "sent_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Packets Sent)", + "unit": "pps" + }, + { + "source_fields": [ + "received_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Packets Received)", + "unit": "pps" + } + ], + "order_by": "RATE(Packets Sent, Packets Received)", + "source": "statistics_rule", + "series_limit": "" + } + } ] - }], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "http", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [ - ], - "protocol_filed": [], - "sub_action_override": True, - "sub_action": [ - ], - "packet_capture": [] + }, + "vsys": 1 + } }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "app" - ], - "order_by": "sessions", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "app" - ], - "order_by": "sessions", - "row_limit": "50", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] - }, - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key": "server_fqdn", "query_value": "www.yumi.com"}], - "traffic": { - "protocol": "", - "type": "trex", - "clients_start_ip": "10.64.224.31", - "clients_end_ip": "10.64.224.31", - "servers_start_ip": "2.1.1.31", - "servers_end_ip": "2.1.1.31", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - # "traffic": { - # "type": "trex", - # "clients_start_ip": "192.168.64.86", - # "clients_end_ip": "192.168.64.86", - # "servers_start_ip": "2.1.1.1", - # "servers_end_ip": "2.1.1.2", - # "m": 1, - # "d": 1, - # "yaml_name": "test", - # "pcap_name": "test_https" - # }, - # "traffic": { - # "type": "nslookup", - # "command": "nslookup www.facebook.com" - # }, - # "traffic": { - # "type": "wget", - # "command": "wget -q -O- http://open.node.com:180" - # }, - "token": "" + "is_enabled": 1, + "log_option": "metadata" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - # 脚本结束时间和耗时 - script_end_time = time.time() - duration = script_end_time - script_start_time - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], - "Finish test case: " + parameter["test_case_name"], flush=True) - return result + traffic_generation = { + "tool": "trex", # or trex/http + "clients_start_ip": "10.64.224.9", + "clients_end_ip": "10.64.224.9", + "servers_start_ip": "2.1.1.9", + "servers_end_ip": "2.1.1.9", + "m": 1, + "d": 1, + "yaml_name": "test", + "pcap_name": "http_87pkts" + } + + verification_result = { + "excepted_traffic_result": {"total_bytes": 94679, "total_bytes_received": 93284, "total_bytes_sent": 1395, + "total_packets": 87, "total_packets_received": 65, "total_packets_sent": 22, + "total_syn_pkt": 1}, + "expected_query": {"hits": 1}, + "expected_log": [ + {"query_field_key": "decoded_as", "query_value": "HTTP"}, + {"query_field_key": "server_fqdn", "query_value": "www.yumi.com"} + ] + } + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + rules_tuple, ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token,()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # {uuid, type}, i.e., {"12341-232-a21", "ip"} + objects_tuple, api_error = api_client.create_objects(policy_configuration) + if len(api_error) > 0: + return api_error + # 处理Profiles + profiles_tuple, api_error = api_client.create_profiles(policy_configuration) + if len(api_error) > 0: + return api_error + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_tuple, "", profiles_tuple) + if len(api_error) > 0: + return api_error + # 等待下发配置生效 + time.sleep(3) + + # 类实例化 + generator = TrafficGenerator() + + # 获取当前时间 + utc_tz = pytz.timezone('UTC') + current_utc_time = datetime.now(utc_tz) + start_time = current_utc_time.strftime('%Y-%m-%dT%H:%M:%SZ') + + # 触发流量 + traffic_result = generator.run(policy_configuration, traffic_generation) + + # 验证流量生成器的返回值是否符合策略执行的预期 + excepted_traffic_result, error = generator.result(verification_result, traffic_result) + if excepted_traffic_result == False: + return error + + # 验证tsg的日志是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + log_result = ui_client.query_rule_log(verification_result, rules_tuple, traffic_result) + elif parameter["initiation_method"] == "api": + log_result = api_client.query_rule_log(traffic_generation, verification_result, rules_tuple, start_time, traffic_result) + if log_result == True: + test_summary["log"] = "Pass." + elif log_result == False: + test_summary["log"] = "The failure reason: the returned log does not match the expected result." + elif log_result == None: + test_summary["log"] = "The failure reason: the returned log is empty." + elif len(log_result) > 0: + test_summary["log"] = log_result + + # 验证tsg的metric是否符合策略执行的预期 + if parameter["initiation_method"] == "ui": + metric_result = ui_client.query_rule_metric(verification_result, traffic_result) + elif parameter["initiation_method"] == "api": + metric_result = api_client.query_rule_metric(verification_result, rules_tuple, start_time, traffic_result) + if metric_result == True: + test_summary["metric"] = "Pass." + elif metric_result == False: + test_summary["metric"] = "The failure reason: the returned metric does not match the expected result." + elif metric_result == None: + test_summary["metric"] = "The failure reason: the returned metric is empty." + elif len(metric_result) > 0: + test_summary["metric"] = metric_result + #if rules_tuple: + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "DEBUG:Case:{},rule:{},log_result:{},excepted_traffic_result:{},metric_result:{}".format(os.path.basename(__file__)[:-3], list(rules_tuple)[0]["uuid"], log_result, excepted_traffic_result, metric_result), flush=True) + return test_summary except Exception as e: exception_result = str(e) - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, - flush=True) - traceback.print_exc() - return "Error: " + str(e) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() + # 删除 + if parameter["initiation_method"] == "ui": + if rules_tuple: + ui_client.delete_rules(parameter, policy_configuration) + elif parameter["initiation_method"] == "api": + if rules_tuple: + api_client.delete_rules(rules_tuple) + if profiles_tuple: + api_client.delete_profiles(profiles_tuple) + if objects_tuple: + api_client.delete_objects(objects_tuple) + # 脚本结束时间和耗时 + script_end_time = time.time() + duration = script_end_time - script_start_time + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Duration of running the test case: ", "{:.3f}".format(duration), flush=True) + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Finish test case: " + parameter["test_case_name"], flush=True) + # 生成csv报告 update = ReportUpdate() update.write_result(parameter, result, exception_result) if __name__ == '__main__': parameter = { - "username": "zhaokun", "password": "zhaokun1", "test_pc_ip": "192.168.64.73", "api_server": "http://192.168.44.72", - "is_log": 1, + "is_log": 0, "env": "tsgx", - "vsys_id": 1, # 6 + "vsys": 1, # 6 "root_path": "D:/tsgcode/tsg_test", "path": "tsgcode/tsg_test/testcase/ui", "module_name": "statistics", "test_case_name": os.path.basename(__file__)[:-3], "debug_flag": "local", "test_subcriber_id": "test6489", - "script_type": "ui", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) """ @@ -218,4 +419,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """
\ No newline at end of file + """ |
