From 6f11f6bdacfcffe07a266b8188fdf6c89f1c6479 Mon Sep 17 00:00:00 2001 From: "hebingning@geedgenetworks.com" Date: Thu, 28 Nov 2024 15:12:59 +0800 Subject: update mail cases to new format --- .../sec_deny_negate_srcip_dns_drop_hit_no.py | 2 +- .../sec_deny_negate_srcip_dns_drop_hit_yes.py | 2 +- ...ec_deny_srcip_mail_exactly_subject_block_550.py | 320 +++++---- ...ec_deny_srcip_mail_exactly_subject_block_551.py | 324 +++++---- ...deny_srcip_mail_exactly_subject_block_tamper.py | 323 +++++---- .../sec_deny_srcip_mail_exactly_subject_drop.py | 326 +++++---- ...ec_deny_srcip_mail_exactly_subject_rate_high.py | 325 +++++---- ...sec_deny_srcip_mail_exactly_subject_rate_low.py | 325 +++++---- .../sec_deny_srcip_mail_exactly_subject_rst.py | 326 +++++---- .../sec_deny_srcip_mail_exactly_to_drop.py | 324 +++++---- .../sec_deny_srcip_mail_exactly_to_rate_high.py | 323 +++++---- .../sec_deny_srcip_mail_exactly_to_rate_low.py | 323 +++++---- .../security/sec_deny_srcip_mail_exactly_to_rst.py | 324 +++++---- .../sec_deny_srcip_mail_exactly_to_tamper.py | 320 +++++---- ...eny_srcip_mail_pre_attachment_name_block_550.py | 223 ++++++ ...eny_srcip_mail_pre_attachment_name_block_551.py | 224 ++++++ ...sec_deny_srcip_mail_pre_attachment_name_drop.py | 224 ++++++ ...deny_srcip_mail_pre_attachment_name_drop_rst.py | 224 ++++++ ...rcip_mail_pre_attachment_name_rate_block_550.py | 181 ----- ...rcip_mail_pre_attachment_name_rate_block_551.py | 181 ----- ...eny_srcip_mail_pre_attachment_name_rate_drop.py | 183 ----- ...eny_srcip_mail_pre_attachment_name_rate_high.py | 770 +++++++++++++++++---- ...deny_srcip_mail_pre_attachment_name_rate_low.py | 328 +++++---- ...deny_srcip_mail_pre_attachment_name_rate_rst.py | 180 ----- ...y_srcip_mail_pre_attachment_name_rate_tamper.py | 181 ----- ...c_deny_srcip_mail_pre_attachment_name_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_pre_content_block_550.py | 223 ++++++ .../sec_deny_srcip_mail_pre_content_block_551.py | 224 ++++++ .../sec_deny_srcip_mail_pre_content_drop.py | 224 ++++++ .../sec_deny_srcip_mail_pre_content_drop_rst.py | 224 ++++++ ...c_deny_srcip_mail_pre_content_rate_block_550.py | 181 ----- ...c_deny_srcip_mail_pre_content_rate_block_551.py | 181 ----- .../sec_deny_srcip_mail_pre_content_rate_drop.py | 183 ----- .../sec_deny_srcip_mail_pre_content_rate_high.py | 329 +++++---- .../sec_deny_srcip_mail_pre_content_rate_low.py | 328 +++++---- .../sec_deny_srcip_mail_pre_content_rate_rst.py | 180 ----- .../sec_deny_srcip_mail_pre_content_rate_tamper.py | 181 ----- .../sec_deny_srcip_mail_pre_content_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_pre_subject_block_550.py | 319 +++++---- .../sec_deny_srcip_mail_pre_subject_block_551.py | 324 +++++---- ...sec_deny_srcip_mail_pre_subject_block_tamper.py | 323 +++++---- .../sec_deny_srcip_mail_pre_subject_drop.py | 326 +++++---- .../sec_deny_srcip_mail_pre_subject_rate_high.py | 325 +++++---- .../sec_deny_srcip_mail_pre_subject_rate_low.py | 325 +++++---- .../sec_deny_srcip_mail_pre_subject_rst.py | 326 +++++---- tests/security/sec_deny_srcip_mail_pre_to_drop.py | 324 +++++---- .../sec_deny_srcip_mail_pre_to_rate_high.py | 323 +++++---- .../sec_deny_srcip_mail_pre_to_rate_low.py | 323 +++++---- tests/security/sec_deny_srcip_mail_pre_to_rst.py | 324 +++++---- .../security/sec_deny_srcip_mail_pre_to_tamper.py | 320 +++++---- ..._srcip_mail_sub_attachment_content_block_550.py | 223 ++++++ ..._srcip_mail_sub_attachment_content_block_551.py | 224 ++++++ ..._deny_srcip_mail_sub_attachment_content_drop.py | 224 ++++++ ...y_srcip_mail_sub_attachment_content_drop_rst.py | 224 ++++++ ...p_mail_sub_attachment_content_rate_block_550.py | 181 ----- ...p_mail_sub_attachment_content_rate_block_551.py | 181 ----- ..._srcip_mail_sub_attachment_content_rate_drop.py | 183 ----- ..._srcip_mail_sub_attachment_content_rate_high.py | 329 +++++---- ...y_srcip_mail_sub_attachment_content_rate_low.py | 328 +++++---- ...y_srcip_mail_sub_attachment_content_rate_rst.py | 180 ----- ...rcip_mail_sub_attachment_content_rate_tamper.py | 181 ----- ...eny_srcip_mail_sub_attachment_content_tamper.py | 223 ++++++ ...eny_srcip_mail_sub_attachment_name_block_550.py | 223 ++++++ ...eny_srcip_mail_sub_attachment_name_block_551.py | 224 ++++++ ...sec_deny_srcip_mail_sub_attachment_name_drop.py | 673 ++++++++++++++++++ ...deny_srcip_mail_sub_attachment_name_drop_rst.py | 224 ++++++ ...rcip_mail_sub_attachment_name_rate_block_550.py | 181 ----- ...rcip_mail_sub_attachment_name_rate_block_551.py | 181 ----- ...eny_srcip_mail_sub_attachment_name_rate_drop.py | 183 ----- ...eny_srcip_mail_sub_attachment_name_rate_high.py | 328 +++++---- ...deny_srcip_mail_sub_attachment_name_rate_low.py | 328 +++++---- ...deny_srcip_mail_sub_attachment_name_rate_rst.py | 180 ----- ...y_srcip_mail_sub_attachment_name_rate_tamper.py | 181 ----- ...c_deny_srcip_mail_sub_attachment_name_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_sub_content_block_550.py | 223 ++++++ .../sec_deny_srcip_mail_sub_content_block_551.py | 224 ++++++ .../sec_deny_srcip_mail_sub_content_drop.py | 224 ++++++ .../sec_deny_srcip_mail_sub_content_drop_rst.py | 224 ++++++ ...c_deny_srcip_mail_sub_content_rate_block_550.py | 181 ----- ...c_deny_srcip_mail_sub_content_rate_block_551.py | 181 ----- .../sec_deny_srcip_mail_sub_content_rate_drop.py | 183 ----- .../sec_deny_srcip_mail_sub_content_rate_high.py | 329 +++++---- .../sec_deny_srcip_mail_sub_content_rate_low.py | 328 +++++---- .../sec_deny_srcip_mail_sub_content_rate_rst.py | 180 ----- .../sec_deny_srcip_mail_sub_content_rate_tamper.py | 181 ----- .../sec_deny_srcip_mail_sub_content_tamper.py | 223 ++++++ ...sec_deny_srcip_mail_substr_subject_block_550.py | 319 +++++---- ...sec_deny_srcip_mail_substr_subject_block_551.py | 324 +++++---- ..._deny_srcip_mail_substr_subject_block_tamper.py | 323 +++++---- .../sec_deny_srcip_mail_substr_subject_drop.py | 326 +++++---- ...sec_deny_srcip_mail_substr_subject_rate_high.py | 325 +++++---- .../sec_deny_srcip_mail_substr_subject_rate_low.py | 325 +++++---- .../sec_deny_srcip_mail_substr_subject_rst.py | 326 +++++---- .../security/sec_deny_srcip_mail_substr_to_drop.py | 324 +++++---- .../sec_deny_srcip_mail_substr_to_rate_high.py | 319 +++++---- .../sec_deny_srcip_mail_substr_to_rate_low.py | 323 +++++---- .../security/sec_deny_srcip_mail_substr_to_rst.py | 324 +++++---- .../sec_deny_srcip_mail_substr_to_tamper.py | 320 +++++---- ...ny_srcip_mail_suff_attachment_name_block_550.py | 223 ++++++ ...ny_srcip_mail_suff_attachment_name_block_551.py | 224 ++++++ ...ec_deny_srcip_mail_suff_attachment_name_drop.py | 224 ++++++ ...eny_srcip_mail_suff_attachment_name_drop_rst.py | 224 ++++++ ...cip_mail_suff_attachment_name_rate_block_550.py | 181 ----- ...cip_mail_suff_attachment_name_rate_block_551.py | 181 ----- ...ny_srcip_mail_suff_attachment_name_rate_drop.py | 183 ----- ...ny_srcip_mail_suff_attachment_name_rate_high.py | 770 +++++++++++++++++---- ...eny_srcip_mail_suff_attachment_name_rate_low.py | 328 +++++---- ...eny_srcip_mail_suff_attachment_name_rate_rst.py | 180 ----- ..._srcip_mail_suff_attachment_name_rate_tamper.py | 181 ----- ..._deny_srcip_mail_suff_attachment_name_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_suff_content_block_550.py | 223 ++++++ .../sec_deny_srcip_mail_suff_content_block_551.py | 224 ++++++ .../sec_deny_srcip_mail_suff_content_drop.py | 224 ++++++ .../sec_deny_srcip_mail_suff_content_drop_rst.py | 224 ++++++ ..._deny_srcip_mail_suff_content_rate_block_550.py | 181 ----- ..._deny_srcip_mail_suff_content_rate_block_551.py | 181 ----- .../sec_deny_srcip_mail_suff_content_rate_drop.py | 183 ----- .../sec_deny_srcip_mail_suff_content_rate_high.py | 329 +++++---- .../sec_deny_srcip_mail_suff_content_rate_low.py | 328 +++++---- .../sec_deny_srcip_mail_suff_content_rate_rst.py | 180 ----- ...sec_deny_srcip_mail_suff_content_rate_tamper.py | 181 ----- .../sec_deny_srcip_mail_suff_content_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_suff_subject_block_550.py | 320 +++++---- .../sec_deny_srcip_mail_suff_subject_block_551.py | 324 +++++---- ...ec_deny_srcip_mail_suff_subject_block_tamper.py | 177 ----- .../sec_deny_srcip_mail_suff_subject_drop.py | 326 +++++---- .../sec_deny_srcip_mail_suff_subject_drop_rst.py | 223 ++++++ .../sec_deny_srcip_mail_suff_subject_rate_high.py | 325 +++++---- .../sec_deny_srcip_mail_suff_subject_rate_low.py | 325 +++++---- .../sec_deny_srcip_mail_suff_subject_rst.py | 141 ---- .../sec_deny_srcip_mail_suff_subject_tamper.py | 222 ++++++ tests/security/sec_deny_srcip_mail_suff_to_drop.py | 324 +++++---- .../sec_deny_srcip_mail_suff_to_rate_high.py | 323 +++++---- .../sec_deny_srcip_mail_suff_to_rate_low.py | 323 +++++---- tests/security/sec_deny_srcip_mail_suff_to_rst.py | 324 +++++---- .../security/sec_deny_srcip_mail_suff_to_tamper.py | 320 +++++---- ...eny_srcip_mail_xly_attachment_name_block_550.py | 223 ++++++ ...eny_srcip_mail_xly_attachment_name_block_551.py | 224 ++++++ ...sec_deny_srcip_mail_xly_attachment_name_drop.py | 224 ++++++ ...deny_srcip_mail_xly_attachment_name_drop_rst.py | 224 ++++++ ...rcip_mail_xly_attachment_name_rate_block_550.py | 181 ----- ...rcip_mail_xly_attachment_name_rate_block_551.py | 181 ----- ...eny_srcip_mail_xly_attachment_name_rate_drop.py | 183 ----- ...eny_srcip_mail_xly_attachment_name_rate_high.py | 770 +++++++++++++++++---- ...deny_srcip_mail_xly_attachment_name_rate_low.py | 328 +++++---- ...deny_srcip_mail_xly_attachment_name_rate_rst.py | 180 ----- ...y_srcip_mail_xly_attachment_name_rate_tamper.py | 181 ----- ...c_deny_srcip_mail_xly_attachment_name_tamper.py | 223 ++++++ .../sec_deny_srcip_mail_xly_content_block_550.py | 223 ++++++ .../sec_deny_srcip_mail_xly_content_block_551.py | 224 ++++++ .../sec_deny_srcip_mail_xly_content_drop.py | 224 ++++++ .../sec_deny_srcip_mail_xly_content_drop_rst.py | 224 ++++++ ...c_deny_srcip_mail_xly_content_rate_block_550.py | 181 ----- ...c_deny_srcip_mail_xly_content_rate_block_551.py | 181 ----- .../sec_deny_srcip_mail_xly_content_rate_drop.py | 183 ----- .../sec_deny_srcip_mail_xly_content_rate_high.py | 329 +++++---- .../sec_deny_srcip_mail_xly_content_rate_low.py | 328 +++++---- .../sec_deny_srcip_mail_xly_content_rate_rst.py | 180 ----- .../sec_deny_srcip_mail_xly_content_rate_tamper.py | 181 ----- .../sec_deny_srcip_mail_xly_content_tamper.py | 223 ++++++ ...bstr_fqdn_http_alert_200_responsetext_hit_no.py | 2 +- ...substr_fqdn_http_substr_url_redirect_hit_yes.py | 2 +- tests/security/sec_shunt_extip_suff_fqdn_ssl.py | 2 +- 163 files changed, 24153 insertions(+), 17397 deletions(-) create mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_attachment_name_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_content_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_content_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_content_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_content_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_content_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_content_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_content_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_content_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_pre_content_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_pre_content_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_content_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_attachment_name_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_content_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_content_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_content_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_content_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_content_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_content_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_content_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_content_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_sub_content_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_sub_content_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_attachment_name_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_content_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_content_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_content_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_content_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_content_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_content_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_content_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_content_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_content_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_content_tamper.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_subject_block_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_subject_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_suff_subject_rst.py create mode 100644 tests/security/sec_deny_srcip_mail_suff_subject_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_attachment_name_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_content_block_550.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_content_block_551.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_content_drop.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_content_drop_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_content_rate_block_550.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_content_rate_block_551.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_content_rate_drop.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_content_rate_rst.py delete mode 100644 tests/security/sec_deny_srcip_mail_xly_content_rate_tamper.py create mode 100644 tests/security/sec_deny_srcip_mail_xly_content_tamper.py diff --git a/tests/security/sec_deny_negate_srcip_dns_drop_hit_no.py b/tests/security/sec_deny_negate_srcip_dns_drop_hit_no.py index d33f7f40e..2d8f5f10d 100644 --- a/tests/security/sec_deny_negate_srcip_dns_drop_hit_no.py +++ b/tests/security/sec_deny_negate_srcip_dns_drop_hit_no.py @@ -34,7 +34,7 @@ def run(parameter): "attribute_name": "ATTR_SOURCE_IP", "type": "ip", "sub_type": "ip", - "name": "sec_extip", + "name": "sec_ip", "items": [ { "op": "add", diff --git a/tests/security/sec_deny_negate_srcip_dns_drop_hit_yes.py b/tests/security/sec_deny_negate_srcip_dns_drop_hit_yes.py index 8310d56db..190a2b25c 100644 --- a/tests/security/sec_deny_negate_srcip_dns_drop_hit_yes.py +++ b/tests/security/sec_deny_negate_srcip_dns_drop_hit_yes.py @@ -34,7 +34,7 @@ def run(parameter): "attribute_name": "ATTR_SOURCE_IP", "type": "ip", "sub_type": "ip", - "name": "sec_extip", + "name": "sec_sec_ip", "items": [ { "op": "add", diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_block_550.py b/tests/security/sec_deny_srcip_mail_exactly_subject_block_550.py index 0a8418b18..ecdb88969 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_block_550.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_block_550.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { "username": "hebingning", "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_block_551.py b/tests/security/sec_deny_srcip_mail_exactly_subject_block_551.py index bf3fcf703..76d4f11ce 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_block_551.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_block_551.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_block_551", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_block_tamper.py b/tests/security/sec_deny_srcip_mail_exactly_subject_block_tamper.py index b286eba00..51020de65 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_block_tamper.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_block_tamper.py @@ -1,177 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_block_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_drop.py b/tests/security/sec_deny_srcip_mail_exactly_subject_drop.py index 09565e56d..30b0d74de 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_drop.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_drop.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_rate_high.py b/tests/security/sec_deny_srcip_mail_exactly_subject_rate_high.py index 86bfcb63b..1a35d3b86 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_rate_high.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_rate_low.py b/tests/security/sec_deny_srcip_mail_exactly_subject_rate_low.py index 3ed350a54..80e2293e8 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_rate_low.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_subject_rst.py b/tests/security/sec_deny_srcip_mail_exactly_subject_rst.py index 2a1c2f046..3ab2d038b 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_subject_rst.py +++ b/tests/security/sec_deny_srcip_mail_exactly_subject_rst.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_subject_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["$Bestman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Bestman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_to_drop.py b/tests/security/sec_deny_srcip_mail_exactly_to_drop.py index 0fc0d5e4d..5eed3df5b 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_to_drop.py +++ b/tests/security/sec_deny_srcip_mail_exactly_to_drop.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_to_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "$autotest@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_to_rate_high.py b/tests/security/sec_deny_srcip_mail_exactly_to_rate_high.py index 5e763bffa..c64431e62 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_to_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_exactly_to_rate_high.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_to_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "$autotest@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_to_rate_low.py b/tests/security/sec_deny_srcip_mail_exactly_to_rate_low.py index 53ef761d9..dd696935f 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_to_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_exactly_to_rate_low.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_to_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "$autotest@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_to_rst.py b/tests/security/sec_deny_srcip_mail_exactly_to_rst.py index 24b51722d..fe91f1e4a 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_to_rst.py +++ b/tests/security/sec_deny_srcip_mail_exactly_to_rst.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_to_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "$autotest@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_exactly_to_tamper.py b/tests/security/sec_deny_srcip_mail_exactly_to_tamper.py index a234bb9a1..3371915a6 100644 --- a/tests/security/sec_deny_srcip_mail_exactly_to_tamper.py +++ b/tests/security/sec_deny_srcip_mail_exactly_to_tamper.py @@ -1,173 +1,221 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_exactly_to_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "$autotest@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_550.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_550.py new file mode 100644 index 000000000..5e2b933c3 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_551.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_551.py new file mode 100644 index 000000000..527210d95 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop.py new file mode 100644 index 000000000..19db031f3 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop_rst.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop_rst.py new file mode 100644 index 000000000..5ed581fc7 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_550.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_550.py deleted file mode 100644 index 363fd5faa..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_551.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_551.py deleted file mode 100644 index 7ec9c500d..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_drop.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_drop.py deleted file mode 100644 index 3d8473649..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_high.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_high.py index fe51ebf25..71d7ddf86 100644 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_high.py @@ -1,181 +1,673 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir + parameter = { + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_low.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_low.py index 63b7992c3..136889867 100644 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_rst.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_rst.py deleted file mode 100644 index 3f7460be5..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_tamper.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_tamper.py deleted file mode 100644 index fe51ebf25..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_attachment_name_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/app/support/packet_generator/mail_file/subjectEnglis*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_attachment_name_tamper.py b/tests/security/sec_deny_srcip_mail_pre_attachment_name_tamper.py new file mode 100644 index 000000000..05c7ee526 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_attachment_name_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/pac" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_block_550.py b/tests/security/sec_deny_srcip_mail_pre_content_block_550.py new file mode 100644 index 000000000..2898d82d7 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_content_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_block_551.py b/tests/security/sec_deny_srcip_mail_pre_content_block_551.py new file mode 100644 index 000000000..3a6abce12 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_content_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_drop.py b/tests/security/sec_deny_srcip_mail_pre_content_drop.py new file mode 100644 index 000000000..474fff135 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_content_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_drop_rst.py b/tests/security/sec_deny_srcip_mail_pre_content_drop_rst.py new file mode 100644 index 000000000..28f868257 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_content_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_block_550.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_block_550.py deleted file mode 100644 index a8103b55d..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_block_551.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_block_551.py deleted file mode 100644 index 01cda257c..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_drop.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_drop.py deleted file mode 100644 index 079050dd9..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_high.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_high.py index 7534f5ab4..aec76384f 100644 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_pre_content_rate_high.py @@ -1,181 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_low.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_low.py index c414c833e..0ef5a47c6 100644 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_pre_content_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_rst.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_rst.py deleted file mode 100644 index f02c8dc9b..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_rate_tamper.py b/tests/security/sec_deny_srcip_mail_pre_content_rate_tamper.py deleted file mode 100644 index 7534f5ab4..000000000 --- a/tests/security/sec_deny_srcip_mail_pre_content_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content*" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_content_tamper.py b/tests/security/sec_deny_srcip_mail_pre_content_tamper.py new file mode 100644 index 000000000..4ba5a6a8b --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_pre_content_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^conten" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_block_550.py b/tests/security/sec_deny_srcip_mail_pre_subject_block_550.py index ceaa469a0..c1ce54661 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_block_550.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_block_550.py @@ -1,176 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_block_550", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { "username": "hebingning", "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_block_551.py b/tests/security/sec_deny_srcip_mail_pre_subject_block_551.py index 95c0d3084..b09c1d0e3 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_block_551.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_block_551.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_block_551", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_block_tamper.py b/tests/security/sec_deny_srcip_mail_pre_subject_block_tamper.py index cd307bb02..ad5377394 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_block_tamper.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_block_tamper.py @@ -1,177 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_block_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_drop.py b/tests/security/sec_deny_srcip_mail_pre_subject_drop.py index ce0aa1526..6be502a10 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_drop.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_drop.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_rate_high.py b/tests/security/sec_deny_srcip_mail_pre_subject_rate_high.py index b34f2a223..193f3039b 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_rate_high.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_rate_low.py b/tests/security/sec_deny_srcip_mail_pre_subject_rate_low.py index f718e3d10..d6374e75d 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_rate_low.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_subject_rst.py b/tests/security/sec_deny_srcip_mail_pre_subject_rst.py index 3bb29be54..88c70cae3 100644 --- a/tests/security/sec_deny_srcip_mail_pre_subject_rst.py +++ b/tests/security/sec_deny_srcip_mail_pre_subject_rst.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_subject_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["Best*"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_to_drop.py b/tests/security/sec_deny_srcip_mail_pre_to_drop.py index 44d8c64b6..c161c5b53 100644 --- a/tests/security/sec_deny_srcip_mail_pre_to_drop.py +++ b/tests/security/sec_deny_srcip_mail_pre_to_drop.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_to_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "auto*" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_to_rate_high.py b/tests/security/sec_deny_srcip_mail_pre_to_rate_high.py index d601e11e6..d866d60d3 100644 --- a/tests/security/sec_deny_srcip_mail_pre_to_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_pre_to_rate_high.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_to_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "auto*" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_to_rate_low.py b/tests/security/sec_deny_srcip_mail_pre_to_rate_low.py index bc063ff8e..95b41c074 100644 --- a/tests/security/sec_deny_srcip_mail_pre_to_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_pre_to_rate_low.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_to_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "auto*" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_to_rst.py b/tests/security/sec_deny_srcip_mail_pre_to_rst.py index 4b825475b..f965e21ac 100644 --- a/tests/security/sec_deny_srcip_mail_pre_to_rst.py +++ b/tests/security/sec_deny_srcip_mail_pre_to_rst.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_to_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "auto*" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_pre_to_tamper.py b/tests/security/sec_deny_srcip_mail_pre_to_tamper.py index a3cc5273f..d650a2a10 100644 --- a/tests/security/sec_deny_srcip_mail_pre_to_tamper.py +++ b/tests/security/sec_deny_srcip_mail_pre_to_tamper.py @@ -1,173 +1,221 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_pre_to_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "auto*" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_550.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_550.py new file mode 100644 index 000000000..d4a0fc42a --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_551.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_551.py new file mode 100644 index 000000000..b434f2bb9 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop.py new file mode 100644 index 000000000..72b3e2605 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop_rst.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop_rst.py new file mode 100644 index 000000000..54f638919 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_550.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_550.py deleted file mode 100644 index b2af8be97..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_551.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_551.py deleted file mode 100644 index 4aae62819..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_drop.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_drop.py deleted file mode 100644 index 416678db2..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_high.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_high.py index 0ac160ff2..016ced7dc 100644 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_high.py @@ -1,181 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_low.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_low.py index 69861cb86..01f6de1bb 100644 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_rst.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_rst.py deleted file mode 100644 index b0a1afeff..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_tamper.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_tamper.py deleted file mode 100644 index b6438c345..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_content_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_attachment_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_content", - "item_value": "TEXT", - "value": [ - "Subject" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_content_tamper.py b/tests/security/sec_deny_srcip_mail_sub_attachment_content_tamper.py new file mode 100644 index 000000000..4654fe0fc --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_content_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_att_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Subject" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_550.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_550.py new file mode 100644 index 000000000..5b5aa23d4 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_551.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_551.py new file mode 100644 index 000000000..74fad8c38 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop.py new file mode 100644 index 000000000..4866d96f4 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop.py @@ -0,0 +1,673 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop_rst.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop_rst.py new file mode 100644 index 000000000..ea4b83a01 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_550.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_550.py deleted file mode 100644 index e9d12af7e..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_551.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_551.py deleted file mode 100644 index 62aa28f18..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_drop.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_drop.py deleted file mode 100644 index 5a2090371..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_high.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_high.py index b294e7e29..d0cc55021 100644 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_high.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_low.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_low.py index dcc560755..d0cc55021 100644 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_rst.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_rst.py deleted file mode 100644 index 9c9924692..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_tamper.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_tamper.py deleted file mode 100644 index b294e7e29..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_attachment_name_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "/support/packet_generator/mail_file/" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_attachment_name_tamper.py b/tests/security/sec_deny_srcip_mail_sub_attachment_name_tamper.py new file mode 100644 index 000000000..f536264ac --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_attachment_name_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "support" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_block_550.py b/tests/security/sec_deny_srcip_mail_sub_content_block_550.py new file mode 100644 index 000000000..2f03c3194 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_content_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_block_551.py b/tests/security/sec_deny_srcip_mail_sub_content_block_551.py new file mode 100644 index 000000000..68c13af6f --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_content_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_drop.py b/tests/security/sec_deny_srcip_mail_sub_content_drop.py new file mode 100644 index 000000000..f6e802216 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_content_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_drop_rst.py b/tests/security/sec_deny_srcip_mail_sub_content_drop_rst.py new file mode 100644 index 000000000..f18b7cac0 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_content_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_block_550.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_block_550.py deleted file mode 100644 index 5a96305f5..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_block_551.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_block_551.py deleted file mode 100644 index 6242165cf..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_drop.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_drop.py deleted file mode 100644 index fd3f39c0d..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_high.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_high.py index e1422a264..68e3b7eb0 100644 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_sub_content_rate_high.py @@ -1,181 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_low.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_low.py index 56d6ccce1..8865e41e6 100644 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_sub_content_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_rst.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_rst.py deleted file mode 100644 index 47e2afbf9..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_rate_tamper.py b/tests/security/sec_deny_srcip_mail_sub_content_rate_tamper.py deleted file mode 100644 index e1422a264..000000000 --- a/tests/security/sec_deny_srcip_mail_sub_content_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "content" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_sub_content_tamper.py b/tests/security/sec_deny_srcip_mail_sub_content_tamper.py new file mode 100644 index 000000000..ccea270ce --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_sub_content_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "content" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_block_550.py b/tests/security/sec_deny_srcip_mail_substr_subject_block_550.py index 45d34014f..3de110817 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_block_550.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_block_550.py @@ -1,176 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_block_550", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { "username": "hebingning", "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_block_551.py b/tests/security/sec_deny_srcip_mail_substr_subject_block_551.py index c6269ce0f..5ca8634a7 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_block_551.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_block_551.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_block_551", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_block_tamper.py b/tests/security/sec_deny_srcip_mail_substr_subject_block_tamper.py index 84623e0fa..ac4d9322f 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_block_tamper.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_block_tamper.py @@ -1,177 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_block_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_drop.py b/tests/security/sec_deny_srcip_mail_substr_subject_drop.py index 8a024e6c6..eaa974baa 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_drop.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_drop.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_rate_high.py b/tests/security/sec_deny_srcip_mail_substr_subject_rate_high.py index 7a9d02184..55ebb9d98 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_rate_high.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_rate_low.py b/tests/security/sec_deny_srcip_mail_substr_subject_rate_low.py index fe987f77c..f01302fe9 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_rate_low.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_subject_rst.py b/tests/security/sec_deny_srcip_mail_substr_subject_rst.py index 3f703020a..e76637961 100644 --- a/tests/security/sec_deny_srcip_mail_substr_subject_rst.py +++ b/tests/security/sec_deny_srcip_mail_substr_subject_rst.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_subject_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["man"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "Best" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_to_drop.py b/tests/security/sec_deny_srcip_mail_substr_to_drop.py index d4d76ecd7..d51f8c61b 100644 --- a/tests/security/sec_deny_srcip_mail_substr_to_drop.py +++ b/tests/security/sec_deny_srcip_mail_substr_to_drop.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_to_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "autotest" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_to_rate_high.py b/tests/security/sec_deny_srcip_mail_substr_to_rate_high.py index fd73084d2..dc63cb57b 100644 --- a/tests/security/sec_deny_srcip_mail_substr_to_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_substr_to_rate_high.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_to_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "autotest" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/root/aaa.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { "username": "hebingning", "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "test_pc_ip": "192.168.64.65", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_to_rate_low.py b/tests/security/sec_deny_srcip_mail_substr_to_rate_low.py index fa35a6675..af4a2286a 100644 --- a/tests/security/sec_deny_srcip_mail_substr_to_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_substr_to_rate_low.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_to_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "autotest" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_to_rst.py b/tests/security/sec_deny_srcip_mail_substr_to_rst.py index fc0461c09..58784830f 100644 --- a/tests/security/sec_deny_srcip_mail_substr_to_rst.py +++ b/tests/security/sec_deny_srcip_mail_substr_to_rst.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_to_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "autotest" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_substr_to_tamper.py b/tests/security/sec_deny_srcip_mail_substr_to_tamper.py index 0271da766..55f5527ed 100644 --- a/tests/security/sec_deny_srcip_mail_substr_to_tamper.py +++ b/tests/security/sec_deny_srcip_mail_substr_to_tamper.py @@ -1,173 +1,221 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_substr_to_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "autotest" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "autotest@" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_550.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_550.py new file mode 100644 index 000000000..9784947f8 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_551.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_551.py new file mode 100644 index 000000000..aa2d2f74c --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop.py new file mode 100644 index 000000000..4cd1e387b --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop_rst.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop_rst.py new file mode 100644 index 000000000..87c71db8c --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_550.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_550.py deleted file mode 100644 index c8682df82..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_551.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_551.py deleted file mode 100644 index 6b1ab082a..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_drop.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_drop.py deleted file mode 100644 index d75ca03fd..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_high.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_high.py index 99a3e95b9..644c87be0 100644 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_high.py @@ -1,181 +1,673 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir + parameter = { + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_low.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_low.py index 0413c3350..0d42bc0db 100644 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_rst.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_rst.py deleted file mode 100644 index c7c512659..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_tamper.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_tamper.py deleted file mode 100644 index 99a3e95b9..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_attachment_name_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "*/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_attachment_name_tamper.py b/tests/security/sec_deny_srcip_mail_suff_attachment_name_tamper.py new file mode 100644 index 000000000..685909ca9 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_attachment_name_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_block_550.py b/tests/security/sec_deny_srcip_mail_suff_content_block_550.py new file mode 100644 index 000000000..8b1d8623b --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_content_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_block_551.py b/tests/security/sec_deny_srcip_mail_suff_content_block_551.py new file mode 100644 index 000000000..d1a7e35c5 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_content_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_drop.py b/tests/security/sec_deny_srcip_mail_suff_content_drop.py new file mode 100644 index 000000000..af45bcdbf --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_content_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_drop_rst.py b/tests/security/sec_deny_srcip_mail_suff_content_drop_rst.py new file mode 100644 index 000000000..8406767f2 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_content_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_block_550.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_block_550.py deleted file mode 100644 index cdd96aceb..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_block_551.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_block_551.py deleted file mode 100644 index 9f980fef3..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_drop.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_drop.py deleted file mode 100644 index d2fd80fff..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_high.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_high.py index 5ca5e408d..66270b406 100644 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_suff_content_rate_high.py @@ -1,181 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_low.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_low.py index 34328dcd4..94b9a8b84 100644 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_suff_content_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_rst.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_rst.py deleted file mode 100644 index 310bae793..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_rate_tamper.py b/tests/security/sec_deny_srcip_mail_suff_content_rate_tamper.py deleted file mode 100644 index 5ca5e408d..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_content_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "*tent123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_content_tamper.py b/tests/security/sec_deny_srcip_mail_suff_content_tamper.py new file mode 100644 index 000000000..60333e9ac --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_content_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "ntent123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_block_550.py b/tests/security/sec_deny_srcip_mail_suff_subject_block_550.py index 422859cff..9836fe71e 100644 --- a/tests/security/sec_deny_srcip_mail_suff_subject_block_550.py +++ b/tests/security/sec_deny_srcip_mail_suff_subject_block_550.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_block_550", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { "username": "hebingning", "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_block_551.py b/tests/security/sec_deny_srcip_mail_suff_subject_block_551.py index 6a47bcb32..bb9d23817 100644 --- a/tests/security/sec_deny_srcip_mail_suff_subject_block_551.py +++ b/tests/security/sec_deny_srcip_mail_suff_subject_block_551.py @@ -1,177 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_block_551", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_block_tamper.py b/tests/security/sec_deny_srcip_mail_suff_subject_block_tamper.py deleted file mode 100644 index 05f919811..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_subject_block_tamper.py +++ /dev/null @@ -1,177 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_block_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_drop.py b/tests/security/sec_deny_srcip_mail_suff_subject_drop.py index 9db06d36b..c7b3d0612 100644 --- a/tests/security/sec_deny_srcip_mail_suff_subject_drop.py +++ b/tests/security/sec_deny_srcip_mail_suff_subject_drop.py @@ -1,179 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_drop_rst.py b/tests/security/sec_deny_srcip_mail_suff_subject_drop_rst.py new file mode 100644 index 000000000..7e2c59ec4 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_subject_drop_rst.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_rate_high.py b/tests/security/sec_deny_srcip_mail_suff_subject_rate_high.py index 2d7ee3baa..2a0966058 100644 --- a/tests/security/sec_deny_srcip_mail_suff_subject_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_suff_subject_rate_high.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_rate_low.py b/tests/security/sec_deny_srcip_mail_suff_subject_rate_low.py index 422859cff..595e59c92 100644 --- a/tests/security/sec_deny_srcip_mail_suff_subject_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_suff_subject_rate_low.py @@ -1,177 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_subject_block_550", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_subject", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "mail_subject", - "item_value": "TEXT", - "value": ["*tman"] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_subject", "query_value": "Bestman"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_rst.py b/tests/security/sec_deny_srcip_mail_suff_subject_rst.py deleted file mode 100644 index 5d75a183e..000000000 --- a/tests/security/sec_deny_srcip_mail_suff_subject_rst.py +++ /dev/null @@ -1,141 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "allow", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [], - "server_fqdn": [], - "protocol_filed": [], - "ip_protocol":[ - "UDP" - ], - "sub_action_override": True, - "sub_action": [], - "packet_capture": [] - }, - "profile": [], - "expected_return": "facebook", - "counters": {"hits": 2}, - "log_query_param": [{"query_field_key":"ip_protocol", "query_value": "udp"}], - "traffic": { - "protocol": "dns", - "type": "nslookup", - "command": "nslookup www.facebook.com -timeout=1 8.8.8.8" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_subject_tamper.py b/tests/security/sec_deny_srcip_mail_suff_subject_tamper.py new file mode 100644 index 000000000..8b1b7cd9a --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_suff_subject_tamper.py @@ -0,0 +1,222 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_to_drop.py b/tests/security/sec_deny_srcip_mail_suff_to_drop.py index 363ccb5c4..5edf9077b 100644 --- a/tests/security/sec_deny_srcip_mail_suff_to_drop.py +++ b/tests/security/sec_deny_srcip_mail_suff_to_drop.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_to_drop", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "*@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "t@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_to_rate_high.py b/tests/security/sec_deny_srcip_mail_suff_to_rate_high.py index 936bd7130..4976473f3 100644 --- a/tests/security/sec_deny_srcip_mail_suff_to_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_suff_to_rate_high.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_src_mail_suff_to_rate_high", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "*@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "t@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_to_rate_low.py b/tests/security/sec_deny_srcip_mail_suff_to_rate_low.py index df5b7653d..495ff0a9b 100644 --- a/tests/security/sec_deny_srcip_mail_suff_to_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_suff_to_rate_low.py @@ -1,174 +1,223 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_to_rate_low", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "*@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "t@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_to_rst.py b/tests/security/sec_deny_srcip_mail_suff_to_rst.py index 317077039..ff48beb70 100644 --- a/tests/security/sec_deny_srcip_mail_suff_to_rst.py +++ b/tests/security/sec_deny_srcip_mail_suff_to_rst.py @@ -1,176 +1,222 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_to_rst", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "*@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": True, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "t@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 }, - "profile": [], - "expected_return": "reset", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_suff_to_tamper.py b/tests/security/sec_deny_srcip_mail_suff_to_tamper.py index 817782459..a13c39c60 100644 --- a/tests/security/sec_deny_srcip_mail_suff_to_tamper.py +++ b/tests/security/sec_deny_srcip_mail_suff_to_tamper.py @@ -1,173 +1,221 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": "sec_deny_srcip_mail_suff_to_tamper", - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "sec_mail_to", - "object_type": "account", - "item_operation": "add", - "select_type": False, - "negate": False, - "items": [ - { - "item_operation": "add", - "item_type": "to", - "item_value": "*@163.com" - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_TO", + "type": "account", + "name": "sec_mail_to", + "member_type": "item", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "t@163.com$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [{"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"}], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_account", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_to_cmd", "query_value": "autotest@163.com"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 5, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_550.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_550.py new file mode 100644 index 000000000..4fad9cef7 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_551.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_551.py new file mode 100644 index 000000000..ce36effc5 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop.py new file mode 100644 index 000000000..9136bc1d4 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop_rst.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop_rst.py new file mode 100644 index 000000000..f3b629198 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_550.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_550.py deleted file mode 100644 index dbdf98707..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_551.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_551.py deleted file mode 100644 index 39b39b419..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_drop.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_drop.py deleted file mode 100644 index 6018d8bfa..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_high.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_high.py index feaf74d96..604a943b7 100644 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_high.py @@ -1,181 +1,673 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir + parameter = { + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) + +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_SUBJECT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_subject", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "tman$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_low.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_low.py index 8c0fc1c6f..eacdd820a 100644 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_rst.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_rst.py deleted file mode 100644 index e48ee95ad..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_tamper.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_tamper.py deleted file mode 100644 index feaf74d96..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_attachment_name_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "monitor_mail_attachment_name", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "attachment_name", - "item_value": "TEXT", - "value": [ - "$/app/support/packet_generator/mail_file/subjectEnglish.txt" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_attachment_name_tamper.py b/tests/security/sec_deny_srcip_mail_xly_attachment_name_tamper.py new file mode 100644 index 000000000..bbd57219e --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_attachment_name_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_ATT_NAME", + "type": "keyword", + "member_type": "item", + "name": "monitor_mail_att_name", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^/app/support/packet_generator/mail_file/subjectEnglish.txt$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_block_550.py b/tests/security/sec_deny_srcip_mail_xly_content_block_550.py new file mode 100644 index 000000000..2728c9d82 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_content_block_550.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 550, + "packet_capture": { + "enable": 0 + }, + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "550", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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": "hebingning", + "password": "hbn66AAA", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_block_551.py b/tests/security/sec_deny_srcip_mail_xly_content_block_551.py new file mode 100644 index 000000000..28a1c03ce --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_content_block_551.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "block", + "code": 551, + "packet_capture": { + "enable": 0 + }, + + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "551", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_drop.py b/tests/security/sec_deny_srcip_mail_xly_content_drop.py new file mode 100644 index 000000000..39a31c530 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_content_drop.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_drop_rst.py b/tests/security/sec_deny_srcip_mail_xly_content_drop_rst.py new file mode 100644 index 000000000..9294b70ec --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_content_drop_rst.py @@ -0,0 +1,224 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "drop", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 1, + "send_icmp_unreachable": 0, + "after_n_packets": 0 + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "reset", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_block_550.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_block_550.py deleted file mode 100644 index d11b3d0eb..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_block_550.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 550 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "550", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_block_551.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_block_551.py deleted file mode 100644 index ab2031d73..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_block_551.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "block", - "code": 551 - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "551", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_drop.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_drop.py deleted file mode 100644 index 9decd1328..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_drop.py +++ /dev/null @@ -1,183 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "drop", - "drop_packet": 0, - "send_tcp_rst": False, - "send_icmp_unreachable": False - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_high.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_high.py index aed0f16ea..6224d215b 100644 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_high.py +++ b/tests/security/sec_deny_srcip_mail_xly_content_rate_high.py @@ -1,181 +1,224 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10000000, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0, }, - "token": "" + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "Email sent successfully", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"} + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_low.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_low.py index b771983a1..66687a0fa 100644 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_low.py +++ b/tests/security/sec_deny_srcip_mail_xly_content_rate_low.py @@ -1,181 +1,225 @@ # -*- coding: UTF-8 -*- -import time import os import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz from datetime import datetime +from support.ui_utils.workpath import workdir +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 -from support.common_utils.create_policy import CreatePolicy 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) + # 参数初始化 - exception_result = "" - result = {} + result, exception_result = "", "" + test_summary = {} # 脚本启动时间 script_start_time = time.time() - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "0.1", - } - ], - "packet_capture": [] + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "rate_limit", + "bps": 10, + "limitUnit": "Kbps", + "packet_capture": { + "enable": 0 + }, + "send_tcp_reset": 0, + "send_icmp_unreachable": 0 }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" + ] } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - return result + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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) - 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 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", + "username": "zhaokun", + "password": "zhaokun1", + "test_pc_ip": "192.168.64.87", + "test_subcriber_id": "test6776", "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, + "initiation_method": "api", "env": "tsgx", - "vsys_id": 1, + "vsys": 1, "root_path": workdir, - "path": workdir + "/tests/api", + "path": workdir + "/tests", "module_name": "security", "test_case_name": os.path.basename(__file__)[:-3] } - parameter = replace_paras(parameter) run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_rst.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_rst.py deleted file mode 100644 index 97c2a51d7..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_rst.py +++ /dev/null @@ -1,180 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "tamper" - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "timed out", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_rate_tamper.py b/tests/security/sec_deny_srcip_mail_xly_content_rate_tamper.py deleted file mode 100644 index aed0f16ea..000000000 --- a/tests/security/sec_deny_srcip_mail_xly_content_rate_tamper.py +++ /dev/null @@ -1,181 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys -sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))) -from datetime import datetime -from support.report_update import ReportUpdate -from support.common_utils.create_policy import CreatePolicy - -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - script_start_time = time.time() - - # 测试数据 - test_data = { - "is_multi_priority": False, - "rule_num": 1, - "policy_type": "security", - "rule_name": os.path.splitext(os.path.basename(__file__))[0], - "rule_action": "deny", - "rule_type": "create", - "condition": { - "source_ip": [ - { - "name": "sec_srcip", - "object_type": "ip", - "select_type": False, - "negate": False, - "item": [ - { - "item_operation": "add", - "item_type": "ipv4", - "item_value": parameter['test_pc_ip'], - } - ] - } - ], - "source_port": [], - "destination_ip": [], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - { - "name": "mail", # - "object_type": "application", - "negate": False - } - ], - "server_fqdn": [], - "protocol_filed": [ - { - "name": "mail_content", - "object_type": "keywords", - "item_operation": "add", - "select_type": False, - "negate": False, - "plus": False, - "Statistics": "None", - "item": [ - { - "item_operation": "add", - "item_type": "content", - "item_value": "TEXT", - "value": [ - "$content123" - ] - } - ], - } - ], - "sub_action_override": True, - "sub_action": [ - { - "type": "rate_limit", - "rate_value": "1000000", - } - ], - "packet_capture": [] - }, - "profile": [], - "expected_return": "Email sent successfully", - "counters": {"hits": 1}, - "log_query_param": [ - {"query_field_key":"decoded_as", "query_value": "MAIL"}, - {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} - ], - "traffic": { - "protocol": "mail", - "type": "client", # client/curl - "mail_type": "smtp", # gmail or smtp or smtp_ssl - "mail_server": "192.168.40.206", # gmail: smtp.gmail.com - "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 - "mail_timeout": 20, - "sender": "hbn@163.com", - "password": "111111", - "receiver": "autotest@163.com", - "subject": "Bestman", - "body": "content123", - "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" - }, - "token": "" - } - # 测试用例实例化 - create = CreatePolicy(test_data, parameter) - result = create.create_policy() - - return result - 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) - return "Error: " + str(e) - finally: - # 清理环境并删除配置 - if isinstance(create, CreatePolicy): - create.clean_up() - # 统计脚本用时 - 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__': - # ui - # parameter = { - # "username": "hebingning", - # "password": "hbn66AAA", - # "test_pc_ip": "192.168.64.65", - # "test_subcriber_id": "test6776", - # "api_server": "http://192.168.44.72", - # "debug_flag": "local", - # "script_type": "ui", - # "env": "tsgx", - # "vsys_id": 1, - # "is_log": 1, - # "root_path": "D:/Document/Project-TSG/Code/git/tsg_test", - # "path": "D:/Document/Project-TSG/Code/git/tsg_test/tests/ui", - # "module_name": "security", - # "test_case_name": "deny_srcip_fqdn_drop_rst_icmp" - # } - # run(parameter) - # api - from support.ui_utils.element_position.map_element_position_library import replace_paras - from support.ui_utils.workpath import workdir - - parameter = { - "username": "hebingning", - "password": "hbn66AAA", - "test_pc_ip": "192.168.64.93", - "test_subcriber_id": "test6491", - "api_server": "http://192.168.44.72", - "debug_flag": "local", - "script_type": "api", # api ui 空字符串 - "is_log": 1, - "env": "tsgx", - "vsys_id": 1, - "root_path": workdir, - "path": workdir + "/tests/api", - "module_name": "security", - "test_case_name": os.path.basename(__file__)[:-3] - } - parameter = replace_paras(parameter) - run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_mail_xly_content_tamper.py b/tests/security/sec_deny_srcip_mail_xly_content_tamper.py new file mode 100644 index 000000000..891629282 --- /dev/null +++ b/tests/security/sec_deny_srcip_mail_xly_content_tamper.py @@ -0,0 +1,223 @@ +# -*- coding: UTF-8 -*- +import os +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +import time +import pytz +from datetime import datetime +from support.ui_utils.workpath import workdir +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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + policy_configuration = { + "name": os.path.splitext(os.path.basename(__file__))[0], + "type": "security", + "action": "deny", + "and_conditions": [ + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_SOURCE_IP", + "type": "ip", + "sub_type": "ip", + "name": "sec_srcip", + "items": [ + { + "op": "add", + "ip": parameter['test_pc_ip'], + "interval": "0-65535" + } + ] + } + ] + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["mail"] + } + ], + }, + { + "negate_option": False, + "or_conditions": [ + { + "attribute_name": "ATTR_MAIL_CONTENT", + "type": "keyword", + "member_type": "item", + "name": "sec_mail_content", + "items": [ + { + "op": "add", + "expr_type": "and", + "expression": "^content123$" + } + ] + } + ] + } + ], + "action_parameter": { + "sub_action": "tamper", + "packet_capture": { + "enable": 0 + }, + "tamper_mode": "complete", + "send_icmp_unreachable": 0, + }, + "is_enabled": 1, + "log_option": "metadata", + } + + traffic_generation = { + "tool": "mail", # or trex/http + "type": "client", # client/curl + "mail_type": "smtp", # gmail or smtp or smtp_ssl + "mail_server": "192.168.40.206", # gmail: smtp.gmail.com + "mail_port": 25, # gmail:465(用于SSL)、587(用于启动TLS)。smtp默认:25 + "mail_timeout": 20, + "sender": "hbn@163.com", + "password": "111111", + "receiver": "autotest@163.com", + "subject": "Bestman", + "body": "content123", + "attach": "/app/support/packet_generator/mail_file/subjectEnglish.txt" + } + + verification_result = { + "excepted_traffic_result": "timed out", + "expected_metric": {"hits": 1}, + "expected_log": [ + {"query_field_key":"server_ip", "query_value": "192.168.40.206"}, + {"query_field_key":"decoded_as", "query_value": "MAIL"}, + {"query_field_key":"mail_from_cmd", "query_value": "hbn@163.com"}, + {"query_field_key":"mail_subject", "query_value": "Bestman"}, + {"query_field_key": "mail_attachment_name", "query_value": "/app/support/packet_generator/mail_file/subjectEnglish.txt"} + ] + } + + # 创建 + if parameter["initiation_method"] == "ui": + ui_client = UIClient() + objects_tuple, ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.create_rules(policy_configuration, objects_tuple) + if len(ui_error) > 0: + return ui_error + elif parameter["initiation_method"] == "api": + api_client = APIClient(parameter) + # {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 + rules_tuple, api_error = api_client.create_rules(policy_configuration, objects_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 + + 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], "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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 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.87", + "test_subcriber_id": "test6776", + "api_server": "http://192.168.44.72", + "initiation_method": "api", + "env": "tsgx", + "vsys": 1, + "root_path": workdir, + "path": workdir + "/tests", + "module_name": "security", + "test_case_name": os.path.basename(__file__)[:-3] + } + run(parameter) \ No newline at end of file diff --git a/tests/security/sec_deny_srcip_negate_substr_fqdn_http_alert_200_responsetext_hit_no.py b/tests/security/sec_deny_srcip_negate_substr_fqdn_http_alert_200_responsetext_hit_no.py index 1032ffc69..2408c046d 100644 --- a/tests/security/sec_deny_srcip_negate_substr_fqdn_http_alert_200_responsetext_hit_no.py +++ b/tests/security/sec_deny_srcip_negate_substr_fqdn_http_alert_200_responsetext_hit_no.py @@ -93,7 +93,7 @@ def run(parameter): verification_result = { "excepted_traffic_result": "POST", - "expected_metric": {}, + "expected_metric": {"hits": 0}, "expected_log": [] } diff --git a/tests/security/sec_deny_srcip_negate_substr_fqdn_http_substr_url_redirect_hit_yes.py b/tests/security/sec_deny_srcip_negate_substr_fqdn_http_substr_url_redirect_hit_yes.py index df75161c6..dd0f8060b 100644 --- a/tests/security/sec_deny_srcip_negate_substr_fqdn_http_substr_url_redirect_hit_yes.py +++ b/tests/security/sec_deny_srcip_negate_substr_fqdn_http_substr_url_redirect_hit_yes.py @@ -34,7 +34,7 @@ def run(parameter): "attribute_name": "ATTR_EXTERNAL_IP", "type": "ip", "sub_type": "ip", - "name": "sec_extip", + "name": "sec_sec_ip", "items": [ { "op": "add", diff --git a/tests/security/sec_shunt_extip_suff_fqdn_ssl.py b/tests/security/sec_shunt_extip_suff_fqdn_ssl.py index 1b86bb47b..c5d45e383 100644 --- a/tests/security/sec_shunt_extip_suff_fqdn_ssl.py +++ b/tests/security/sec_shunt_extip_suff_fqdn_ssl.py @@ -22,7 +22,7 @@ def run(parameter): "is_multi_priority": False, "rule_num": 1, "policy_type": "security", - "rule_name": "sec_shunt_extip_suff_serverfqdn_ssl", + "rule_name": "sec_shunt_sec_ip_suff_serverfqdn_ssl", "rule_action": "shunt", "rule_type": "create", "condition": { -- cgit v1.2.3 From e080de627cb6814c7edad51d81283827dbf87145 Mon Sep 17 00:00:00 2001 From: yang liu Date: Thu, 28 Nov 2024 15:16:44 +0800 Subject: update sc case and log verify logic --- support/api_utils/query_rule_log.py | 17 +- ...ssl_mirror_block_disabled_vxlan_activeip_bfd.py | 4 +- ...city_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 8 +- ..._extport_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...n_negate_intport_ssl_mirror_bypass_vlan_none.py | 4 +- ...rypted_srcip_fqdn_ssl_mirror_block_vlan_none.py | 6 +- ...cip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py | 6 +- ...ip_fqdn_ssl_mirror_block_vxlan_activeip_none.py | 6 +- ...p_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py | 6 +- ..._fqdn_ssl_mirror_block_vxlan_inactiveip_none.py | 6 +- ...qdn_ssl_mirror_block_vxlan_unavailableip_bfd.py | 6 +- ...dn_ssl_mirror_block_vxlan_unavailableip_none.py | 6 +- ...ypted_srcip_fqdn_ssl_mirror_bypass_vlan_none.py | 6 +- ...ip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py | 6 +- ...p_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py | 6 +- ..._fqdn_ssl_mirror_bypass_vxlan_inactiveip_bfd.py | 4 +- ...fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py | 4 +- ...dn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py | 6 +- ...n_ssl_mirror_bypass_vxlan_unavailableip_none.py | 4 +- ...p_fqdn_ssl_mirror_redispatch_block_vlan_none.py | 6 +- ...l_mirror_redispatch_block_vxlan_activeip_bfd.py | 6 +- ..._mirror_redispatch_block_vxlan_activeip_none.py | 6 +- ...mirror_redispatch_block_vxlan_inactiveip_bfd.py | 6 +- ...irror_redispatch_block_vxlan_inactiveip_none.py | 6 +- ...ror_redispatch_block_vxlan_unavailableip_bfd.py | 6 +- ...or_redispatch_block_vxlan_unavailableip_none.py | 6 +- ...edispatch_bypass_disabled_vxlan_activeip_bfd.py | 6 +- ..._fqdn_ssl_mirror_redispatch_bypass_vlan_none.py | 6 +- ..._mirror_redispatch_bypass_vxlan_activeip_bfd.py | 4 +- ...mirror_redispatch_bypass_vxlan_activeip_none.py | 6 +- ...irror_redispatch_bypass_vxlan_inactiveip_bfd.py | 6 +- ...rror_redispatch_bypass_vxlan_inactiveip_none.py | 6 +- ...or_redispatch_bypass_vxlan_unavailableip_bfd.py | 6 +- ...r_redispatch_bypass_vxlan_unavailableip_none.py | 6 +- ..._ssl_steer_block_disabled_vxlan_activeip_bfd.py | 8 +- ...rcip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py | 8 +- ...cip_fqdn_ssl_steer_block_vxlan_activeip_none.py | 8 +- ...ip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py | 8 +- ...p_fqdn_ssl_steer_block_vxlan_inactiveip_none.py | 8 +- ...fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py | 8 +- ...qdn_ssl_steer_block_vxlan_unavailableip_none.py | 6 +- ...ssl_steer_bypass_disabled_vxlan_activeip_bfd.py | 8 +- ...cip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 8 +- ...ip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py | 8 +- ...p_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py | 8 +- ..._fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py | 8 +- ...qdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py | 8 +- ...dn_ssl_steer_bypass_vxlan_unavailableip_none.py | 8 +- ...global_bypass_diff_device_vxlan_activeip_bfd.py | 8 +- ...nearby_bypass_diff_device_vxlan_activeip_bfd.py | 8 +- ...redispatch_block_disabled_vxlan_activeip_bfd.py | 8 +- ...sl_steer_redispatch_block_vxlan_activeip_bfd.py | 8 +- ...l_steer_redispatch_block_vxlan_activeip_none.py | 8 +- ..._steer_redispatch_block_vxlan_inactiveip_bfd.py | 8 +- ...steer_redispatch_block_vxlan_inactiveip_none.py | 8 +- ...eer_redispatch_block_vxlan_unavailableip_bfd.py | 8 +- ...er_redispatch_block_vxlan_unavailableip_none.py | 8 +- ...edispatch_bypass_disabled_vxlan_activeip_bfd.py | 8 +- ...l_steer_redispatch_bypass_vxlan_activeip_bfd.py | 8 +- ..._steer_redispatch_bypass_vxlan_activeip_none.py | 8 +- ...steer_redispatch_bypass_vxlan_inactiveip_bfd.py | 8 +- ...teer_redispatch_bypass_vxlan_inactiveip_none.py | 6 +- ...er_redispatch_bypass_vxlan_unavailableip_bfd.py | 8 +- ...r_redispatch_bypass_vxlan_unavailableip_none.py | 8 +- ...risk_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ..._asn_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...city_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...ntry_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...vice_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...reat_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...tion_library_fqdn_ssl_mirror_block_vlan_none.py | 4 +- ...negate_imei_fqdn_ssl_mirror_bypass_vlan_none.py | 4 +- ...mei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...negate_imsi_fqdn_ssl_mirror_bypass_vlan_none.py | 6 +- ...msi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 4 +- ...p_negate_pn_fqdn_ssl_mirror_bypass_vlan_none.py | 4 +- ..._pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 4 +- ...negate_srcport_range_mirror_bypass_vlan_none.py | 4 +- ...rcport_range_steer_bypass_vxlan_activeip_bfd.py | 4 +- ...egate_subid_fqdn_ssl_mirror_bypass_vlan_none.py | 4 +- ...bid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...c_decrypted_srcip_ssl_mirror_block_vlan_none.py | 6 +- ...d_srcip_ssl_mirror_block_vxlan_activeip_none.py | 6 +- ...d_srcip_ssl_mirror_bypass_disabled_vlan_none.py | 6 +- ..._decrypted_srcip_ssl_mirror_bypass_vlan_none.py | 6 +- ...l_mirror_global_bypass_diff_device_vlan_none.py | 6 +- ...l_mirror_nearby_bypass_diff_device_vlan_none.py | 6 +- ...redispatch_block_disabled_vxlan_activeip_bfd.py | 6 +- ...l_mirror_redispatch_block_vxlan_activeip_bfd.py | 6 +- ...mirror_redispatch_block_vxlan_inactiveip_bfd.py | 6 +- ...irror_redispatch_block_vxlan_inactiveip_none.py | 6 +- ...srcip_ssl_mirror_redispatch_bypass_vlan_none.py | 6 +- ...srcip_ssl_steer_bypass_vxlan_inactiveip_none.py | 6 +- ...l_steer_redispatch_bypass_vxlan_activeip_bfd.py | 8 +- ...er_redispatch_bypass_vxlan_unavailableip_bfd.py | 6 +- ...r_redispatch_bypass_vxlan_unavailableip_none.py | 6 +- ...srcip_subid_fqdn_ssl_mirror_bypass_vlan_none.py | 4 +- ...bid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ..._subid_imsi_fqdn_ssl_mirror_bypass_vlan_none.py | 6 +- ...msi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...ubid_pn_apn_fqdn_ssl_mirror_bypass_vlan_none.py | 6 +- ...apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...ip_subid_pn_fqdn_ssl_mirror_bypass_vlan_none.py | 4 +- ..._pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 6 +- ..._dstip_dstport_range_mirror_bypass_vlan_none.py | 14 +- ...stport_range_steer_bypass_vxlan_activeip_bfd.py | 16 +- ..._external_port_range_mirror_bypass_vlan_none.py | 16 +- ...l_port_range_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...w_imsi_fqdn_http_url_mirror_bypass_vlan_none.py | 15 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 15 +- ...ultiple_port_steer_bypass_vxlan_activeip_bfd.py | 13 +- ..._negate_apn_fqdn_ssl_mirror_bypass_vlan_none.py | 8 +- ...apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 8 +- ...e_imei_fqdn_http_url_mirror_bypass_vlan_none.py | 13 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 12 +- ...e_imsi_fqdn_http_url_mirror_bypass_vlan_none.py | 15 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 13 +- ...ate_pn_fqdn_http_url_mirror_bypass_vlan_none.py | 13 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 13 +- ...srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py | 16 +- ...tip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 16 +- ..._subid_fqdn_http_url_mirror_bypass_vlan_none.py | 13 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 12 +- ...raw_pn_fqdn_http_url_mirror_bypass_vlan_none.py | 15 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 14 +- .../sc_raw_rtp_mirror_bypass_vlan_none.py | 15 +- .../sc_raw_rtp_steer_bypass_vxlan_activeip_bfd.py | 15 +- ...n_ssl_mirror_bypass_vxlan_unavailableip_none.py | 18 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 20 ++- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 6 +- ..._raw_srcip_dstip_dns_mirror_bypass_vlan_none.py | 18 +- ...rcip_dstip_dns_qname_mirror_bypass_vlan_none.py | 18 +- ...ip_dns_qname_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...ip_dstip_dns_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 20 ++- ...oto_http_url_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...ipproto_quic_steer_bypass_vxlan_activeip_bfd.py | 16 +- ..._dstip_fqdn_http_url_mirror_bypass_vlan_none.py | 17 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 21 ++- ..._ipprototcp_http_url_mirror_bypass_vlan_none.py | 14 +- ...tcp_http_url_steer_bypass_vxlan_activeip_bfd.py | 17 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...n_negate_extport_ssl_mirror_bypass_vlan_none.py | 10 +- ..._extport_ssl_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...n_negate_intport_ssl_mirror_bypass_vlan_none.py | 14 +- ..._intport_ssl_steer_bypass_vxlan_activeip_bfd.py | 10 +- ...rcip_dstip_fqdn_quic_mirror_bypass_vlan_none.py | 10 +- ...ip_fqdn_quic_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...p_fqdn_ssl_ech_false_mirror_bypass_vlan_none.py | 20 ++- ...sl_ech_false_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...ip_fqdn_ssl_ech_true_mirror_bypass_vlan_none.py | 14 +- ...ssl_ech_true_steer_bypass_vxlan_activeip_bfd.py | 16 +- ..._fqdn_ssl_esni_false_mirror_bypass_vlan_none.py | 14 +- ...l_esni_false_steer_bypass_vxlan_activeip_bfd.py | 20 ++- ...ssl_mirror_block_disabled_vxlan_activeip_bfd.py | 21 ++- ...tip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py | 17 +- ...ip_fqdn_ssl_mirror_block_vxlan_activeip_none.py | 21 ++- ...p_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py | 21 ++- ..._fqdn_ssl_mirror_block_vxlan_inactiveip_none.py | 21 ++- ...srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py | 33 +++- ...ip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py | 21 ++- ...p_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py | 17 +- ...fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py | 21 ++- ...dn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py | 23 +-- ...n_ssl_mirror_bypass_vxlan_unavailableip_none.py | 17 +- ...l_mirror_global_bypass_diff_device_vlan_none.py | 21 ++- ...l_mirror_nearby_bypass_diff_device_vlan_none.py | 17 +- ...redispatch_block_disabled_vxlan_activeip_bfd.py | 21 ++- ...p_fqdn_ssl_mirror_redispatch_block_vlan_none.py | 14 +- ...l_mirror_redispatch_block_vxlan_activeip_bfd.py | 21 ++- ..._mirror_redispatch_block_vxlan_activeip_none.py | 17 +- ...mirror_redispatch_block_vxlan_inactiveip_bfd.py | 17 +- ...irror_redispatch_block_vxlan_inactiveip_none.py | 17 +- ...ror_redispatch_block_vxlan_unavailableip_bfd.py | 21 ++- ...or_redispatch_block_vxlan_unavailableip_none.py | 21 ++- ...edispatch_bypass_disabled_vxlan_activeip_bfd.py | 21 ++- ..._fqdn_ssl_mirror_redispatch_bypass_vlan_none.py | 21 ++- ..._mirror_redispatch_bypass_vxlan_activeip_bfd.py | 21 ++- ...mirror_redispatch_bypass_vxlan_activeip_none.py | 21 ++- ...irror_redispatch_bypass_vxlan_inactiveip_bfd.py | 21 ++- ...rror_redispatch_bypass_vxlan_inactiveip_none.py | 21 ++- ...or_redispatch_bypass_vxlan_unavailableip_bfd.py | 17 +- ...r_redispatch_bypass_vxlan_unavailableip_none.py | 21 ++- ...qdn_ssl_no_sni_false_mirror_bypass_vlan_none.py | 10 +- ...no_sni_false_steer_bypass_vxlan_activeip_bfd.py | 10 +- ...stip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py | 17 +- ...tip_fqdn_ssl_steer_block_vxlan_activeip_none.py | 21 ++- ...ip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py | 17 +- ...p_fqdn_ssl_steer_block_vxlan_inactiveip_none.py | 25 ++- ...fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py | 21 ++- ...qdn_ssl_steer_block_vxlan_unavailableip_none.py | 29 +++- ...tip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 17 +- ...ip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py | 25 ++- ...p_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py | 21 ++- ..._fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py | 21 ++- ...qdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py | 21 ++- ...dn_ssl_steer_bypass_vxlan_unavailableip_none.py | 21 ++- ...sl_steer_redispatch_block_vxlan_activeip_bfd.py | 18 +- ...l_steer_redispatch_block_vxlan_activeip_none.py | 22 ++- ..._steer_redispatch_block_vxlan_inactiveip_bfd.py | 22 ++- ...steer_redispatch_block_vxlan_inactiveip_none.py | 22 ++- ...eer_redispatch_block_vxlan_unavailableip_bfd.py | 22 ++- ...er_redispatch_block_vxlan_unavailableip_none.py | 18 +- ...l_steer_redispatch_bypass_vxlan_activeip_bfd.py | 26 ++- ..._steer_redispatch_bypass_vxlan_activeip_none.py | 22 ++- ...steer_redispatch_bypass_vxlan_inactiveip_bfd.py | 22 ++- ...teer_redispatch_bypass_vxlan_inactiveip_none.py | 22 ++- ...er_redispatch_bypass_vxlan_unavailableip_bfd.py | 22 ++- ...r_redispatch_bypass_vxlan_unavailableip_none.py | 18 +- ...ip_dstip_ftp_account_mirror_bypass_vlan_none.py | 12 +- ..._ftp_account_steer_bypass_vxlan_activeip_bfd.py | 11 +- ..._ftp_account_steer_bypass_vxlan_activeip_bfd.py | 16 +- ...dp_dns_qname_steer_bypass_vxlan_activeip_bfd.py | 17 +- ...mail_from_to_account_mirror_bypass_vlan_none.py | 21 ++- ...m_to_account_steer_bypass_vxlan_activeip_bfd.py | 21 ++- ...srcip_dstip_mirror_block_vxlan_activeip_none.py | 21 ++- ...ip_mirror_bypass_disabled_vxlan_activeip_bfd.py | 29 +++- ...srcip_dstip_mirror_bypass_vxlan_activeip_bfd.py | 21 ++- ...rcip_dstip_mirror_bypass_vxlan_activeip_none.py | 21 ++- ..._dstip_multiple_port_mirror_bypass_vlan_none.py | 18 +- ...te_dstport_dns_qname_mirror_bypass_vlan_none.py | 14 +- ...rt_dns_qname_steer_bypass_vxlan_activeip_bfd.py | 10 +- ...stip_negate_fqdn_ssl_mirror_bypass_vlan_none.py | 10 +- ...ate_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 10 +- ..._srcip_dstip_openvpn_mirror_bypass_vlan_none.py | 17 +- ...stip_openvpn_steer_bypass_vxlan_activeip_bfd.py | 10 +- ...raw_srcip_dstip_quic_mirror_bypass_vlan_none.py | 17 +- ...p_dstip_quic_steer_bypass_vxlan_activeip_bfd.py | 25 ++- ..._raw_srcip_dstip_rdp_mirror_bypass_vlan_none.py | 21 ++- ...ip_dstip_rdp_steer_bypass_vxlan_activeip_bfd.py | 21 ++- ..._dstip_ssl_esni_true_mirror_bypass_vlan_none.py | 14 +- ...sl_esni_true_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...c_raw_srcip_dstip_ssl_mirror_block_vlan_none.py | 21 ++- ...ip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py | 14 +- ...p_dstip_ssl_mirror_block_vxlan_activeip_none.py | 21 ++- ..._dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py | 21 ++- ...dstip_ssl_mirror_block_vxlan_inactiveip_none.py | 21 ++- ...tip_ssl_mirror_block_vxlan_unavailableip_bfd.py | 17 +- ...ip_ssl_mirror_block_vxlan_unavailableip_none.py | 21 ++- ...p_dstip_ssl_mirror_bypass_disabled_vlan_none.py | 21 ++- ..._raw_srcip_dstip_ssl_mirror_bypass_vlan_none.py | 21 ++- ...stip_ssl_no_sni_true_mirror_bypass_vlan_none.py | 14 +- ..._no_sni_true_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...tip_steer_bypass_disabled_vxlan_activeip_bfd.py | 21 ++- ..._srcip_dstip_steer_bypass_vxlan_activeip_bfd.py | 17 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 26 ++- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...ary_http_url_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...xternal_port_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...sc_raw_srcip_dstport_mirror_bypass_vlan_none.py | 18 +- ..._external_port_range_mirror_bypass_vlan_none.py | 14 +- ...rcip_dstport_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...cip_dtsip_mirror_bypass_vxlan_inactiveip_bfd.py | 21 ++- ...prototcp_ssl_steer_bypass_vxlan_activeip_bfd.py | 10 +- ..._ssl_steer_block_disabled_vxlan_activeip_bfd.py | 14 +- ...ssl_steer_bypass_disabled_vxlan_activeip_bfd.py | 10 +- ...global_bypass_diff_device_vxlan_activeip_bfd.py | 14 +- ...nearby_bypass_diff_device_vxlan_activeip_bfd.py | 14 +- ..._internal_port_range_mirror_bypass_vlan_none.py | 14 +- ...l_port_range_steer_bypass_vxlan_activeip_bfd.py | 14 +- ..._raw_srcip_mirror_block_vxlan_inactiveip_bfd.py | 17 +- ...raw_srcip_mirror_block_vxlan_inactiveip_none.py | 17 +- ...w_srcip_mirror_block_vxlan_unavailableip_bfd.py | 21 ++- ..._srcip_mirror_block_vxlan_unavailableip_none.py | 20 ++- ..._srcip_mirror_bypass_vxlan_unavailableip_bfd.py | 21 ++- ...mirror_redispatch_block_vxlan_inactiveip_bfd.py | 21 ++- ...egate_dstip_fqdn_ssl_mirror_bypass_vlan_none.py | 14 +- ...tip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py | 14 +- ...negate_srcport_range_mirror_bypass_vlan_none.py | 18 +- ...rcport_range_steer_bypass_vxlan_activeip_bfd.py | 18 +- ...c_raw_srcip_steer_block_vxlan_inactiveip_bfd.py | 21 ++- ..._raw_srcip_steer_block_vxlan_inactiveip_none.py | 21 ++- ...aw_srcip_steer_block_vxlan_unavailableip_bfd.py | 21 ++- ...w_srcip_steer_block_vxlan_unavailableip_none.py | 21 ++- ..._raw_srcip_steer_bypass_vxlan_inactiveip_bfd.py | 21 ++- ...w_srcip_steer_bypass_vxlan_unavailableip_bfd.py | 21 ++- ..._srcip_steer_bypass_vxlan_unavailableip_none.py | 17 +- ..._steer_redispatch_block_vxlan_inactiveip_bfd.py | 17 +- ...steer_redispatch_bypass_vxlan_inactiveip_bfd.py | 17 +- .../sc_raw_srcport_mirror_bypass_vlan_none.py | 14 +- ...sc_raw_srcport_range_mirror_bypass_vlan_none.py | 10 +- ...rcport_range_steer_bypass_vxlan_activeip_bfd.py | 10 +- ..._raw_srcport_steer_bypass_vxlan_activeip_bfd.py | 10 +- ..._subid_fqdn_http_url_mirror_bypass_vlan_none.py | 6 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 6 +- ...ei_apn_fqdn_http_url_mirror_bypass_vlan_none.py | 10 +- ...qdn_http_url_steer_bypass_vxlan_activeip_bfd.py | 182 ++------------------- 307 files changed, 2212 insertions(+), 2076 deletions(-) diff --git a/support/api_utils/query_rule_log.py b/support/api_utils/query_rule_log.py index 8d516a00c..7f8ede0b6 100644 --- a/support/api_utils/query_rule_log.py +++ b/support/api_utils/query_rule_log.py @@ -3,6 +3,10 @@ import json import time import pytz import requests +import os +import configparser +from support.organize_config import * +from support.packet_generator.workpath import workdir from datetime import datetime from support.ui_utils.element_position.policy_element_position import * @@ -368,7 +372,18 @@ class QueryRuleLog: temp_log_result_list.append(False) elif query_field_key in {"sc_rsp_raw_uuid_list", "sc_rsp_decrypted_uuid_list"}: query_value = self.policy_configuration["action_parameter"]["sff_profiles"][0]["service_func_profiles"] - if query_value == log[query_field_key]: + conf_path = os.path.join(workdir, "configuration_file.ini") + conf = configparser.ConfigParser() + conf.read(conf_path, encoding="utf-8") + active_dst_ip_list = conf.get("sc_active_dst_ip", "ip_list") + effective_device_tag_list = ["group-xxg-tsgx", "center-xxg-tsgx"] + if log[query_field_key] == [] and self.policy_configuration["action_parameter"]["sf_configuration"][0]["connectivity"]["method"] == "vxlan_g" and self.policy_configuration["action_parameter"]["sf_configuration"][0]["connectivity"]["dest_ip"] not in active_dst_ip_list: + temp_log_result_list.append(True) + elif log[query_field_key] == [] and self.policy_configuration["action_parameter"]["sf_configuration"][0]["device_group"]["value"] not in effective_device_tag_list: + temp_log_result_list.append(True) + elif log[query_field_key] == [] and self.policy_configuration["action_parameter"]["sf_configuration"][0]["admin_status"] == 0: + temp_log_result_list.append(True) + elif query_value == log[query_field_key]: temp_log_result_list.append(True) else: temp_log_result_list.append(False) diff --git a/tests/service_chaining/sc_decrypted_scrip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_scrip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py index a1da35286..4746b378b 100644 --- a/tests/service_chaining/sc_decrypted_scrip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_scrip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py @@ -70,7 +70,7 @@ def run(parameter): { "op": "add", "expr_type": "and", - "expression": "baidu.com", + "expression": "baidu", } ] } @@ -143,7 +143,7 @@ def run(parameter): return api_error # 等待下发配置生效 - time.sleep(20) + time.sleep(30) # 类实例化 generator = TrafficGenerator() diff --git a/tests/service_chaining/sc_decrypted_srcip_ext_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ext_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py index c3a2e347f..b7f7e74d1 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ext_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ext_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py @@ -266,8 +266,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_ext_geoip_cityblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index e8e05cd02..9e2958b66 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -249,12 +249,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "sc_" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py index 90672775c..1d8f4e7b9 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -248,10 +248,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "sc_fqdn_negate_extport_ssl" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py index 9addcbfe5..69b0c1cf5 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py @@ -249,8 +249,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vlan_none.py index 3129c4d9f..d8a3cdae8 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vlan_none.py @@ -230,10 +230,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py index 796fe6bf0..7274adf02 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py @@ -225,10 +225,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_none.py index 63af299d8..3e65e8873 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_activeip_none.py @@ -228,10 +228,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_activeip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py index 098428b35..49807fb40 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py @@ -225,10 +225,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py index 23a76ac08..d62484a48 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py @@ -227,10 +227,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_inactiveip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_bfd.py index 8f0c7b6b4..ca19a5ba9 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_bfd.py @@ -228,10 +228,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_unavailableip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_none.py index 9524a56bf..ba956c873 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_block_vxlan_unavailableip_none.py @@ -227,10 +227,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnblock_vxlan_unavailableip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vlan_none.py index bc04b0a5e..3bfcbbd4d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vlan_none.py @@ -229,10 +229,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py index 37cab6a99..0a300d093 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py @@ -228,10 +228,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py index 3ea913dd7..cdf44eecf 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py @@ -227,10 +227,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_activeip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_bfd.py index 8e8642db6..50f87b6e8 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_bfd.py @@ -230,8 +230,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py index b6be95c53..f6fb863b5 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py @@ -230,8 +230,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_inactiveip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py index 627498d24..41840afda 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py @@ -228,10 +228,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_unavailableip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py index a2e40b3b6..826e6719f 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py @@ -229,8 +229,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnbypass_vxlan_unavailableip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vlan_none.py index 27f95675f..7280fb30c 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vlan_none.py @@ -232,10 +232,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py index 65eb18b80..4c8753bce 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py @@ -229,10 +229,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py index 5e2349175..8e4637c10 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py @@ -230,10 +230,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_activeip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py index 6e36934a5..88b9bc48b 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py @@ -229,10 +229,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py index 13ab87b34..3b740d7fe 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py @@ -231,10 +231,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py index b56437473..60b11514a 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py @@ -231,10 +231,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_unavailableip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py index 5a7807169..b3710dcf2 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py @@ -230,10 +230,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_block_vxlan_unavailableip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py index 4c7cc3b28..bd8193587 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py @@ -231,10 +231,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_disabled_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py index f839f9ead..766674e50 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py @@ -233,10 +233,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py index 0bab21fd0..33851fb8c 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py @@ -231,8 +231,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py index 172c1b400..a74fae618 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py @@ -230,10 +230,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_activeip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py index 02098ab22..6f0db32ad 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -232,10 +232,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py index e1c4af223..6a62e7fe4 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py @@ -231,10 +231,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_inactiveip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py index f17dd3252..90afb1353 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -229,10 +229,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_unavailableip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py index c0e3f87db..333bd3b4f 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py @@ -231,10 +231,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_fqdnredispatch_bypass_vxlan_unavailableip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py index 5816cdce9..a0fcb01ae 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd" + "test_case_name": "_steer_block_disabled_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py index 9ea76bea0..79c52fcee 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_activeip_bfd" + "test_case_name": "_steer_block_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_none.py index d538f068b..2c008bb42 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_activeip_none.py @@ -226,12 +226,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_activeip_none" + "test_case_name": "_steer_block_vxlan_activeip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py index f146b32c3..aafd95b21 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_inactiveip_bfd" + "test_case_name": "_steer_block_vxlan_inactiveip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py index 71ca63542..a54245e81 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py @@ -228,12 +228,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_inactiveip_none" + "test_case_name": "_steer_block_vxlan_inactiveip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py index c553c0ea2..5cd91feb9 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_unavailableip_bfd" + "test_case_name": "_steer_block_vxlan_unavailableip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py index 7cbd2cb62..de69d32f7 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py @@ -229,10 +229,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_block_vxlan_unavailableip_none" + "test_case_name": "_steer_block_vxlan_unavailableip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py index 928b3589a..1a8f9f979 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py @@ -225,12 +225,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd" + "test_case_name": "_steer_bypass_disabled_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 5e59708c2..6dc576ed8 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -228,12 +228,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py index e2313ef77..e01c64a52 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py @@ -226,12 +226,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_activeip_none" + "test_case_name": "_steer_bypass_vxlan_activeip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py index dad204668..5fc5d9ba9 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd" + "test_case_name": "_steer_bypass_vxlan_inactiveip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py index c2a484cd1..5d69dba90 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_inactiveip_none" + "test_case_name": "_steer_bypass_vxlan_inactiveip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py index a206b9d28..9458aaacb 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd" + "test_case_name": "_steer_bypass_vxlan_unavailableip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py index 2dc0333ca..836427c98 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py @@ -226,12 +226,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_unavailableip_none" + "test_case_name": "_steer_bypass_vxlan_unavailableip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py index 1284e80de..9b1b55a44 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py @@ -226,12 +226,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd" + "test_case_name": "_steer_global_bypass_diff_device_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py index 333362c3b..63d9bf1ac 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py @@ -227,12 +227,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd" + "test_case_name": "_steer_nearby_bypass_diff_device_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_disabled_vxlan_activeip_bfd.py index 8ac4c64fa..9f982eab6 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_disabled_vxlan_activeip_bfd.py @@ -229,12 +229,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_disabled_vxlan_activeip_bfd" + "test_case_name": "_steer_redispatch_block_disabled_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py index f6a95e7d8..5fb24d8bb 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py @@ -231,12 +231,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd" + "test_case_name": "_steer_redispatch_block_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py index 70ae74757..efea2823b 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none" + "test_case_name": "_steer_redispatch_block_vxlan_activeip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py index 749cbcbfa..9edc31088 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd" + "test_case_name": "_steer_redispatch_block_vxlan_inactiveip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py index 4f6b63f75..febc08f6f 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py @@ -229,12 +229,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none" + "test_case_name": "_steer_redispatch_block_vxlan_inactiveip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py index 4183f5b5b..c8d5cc68c 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd" + "test_case_name": "_steer_redispatch_block_vxlan_unavailableip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py index 6200e65c9..8ab04d15d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py @@ -229,12 +229,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none" + "test_case_name": "_steer_redispatch_block_vxlan_unavailableip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_disabled_vxlan_activeip_bfd.py index 23f40ed2b..e826ae94d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_disabled_vxlan_activeip_bfd.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_disabled_vxlan_activeip_bfd" + "test_case_name": "_steer_redispatch_bypass_disabled_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py index cf33e25da..ab149b86f 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py @@ -232,12 +232,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd" + "test_case_name": "_steer_redispatch_bypass_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py index 1ea3c72c9..9e2b80d07 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none" + "test_case_name": "_steer_redispatch_bypass_vxlan_activeip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py index e29ac7d68..ad281548b 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -231,12 +231,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd" + "test_case_name": "_steer_redispatch_bypass_vxlan_inactiveip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py index 493e223a0..95ea3d423 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py @@ -234,10 +234,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none" + "test_case_name": "_steer_redispatch_bypass_vxlan_inactiveip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py index 08a9abbdd..3b260db7d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -229,12 +229,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd" + "test_case_name": "_steer_redispatch_bypass_vxlan_unavailableip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py index c57529c5c..1d2afc439 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py @@ -230,12 +230,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none" + "test_case_name": "_steer_redispatch_bypass_vxlan_unavailableip_none" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_int_compliance_risk_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_compliance_risk_library_fqdn_ssl_mirror_block_vlan_none.py index 6719773f0..9df62db29 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_compliance_risk_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_compliance_risk_library_fqdn_ssl_mirror_block_vlan_none.py @@ -261,8 +261,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_compliance_riskblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_geoip_asn_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_geoip_asn_library_fqdn_ssl_mirror_block_vlan_none.py index 9cc928fc2..774b7a00e 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_geoip_asn_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_geoip_asn_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_intblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py index 9a2430d36..9237c3c84 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_geoip_city_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_geoip_cityblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_geoip_country_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_geoip_country_library_fqdn_ssl_mirror_block_vlan_none.py index 065fa03ac..ed6538c47 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_geoip_country_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_geoip_country_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_geoip_countryblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_internet_service_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_internet_service_library_fqdn_ssl_mirror_block_vlan_none.py index 272161152..f02dcc7c4 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_internet_service_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_internet_service_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_internet_serviceblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_security_threat_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_security_threat_library_fqdn_ssl_mirror_block_vlan_none.py index 78e7e6893..bda27df31 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_security_threat_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_security_threat_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_security_threatblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_int_website_classification_library_fqdn_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_int_website_classification_library_fqdn_ssl_mirror_block_vlan_none.py index 5e95684df..aa567f791 100644 --- a/tests/service_chaining/sc_decrypted_srcip_int_website_classification_library_fqdn_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_int_website_classification_library_fqdn_ssl_mirror_block_vlan_none.py @@ -260,8 +260,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_int_website_classificationblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_mirror_bypass_vlan_none.py index b24fdc774..e898822fd 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_mirror_bypass_vlan_none.py @@ -253,8 +253,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "imei_" } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 8af44fcef..840b81584 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_imei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -252,10 +252,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "imei_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_mirror_bypass_vlan_none.py index 78c399ac4..9e32e9bde 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_mirror_bypass_vlan_none.py @@ -253,10 +253,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "imsi_" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index eb24005d8..8d2070ea6 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -252,8 +252,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_mirror_bypass_vlan_none.py index c6bc58b2f..6eea8649e 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_mirror_bypass_vlan_none.py @@ -253,8 +253,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 2d834ad31..2ceeb7521 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -252,8 +252,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_mirror_bypass_vlan_none.py index e9eca5874..bbd5dec47 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_mirror_bypass_vlan_none.py @@ -223,8 +223,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py index 1348788af..694d18095 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py @@ -222,8 +222,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_mirror_bypass_vlan_none.py index 158c83dae..b1f66e978 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_mirror_bypass_vlan_none.py @@ -251,8 +251,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index e99442355..ab9ee0fb0 100644 --- a/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_negate_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -257,10 +257,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vlan_none.py index 6881d9cf9..7b9fdaca7 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vlan_none.py @@ -211,10 +211,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scblock_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vxlan_activeip_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vxlan_activeip_none.py index b8747d7c2..b41c6cba5 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_block_vxlan_activeip_none.py @@ -209,10 +209,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scblock_vxlan_activeip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_disabled_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_disabled_vlan_none.py index 0e4d3fefd..4a8431ebb 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_disabled_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_disabled_vlan_none.py @@ -208,10 +208,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scbypass_disabled_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_vlan_none.py index f902aa71e..1533b05f6 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_bypass_vlan_none.py @@ -210,10 +210,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scbypass_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_global_bypass_diff_device_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_global_bypass_diff_device_vlan_none.py index 8640c8b43..336979fd0 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_global_bypass_diff_device_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_global_bypass_diff_device_vlan_none.py @@ -210,10 +210,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scnearby_bypass_diff_device_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_nearby_bypass_diff_device_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_nearby_bypass_diff_device_vlan_none.py index 5b9fdad4b..93ff9064f 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_nearby_bypass_diff_device_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_nearby_bypass_diff_device_vlan_none.py @@ -210,10 +210,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scnearby_bypass_diff_device_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py index e96f0e4ad..2d29ea7e2 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py @@ -210,10 +210,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scredispatch_block_disabled_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py index 52d983887..f6a27210a 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py @@ -211,10 +211,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scredispatch_block_vxlan_activeip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py index 68bc34e67..ef404a3b1 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py @@ -207,10 +207,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scredispatch_block_vxlan_inactiveip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py index b98a8eaca..ffd6c812b 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py @@ -215,10 +215,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scredispatch_block_vxlan_inactiveip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_bypass_vlan_none.py index 7830c9573..2fe38aa0d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_mirror_redispatch_bypass_vlan_none.py @@ -215,10 +215,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "scredispatch_bypass_vlan_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_bypass_vxlan_inactiveip_none.py index 22ee029fa..a9e0a2692 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_bypass_vxlan_inactiveip_none.py @@ -208,10 +208,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_ssl_steer_bypass_vxlan_inactiveip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py index 081eee855..47f52e13c 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py @@ -213,12 +213,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd" + "test_case_name": "_steer_redispatch_bypass_vxlan_activeip_bfd" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py index 6a172af09..c09ad39c2 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -211,10 +211,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd" } diff --git a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py index 365e7e1ff..f85781cdb 100644 --- a/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py @@ -213,10 +213,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_ssl_steer_redispatch_bypass_vxlan_unavailableip_none" } diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_mirror_bypass_vlan_none.py index 23345259e..9fb2fcf15 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_mirror_bypass_vlan_none.py @@ -258,8 +258,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_" } diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index dd9af1238..ed79f1b01 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -249,10 +249,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_mirror_bypass_vlan_none.py index 274601078..a44957ddb 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_mirror_bypass_vlan_none.py @@ -277,10 +277,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_imsi_" + "test_case_name": "sc_" } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 4ed58e00d..abe4d7413 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -271,10 +271,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_imsi_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_mirror_bypass_vlan_none.py index 5ca8dd554..a32d2d05d 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_mirror_bypass_vlan_none.py @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_pn_apn_" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 315d7863b..f715058d7 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_pn_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -290,10 +290,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_pn_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_mirror_bypass_vlan_none.py index a6f539996..55f713a63 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_mirror_bypass_vlan_none.py @@ -277,8 +277,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": "sc_pn_" } diff --git a/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 89fbfbaf6..384714108 100644 --- a/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_subid_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -271,10 +271,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_pn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_dstip_dstport_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_dstip_dstport_range_mirror_bypass_vlan_none.py index ab9211d3a..cdc1d8231 100644 --- a/tests/service_chaining/sc_raw_dstip_dstport_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_dstip_dstport_range_mirror_bypass_vlan_none.py @@ -127,15 +127,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -145,6 +137,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_dstip_dstport_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_dstip_dstport_range_steer_bypass_vxlan_activeip_bfd.py index 8873daeb5..d8e31d8a0 100644 --- a/tests/service_chaining/sc_raw_dstip_dstport_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_dstip_dstport_range_steer_bypass_vxlan_activeip_bfd.py @@ -126,11 +126,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -140,6 +136,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -239,10 +239,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_dstip_dstport_range_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "_dstport_range" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_dstip_external_port_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_dstip_external_port_range_mirror_bypass_vlan_none.py index 2d13dfae8..42f1b92aa 100644 --- a/tests/service_chaining/sc_raw_dstip_external_port_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_dstip_external_port_range_mirror_bypass_vlan_none.py @@ -147,11 +147,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -161,6 +157,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -260,10 +260,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_dstip_external_port_range_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_dstip_external_port_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_dstip_external_port_range_steer_bypass_vxlan_activeip_bfd.py index 2f89caff1..624112e73 100644 --- a/tests/service_chaining/sc_raw_dstip_external_port_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_dstip_external_port_range_steer_bypass_vxlan_activeip_bfd.py @@ -126,11 +126,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -140,6 +136,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -239,10 +239,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_dstip_external_port_range_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_imsi_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_imsi_fqdn_http_url_mirror_bypass_vlan_none.py index ad498864d..0e066bae2 100644 --- a/tests/service_chaining/sc_raw_imsi_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_imsi_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -161,11 +160,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -271,12 +266,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_imsi_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 7e35d8dfd..8f49671e5 100644 --- a/tests/service_chaining/sc_raw_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -160,11 +159,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -270,12 +265,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_multiple_port_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_multiple_port_steer_bypass_vxlan_activeip_bfd.py index dabbff435..e9d4ae50d 100644 --- a/tests/service_chaining/sc_raw_multiple_port_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_multiple_port_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -160,11 +159,7 @@ def run(parameter): {"query_field_key": "server_port", "query_value": "443"}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -273,10 +268,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_multiple_port_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_mirror_bypass_vlan_none.py index f7e2db53d..02d573adb 100644 --- a/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -131,10 +130,7 @@ def run(parameter): "excepted_traffic_result": {'total_packets': 175, 'total_packets_sent': 45, 'total_packets_received': 130, 'total_bytes': 180025, 'total_bytes_sent': 5799, 'total_bytes_received': 174226, 'total_syn_pkt': 1}, "expected_metric": {"hits": 0}, "expected_log": [] - } - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -247,7 +243,7 @@ if __name__ == '__main__': "root_path": "C:/automation_project/tsg_test", "path": "C:/automation_project/tsg_test/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_apn_" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 118349854..cdb519d74 100644 --- a/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -129,10 +128,7 @@ def run(parameter): "excepted_traffic_result": {'total_packets': 175, 'total_packets_sent': 45, 'total_packets_received': 130, 'total_bytes': 180025, 'total_bytes_sent': 5799, 'total_bytes_received': 174226, 'total_syn_pkt': 1}, "expected_metric": {"hits": 0}, "expected_log": [] - } - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -245,7 +241,7 @@ if __name__ == '__main__': "root_path": "C:/automation_project/tsg_test", "path": "C:/automation_project/tsg_test/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_apn_fqdn_ssl_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_mirror_bypass_vlan_none.py index 25c8fbf36..3bea52157 100644 --- a/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -150,11 +149,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -262,10 +257,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_imei_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": "imei_" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 9e62fb0e1..4aca0ad0d 100644 --- a/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_imei_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -150,11 +149,6 @@ def run(parameter): "expected_metric": {"hits": 0}, "expected_log": [] } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - # 创建 if parameter["initiation_method"] == "ui": ui_client = UIClient() @@ -261,10 +255,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_imei_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "imei_" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_mirror_bypass_vlan_none.py index 6670d55ef..133954cba 100644 --- a/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -151,11 +150,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -261,12 +256,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_imsi_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 8df9f759b..a0c5ee76b 100644 --- a/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -149,11 +148,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -261,10 +256,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_imsi_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_mirror_bypass_vlan_none.py index 9c6111eb2..13ffd51b4 100644 --- a/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -150,11 +149,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -263,10 +258,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_pn_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 2d69fd85a..b6788e5b8 100644 --- a/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -149,11 +148,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -261,10 +256,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py index 6394c6cc6..0e29967eb 100644 --- a/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py @@ -153,11 +153,7 @@ def run(parameter): "excepted_traffic_result": {'total_packets': 175, 'total_packets_sent': 45, 'total_packets_received': 130, 'total_bytes': 180025, 'total_bytes_sent': 5799, 'total_bytes_received': 174226, 'total_syn_pkt': 1}, "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +265,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_srcip_dstip_" + "test_case_name": "srcip_dstip_" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index a2ccf46d7..eea81004e 100644 --- a/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -152,11 +152,7 @@ def run(parameter): "excepted_traffic_result": {'total_packets': 175, 'total_packets_sent': 45, 'total_packets_received': 130, 'total_bytes': 180025, 'total_bytes_sent': 5799, 'total_bytes_received': 174226, 'total_syn_pkt': 1}, "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -166,6 +162,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -264,10 +264,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_mirror_bypass_vlan_none.py index 40abd4e13..540819122 100644 --- a/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -148,11 +147,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -260,10 +255,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 738a3cb2e..a8b9479bf 100644 --- a/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_negate_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -147,11 +147,7 @@ def run(parameter): "excepted_traffic_result": "", "expected_metric": {"hits": 0}, "expected_log": [] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -259,10 +255,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_negate_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_pn_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_pn_fqdn_http_url_mirror_bypass_vlan_none.py index 14956322e..d63bcb952 100644 --- a/tests/service_chaining/sc_raw_pn_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_pn_fqdn_http_url_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -161,11 +160,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -271,12 +266,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_pn_fqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 52bd0b8eb..0409c32a6 100644 --- a/tests/service_chaining/sc_raw_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -161,10 +160,7 @@ def run(parameter): {"query_field_key": "phone_number", "query_value": "12345678902"} ] } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # 创建 if parameter["initiation_method"] == "ui": @@ -270,12 +266,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_pn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_rtp_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_rtp_mirror_bypass_vlan_none.py index 3ca5f56f2..f629e5ff3 100644 --- a/tests/service_chaining/sc_raw_rtp_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_rtp_mirror_bypass_vlan_none.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -97,11 +96,7 @@ def run(parameter): "expected_log": [ {"query_field_key": "sc_rsp_raw_uuid_list", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -207,12 +202,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_rtp_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_rtp_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_rtp_steer_bypass_vxlan_activeip_bfd.py index 65e7bc358..4238506d7 100644 --- a/tests/service_chaining/sc_raw_rtp_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_rtp_steer_bypass_vxlan_activeip_bfd.py @@ -8,7 +8,6 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate def run(parameter): @@ -96,11 +95,7 @@ def run(parameter): "expected_log": [ {"query_field_key": "sc_rsp_raw_uuid_list", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -206,12 +201,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_rtp_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_scrip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_scrip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py index b1f585162..d72509d3d 100644 --- a/tests/service_chaining/sc_raw_scrip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_scrip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py @@ -157,11 +157,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -171,6 +167,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -267,12 +267,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_scrip_dstip_fqdnbypass_vxlan_unavailableip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 0f6d333a6..356308ac8 100644 --- a/tests/service_chaining/sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -191,11 +191,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -205,6 +201,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -303,10 +303,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_compliance_risk_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_geoip_asn_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_geoip_asn_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index dac3e2026..b03029746 100644 --- a/tests/service_chaining/sc_raw_src_geoip_asn_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_geoip_asn_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -190,11 +190,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -204,6 +200,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -302,10 +302,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_geoip_city_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_geoip_city_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 4d8666ebe..1587212e1 100644 --- a/tests/service_chaining/sc_raw_src_geoip_city_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_geoip_city_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -297,10 +297,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_geoip_city_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_geoip_country_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_geoip_country_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 8fab03ed1..88d278aae 100644 --- a/tests/service_chaining/sc_raw_src_geoip_country_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_geoip_country_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -189,11 +189,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -203,6 +199,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -301,10 +301,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_geoip_country_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_internet_service_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_internet_service_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index f2e7c8aaa..b546f93e7 100644 --- a/tests/service_chaining/sc_raw_src_internet_service_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_internet_service_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -191,11 +191,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -205,6 +201,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -303,10 +303,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index ebb266ccd..962d60733 100644 --- a/tests/service_chaining/sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_http_url_mirror_bypass_vlan_none" + "test_case_name": "sc" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_security_threat_library_iprange_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_security_threat_library_iprange_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 5880c0cf7..c3020f796 100644 --- a/tests/service_chaining/sc_raw_src_security_threat_library_iprange_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_security_threat_library_iprange_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_library_iprange_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "src_library_iprange_dstip_" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_src_website_classification_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_src_website_classification_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index aa5512c46..40d524649 100644 --- a/tests/service_chaining/sc_raw_src_website_classification_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_src_website_classification_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -189,11 +189,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -203,6 +199,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -301,10 +305,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_src_library_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_compliance_risk_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_compliance_risk_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 592b6999f..6791876c0 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_compliance_risk_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_compliance_risk_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sccompliance_risk_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "sccompliance_risk_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_geoip_asn_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_geoip_asn_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 1e2b8fecb..61fcdb329 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_geoip_asn_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_geoip_asn_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "schttp_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "schttp_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_geoip_city_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_geoip_city_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 38af788dd..33fa95442 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_geoip_city_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_geoip_city_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scgeoip_city_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scgeoip_city_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_geoip_country_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_geoip_country_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index d04c219cb..7a9b4c4cf 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_geoip_country_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_geoip_country_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scgeoip_country_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scgeoip_country_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_internet_service_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_internet_service_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index e1c30979b..614eddcdd 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_internet_service_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_internet_service_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "schttp_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "schttp_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 5e3e58184..e06aa8ff9 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "schttp_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "schttp_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_security_threat_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_security_threat_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 6ae56f736..b3fe24917 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_security_threat_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_security_threat_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -184,11 +184,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -198,6 +194,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "schttp_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "schttp_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dst_website_classification_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dst_website_classification_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 935ad86ed..b74271cdc 100644 --- a/tests/service_chaining/sc_raw_srcip_dst_website_classification_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dst_website_classification_library_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -214,10 +214,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "schttp_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "schttp_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_dns_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_dns_mirror_bypass_vlan_none.py index 2625f7f36..d86e0f0fa 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_dns_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_dns_mirror_bypass_vlan_none.py @@ -141,11 +141,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -155,6 +151,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -251,12 +251,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scdns_mirror_bypass_vlan_none" + "test_case_name": "scdns" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_mirror_bypass_vlan_none.py index 41e3ba2bd..ce319e304 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_mirror_bypass_vlan_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -269,12 +269,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scdns_qname_mirror_bypass_vlan_none" + "test_case_name": "scdns_qname" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_steer_bypass_vxlan_activeip_bfd.py index 656e92280..f64211276 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_dns_qname_steer_bypass_vxlan_activeip_bfd.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,12 +268,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scdns_qname_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scdns_qname" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_dns_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_dns_steer_bypass_vxlan_activeip_bfd.py index 24cc5c0bf..70407aecb 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_dns_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_dns_steer_bypass_vxlan_activeip_bfd.py @@ -140,11 +140,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -154,6 +150,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -250,12 +250,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scdns_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scdns" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_category_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_category_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 299e10374..d208f8a14 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_category_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_category_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -186,11 +186,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -200,6 +196,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -298,10 +298,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scexact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scexact_fqdn_library_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_geoip_city_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_geoip_city_library_http_url_steer_bypass_vxlan_activeip_bfd.py index c8306c58d..79c19fef8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_geoip_city_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_geoip_city_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -297,10 +297,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scexact_fqdn_geoip_city_library_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scexact_fqdn_geoip_city_library_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 30d56fc24..516686c1d 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -297,10 +297,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scexact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scexact_fqdn_library_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_security_threat_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_security_threat_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 3a2670cb9..39597e1f5 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_security_threat_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_exact_fqdn_security_threat_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +300,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scexact_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scexact_fqdn_library_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_http_url_steer_bypass_vxlan_activeip_bfd.py index f18c9ca00..360782116 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -186,11 +186,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -200,6 +196,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -298,10 +298,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_all_ipproto_http_url_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scfqdn_all_ipproto_http_url" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_quic_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_quic_steer_bypass_vxlan_activeip_bfd.py index eb2823401..fd5a5e98c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_quic_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_all_ipproto_quic_steer_bypass_vxlan_activeip_bfd.py @@ -26,7 +26,7 @@ def run(parameter): # 测试数据 policy_configuration = { "type": "service_chaining", - "name": "scfqdn_all_ipproto_quic_steer_bypass_vxlan_activeip_bfd", + "name": "scfqdn_all_ipproto_quic", "action": "service_chaining", "and_conditions": [ { @@ -170,11 +170,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ip_protocol", "query_value": "udp"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -184,6 +180,14 @@ def run(parameter): 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) + + + 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: diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_mirror_bypass_vlan_none.py index a7610b70f..48d6c482b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_mirror_bypass_vlan_none.py @@ -177,11 +177,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -191,6 +187,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -287,10 +287,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 4a874644b..e68d5f8e3 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -176,11 +176,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -190,6 +186,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -286,10 +290,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_mirror_bypass_vlan_none.py index 4f85fd341..59705698e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_mirror_bypass_vlan_none.py @@ -187,11 +187,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -201,6 +197,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_steer_bypass_vxlan_activeip_bfd.py index 55d3e3491..3f58c229c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ipprototcp_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -186,11 +186,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -200,6 +196,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -296,10 +296,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 756e7421b..91ea6394b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -192,11 +192,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -206,6 +202,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_mirror_bypass_vlan_none.py index 3cf2f8f5b..6f471f977 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_mirror_bypass_vlan_none.py @@ -176,11 +176,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -190,6 +186,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py index 5456f3edb..de051bb1c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_extport_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -175,11 +175,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -189,6 +185,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py index f701d1e3c..8c00b6a35 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_mirror_bypass_vlan_none.py @@ -176,11 +176,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -190,6 +186,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_steer_bypass_vxlan_activeip_bfd.py index d0c2d87ba..2130bf1a2 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_negate_intport_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -176,11 +176,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -190,6 +186,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_mirror_bypass_vlan_none.py index 319f177eb..50fdf1d3e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_mirror_bypass_vlan_none.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_steer_bypass_vxlan_activeip_bfd.py index 03978045d..b6a55db1e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_quic_steer_bypass_vxlan_activeip_bfd.py @@ -157,11 +157,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -171,6 +167,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_mirror_bypass_vlan_none.py index 340f1b8ac..1f839d7a7 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_mirror_bypass_vlan_none.py @@ -170,11 +170,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_ech_flag", "query_value": "False"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -184,6 +180,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -282,10 +286,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_ech_false_mirror_bypass_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_steer_bypass_vxlan_activeip_bfd.py index 07ff947a1..9c252f67b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_false_steer_bypass_vxlan_activeip_bfd.py @@ -169,11 +169,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_ech_flag", "query_value": "False"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -183,6 +179,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -281,10 +281,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_ech_false_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_mirror_bypass_vlan_none.py index 0075fe382..50030c0a8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_mirror_bypass_vlan_none.py @@ -170,11 +170,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_ech_flag", "query_value": "True"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -184,6 +180,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_steer_bypass_vxlan_activeip_bfd.py index c35405338..309b940d0 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_ech_true_steer_bypass_vxlan_activeip_bfd.py @@ -169,11 +169,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_ech_flag", "query_value": "True"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -183,6 +179,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -281,10 +281,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_ech_true_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_mirror_bypass_vlan_none.py index 9a531c88f..c98179d27 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_mirror_bypass_vlan_none.py @@ -170,11 +170,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_esni_flag", "query_value": "False"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -184,6 +180,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_steer_bypass_vxlan_activeip_bfd.py index c160b953b..8dd899895 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_esni_false_steer_bypass_vxlan_activeip_bfd.py @@ -169,11 +169,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_esni_flag", "query_value": "False"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -183,6 +179,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -281,10 +285,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_esni_false_steer_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py index 4b0bd2cdd..96ace3424 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_disabled_vxlan_activeip_bfd.py @@ -156,11 +156,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -170,6 +166,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -266,10 +270,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py index 5c12d6868..2890a0fd3 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_bfd.py @@ -154,11 +154,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -168,6 +164,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -264,10 +264,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_none.py index 3f945f5e9..e8e40e68e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_activeip_none.py @@ -156,11 +156,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -170,6 +166,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -266,10 +270,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py index 1872b456e..5146ab5ef 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,10 +267,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py index 950d54462..19dbdee03 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_block_vxlan_inactiveip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +269,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py index aa302fd48..0147cb688 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vlan_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,26 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -269,10 +285,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py index 8599c5515..933ec459c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,10 +267,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py index ec711f360..8b896a454 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_activeip_none.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,10 +268,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py index 6f384a1c4..99e3204d8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_inactiveip_none.py @@ -156,11 +156,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -170,6 +166,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -266,10 +270,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py index 38ae5b7a5..aa040675c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py @@ -8,8 +8,8 @@ from support.ui_utils.workpath import workdir 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.organize_config import OrganizeConfig from support.report_update import ReportUpdate +from support.organize_config import OrganizeConfig def run(parameter): try: @@ -150,11 +150,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -164,6 +160,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -260,10 +264,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py index 18f094549..e7691bc35 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py @@ -152,11 +152,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -166,6 +162,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -262,10 +262,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_global_bypass_diff_device_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_global_bypass_diff_device_vlan_none.py index b83b58bd4..671c2b4b7 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_global_bypass_diff_device_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_global_bypass_diff_device_vlan_none.py @@ -157,11 +157,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -171,6 +167,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -267,10 +271,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_nearby_bypass_diff_device_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_nearby_bypass_diff_device_vlan_none.py index feb9d0f90..7625e88cc 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_nearby_bypass_diff_device_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_nearby_bypass_diff_device_vlan_none.py @@ -156,11 +156,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -170,6 +166,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -266,10 +266,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py index 16018797f..79fa82633 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_disabled_vxlan_activeip_bfd.py @@ -161,11 +161,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -175,6 +171,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -271,10 +275,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vlan_none.py index 2a960a601..8bf42f1f7 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vlan_none.py @@ -163,11 +163,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -177,6 +173,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -275,8 +275,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py index eaae270b8..78ef9f540 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_bfd.py @@ -161,11 +161,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -175,6 +171,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -271,10 +275,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py index e4626e340..54232d1b0 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_activeip_none.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,10 +268,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py index 4670d28c5..04c4ec378 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_bfd.py @@ -161,11 +161,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -175,6 +171,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -271,10 +271,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py index 31812389d..ee7138f62 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_inactiveip_none.py @@ -161,11 +161,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -175,6 +171,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -271,10 +271,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py index 539c16cd3..153f39230 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py @@ -154,11 +154,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -168,6 +164,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -264,10 +268,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py index acdbb6fb4..1505a8323 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_none.py @@ -161,11 +161,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -175,6 +171,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -271,10 +275,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py index 0375f39c0..e9987974a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_disabled_vxlan_activeip_bfd.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py index 08173223c..f455f0505 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vlan_none.py @@ -163,11 +163,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -177,6 +173,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -273,10 +277,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py index fa099d038..a05ce6607 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_bfd.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py index 860363828..29d541d82 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_activeip_none.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py index 6e73b526b..23f756b7b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py index 38a6e96a4..309641774 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_inactiveip_none.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py index b6e50369b..9a10123c2 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +272,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py index c3b50b6f2..23785c446 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_none.py @@ -162,11 +162,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -176,6 +172,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -272,10 +276,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_mirror_bypass_vlan_none.py index e434cb599..b4b32e9b5 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_mirror_bypass_vlan_none.py @@ -170,11 +170,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_sni", "query_value": "www.baidu.com"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -184,6 +180,10 @@ def run(parameter): 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: diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_steer_bypass_vxlan_activeip_bfd.py index 856268476..7a995b622 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_no_sni_false_steer_bypass_vxlan_activeip_bfd.py @@ -168,11 +168,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -182,6 +178,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py index 0e91bc1a5..36dded2b6 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_bfd.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +265,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_none.py index 2311650fa..3abd89245 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_activeip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +269,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py index 382544851..4f72b4e83 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py @@ -149,11 +149,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -163,6 +159,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -259,10 +259,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py index 005da4999..2664062e4 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +273,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py index 3c1ad3930..f866b0b55 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py @@ -148,11 +148,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -162,6 +158,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -258,10 +262,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py index 88e6c36af..8e995d84a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py @@ -152,11 +152,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -166,6 +162,22 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -262,10 +274,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 6ca695ee0..b9801d98c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,10 +268,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py index 3c2085ab8..7a3973719 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +273,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py index 6a018240f..1ee825d14 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py @@ -149,11 +149,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -163,6 +159,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -259,10 +263,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py index 1894cd46e..c6bff4bf1 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,10 +269,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py index 5d9539bde..76c91b9da 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py @@ -146,11 +146,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -160,6 +156,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -256,10 +260,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py index 571e948c1..b4b2bd7a7 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,10 +267,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py index 5c3efe0ce..e122d9d39 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,12 +263,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py index 00d616a64..62fb098a6 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_activeip_none.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,12 +272,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_activeip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py index 34d241f13..6d937ded1 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py @@ -152,11 +152,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -166,6 +162,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -262,12 +266,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py index 4b2e56597..2dd6383ef 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -268,12 +272,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_inactiveip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py index 2f78d7e05..02cd5e0a7 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,12 +267,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py index 2b90918b7..f18607a31 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,12 +265,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py index c7cd6522e..fd435b90c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,12 +271,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_activeip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py index 27ad46a83..342c53a6e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -269,12 +273,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_activeip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py index 98cfbe356..395920001 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -153,11 +153,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -167,6 +163,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -263,12 +267,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py index 6a4baa2d8..85d37f000 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -269,12 +273,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py index 2e064ea51..4ed9911e9 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -155,11 +155,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -169,6 +165,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,12 +269,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py index 1325255a4..5069a6214 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py @@ -156,11 +156,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -170,6 +166,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -266,12 +266,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scfqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_mirror_bypass_vlan_none.py index 0f0afbff0..94bf2a88f 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_mirror_bypass_vlan_none.py @@ -169,6 +169,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -265,12 +269,12 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "sc_raw_srcip_dstipblock_vlan_none" + "test_case_name": os.path.basename(__file__)[:-3] } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_steer_bypass_vxlan_activeip_bfd.py index f5e5000e1..a345ad922 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ftp_account_steer_bypass_vxlan_activeip_bfd.py @@ -166,6 +166,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -262,10 +266,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ipprototcp_ftp_account_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ipprototcp_ftp_account_steer_bypass_vxlan_activeip_bfd.py index 5b3008433..1981ae7e1 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ipprototcp_ftp_account_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ipprototcp_ftp_account_steer_bypass_vxlan_activeip_bfd.py @@ -165,11 +165,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ip_protocol", "query_value": "tcp"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -179,6 +175,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -277,10 +277,10 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", - "test_case_name": "scipprototcp_ftp_account_steer_bypass_vxlan_activeip_bfd" + "test_case_name": "scipprototcp_ftp_account" } run(parameter) """ diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ipprotoudp_dns_qname_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ipprotoudp_dns_qname_steer_bypass_vxlan_activeip_bfd.py index 636ebfcf7..5a7d54fd3 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ipprotoudp_dns_qname_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ipprotoudp_dns_qname_steer_bypass_vxlan_activeip_bfd.py @@ -169,11 +169,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ip_protocol", "query_value": "udp"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -183,6 +179,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -279,10 +279,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_mirror_bypass_vlan_none.py index 6b8a04f50..e550b8eba 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_mirror_bypass_vlan_none.py @@ -195,11 +195,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -209,6 +205,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -305,10 +309,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_steer_bypass_vxlan_activeip_bfd.py index 47f0132f5..0af85a321 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mail_from_to_account_steer_bypass_vxlan_activeip_bfd.py @@ -189,11 +189,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -203,6 +199,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -299,10 +303,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mirror_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_mirror_block_vxlan_activeip_none.py index 958f9d464..73b877a4a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mirror_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mirror_block_vxlan_activeip_none.py @@ -127,11 +127,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -141,6 +137,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -237,10 +241,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_disabled_vxlan_activeip_bfd.py index 0de1ea85a..478516c52 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_disabled_vxlan_activeip_bfd.py @@ -125,11 +125,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -139,6 +135,22 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -235,10 +247,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_bfd.py index 02e32ffdb..cc250a9c2 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_bfd.py @@ -125,11 +125,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -139,6 +135,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -235,10 +239,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_none.py index 10e17a811..9900c4eb4 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_mirror_bypass_vxlan_activeip_none.py @@ -127,11 +127,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -141,6 +137,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -237,10 +241,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_multiple_port_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_multiple_port_mirror_bypass_vlan_none.py index 161763025..5cb18700a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_multiple_port_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_multiple_port_mirror_bypass_vlan_none.py @@ -201,11 +201,7 @@ def run(parameter): {"query_field_key": "server_port", "query_value": "443"}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -215,6 +211,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -314,8 +318,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_mirror_bypass_vlan_none.py index 97a8658fb..6d6f521f3 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_mirror_bypass_vlan_none.py @@ -178,11 +178,7 @@ def run(parameter): {"query_field_key": "server_port", "query_value": "443"}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -192,6 +188,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_steer_bypass_vxlan_activeip_bfd.py index b8e21aa4a..0467c6299 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_negate_dstport_dns_qname_steer_bypass_vxlan_activeip_bfd.py @@ -177,11 +177,7 @@ def run(parameter): {"query_field_key": "server_port", "query_value": "443"}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -191,6 +187,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_mirror_bypass_vlan_none.py index 213a850fb..7a74349ad 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_mirror_bypass_vlan_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index cf1784660..f6f318d73 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_negate_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_openvpn_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_openvpn_mirror_bypass_vlan_none.py index 53261ba5a..32317ae41 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_openvpn_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_openvpn_mirror_bypass_vlan_none.py @@ -141,11 +141,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -155,6 +151,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -251,10 +251,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_openvpn_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_openvpn_steer_bypass_vxlan_activeip_bfd.py index 948714645..cea0cf5a6 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_openvpn_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_openvpn_steer_bypass_vxlan_activeip_bfd.py @@ -141,11 +141,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""} ] } - - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + # 创建 if parameter["initiation_method"] == "ui": @@ -155,6 +151,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_quic_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_quic_mirror_bypass_vlan_none.py index 7faee998e..d2edbdb2f 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_quic_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_quic_mirror_bypass_vlan_none.py @@ -140,11 +140,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -154,6 +150,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -250,10 +250,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_quic_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_quic_steer_bypass_vxlan_activeip_bfd.py index fbda86344..b145d3a8d 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_quic_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_quic_steer_bypass_vxlan_activeip_bfd.py @@ -139,11 +139,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -153,6 +149,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -249,10 +257,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_rdp_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_rdp_mirror_bypass_vlan_none.py index d372a6410..fbdaec15b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_rdp_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_rdp_mirror_bypass_vlan_none.py @@ -141,11 +141,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -155,6 +151,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -251,10 +255,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_rdp_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_rdp_steer_bypass_vxlan_activeip_bfd.py index 44dd02739..384104fd1 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_rdp_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_rdp_steer_bypass_vxlan_activeip_bfd.py @@ -140,11 +140,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -154,6 +150,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -250,10 +254,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_mirror_bypass_vlan_none.py index e82792c19..4dab9c5cd 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_mirror_bypass_vlan_none.py @@ -152,11 +152,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_esni_flag", "query_value": "True"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -166,6 +162,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_steer_bypass_vxlan_activeip_bfd.py index 26befb1bd..75ce66e74 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_esni_true_steer_bypass_vxlan_activeip_bfd.py @@ -151,11 +151,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ssl_esni_flag", "query_value": "True"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -165,6 +161,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vlan_none.py index f98a5f03a..dc4c1aa7b 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vlan_none.py @@ -139,11 +139,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -153,6 +149,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -249,10 +253,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py index df5879375..06d7594a8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py @@ -131,11 +131,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -145,6 +141,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_none.py index e4593b2ec..9abb8c971 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_none.py @@ -137,11 +137,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -151,6 +147,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -247,10 +251,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py index 792142197..70d7f55d8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py @@ -131,11 +131,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -145,6 +141,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,10 +245,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_none.py index 3b9890c02..66688992a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_none.py @@ -138,11 +138,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -152,6 +148,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -248,10 +252,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py index 7feff6364..10360bc46 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -238,10 +238,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py index ae654951c..476dd916a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py @@ -134,11 +134,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -148,6 +144,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -244,10 +248,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 5, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_disabled_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_disabled_vlan_none.py index b1b8b80b2..a5c450a87 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_disabled_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_disabled_vlan_none.py @@ -139,11 +139,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -153,6 +149,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -249,10 +253,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_vlan_none.py index 47a62945a..d4ec1bf90 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_bypass_vlan_none.py @@ -141,11 +141,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -155,6 +151,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -251,10 +255,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_mirror_bypass_vlan_none.py index 150c2a2b5..2924cb032 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_mirror_bypass_vlan_none.py @@ -151,11 +151,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -165,6 +161,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_steer_bypass_vxlan_activeip_bfd.py index f8f468dbc..55a0ae310 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_no_sni_true_steer_bypass_vxlan_activeip_bfd.py @@ -150,11 +150,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -164,6 +160,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_disabled_vxlan_activeip_bfd.py index e346053c3..9c2b73c90 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_disabled_vxlan_activeip_bfd.py @@ -124,11 +124,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -138,6 +134,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -234,10 +238,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_vxlan_activeip_bfd.py index 0ad42aa02..41c8d0d76 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_steer_bypass_vxlan_activeip_bfd.py @@ -121,11 +121,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -135,6 +131,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -231,10 +231,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_compliance_risk_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_compliance_risk_library_http_url_steer_bypass_vxlan_activeip_bfd.py index acad63da3..b442de7f6 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_compliance_risk_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_compliance_risk_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_geoip_asn_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_geoip_asn_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 0e01d187f..f0c0131b5 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_geoip_asn_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_geoip_asn_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -192,11 +192,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -206,6 +202,26 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_geoip_country_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_geoip_country_library_http_url_steer_bypass_vxlan_activeip_bfd.py index efd548698..8a0c67d4c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_geoip_country_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_geoip_country_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -186,11 +186,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -200,6 +196,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_service_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_service_library_http_url_steer_bypass_vxlan_activeip_bfd.py index bfd39826c..c1c9e6403 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_service_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_internet_service_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -185,11 +185,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -199,6 +195,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py index 37e6ba636..20067d594 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_suff_fqdn_library_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -191,11 +191,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -205,6 +201,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_dstport_external_port_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstport_external_port_steer_bypass_vxlan_activeip_bfd.py index 26062faaa..4d5822262 100644 --- a/tests/service_chaining/sc_raw_srcip_dstport_external_port_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstport_external_port_steer_bypass_vxlan_activeip_bfd.py @@ -145,11 +145,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -159,6 +155,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -258,8 +262,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstport_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstport_mirror_bypass_vlan_none.py index af003a0d9..14638e2a4 100644 --- a/tests/service_chaining/sc_raw_srcip_dstport_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstport_mirror_bypass_vlan_none.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,8 +245,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstport_range_external_port_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_dstport_range_external_port_range_mirror_bypass_vlan_none.py index 7a143c37e..0655aed44 100644 --- a/tests/service_chaining/sc_raw_srcip_dstport_range_external_port_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstport_range_external_port_range_mirror_bypass_vlan_none.py @@ -146,11 +146,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -160,6 +156,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -259,8 +259,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dstport_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstport_steer_bypass_vxlan_activeip_bfd.py index fa4a13d47..8bbe88a0c 100644 --- a/tests/service_chaining/sc_raw_srcip_dstport_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstport_steer_bypass_vxlan_activeip_bfd.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "server_port", "query_value": "443"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,8 +245,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_dtsip_mirror_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dtsip_mirror_bypass_vxlan_inactiveip_bfd.py index 329042e40..51a807206 100644 --- a/tests/service_chaining/sc_raw_srcip_dtsip_mirror_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dtsip_mirror_bypass_vxlan_inactiveip_bfd.py @@ -122,11 +122,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -136,6 +132,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -232,10 +236,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_fqdn_ipprototcp_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_fqdn_ipprototcp_ssl_steer_bypass_vxlan_activeip_bfd.py index e4382d78a..343f00cd0 100644 --- a/tests/service_chaining/sc_raw_srcip_fqdn_ipprototcp_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_fqdn_ipprototcp_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -147,11 +147,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "ip_protocol", "query_value": "tcp"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -161,6 +157,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py index 2a6063632..77b7238b8 100644 --- a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_block_disabled_vxlan_activeip_bfd.py @@ -137,11 +137,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -151,6 +147,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py index 5aff11594..78eeeefb8 100644 --- a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_bypass_disabled_vxlan_activeip_bfd.py @@ -136,11 +136,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -150,6 +146,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py index 9307b0388..7a66f610c 100644 --- a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_global_bypass_diff_device_vxlan_activeip_bfd.py @@ -134,11 +134,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -148,6 +144,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py index 84f6028bc..191da0da1 100644 --- a/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_fqdn_ssl_steer_nearby_bypass_diff_device_vxlan_activeip_bfd.py @@ -133,11 +133,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -147,6 +143,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_internal_port_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_internal_port_range_mirror_bypass_vlan_none.py index f86ed8041..bb039e6a5 100644 --- a/tests/service_chaining/sc_raw_srcip_internal_port_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_internal_port_range_mirror_bypass_vlan_none.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,8 +241,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_internal_port_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_internal_port_range_steer_bypass_vxlan_activeip_bfd.py index b6af6f70b..a16983b9f 100644 --- a/tests/service_chaining/sc_raw_srcip_internal_port_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_internal_port_range_steer_bypass_vxlan_activeip_bfd.py @@ -127,11 +127,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -141,6 +137,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -240,8 +240,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_bfd.py index a8d398f6b..1eca0bb32 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_bfd.py @@ -110,11 +110,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -124,6 +120,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -220,10 +220,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_none.py index 362839fa5..053da1588 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_inactiveip_none.py @@ -110,11 +110,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -124,6 +120,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -220,10 +220,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_bfd.py index 95bf52c03..413ed232a 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_bfd.py @@ -110,11 +110,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -124,6 +120,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -220,10 +224,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_none.py index cbf2fc262..3b18512f4 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_block_vxlan_unavailableip_none.py @@ -110,11 +110,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -124,6 +120,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -222,10 +226,10 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, + "vsys": 5, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_mirror_bypass_vxlan_unavailableip_bfd.py index 81e5f2d04..8160dee1b 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_bypass_vxlan_unavailableip_bfd.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_mirror_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_mirror_redispatch_block_vxlan_inactiveip_bfd.py index 34e21d886..d4891ba42 100644 --- a/tests/service_chaining/sc_raw_srcip_mirror_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_mirror_redispatch_block_vxlan_inactiveip_bfd.py @@ -113,11 +113,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -127,6 +123,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -223,10 +227,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_mirror_bypass_vlan_none.py index aca432558..ad41d53c2 100644 --- a/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_mirror_bypass_vlan_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -173,6 +169,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py index 38f7198cf..e620ec524 100644 --- a/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_negate_dstip_fqdn_ssl_steer_bypass_vxlan_activeip_bfd.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -172,6 +168,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error diff --git a/tests/service_chaining/sc_raw_srcip_negate_srcport_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcip_negate_srcport_range_mirror_bypass_vlan_none.py index 80d370416..95f06b5be 100644 --- a/tests/service_chaining/sc_raw_srcip_negate_srcport_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcip_negate_srcport_range_mirror_bypass_vlan_none.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,8 +245,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py index 80d370416..95f06b5be 100644 --- a/tests/service_chaining/sc_raw_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_negate_srcport_range_steer_bypass_vxlan_activeip_bfd.py @@ -128,11 +128,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -142,6 +138,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -241,8 +245,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_bfd.py index ca411aa34..1f5eb3e24 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_bfd.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_none.py b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_none.py index 5e924f61b..422b1ac9c 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_none.py +++ b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_inactiveip_none.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_bfd.py index c08ee9e43..599eb25c6 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_bfd.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_none.py index 831ec6f24..c522de3e8 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_steer_block_vxlan_unavailableip_none.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_inactiveip_bfd.py index 27f65e295..d73d258e0 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_inactiveip_bfd.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_bfd.py index 9e93d803f..a3c4a1adb 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_bfd.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,14 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_none.py index 63e2158a0..03d000af0 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_steer_bypass_vxlan_unavailableip_none.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -218,10 +218,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_redispatch_block_vxlan_inactiveip_bfd.py index 05a79daf2..9d0f8265d 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_redispatch_block_vxlan_inactiveip_bfd.py @@ -111,11 +111,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -125,6 +121,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -221,10 +221,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcip_steer_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_steer_redispatch_bypass_vxlan_inactiveip_bfd.py index ba83b5397..e401b1dd0 100644 --- a/tests/service_chaining/sc_raw_srcip_steer_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_steer_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -112,11 +112,7 @@ def run(parameter): {"query_field_key": "sent_bytes", "query_value": ""}, {"query_field_key": "received_bytes", "query_value": ""} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -126,6 +122,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -222,10 +222,9 @@ if __name__ == '__main__': "api_server": "http://192.168.44.72", "initiation_method": "api", "env": "tsgx", - "vsys_id": 11, - "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "vsys": 5, + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcport_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcport_mirror_bypass_vlan_none.py index 4ce9c1645..2edbb8fa0 100644 --- a/tests/service_chaining/sc_raw_srcport_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcport_mirror_bypass_vlan_none.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -122,6 +118,10 @@ def run(parameter): 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) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error @@ -221,8 +221,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcport_range_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_srcport_range_mirror_bypass_vlan_none.py index c4665b2cd..3d294385f 100644 --- a/tests/service_chaining/sc_raw_srcport_range_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_srcport_range_mirror_bypass_vlan_none.py @@ -108,11 +108,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -221,8 +217,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcport_range_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcport_range_steer_bypass_vxlan_activeip_bfd.py index 88cee1429..1bd97cbd3 100644 --- a/tests/service_chaining/sc_raw_srcport_range_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcport_range_steer_bypass_vxlan_activeip_bfd.py @@ -115,11 +115,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -228,8 +224,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_srcport_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcport_steer_bypass_vxlan_activeip_bfd.py index f47cbda65..2081b81ce 100644 --- a/tests/service_chaining/sc_raw_srcport_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcport_steer_bypass_vxlan_activeip_bfd.py @@ -103,11 +103,7 @@ def run(parameter): {"query_field_key": "received_bytes", "query_value": ""}, {"query_field_key": "client_port", "query_value": "41668"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -216,8 +212,8 @@ if __name__ == '__main__': "env": "tsgx", "vsys_id": 1, "is_log": 1, - "root_path": "C:/automation_project/tsg_test", - "path": "C:/automation_project/tsg_test/tests/ui", + "root_path": workdir, + "path": workdir + "/tests", "module_name": "service_chaining", "test_case_name": os.path.basename(__file__)[:-3] } diff --git a/tests/service_chaining/sc_raw_subid_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_subid_fqdn_http_url_mirror_bypass_vlan_none.py index 32a569bdc..ecdb031a9 100644 --- a/tests/service_chaining/sc_raw_subid_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_subid_fqdn_http_url_mirror_bypass_vlan_none.py @@ -159,11 +159,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": diff --git a/tests/service_chaining/sc_raw_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 4d6e7dbf1..0f893fbaa 100644 --- a/tests/service_chaining/sc_raw_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_subid_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -158,11 +158,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": diff --git a/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_mirror_bypass_vlan_none.py b/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_mirror_bypass_vlan_none.py index 7980fae90..c22a37f86 100644 --- a/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_mirror_bypass_vlan_none.py +++ b/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_mirror_bypass_vlan_none.py @@ -196,15 +196,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": diff --git a/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py index 8fb783200..fd2289c3f 100644 --- a/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_subid_imei_apn_fqdn_http_url_steer_bypass_vxlan_activeip_bfd.py @@ -195,175 +195,7 @@ def run(parameter): {"query_field_key": "apn", "query_value": "www.lyrawautotest.com"}, {"query_field_key": "phone_number", "query_value": "12345678902"} ] - } - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) - - # 替换condition中的ip为随机ip - organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) - organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + } # 创建 if parameter["initiation_method"] == "ui": @@ -373,6 +205,18 @@ def run(parameter): 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) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + + + organize_config = OrganizeConfig(parameter, policy_configuration, api_client.token, ()) + organize_config.generate_random_ip(parameter, policy_configuration, traffic_generation) + objects_tuple, api_error = api_client.create_objects(policy_configuration) if len(api_error) > 0: return api_error -- cgit v1.2.3 From 770a5ad835fca2150f12c4723e1782ac4567ef61 Mon Sep 17 00:00:00 2001 From: yang liu Date: Thu, 28 Nov 2024 15:34:59 +0800 Subject: update sc case --- ...dn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py | 5 ++++- ...ip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py | 5 ++++- ...p_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py | 5 ++++- ...qdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py | 5 ++++- ...w_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py | 6 +++++- ...rcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py | 11 +++++++++-- ...cip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py | 5 ++++- ..._srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py | 6 +++++- ...cip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py | 11 +++++++++-- ...ip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py | 5 ++++- ...ip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py | 6 +++++- ...fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py | 5 ++++- ...qdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py | 5 ++++- ...p_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py | 6 +++++- ...qdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py | 5 ++++- ...dn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py | 5 ++++- .../sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py | 6 +++++- ...c_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py | 6 +++++- ...aw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py | 11 +++++++++-- ...w_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py | 5 ++++- 20 files changed, 101 insertions(+), 23 deletions(-) diff --git a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py index 90afb1353..df98ef962 100644 --- a/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_decrypted_srcip_fqdn_ssl_mirror_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -99,7 +99,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:3.3.3.46", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "bfd", "interval_ms": 10, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py index aa040675c..94acdec4a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_bfd.py @@ -115,7 +115,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:22.22.22.22", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "bfd", "interval_ms": 10, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py index e7691bc35..ac441b7d2 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_bypass_vxlan_unavailableip_none.py @@ -115,7 +115,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:22.22.22.22", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py index 153f39230..2778a3827 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_mirror_redispatch_block_vxlan_unavailableip_bfd.py @@ -119,7 +119,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:6.6.6.30", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "bfd", "interval_ms": 10, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py index 4f72b4e83..56407d148 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_inactiveip_bfd.py @@ -119,7 +119,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.55" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py index f866b0b55..58bfcae70 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_bfd.py @@ -115,8 +115,15 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:183.183.183.183", - "health_check": "BFD:200:5", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py index 8e995d84a..963cc7b76 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_block_vxlan_unavailableip_none.py @@ -115,7 +115,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:5.6.8.3", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py index 1ee825d14..d798e685e 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_inactiveip_bfd.py @@ -119,7 +119,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.55" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py index 76c91b9da..8bb629924 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_bfd.py @@ -115,8 +115,15 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:11.24.28.10", - "health_check": "BFD:200:5", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py index b4b2bd7a7..53658d9eb 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_bypass_vxlan_unavailableip_none.py @@ -116,7 +116,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:11.24.28.10", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py index 6d937ded1..8abf25f8d 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_inactiveip_bfd.py @@ -122,7 +122,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.55" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py index 02cd5e0a7..22c3a9a47 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_bfd.py @@ -118,7 +118,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:6.6.6.6", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "bfd", "interval_ms": 10, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py index f18607a31..d62eb9f6d 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_block_vxlan_unavailableip_none.py @@ -118,7 +118,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:4.6.1.5", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py index 395920001..2627b5881 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_inactiveip_bfd.py @@ -123,7 +123,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.55" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py index 4ed9911e9..4adc530ff 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_bfd.py @@ -120,7 +120,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:6.6.6.8", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "bfd", "interval_ms": 60000, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py index 5069a6214..fc789ff92 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_fqdn_ssl_steer_redispatch_bypass_vxlan_unavailableip_none.py @@ -119,7 +119,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:6.6.6.10", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py index 06d7594a8..f1347a19a 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_activeip_bfd.py @@ -101,7 +101,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.53" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py index 70d7f55d8..ae0b78d18 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_inactiveip_bfd.py @@ -101,7 +101,11 @@ def run(parameter): "method": "vxlan_g", "dest_ip" : "2.2.2.55" }, - "health_check": "BFD:200:5", + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py index 10360bc46..81ba665cb 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_bfd.py @@ -97,8 +97,15 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:5.6.3.2", - "health_check": "BFD:200:5", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, + "health_check": { + "method": "bfd", + "interval_ms": 200, + "retires": 5 + } } ] } diff --git a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py index 476dd916a..04fbb59e8 100644 --- a/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py +++ b/tests/service_chaining/sc_raw_srcip_dstip_ssl_mirror_block_vxlan_unavailableip_none.py @@ -97,7 +97,10 @@ def run(parameter): "value": "group-xxg-tsgx", "tag": "device_group" }, - "connectivity": "VXLAN-G:5.6.3.4", + "connectivity": { + "method": "vxlan_g", + "dest_ip" : "3.3.3.48" + }, "health_check": { "method": "none", "interval_ms": 200, -- cgit v1.2.3 From 13f7496dae48c04944b2f8ddf16f8e588e8a92c5 Mon Sep 17 00:00:00 2001 From: dongxiaoyan Date: Thu, 28 Nov 2024 15:40:10 +0800 Subject: 测试数据适配重构 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._srcip_dstip_http_table_all_sessions_dim_app.py | 88 +-- ..._http_table_all_sessions_dim_app_appcategory.py | 477 ++++++++----- ...stip_http_table_all_sessions_dim_appcategory.py | 475 +++++++----- ..._http_table_all_sessions_dim_c2s_s2c_link_id.py | 477 ++++++++----- ...stip_http_table_all_sessions_dim_c2s_s2c_ttl.py | 477 ++++++++----- ...srcip_dstip_http_table_all_sessions_dim_c_ip.py | 473 +++++++----- ...tip_http_table_all_sessions_dim_c_ip_objects.py | 475 +++++++----- ...cip_dstip_http_table_all_sessions_dim_c_port.py | 474 +++++++----- ...cip_dstip_http_table_all_sessions_dim_c_s_ip.py | 478 ++++++++----- ...p_http_table_all_sessions_dim_c_s_ip_objects.py | 478 ++++++++----- ...stip_http_table_all_sessions_dim_c_s_ip_port.py | 480 ++++++++----- ...p_dstip_http_table_all_sessions_dim_c_s_port.py | 476 ++++++++----- ...tip_http_table_all_sessions_dim_device_group.py | 471 +++++++----- ...stip_http_table_all_sessions_dim_fqdn_domain.py | 477 ++++++++----- ..._dstip_http_table_all_sessions_dim_http_host.py | 476 ++++++++----- ...stip_http_table_all_sessions_dim_s2c_link_id.py | 475 +++++++----- ...ip_dstip_http_table_all_sessions_dim_s2c_ttl.py | 475 +++++++----- ..._dstip_http_table_all_sessions_dim_server_ip.py | 476 ++++++++----- ...ttp_table_all_sessions_dim_server_ip_objects.py | 476 ++++++++----- ...stip_http_table_all_sessions_dim_server_port.py | 475 +++++++----- ...ip_dstip_http_table_all_sessions_dim_sled_ip.py | 475 +++++++----- ...cip_dstip_http_table_all_sessions_dim_sub_id.py | 475 +++++++----- ...cip_dstip_http_table_closed_sessions_dim_app.py | 475 +++++++----- ...tp_table_closed_sessions_dim_app_appcategory.py | 505 ++++++++----- ...p_http_table_closed_sessions_dim_appcategory.py | 475 +++++++----- ...tp_table_closed_sessions_dim_c2s_s2c_link_id.py | 505 ++++++++----- ...p_http_table_closed_sessions_dim_c2s_s2c_ttl.py | 505 ++++++++----- ...ip_dstip_http_table_closed_sessions_dim_c_ip.py | 473 +++++++----- ..._http_table_closed_sessions_dim_c_ip_objects.py | 475 +++++++----- ..._dstip_http_table_closed_sessions_dim_c_port.py | 474 +++++++----- ..._dstip_http_table_closed_sessions_dim_c_s_ip.py | 506 ++++++++----- ...ttp_table_closed_sessions_dim_c_s_ip_objects.py | 506 ++++++++----- ...p_http_table_closed_sessions_dim_c_s_ip_port.py | 564 ++++++++++----- ...stip_http_table_closed_sessions_dim_c_s_port.py | 504 ++++++++----- ..._http_table_closed_sessions_dim_device_group.py | 471 +++++++----- ...p_http_table_closed_sessions_dim_fqdn_domain.py | 477 ++++++++----- ...tip_http_table_closed_sessions_dim_http_host.py | 476 ++++++++----- ...tip_http_table_closed_sessions_dim_server_ip.py | 476 ++++++++----- ..._table_closed_sessions_dim_server_ip_objects.py | 476 ++++++++----- ...p_http_table_closed_sessions_dim_server_port.py | 475 +++++++----- ..._srcip_dstip_http_table_in_out_bytes_dim_app.py | 703 +++++++++++++----- ..._http_table_in_out_bytes_dim_app_appcategory.py | 748 ++++++++++++++----- ...stip_http_table_in_out_bytes_dim_appcategory.py | 703 +++++++++++++----- ...stip_http_table_in_out_bytes_dim_c2s_link_id.py | 704 +++++++++++++----- ..._http_table_in_out_bytes_dim_c2s_s2c_link_id.py | 748 ++++++++++++++----- ...stip_http_table_in_out_bytes_dim_c2s_s2c_ttl.py | 748 ++++++++++++++----- ...ip_dstip_http_table_in_out_bytes_dim_c2s_ttl.py | 704 +++++++++++++----- ..._dstip_http_table_in_out_bytes_dim_c_country.py | 703 +++++++++++++----- ...srcip_dstip_http_table_in_out_bytes_dim_c_ip.py | 704 +++++++++++++----- ...tip_http_table_in_out_bytes_dim_c_ip_objects.py | 742 ++++++++++++++----- ..._dstip_http_table_in_out_bytes_dim_c_ip_s_ip.py | 247 ------- ...cip_dstip_http_table_in_out_bytes_dim_c_port.py | 703 +++++++++++++----- ...stip_http_table_in_out_bytes_dim_c_s_country.py | 748 ++++++++++++++----- ..._http_table_in_out_bytes_dim_c_s_country_app.py | 793 ++++++++++++++++----- ...cip_dstip_http_table_in_out_bytes_dim_c_s_ip.py | 605 ++++++++++++++++ ...p_http_table_in_out_bytes_dim_c_s_ip_objects.py | 748 ++++++++++++++----- ...p_dstip_http_table_in_out_bytes_dim_c_s_port.py | 748 ++++++++++++++----- ...dstip_http_table_in_out_bytes_dim_decoded_as.py | 699 +++++++++++++----- ..._dstip_http_table_in_out_bytes_dim_direction.py | 699 +++++++++++++----- ...cip_dstip_http_table_in_out_bytes_dim_domain.py | 703 +++++++++++++----- ...rcip_dstip_http_table_in_out_bytes_dim_flags.py | 699 +++++++++++++----- ...srcip_dstip_http_table_in_out_bytes_dim_fqdn.py | 725 +++++++++++++------ ...stip_http_table_in_out_bytes_dim_fqdn_domain.py | 734 ++++++++++++++----- ..._dstip_http_table_in_out_bytes_dim_http_host.py | 703 +++++++++++++----- ...stip_http_table_in_out_bytes_dim_s2c_link_id.py | 703 +++++++++++++----- ...ip_dstip_http_table_in_out_bytes_dim_s2c_ttl.py | 703 +++++++++++++----- ..._dstip_http_table_in_out_bytes_dim_s_country.py | 703 +++++++++++++----- ...srcip_dstip_http_table_in_out_bytes_dim_s_ip.py | 703 +++++++++++++----- ...tip_http_table_in_out_bytes_dim_s_ip_objects.py | 702 +++++++++++++----- ...cip_dstip_http_table_in_out_bytes_dim_s_port.py | 703 +++++++++++++----- ...ip_dstip_http_table_in_out_bytes_dim_sled_ip.py | 699 +++++++++++++----- ...cip_dstip_http_table_in_out_bytes_dim_sub_id.py | 699 +++++++++++++----- ...s_srcip_dstip_http_table_in_out_pkts_dim_app.py | 582 ++++++++++----- ...p_http_table_in_out_pkts_dim_app_appcategory.py | 612 +++++++++++----- ...p_http_table_in_out_pkts_dim_app_c_s_country.py | 642 ++++++++++++----- ...ip_dstip_http_table_in_out_pkts_dim_app_fqdn.py | 612 +++++++++++----- ...dstip_http_table_in_out_pkts_dim_appcategory.py | 582 ++++++++++----- ...dstip_http_table_in_out_pkts_dim_c2s_link_id.py | 582 ++++++++++----- ...p_http_table_in_out_pkts_dim_c2s_s2c_link_id.py | 612 +++++++++++----- ...dstip_http_table_in_out_pkts_dim_c2s_s2c_ttl.py | 612 +++++++++++----- ...cip_dstip_http_table_in_out_pkts_dim_c2s_ttl.py | 582 ++++++++++----- ...p_dstip_http_table_in_out_pkts_dim_c_country.py | 582 ++++++++++----- ..._srcip_dstip_http_table_in_out_pkts_dim_c_ip.py | 582 ++++++++++----- ...dstip_http_table_in_out_pkts_dim_c_ip_object.py | 582 ++++++++++----- ...rcip_dstip_http_table_in_out_pkts_dim_c_port.py | 582 ++++++++++----- ...dstip_http_table_in_out_pkts_dim_c_s_country.py | 612 +++++++++++----- ..._in_out_pkts_dim_c_s_country_app_appcategory.py | 672 ++++++++++++----- ...rcip_dstip_http_table_in_out_pkts_dim_c_s_ip.py | 612 +++++++++++----- ...ip_http_table_in_out_pkts_dim_c_s_ip_objects.py | 614 +++++++++++----- ...ip_dstip_http_table_in_out_pkts_dim_c_s_port.py | 612 +++++++++++----- ..._dstip_http_table_in_out_pkts_dim_decoded_as.py | 582 ++++++++++----- ...stip_http_table_in_out_pkts_dim_device_group.py | 582 ++++++++++----- ...able_in_out_pkts_dim_device_group_decoded_as.py | 612 +++++++++++----- ...p_dstip_http_table_in_out_pkts_dim_direction.py | 582 ++++++++++----- ...rcip_dstip_http_table_in_out_pkts_dim_domain.py | 582 ++++++++++----- ...srcip_dstip_http_table_in_out_pkts_dim_flags.py | 582 ++++++++++----- ..._srcip_dstip_http_table_in_out_pkts_dim_fqdn.py | 582 ++++++++++----- ...dstip_http_table_in_out_pkts_dim_fqdn_domain.py | 612 +++++++++++----- ...p_dstip_http_table_in_out_pkts_dim_http_host.py | 582 ++++++++++----- ...dstip_http_table_in_out_pkts_dim_s2c_link_id.py | 582 ++++++++++----- ...cip_dstip_http_table_in_out_pkts_dim_s2c_ttl.py | 582 ++++++++++----- ...p_dstip_http_table_in_out_pkts_dim_s_country.py | 582 ++++++++++----- ..._srcip_dstip_http_table_in_out_pkts_dim_s_ip.py | 582 ++++++++++----- ...ip_dstip_http_table_in_out_pkts_dim_s_ip_app.py | 612 +++++++++++----- ...p_dstip_http_table_in_out_pkts_dim_s_ip_fqdn.py | 612 +++++++++++----- ...dstip_http_table_in_out_pkts_dim_s_ip_object.py | 582 ++++++++++----- ...rcip_dstip_http_table_in_out_pkts_dim_s_port.py | 582 ++++++++++----- ...cip_dstip_http_table_in_out_pkts_dim_sled_ip.py | 582 ++++++++++----- ...rcip_dstip_http_table_in_out_pkts_dim_sub_id.py | 582 ++++++++++----- ...s_srcip_dstip_http_table_max_metric_12_dim_4.py | 638 +++++++++++------ 110 files changed, 44359 insertions(+), 19598 deletions(-) delete mode 100644 tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_s_ip.py create mode 100644 tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip.py 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 3ea77c9e1..6c76bb71e 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 @@ -94,7 +94,7 @@ def run(parameter): "table_type": "regular" }, "uuid": "", - "name": "SUM(Packets Sent, Packets Received), SUM(Packets Sent) and SUM(Packets Received) Distributed by Application", + "name": "SUM(New Sessions) Distributed by Application", "threshold": { "in_bytes": 0, "out_bytes": 0 @@ -120,8 +120,7 @@ def run(parameter): "metrics": [ { "source_fields": [ - "sent_pkts", - "received_pkts" + "new_sessions_for_this_rule" ], "function": { "name": "SUM", @@ -132,43 +131,11 @@ def run(parameter): }, "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" + "label": "SUM(New Sessions)", + "unit": "sessions" } ], - "order_by": "SUM(Packets Sent, Packets Received)", + "order_by": "SUM(New Sessions)", "source": "statistics_rule", "series_limit": "" } @@ -184,7 +151,7 @@ def run(parameter): "table_type": "regular" }, "uuid": "", - "name": "RATE(Packets Sent, Packets Received), RATE(Packets Sent) and RATE(Packets Received) Distributed by Application", + "name": "RATE(New Sessions) Distributed by Application", "threshold": { "in_bytes": 0, "out_bytes": 0 @@ -206,12 +173,11 @@ def run(parameter): "is_drill_down": 0 } ], - "limit": "20", + "limit": "1000", "metrics": [ { "source_fields": [ - "sent_pkts", - "received_pkts" + "new_sessions_for_this_rule" ], "function": { "name": "RATE", @@ -222,43 +188,11 @@ def run(parameter): }, "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" + "label": "RATE(New Sessions)", + "unit": "sps" } ], - "order_by": "RATE(Packets Sent, Packets Received)", + "order_by": "RATE(New Sessions)", "source": "statistics_rule", "series_limit": "" } diff --git a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app_appcategory.py index 9aedf69b4..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_app_appcategory.py @@ -1,217 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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", - "app_category" - ], - "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", - "app_category" - ], - "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) """ @@ -220,4 +353,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_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_appcategory.py index bc7239a3d..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_appcategory.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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_category" - ], - "order_by": "sessions", - "row_limit": "200", - "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_category" - ], - "order_by": "sessions", - "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.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 +353,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_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_link_id.py index eb6883235..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_link_id.py @@ -1,217 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "c2s_link_id", - "s2c_link_id" - ], - "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": [ - "c2s_link_id", - "s2c_link_id" - ], - "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) """ @@ -220,4 +353,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_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_ttl.py index c6467c852..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c2s_s2c_ttl.py @@ -1,217 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "c2s_ttl", - "s2c_ttl" - ], - "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": [ - "c2s_ttl", - "s2c_ttl" - ], - "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) """ @@ -220,4 +353,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_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip.py index 5f931aa7d..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip.py @@ -1,213 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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.27", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": ["app"], - "order_by": "sessions", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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.27", - "clients_end_ip": "10.64.224.27", - "servers_start_ip": "2.1.1.27", - "servers_end_ip": "2.1.1.27", - "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) """ @@ -216,4 +353,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_c_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip_objects.py index 0eda9aaf4..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_ip_objects.py @@ -1,217 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -220,4 +353,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_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_port.py index 68a1f927d..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_port.py @@ -1,216 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "client_port" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -219,4 +353,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_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip.py index dd5cfaf68..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip.py @@ -1,220 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "client_ip", - "server_ip" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -223,4 +353,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_c_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_objects.py index 3241759aa..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_objects.py @@ -1,220 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -223,4 +353,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_c_s_ip_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_port.py index b33090f9e..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_ip_port.py @@ -1,222 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "client_ip", - "server_ip", - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip", - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] + }, + "vsys": 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": 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 +353,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_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_port.py index 636d71cb1..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_c_s_port.py @@ -1,218 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [] - }, - "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": [ - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -221,4 +353,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_device_group.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_device_group.py index 04b87381c..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_device_group.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_device_group.py @@ -1,211 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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.30", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": ["device_group"], - "order_by": "sessions", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": ["device_group"], - "order_by": "sessions", - "row_limit": "1000", - "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.30", - "clients_end_ip": "10.64.224.30", - "servers_start_ip": "2.1.1.30", - "servers_end_ip": "2.1.1.30", - "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) """ @@ -214,4 +353,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_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_fqdn_domain.py index c2e59173d..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_fqdn_domain.py @@ -1,217 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "server_fqdn", - "server_domain" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "sessions", - "row_limit": "200", - "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) """ @@ -220,4 +353,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_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_http_host.py index eeb2e4cba..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_http_host.py @@ -1,216 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "http_host" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "http_host" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,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_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_link_id.py index 7d5790535..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_link_id.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "s2c_link_id" - ], - "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": [ - "s2c_link_id" - ], - "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 +353,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_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_ttl.py index 36aed3c4b..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_s2c_ttl.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "s2c_ttl" - ], - "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": [ - "s2c_ttl" - ], - "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 +353,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_server_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip.py index 73dfb8d97..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip.py @@ -1,216 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "server_ip" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,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_server_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip_objects.py index 3ef836137..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_ip_objects.py @@ -1,216 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "server_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,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_server_port.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_port.py index 722760caa..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_server_port.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "server_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "new_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_port" - ], - "order_by": "sessions", - "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" - }, - # "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 +353,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_sled_ip.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sled_ip.py index d52510575..6c76bb71e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sled_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sled_ip.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "sled_ip" - ], - "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": [ - "sled_ip" - ], - "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 +353,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_sub_id.py b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sub_id.py index f7672baab..31241cbd0 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sub_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_all_sessions_dim_sub_id.py @@ -1,215 +1,350 @@ # -*- 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(New Sessions) 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_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(New Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(New Sessions)", + "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(New Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "new_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(New Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(New Sessions)", + "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": [ - "subscriber_id" - ], - "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": [ - "subscriber_id" - ], - "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 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app.py index 8b85c5fe7..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app.py @@ -1,215 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:all sessions" - ], - "dimensions": [ - "app" - ], - "order_by": "all sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:all sessions" - ], - "dimensions": [ - "app" - ], - "order_by": "all sessions", - "row_limit": "200", - "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 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app_appcategory.py index c32a63735..bc2bb12ef 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_app_appcategory.py @@ -1,217 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:all sessions" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "all sessions", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions rate" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "closed out sessions rate", - "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) """ @@ -220,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_appcategory.py index f9fcff511..538104487 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_appcategory.py @@ -1,215 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:all sessions" - ], - "dimensions": [ - "app_category" - ], - "order_by": "all sessions", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions rate" - ], - "dimensions": [ - "app_category" - ], - "order_by": "closed out sessions rate", - "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.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 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_link_id.py index bf935cac7..bbfa34d4a 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_link_id.py @@ -1,217 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "sessions", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "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) """ @@ -220,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_ttl.py index bddd97dba..d331fe441 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c2s_s2c_ttl.py @@ -1,217 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "sessions", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "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) """ @@ -220,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip.py index 3f044cb94..d3beab391 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip.py @@ -1,213 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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.27", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [] - }, - "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": ["app"], - "order_by": "sessions", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": ["app"], - "order_by": "sessions", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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.27", - "clients_end_ip": "10.64.224.27", - "servers_start_ip": "2.1.1.27", - "servers_end_ip": "2.1.1.27", - "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) """ @@ -216,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip_objects.py index d8b1374ec..d0cb9e068 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_ip_objects.py @@ -1,217 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [] - }, - "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": [ - "client_ip_object_list" - ], - "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": [ - "client_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "50", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -220,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_port.py index e4323a19c..325399ba7 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_port.py @@ -1,216 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [] - }, - "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_port" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] + }, + "vsys": 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": 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) """ @@ -219,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip.py index 0acff7d3f..6e567772b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip.py @@ -1,220 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -223,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_objects.py index 20b0b7bc5..e45f1d1fa 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_objects.py @@ -1,220 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "closed out sessions", - "row_limit": "200", - "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": 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) """ @@ -223,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_port.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_port.py index da565271b..f660b057b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_ip_port.py @@ -1,222 +1,434 @@ # -*- 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(Closed Sessions) Distributed by Client IP, Server IP, Client Port, Server Port", + "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 + }, + { + "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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) Distributed by Client IP, Server IP, Client Port, Server Port", + "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 + }, + { + "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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip", - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_ip", - "server_ip", - "client_port", - "server_port" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -225,4 +437,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_port.py index c3211d512..323b7e4af 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_c_s_port.py @@ -1,218 +1,378 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "sessions", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -221,4 +381,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_device_group.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_device_group.py index 21c2cfeea..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_device_group.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_device_group.py @@ -1,211 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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.30", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": ["device_group"], - "order_by": "sessions", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": ["device_group"], - "order_by": "sessions", - "row_limit": "1000", - "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.30", - "clients_end_ip": "10.64.224.30", - "servers_start_ip": "2.1.1.30", - "servers_end_ip": "2.1.1.30", - "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) """ @@ -214,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_fqdn_domain.py index 459b5b647..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_fqdn_domain.py @@ -1,217 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "sessions", - "row_limit": "200", - "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) """ @@ -220,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_http_host.py index 971952b35..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_http_host.py @@ -1,216 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "http_host" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "http_host" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip.py index 3187f2133..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip.py @@ -1,216 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip_objects.py index 7f4c2fd9a..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_ip_objects.py @@ -1,216 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "sessions", - "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" - }, - # "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) """ @@ -219,4 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_port.py b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_port.py index 40bf950cd..1900075ce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_closed_sessions_dim_server_port.py @@ -1,215 +1,350 @@ # -*- 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(Closed Sessions) 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": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Closed Sessions)", + "unit": "sessions" + } + ], + "order_by": "SUM(Closed Sessions)", + "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(Closed Sessions) 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": "1000", + "metrics": [ + { + "source_fields": [ + "closed_sessions_for_this_rule" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Closed Sessions)", + "unit": "sps" + } + ], + "order_by": "RATE(Closed Sessions)", + "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": [ - "closed_sessions_for_this_rule:SUM:sessions" - ], - "dimensions": [ - "server_port" - ], - "order_by": "sessions", - "row_limit": "100", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "closed_sessions_for_this_rule:RATE:sessions" - ], - "dimensions": [ - "server_port" - ], - "order_by": "sessions", - "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" - }, - # "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 +353,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app.py index ac881d205..e473efc88 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app.py @@ -1,238 +1,557 @@ # -*- 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.31", - }, - ] - }], - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "app" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "app" - ], - "order_by": "in bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "app" - ], - "order_by": "out bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} + } ], - # "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": "" + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app_appcategory.py index 6c2fde081..88d062fda 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_app_appcategory.py @@ -1,241 +1,599 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [ - {"application": "2407TCP", "app_category": "4", "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -244,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_appcategory.py index 416e5357f..c09eced8e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_appcategory.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "app_category" - ], - "order_by": "bytes", - "row_limit": "20", - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"app_category_id": "255","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_link_id.py index 81dd2d712..52c40aa3e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_link_id.py @@ -1,239 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"c2s_link_id": 720, "s2c_link_id": 720, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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" } - # name_list汇总 - # 测试用例实例化 - 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) """ @@ -242,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_link_id.py index 7c4072a94..7553583ba 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_link_id.py @@ -1,241 +1,599 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [ - {"c2s_link_id": 720, "s2c_link_id": 720, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -244,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_ttl.py index 859a71e38..65534a78c 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_s2c_ttl.py @@ -1,241 +1,599 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [ - {"c2s_ttl": 64, "s2c_ttl": 40, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -244,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_ttl.py index f39eec5d4..2c1137b87 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c2s_ttl.py @@ -1,239 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "bytes", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "in bytes", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "out bytes", - "row_limit": "1000", - "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": 64, "s2c_ttl": 40, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -242,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_country.py index 4a501e763..806a23657 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_country.py @@ -1,238 +1,557 @@ # -*- 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.31", - }, - ] - }], - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} + } ], - # "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": "" + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip.py index dca6c475e..616e11107 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip.py @@ -1,243 +1,563 @@ # -*- 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.38.240", - }, - ] - } - ], - "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.66", - }, - ] - } - ], - "destination_port": [], - "internal_ip": [], - "internal_port": [], - "external_ip": [], - "external_port": [], - "source_geography": [], - "destination_geography": [], - "sub_id": [], - "device": [], - "tunnel": [], - "tunnel_level": [], - "flag": [], - "application": [ - ], - "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": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:SUM:SUM(Incoming Bytes)", - "out_bytes:SUM:SUM(Outgoing Bytes)", - "in_bytes+out_bytes:SUM:SUM(Incoming Outgoing Bytes)" - ], - "dimensions": ["client_ip"], - "order_by": "SUM(Incoming Outgoing Bytes)", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, + 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": [ - "in_bytes:RATE:RATE(Incoming Bytes)", - "out_bytes:RATE:RATE(Outgoing Bytes)", - "in_bytes+out_bytes:RATE:RATE(Incoming Outgoing Bytes)" - ], - "dimensions": ["client_ip"], - "order_by": "RATE(Incoming Outgoing Bytes)", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:BITRATE(Incoming Bytes)", - "out_bytes:BITRATE:BITRATE(Outgoing Bytes)", - "in_bytes+out_bytes:BITRATE:BITRATE(Incoming Outgoing Bytes)" - ], - "dimensions": ["client_ip"], - "order_by": "BITRATE(Incoming Outgoing Bytes)", - "row_limit": "500", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Bar", - "metrics": [ - "in_bytes+out_bytes:BITRATE:BITRATE(Incoming Outgoing Bytes)" - ], - "dimensions": ["app"], - "order_by": "BITRATE(Incoming Outgoing Bytes)", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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.38.240", - "clients_end_ip": "10.64.38.240", - "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" + } + ], + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": "admin", - "password": "admin", + "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": 5,#5 + "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) - """ # 在测试文件的当前路径执行如下命令执行测试用例: pytest –cache-clear -v pytest_json.py --alluredir ./allure # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_objects.py index 5ead659ed..44739a525 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_objects.py @@ -1,237 +1,599 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "in bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "out bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [ - {"client_ip_object_list": "1573552", "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -240,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_s_ip.py deleted file mode 100644 index 9ed6f69c7..000000000 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_ip_s_ip.py +++ /dev/null @@ -1,247 +0,0 @@ -# -*- coding: UTF-8 -*- -import time -import os -import sys - -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 -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) - # 参数初始化 - exception_result = "" - result = {} - - # 脚本启动时间 - 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.31", - }, - ] - }], - "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": [ - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "bytes", - "row_limit": "20", - "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": "10.64.21.243", "server_ip": "2.1.1.31", "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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": "" - } - # 测试用例实例化 - 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 - - 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) - finally: - # 清理环境 - if isinstance(create, CreatePolicy): - create.clean_up() - # 生成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, - "env": "tsgx", - "vsys_id": 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", - "policy_type": "statistics" - } - parameter = replace_paras(parameter) - run(parameter) - """ - # 在测试文件的当前路径执行如下命令执行测试用例: - pytest –cache-clear -v pytest_json.py --alluredir ./allure - - # 执行如下命令生成测试报告(自动打开浏览器): - allure serve allure - """ \ No newline at end of file diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_port.py index ff0da4b04..b4b9cc533 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_port.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_port" - ], - "order_by": "bytes", - "row_limit": "20", - "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_port": 41668, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country.py index 302a0cd18..e09f943b2 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country.py @@ -1,241 +1,599 @@ # -*- 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.31", - }, - ] - }], - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} + } ], - # "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": "" + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "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) """ @@ -244,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country_app.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country_app.py index f7c5baeb0..53cb59c32 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_country_app.py @@ -1,244 +1,641 @@ # -*- 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.31", - }, - ] - }], - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_country", - "server_country", - "app" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_country", - "server_country", - "app" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_country", - "server_country", - "app" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} + } ], - # "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": "" + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "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) """ @@ -247,4 +644,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip.py new file mode 100644 index 000000000..229d510f1 --- /dev/null +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip.py @@ -0,0 +1,605 @@ +# -*- coding: UTF-8 -*- +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.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) + + # 参数初始化 + result, exception_result = "", "" + test_summary = {} + + # 脚本启动时间 + script_start_time = time.time() + + 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": [ + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "is_enabled": 1, + "log_option": "metadata" + } + + 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], + "When running test case, the exception error: ", str(e), flush=True) + return "When running test case, the exception error: " + str(e) + finally: + # 删除 + 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": 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", + "initiation_method": "api", + "policy_type": "statistics" + } + parameter = replace_paras(parameter) + run(parameter) + """ + # 在测试文件的当前路径执行如下命令执行测试用例: + pytest –cache-clear -v pytest_json.py --alluredir ./allure + + # 执行如下命令生成测试报告(自动打开浏览器): + allure serve allure + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip_objects.py index fbe928493..44739a525 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_ip_objects.py @@ -1,243 +1,599 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "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_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -246,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_port.py index 7040bc4b2..4b714b420 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_c_s_port.py @@ -1,241 +1,599 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [ - {"client_port": 41668, "server_port": 80, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -244,4 +602,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_decoded_as.py index 80c7a9a05..8e8223335 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_decoded_as.py @@ -1,236 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.3", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "in bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "out bytes", - "row_limit": "100", - "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.3", - "clients_end_ip": "10.64.224.3", - "servers_start_ip": "2.1.1.3", - "servers_end_ip": "2.1.1.3", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ # BITRATE=bytes*8 - {"s2c_link_id": 720, "bytes": 754864} - ], - # "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) """ @@ -239,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_direction.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_direction.py index e8765e42d..f571657af 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_direction.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_direction.py @@ -1,236 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.3", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "direction" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "direction" - ], - "order_by": "in bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "direction" - ], - "order_by": "out bytes", - "row_limit": "100", - "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.3", - "clients_end_ip": "10.64.224.3", - "servers_start_ip": "2.1.1.3", - "servers_end_ip": "2.1.1.3", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ # BITRATE=bytes*8 - {"s2c_link_id": 720, "bytes": 754864} - ], - # "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) """ @@ -239,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_domain.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_domain.py index 38568bfda..cb77f79b2 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_domain.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"s2c_ttl": "255","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_flags.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_flags.py index 8265d2f04..6adf5440b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_flags.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_flags.py @@ -1,236 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.15", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "flags" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "flags" - ], - "order_by": "in bytes", - "row_limit": "50", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "flags" - ], - "order_by": "out bytes", - "row_limit": "100", - "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.15", - "clients_end_ip": "10.64.224.15", - "servers_start_ip": "2.1.1.15", - "servers_end_ip": "2.1.1.15", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","bytes": 754864} - ], - # "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) """ @@ -239,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn.py index 8a728cabe..40edb41c0 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"server_fqdn": "www.yumi.com","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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, - "env": "tsgx", - "vsys_id": 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], +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": 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) """ @@ -241,4 +560,4 @@ def run(parameter): # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn_domain.py index 2c6a058e5..0d484d386 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_fqdn_domain.py @@ -1,241 +1,585 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) Distributed by Server FQDN, Server Domain", + "threshold": { + "in_bytes": 0, + "out_bytes": 0 + }, + "query": { + "dimensions": [ + { + "source_fields": [ + "app" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Application", + "is_drill_down": 0 + } + ], + "limit": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"server_fqdn": "www.yumi.com","server_domain": "yumi.com","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -244,4 +588,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_http_host.py index b6c75da10..5c7f56c67 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_http_host.py @@ -1,240 +1,557 @@ # -*- 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.16", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.16", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "action_parameter": { - "statistics_template": [{ - "name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "profile_type": "statistics_template", - "charts": [ - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "http_host" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "http_host" - ], - "order_by": "in bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "http_host" - ], - "order_by": "out bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - } - ] - }] + }, + "vsys": 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.16", - "clients_end_ip": "10.64.224.16", - "servers_start_ip": "2.1.1.16", - "servers_end_ip": "2.1.1.16", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"client_country": 64, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -243,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_link_id.py index f6342c116..2095bf0b5 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_link_id.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.18", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "bytes", - "row_limit": "50", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "bytes", - "row_limit": "50", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "bytes", - "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.18", - "clients_end_ip": "10.64.224.18", - "servers_start_ip": "2.1.1.18", - "servers_end_ip": "2.1.1.18", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ - {"c2s_link_id": 720, "s2c_link_id": 720, "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_ttl.py index b25466c76..4519ea01b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s2c_ttl.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": parameter["test_case_name"].replace("_srcip_dstip", "", 1), - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "bytes", - "row_limit": "20", - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"s2c_ttl": "40","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_country.py index d5145d1a3..ccc0e7d96 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_country.py @@ -1,238 +1,557 @@ # -*- 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.31", - }, - ] - }], - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, + "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": [ { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_country" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" + "attribute_name": "ATTR_APP_ID", + "type": "application", + "items": ["http"] } ] - }] - }, - "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": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"applicatoin": "2407TCP","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} + } ], - # "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": "" + "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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } + } + ] + }, + "vsys": 1 + } + }, + "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip.py index 13bdb8e9b..5c0a1daa9 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.16", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "bytes", - "row_limit": "100", - "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.16", - "clients_end_ip": "10.64.224.16", - "servers_start_ip": "2.1.1.16", - "servers_end_ip": "2.1.1.16", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"s2c_ttl": "255","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip_objects.py index bb591a4e6..3491459d3 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_ip_objects.py @@ -1,239 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [] - }, - "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_ip_object_list" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - } - ] - }] + }, + "vsys": 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" - }, - "statists_metric_results": [#如果Bytes Aggregate为:BITRATE则Bytes*8 - {"server_ip_object_list": "1154003","in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -242,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_port.py index 730952c97..a8aab995c 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_s_port.py @@ -1,238 +1,557 @@ # -*- 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in bytes", - "out_bytes:SUM:out bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in bytes", - "out_bytes:RATE:out bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in bytes", - "out_bytes:BITRATE:out bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "server_port" - ], - "order_by": "bytes", - "row_limit": "20", - "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,"server_ip_object_list": "1573552", "in_bytes": 1140, "out_bytes": 93218, "bytes": 94358} - ], - # "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) """ @@ -241,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sled_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sled_ip.py index 6b891f5cd..25c76e480 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sled_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sled_ip.py @@ -1,236 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.3", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in_bytes", - "out_bytes:SUM:out_bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "bytes", - "row_limit": "200", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in_bytes", - "out_bytes:RATE:out_bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "in_bytes", - "row_limit": "500", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in_bytes", - "out_bytes:BITRATE:out_bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "out_bytes", - "row_limit": "1000", - "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.3", - "clients_end_ip": "10.64.224.3", - "servers_start_ip": "2.1.1.3", - "servers_end_ip": "2.1.1.3", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ # BITRATE=bytes*8 - {"s2c_link_id": 720, "bytes": 754864} - ], - # "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) """ @@ -239,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sub_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sub_id.py index 39cbeb223..2d05bb01a 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sub_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_bytes_dim_sub_id.py @@ -1,236 +1,557 @@ # -*- 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) + # 参数初始化 - 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(Incoming Bytes, Outgoing Bytes), SUM(Incoming Bytes), SUM(Outgoing Bytes) and BITRATE(Incoming Bytes, Outgoing Bytes) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Bytes)", + "unit": "bytes" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "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.3", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Bytes, Outgoing Bytes), RATE(Incoming Bytes), RATE(Outgoing Bytes) and SUM(Incoming Bytes, Outgoing Bytes) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Bytes)", + "unit": "Bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Bytes, Outgoing Bytes)", + "unit": "bytes" + } + ], + "order_by": "SUM(Incoming Bytes, Outgoing Bytes)", + "source": "statistics_rule", + "series_limit": "" + } }, + { + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "BITRATE(Incoming Bytes, Outgoing Bytes), BITRATE(Incoming Bytes), BITRATE(Outgoing Bytes) and RATE(Incoming Bytes, Outgoing Bytes) 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": "200", + "metrics": [ + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "BITRATE(Incoming Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "out_bytes" + ], + "function": { + "name": "BITRATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "BITRATE(Outgoing Bytes)", + "unit": "bps" + }, + { + "source_fields": [ + "in_bytes", + "out_bytes" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Bytes, Outgoing Bytes)", + "unit": "Bps" + } + ], + "order_by": "BITRATE(Incoming Bytes, Outgoing Bytes)", + "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": [ - "in_bytes:SUM:in_bytes", - "out_bytes:SUM:out_bytes", - "in_bytes+out_bytes:SUM:bytes" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:RATE:in_bytes", - "out_bytes:RATE:out_bytes", - "in_bytes+out_bytes:RATE:bytes" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "in_bytes", - "row_limit": "100", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_bytes:BITRATE:in_bytes", - "out_bytes:BITRATE:out_bytes", - "in_bytes+out_bytes:BITRATE:bytes" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "out_bytes", - "row_limit": "100", - "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.3", - "clients_end_ip": "10.64.224.3", - "servers_start_ip": "2.1.1.3", - "servers_end_ip": "2.1.1.3", - "m": 1, - "d": 1, - "yaml_name": "test", - "pcap_name": "http_87pkts" - }, - "statists_metric_results": [ # BITRATE=bytes*8 - {"s2c_link_id": 720, "bytes": 754864} - ], - # "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) """ @@ -239,4 +560,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app.py index b8b433e85..bf1a4f2b0 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "app" - ], - "order_by": "pkts", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_appcategory.py index ddf858b88..bffe3999d 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_appcategory.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "app_category" - ], - "order_by": "in 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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_c_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_c_s_country.py index 165ccee07..91e0cc988 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_c_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_c_s_country.py @@ -1,226 +1,506 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "client_country", - "server_country" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "app", - "client_country", - "server_country" - ], - "order_by": "pkts", - "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) """ @@ -229,4 +509,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_fqdn.py index b6747ead3..6d87ce0c5 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_app_fqdn.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "server_fqdn" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "server_fqdn" - ], - "order_by": "in 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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_appcategory.py index 9ab78fdb5..7fbe73ca9 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_appcategory.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app_category" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app_category" - ], - "order_by": "in 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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_link_id.py index bd447001e..e30431bf5 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_link_id.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "c2s_link_id" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "c2s_link_id" - ], - "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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_link_id.py index b5654bdd3..2fa9e214e 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_link_id.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "c2s_link_id", - "s2c_link_id" - ], - "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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_ttl.py index 9632295ac..bcecd25a9 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_s2c_ttl.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "c2s_ttl", - "s2c_ttl" - ], - "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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_ttl.py index 3eac44667..7a5df1012 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c2s_ttl.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "c2s_ttl" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "c2s_ttl" - ], - "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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_country.py index 56e3b52a2..c6e033fce 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_country.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_country" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_country" - ], - "order_by": "pkts", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip.py index 5b1464adc..7735de698 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:IN Packets", - "out_pkts:SUM:OUT Packets", - "in_pkts+out_pkts:SUM:Packets" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "Packets", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:IN Packets", - "out_pkts:RATE:OUT Packets", - "in_pkts+out_pkts:RATE:Packets" - ], - "dimensions": [ - "client_ip" - ], - "order_by": "IN 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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip_object.py index 601af430e..34d54515f 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_ip_object.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_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": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_ip_object_list" - ], - "order_by": "out pkts", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_port.py index 067ee55ef..7e2d2e41b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_port.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_port" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_port" - ], - "order_by": "out pkts", - "row_limit": "1000", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country.py index 3815d2f18..15b6fc1ac 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_country", - "server_country" - ], - "order_by": "pkts", - "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) """ @@ -227,4 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country_app_appcategory.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country_app_appcategory.py index 95768fac3..458907bd6 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country_app_appcategory.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_country_app_appcategory.py @@ -1,228 +1,534 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) Distributed by Application, Application Category, 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 + }, + { + "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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) Distributed by Application, Application Category, 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 + }, + { + "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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [] - }, - "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" + }, + "vsys": 1 + } }, - "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 +537,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip.py index 15b68353c..494bef949 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_ip", - "server_ip" - ], - "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": "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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip_objects.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip_objects.py index 171b3988e..6c64741db 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip_objects.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_ip_objects.py @@ -1,226 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "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": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_ip_object_list", - "server_ip_object_list" - ], - "order_by": "out pkts", - "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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_port.py index da83657c9..4c47ba9a4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_c_s_port.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "client_port", - "server_port" - ], - "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": [ - {"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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_decoded_as.py index eff2a8b12..55b393661 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_decoded_as.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:IncomingPackets", - "out_pkts:SUM:Outgoing Packets", - "in_pkts+out_pkts:SUM:Packets" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "IncomingPackets", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:IncomingPackets", - "out_pkts:RATE:Outgoing Packets", - "in_pkts+out_pkts:RATE:Packets" - ], - "dimensions": [ - "decoded_as" - ], - "order_by": "Outgoing 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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group.py index ab089a00f..5c4c444eb 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "device_group" - ], - "order_by": "pkts", - "row_limit": "500", - "min_in_size": "1", - "min_out_size": "1" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "device_group" - ], - "order_by": "pkts", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group_decoded_as.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group_decoded_as.py index 36b3920d0..30773ad97 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group_decoded_as.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_device_group_decoded_as.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "decoded_as", - "device_group" - ], - "order_by": "pkts", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "decoded_as", - "device_group" - ], - "order_by": "pkts", - "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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_direction.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_direction.py index 2a07bbfc6..4ac91f3d8 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_direction.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_direction.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "direction" - ], - "order_by": "pkts", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "direction" - ], - "order_by": "out pkts", - "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) """ @@ -225,4 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_domain.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_domain.py index a9a52ceef..c4759d2e7 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_domain.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "pkts", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_domain" - ], - "order_by": "out pkts", - "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) """ @@ -225,4 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_flags.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_flags.py index 0e3974691..bca543d54 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_flags.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_flags.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "flags" - ], - "order_by": "pkts", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "flags" - ], - "order_by": "out pkts", - "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) """ @@ -225,4 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn.py index 9290cbe30..608db7dcf 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "pkts", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_fqdn" - ], - "order_by": "out pkts", - "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) """ @@ -225,4 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn_domain.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn_domain.py index 45285a5f1..42662d484 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn_domain.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_fqdn_domain.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "pkts", - "row_limit": "1000", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_fqdn", - "server_domain" - ], - "order_by": "in pkts", - "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_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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_http_host.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_http_host.py index d21c82bf2..1e24ceab8 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_http_host.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_http_host.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "http_host" - ], - "order_by": "pkts", - "row_limit": "200", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "http_host" - ], - "order_by": "out pkts", - "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) """ @@ -225,4 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_link_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_link_id.py index 53d27a1c0..a25f993f1 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_link_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_link_id.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "s2c_link_id" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "s2c_link_id" - ], - "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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_ttl.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_ttl.py index 8251d3bb3..32601258b 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_ttl.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s2c_ttl.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "s2c_ttl" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "s2c_ttl" - ], - "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": [ - {"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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_country.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_country.py index c7bd6f851..d1d8a053d 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_country.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_country.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_country" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_country" - ], - "order_by": "pkts", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip.py index 59c5f03cb..6fd39d29a 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_ip" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_ip" - ], - "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": "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_app.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_app.py index a48a2f659..fab6f9f84 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_app.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_app.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) Distributed by Application, Server IP", + "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_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) Distributed by Application, Server IP", + "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_ip" + ], + "function": { + "name": "", + "expression": "", + "variables": [ + + ] + }, + "label": "Server IP", + "is_drill_down": 0 + } + ], + "limit": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "app", - "server_ip" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "app", - "server_ip" - ], - "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": "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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_fqdn.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_fqdn.py index 7cb87cde5..8471553d7 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_fqdn.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_fqdn.py @@ -1,224 +1,478 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_ip", - "server_fqdn" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_ip", - "server_fqdn" - ], - "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": "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 +481,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_object.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_object.py index c272ffb8b..8bdb838cc 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_object.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_ip_object.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "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": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "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": "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_port.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_port.py index 28c79800e..8dfb3d6a4 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_port.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_s_port.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "server_port" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "server_port" - ], - "order_by": "out pkts", - "row_limit": "1000", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sled_ip.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sled_ip.py index ab1ecd5ec..30977fc9f 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sled_ip.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sled_ip.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "sled_ip" - ], - "order_by": "out pkts", - "row_limit": "1000", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sub_id.py b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sub_id.py index 55e66a8b9..45d8b0cf7 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sub_id.py +++ b/tests/statistics/stats_srcip_dstip_http_table_in_out_pkts_dim_sub_id.py @@ -1,222 +1,450 @@ # -*- 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(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": [ - "in_pkts:SUM:in pkts", - "out_pkts:SUM:out pkts", - "in_pkts+out_pkts:SUM:pkts" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "pkts", - "row_limit": "10", - "min_in_size": "0", - "min_out_size": "0" - }, - { - "chart_name": "default name", - "visualization_type": "Table", - "metrics": [ - "in_pkts:RATE:in pkts", - "out_pkts:RATE:out pkts", - "in_pkts+out_pkts:RATE:pkts" - ], - "dimensions": [ - "subscriber_id" - ], - "order_by": "out pkts", - "row_limit": "1000", - "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 +453,4 @@ if __name__ == '__main__': # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ diff --git a/tests/statistics/stats_srcip_dstip_http_table_max_metric_12_dim_4.py b/tests/statistics/stats_srcip_dstip_http_table_max_metric_12_dim_4.py index 5ec49d68d..bf1a4f2b0 100644 --- a/tests/statistics/stats_srcip_dstip_http_table_max_metric_12_dim_4.py +++ b/tests/statistics/stats_srcip_dstip_http_table_max_metric_12_dim_4.py @@ -1,278 +1,456 @@ # -*- 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) + # 参数初始化 - 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.38.240", + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "SUM(Incoming Packets, Outgoing Packets), SUM(Incoming Packets), SUM(Outgoing Packets) and BITRATE(Incoming Packets, Outgoing Packets) 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": "50", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "SUM(Incoming Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "SUM(Outgoing Packets)", + "unit": "packets" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "bps" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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.3", - }, + "id": "", + "display": { + "page_x": None, + "page_y": None, + "height": "", + "width": "", + "type": "table", + "table_type": "regular" + }, + "uuid": "", + "name": "RATE(Incoming Packets, Outgoing Packets), RATE(Incoming Packets), RATE(Outgoing Packets) and SUM(Incoming Packets, Outgoing Packets) 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": "100", + "metrics": [ + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_1", + "label": "RATE(Incoming Packets, Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_2", + "label": "RATE(Incoming Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "out_pkts" + ], + "function": { + "name": "RATE", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_3", + "label": "RATE(Outgoing Packets)", + "unit": "pps" + }, + { + "source_fields": [ + "in_pkts", + "out_pkts" + ], + "function": { + "name": "SUM", + "expression": "", + "variables": [ + + ] + }, + "metric_type": "count", + "metric_name": "count_4", + "label": "SUM(Incoming Packets, Outgoing Packets)", + "unit": "packets" + } + ], + "order_by": "SUM(Incoming Packets, Outgoing Packets)", + "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": "UNIQUE Client IP Object List", - "visualization_type": "Table", - "metrics": [ - "sent_bytes+received_bytes:SUM:SUM Bytes Sent Bytes Received", - "sent_pkts+received_pkts:SUM:SUM Packets Sent Packets Received", - "new_c2s_flows_for_this_rule+new_s2c_flows_for_this_rule:SUM:SUM New Double Flows", - "new_sessions_for_this_rule:SUM:SUM New Sessions", - "client_ip:UNIQUE_COUNT:UNIQUECOUNT Client IP", - "client_port:UNIQUE_COUNT:UNIQUECOUNT Client Port", - "server_ip:UNIQUE_COUNT:UNIQUECOUNT Server IP", - "server_port:UNIQUE_COUNT:UNIQUECOUNT Server Port", - "pkt_length_distribution:MEDIAN:MEDIAN Packet Length Distribution", - "pkt_length_distribution:P95_PERCENTILE:P95 Packet Length Distribution", - "pkt_length_distribution:P99_PERCENTILE:P99 Packet Length Distribution", - "tcp_handshake_latency_ms:P99_PERCENTILE:P99 TCP Handshake Latency" - ], - "dimensions": [ - "client_ip", - "client_port", - "server_ip", - "server_port" - ], - "order_by": "SUM New Sessions", - "row_limit": "20", - "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.38.240", - "clients_end_ip": "10.64.38.240", - "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__': - """56个Fields,其中3个不支持Unique_count,其他的按类别8个一组创建Test - client_ip_object_list - server_ip_object_list - server_fqdn - server_domain - fqdn_category_list - http_host - app - app_category - client_ip - client_country - server_ip - server_country - client_asn - server_asn - client_port - server_port - device_group - sled_ip - decoded_as - c2s_ttl - s2c_ttl - c2s_link_id - s2c_link_id - session_id - direction - in_latency_ms - out_latency_ms - tcp_handshake_latency_ms - subscriber_id - duration_ms - syn_pkts - tcp_rtt_ms - sent_bytes - received_bytes - sent_pkts - received_pkts - in_bytes - out_bytes - in_pkts - out_pkts - new_c2s_flows_for_this_rule - new_s2c_flows_for_this_rule - new_in_sessions - new_out_sessions - closed_in_sessions - closed_out_sessions - c2s_ip_fragments - s2c_ip_fragments - ssl_ja3_hash - ssl_sni - ssl_handshake_latency_ms - dtls_handshake_latency_ms - flags - in_pkt_length_distribution - out_pkt_length_distribution - pkt_length_distribution - """ parameter = { - "username": "AutomationTestAccount", - "password": "1qaz@WSX", - "test_pc_ip": "192.168.64.38", + "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": 5, # 5 + "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": "test6438", - "script_type": "ui", + "test_subcriber_id": "test6489", + "initiation_method": "api", "policy_type": "statistics" - } + } parameter = replace_paras(parameter) run(parameter) - """ # 在测试文件的当前路径执行如下命令执行测试用例: pytest –cache-clear -v pytest_json.py --alluredir ./allure # 执行如下命令生成测试报告(自动打开浏览器): allure serve allure - """ \ No newline at end of file + """ -- cgit v1.2.3 From 8174ed25702c64f16d266121f16b5cd0df4916b3 Mon Sep 17 00:00:00 2001 From: zhangchengwei Date: Thu, 28 Nov 2024 16:33:57 +0800 Subject: 调整ui profile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../map_element_position_library.py | 38 ++-- support/ui_utils/profiles/create_css_file.py | 104 +++++++++++ .../profiles/create_dns_records_profile.py | 67 ------- .../profiles/create_dns_resource_record.py | 68 +++++++ support/ui_utils/profiles/create_hijack_profile.py | 112 ------------ support/ui_utils/profiles/create_insert_profile.py | 104 ----------- support/ui_utils/profiles/create_js_file.py | 104 +++++++++++ support/ui_utils/profiles/create_lua_script.py | 69 +++++++ support/ui_utils/profiles/create_profiles.py | 201 ++++++++++++--------- .../ui_utils/profiles/create_replacement_file.py | 112 ++++++++++++ support/ui_utils/profiles/create_response_page.py | 12 +- .../ui_utils/profiles/create_run_script_profile.py | 69 ------- .../profiles/create_statistics_template.py | 4 +- .../ui_utils/profiles/create_tcp_option_profile.py | 176 ++++++++++++++++++ .../ui_utils/profiles/create_tcp_proxy_profile.py | 176 ------------------ support/ui_utils/profiles/delete_profiles.py | 5 +- support/ui_utils/profiles/edit_profiles.py | 6 +- support/ui_utils/profiles/elemen_position.py | 49 +++++ support/ui_utils/profiles/search_profiles.py | 3 + support/ui_utils/ui_client.py | 12 +- .../manipulation/111man_allow_srcip_dstip_http.py | 54 ++++-- 21 files changed, 883 insertions(+), 662 deletions(-) create mode 100644 support/ui_utils/profiles/create_css_file.py delete mode 100644 support/ui_utils/profiles/create_dns_records_profile.py create mode 100644 support/ui_utils/profiles/create_dns_resource_record.py delete mode 100644 support/ui_utils/profiles/create_hijack_profile.py delete mode 100644 support/ui_utils/profiles/create_insert_profile.py create mode 100644 support/ui_utils/profiles/create_js_file.py create mode 100644 support/ui_utils/profiles/create_lua_script.py create mode 100644 support/ui_utils/profiles/create_replacement_file.py delete mode 100644 support/ui_utils/profiles/create_run_script_profile.py create mode 100644 support/ui_utils/profiles/create_tcp_option_profile.py delete mode 100644 support/ui_utils/profiles/create_tcp_proxy_profile.py create mode 100644 support/ui_utils/profiles/elemen_position.py diff --git a/support/ui_utils/element_position/map_element_position_library.py b/support/ui_utils/element_position/map_element_position_library.py index de3b70531..f5ca1d666 100644 --- a/support/ui_utils/element_position/map_element_position_library.py +++ b/support/ui_utils/element_position/map_element_position_library.py @@ -1538,7 +1538,6 @@ object_apn_object_element_position = { "objectListPage_deleteButton_warningYes_posXpath": apnObjectListPage_deleteButton_warningYes_posXpath } } - object_application_object_element_position = { "page_jump": { "mainPage_navigationBar_logo_posXpath": mainPage_navigationBar_logo_posXpath, @@ -1600,7 +1599,6 @@ object_application_object_element_position = { } } - object_application_group_object_element_position = { "page_jump": { "mainPage_navigationBar_logo_posXpath": mainPage_navigationBar_logo_posXpath, @@ -1768,7 +1766,6 @@ object_tunnel_object_element_position = { } } - object_interval_object_element_position = { "page_jump": { "mainPage_navigationBar_logo_posXpath": mainPage_navigationBar_logo_posXpath, @@ -2165,15 +2162,15 @@ log_session_log_element_position = { } -def get_profile_element_position(key_name): - if key_name == "statistics_template": - element_position_map = profile_statistics_template_element_position - elif key_name == "monitor_profile": - element_position_map = profile_monitor_profile_element_position - else: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "{} no element position map".format(key_name)) - element_position_map = {} - return element_position_map +# def get_profile_element_position(key_name): +# if key_name == "statistics_template": +# element_position_map = profile_statistics_template_element_position +# elif key_name == "monitor_profile": +# element_position_map = profile_monitor_profile_element_position +# else: +# print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "{} no element position map".format(key_name)) +# element_position_map = {} +# return element_position_map def get_name_list(test_data): @@ -2219,7 +2216,7 @@ def get_name_list(test_data): tmp_name_dict["name"] = value[i]["profile_file"]["name"] else: tmp_name_dict["name"] = value[i]["profile_file"]["name"] - + name_list.append(copy.deepcopy(tmp_name_dict)) #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], name_list) #""" @@ -2409,21 +2406,22 @@ def get_element_position(key_name): element_position_map = profile_ssl_decryption_keyring_element_position elif key_name == "ssl_decryption_profile": element_position_map = profile_ssl_decryption_profile_element_position - elif key_name == "tcp_proxy_profile": + elif key_name == "tcp_option_profile": element_position_map = profile_tcp_proxy_profile_element_position elif key_name == "traffic_mirroring_profile": element_position_map = profile_traffic_mirroring_profile_element_position - elif key_name == "hijack_file": + elif key_name == "replacement_file": element_position_map = profile_hijack_file_element_position - elif "insert_script" in key_name: - if "css" in key_name: - element_position_map = profile_insert_script_css_element_position - else: - element_position_map = profile_insert_script_js_element_position + elif key_name == "js_file": + element_position_map = profile_insert_script_js_element_position + elif key_name == "css_file": + element_position_map = profile_insert_script_css_element_position elif key_name =="statistics_template": element_position_map = profile_statistics_template_element_position elif key_name == "run_script": element_position_map = profile_run_script_element_position + elif key_name == "statistics_template": + element_position_map = profile_statistics_template_element_position # policy elif key_name == "proxy_manipulation": element_position_map = policy_manipulation_rule_element_position diff --git a/support/ui_utils/profiles/create_css_file.py b/support/ui_utils/profiles/create_css_file.py new file mode 100644 index 000000000..30d17600f --- /dev/null +++ b/support/ui_utils/profiles/create_css_file.py @@ -0,0 +1,104 @@ +import os +import sys +import time +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * +from datetime import datetime + +class CreateCSSFile: + def __init__(self, driver): + self.driver = driver + + def create_css_file(self, data): + profile_name = data["name"] + if "css" in data["script_type"]: + temp_insertScripts_input_Name_posXpath = insertScripts_input_Name_posXpath + temp_insertScripts_button_oK_posXpath = insertScripts_button_oK_posXpath + else: + temp_insertScripts_input_Name_posXpath = insertScripts_js_input_Name_posXpath + temp_insertScripts_button_oK_posXpath = insertScripts_js_button_oK_posXpath + self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).clear() + self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).send_keys(profile_name) + #页面其它选项操作 + result_code = self._operate_page(data, operation_type="create") + #点击OK + self.driver.find_element(By.XPATH, temp_insertScripts_button_oK_posXpath).click() + if self.driver.is_element_exist_by_value(By.XPATH, insertScripts_button_warningSaveYes_posXpath): + self.driver.find_element(By.XPATH, insertScripts_button_warningSaveYes_posXpath).click() + + def _operate_page(self, data, operation_type="create"): + """ + 除name的其它选项操作,共创建、修改逻辑调用 + :param data: + :param operation_type: create modify。创建 修改时调用 + :return: + """ + if operation_type == "create": + data_index = 0 #新增数据索引 + else: + data_index = -1 #编辑数据索引 + no_modify = "不修改" + #script type脚本类型操作 --先操作文件类型 + + #self._operate_scriptType(data, data_index=data_index) + #File文件上传操作 + file = self._get_value_from_data(data, key="script", data_index=data_index) #解析file文件名称 + if file != no_modify: #判断修改时不修改时跳过操作。 + if file != "": + create_file = data["script"].split("->")[data_index] # 提取上传文件绝对路径 + profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile + file_abs = os.path.join(profile_path, "profile_file", "insert_scripts", create_file) + #上传文件 + if "css" in data["script_type"]: + temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_input_scriptUpLoad_posXpath + else: + temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_js_input_scriptUpLoad_posXpath + self.driver.find_element(By.XPATH, temp_insertScripts_input_scriptUpLoad_posXpath).send_keys(file_abs) + + def _operate_scriptType(self, data, data_index=0, no_modify="不修改"): + """ + 脚本类型操作,。 + :param data: + :param data_index: 0 -1来自上层 + :param no_modify:"不修改" 来自上次调用 + :param operation_type: create modify两个状态, + :return: + """ + #解析输入data数据的数据,脚本类型 + script_type = self._get_value_from_data(data, key="script_type", data_index=data_index) + if script_type != no_modify: #编辑不修改时执行 + if script_type != "": + self.driver.find_element(By.XPATH, insertScripts_input_scriptType_posXpath).click() #点击输入列表框 + if "js" == script_type: #js类型点击 + self.driver.find_element(By.XPATH, insertScripts_dropdown_jsItem_posXpath).click() + #insert position操作 + insert_position = self._get_value_from_data(data, key="insert_position", data_index=data_index) #获取insert position等操作After Page Load + if insert_position != no_modify: + self.driver.find_element(By.XPATH, insertScripts_input_insertPosition_posXpath).click() + if ("after" in insert_position) and ("page" in insert_position) and ("load" in insert_position): #点击 after Page Load选项 + #点击下拉列表选项 + self.driver.find_element(By.XPATH, insertScripts_dropdown_afterPageLoadItem_posXpath).click() + else: #点击 before Page Load选项 + self.driver.find_element(By.XPATH, insertScripts_dropdown_beforePageLoadItem_posXpath).click() + else: #css类型点击列表 + self.driver.find_element(By.XPATH, insertScripts_dropdown_cssItem_posXpath).click() + + def _get_value_from_data(self, data, key="", data_index=0): + """ + 从data数据中,根据key提取值 + :param data: + :param key: + :param data_index: 0 -1 + :return: + """ + try: + switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 + return switch_value + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) + raise + + + + + diff --git a/support/ui_utils/profiles/create_dns_records_profile.py b/support/ui_utils/profiles/create_dns_records_profile.py deleted file mode 100644 index e69a3453f..000000000 --- a/support/ui_utils/profiles/create_dns_records_profile.py +++ /dev/null @@ -1,67 +0,0 @@ -# import os -import inspect -import sys -import time -import traceback -import pytest_check - -# sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) -from support.ui_utils.profiles.page_jump import PageJump -from selenium.webdriver.common.keys import Keys -from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * - -import re -import random -import copy - -from support.ui_utils.random_name import RandomName -from datetime import datetime - -class CreateDnsProfile(): - def __init__(self, driver, secy_dnsRecords_profile_element_position): - self.driver = driver - self.page_jump_element_position = secy_dnsRecords_profile_element_position["page_jump"] - self.create_element_position = secy_dnsRecords_profile_element_position["create"] - - def create_dns_records_profile(self, data, secy_dnsRecords_profile_element_position,source_name): - page_jump_element_position = secy_dnsRecords_profile_element_position["page_jump"] - create_element_position = secy_dnsRecords_profile_element_position["create"] - try: - # 页面跳转 - page_jump = PageJump(self.driver) - page_jump.jump_sub_profile_page(page_jump_element_position) - self.driver.find_element(By.ID, create_element_position["profileListPage_createButton_posId"]).click() - # 在create 创建页面操作 - self.my_random = RandomName() - if source_name: - self.random_name = source_name - self.driver.find_element(By.ID, create_element_position["profilePage_inputName_posId"]).send_keys(self.random_name) - else: - self.random_name = self.my_random.random_name() - self.driver.find_element(By.ID,create_element_position["profilePage_inputName_posId"] ).send_keys(self.random_name) - # 选择Type - if data['type'] == 'A': - self.driver.find_element(By.ID, create_element_position["profilePage_Type_A_posId"]).click() - elif data['type'] == 'AAAA': - self.driver.find_element(By.ID, create_element_position["profilePage_Type_AAAA_posId"]).click() - elif data['type'] == 'CNAME': - self.driver.find_element(By.ID, create_element_position["profilePage_Type_CNAME_posId"]).click() - # 只有1个item,直接输入 - if len(data['items']) == 1: - self.driver.find_element(By.XPATH, create_element_position["dnsRecordsProfiles_input_item_posXpath"]).send_keys(data['items'][0]) - #点击保存 - temp_element_position = create_element_position["dnsRecordsProfile_save_item_posXpath"].format(replaceName=str(0)) - self.driver.find_element(By.ID, temp_element_position).click() - # 点击OK - self.driver.find_element(By.CSS_SELECTOR, create_element_position["profilePage_button_oK_posCss"]).click() - self.driver.find_element(By.CSS_SELECTOR, self.create_element_position["profilePage_button_warningSaveYes_posCss"]).click() - return 200 - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, flush=True) - raise - #return 400 - - - - diff --git a/support/ui_utils/profiles/create_dns_resource_record.py b/support/ui_utils/profiles/create_dns_resource_record.py new file mode 100644 index 000000000..684683d89 --- /dev/null +++ b/support/ui_utils/profiles/create_dns_resource_record.py @@ -0,0 +1,68 @@ +# import os +import inspect +import sys +import time +import traceback +import pytest_check + +# sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +from support.ui_utils.profiles.page_jump import PageJump +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * + +import re +import random +import copy + +from support.ui_utils.random_name import RandomName +from datetime import datetime + +class CreateDnsResourceReccord(): + def __init__(self, driver): + self.driver = driver + secy_dnsRecords_profile_element_position = {} + self.page_jump_element_position = secy_dnsRecords_profile_element_position["page_jump"] + self.create_element_position = secy_dnsRecords_profile_element_position["create"] + + def create_dns_resource_record(self, data, secy_dnsRecords_profile_element_position,source_name): + page_jump_element_position = secy_dnsRecords_profile_element_position["page_jump"] + create_element_position = secy_dnsRecords_profile_element_position["create"] + try: + # 页面跳转 + page_jump = PageJump(self.driver) + page_jump.jump_sub_profile_page(page_jump_element_position) + self.driver.find_element(By.ID, create_element_position["profileListPage_createButton_posId"]).click() + # 在create 创建页面操作 + self.my_random = RandomName() + if source_name: + self.random_name = source_name + self.driver.find_element(By.ID, create_element_position["profilePage_inputName_posId"]).send_keys(self.random_name) + else: + self.random_name = self.my_random.random_name() + self.driver.find_element(By.ID,create_element_position["profilePage_inputName_posId"] ).send_keys(self.random_name) + # 选择Type + if data['type'] == 'A': + self.driver.find_element(By.ID, create_element_position["profilePage_Type_A_posId"]).click() + elif data['type'] == 'AAAA': + self.driver.find_element(By.ID, create_element_position["profilePage_Type_AAAA_posId"]).click() + elif data['type'] == 'CNAME': + self.driver.find_element(By.ID, create_element_position["profilePage_Type_CNAME_posId"]).click() + # 只有1个item,直接输入 + if len(data['items']) == 1: + self.driver.find_element(By.XPATH, create_element_position["dnsRecordsProfiles_input_item_posXpath"]).send_keys(data['items'][0]) + #点击保存 + temp_element_position = create_element_position["dnsRecordsProfile_save_item_posXpath"].format(replaceName=str(0)) + self.driver.find_element(By.ID, temp_element_position).click() + # 点击OK + self.driver.find_element(By.CSS_SELECTOR, create_element_position["profilePage_button_oK_posCss"]).click() + self.driver.find_element(By.CSS_SELECTOR, self.create_element_position["profilePage_button_warningSaveYes_posCss"]).click() + return 200 + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Error: ", e, flush=True) + raise + #return 400 + + + + diff --git a/support/ui_utils/profiles/create_hijack_profile.py b/support/ui_utils/profiles/create_hijack_profile.py deleted file mode 100644 index df5401b5b..000000000 --- a/support/ui_utils/profiles/create_hijack_profile.py +++ /dev/null @@ -1,112 +0,0 @@ -import os -import sys -import time -from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * -from datetime import datetime - -class CreateHijackProfile: - def __init__(self, driver): - self.driver = driver - - def create_hijack_file(self, data): - try: - profile_name = data["name"] - self.driver.find_element(By.XPATH, hijackFiles_input_Name_posXpath).clear() - self.driver.find_element(By.XPATH, hijackFiles_input_Name_posXpath).send_keys(profile_name) - #self.driver.find_element(By.XPATH, "//label[text()='Name']").click() - #页面其它选项操作 - self._operate_page(data, operation_type="create") - #点击OK - self.driver.find_element(By.XPATH, hijackFiles_button_oK_posXpath).click() - if self.driver.is_element_exist_by_value(By.XPATH, hijackFiles_button_warningSaveYes_posXpath): - self.driver.find_element(By.XPATH, hijackFiles_button_warningSaveYes_posXpath).click() - except Exception as e: - raise - - def _operate_page(self, data, operation_type="create"): - """ - 除name的其它选项操作,共创建、修改逻辑调用 - :param data: - :param operation_type: create modify。创建 修改时调用 - :return: - """ - try: - if operation_type == "create": - data_index = 0 #新增数据索引 - else: - data_index = -1 #编辑数据索引 - no_modify = "不修改" - #解析输入data数据的数据 - #File文件上传操作 - file = self._get_value_from_data(data, key="file", data_index=data_index) #解析file文件名称 - # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file) - if file != no_modify: #判断修改时不修改时跳过操作。 - create_file = data["file"].split("->")[data_index] # 提取上传文件绝对路径 - profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile - file_abs = os.path.join(profile_path, "profile_file", "hijack_file", create_file) - #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file_abs) - self.driver.find_element(By.XPATH, hijackFiles_input_fileUpLoad_posXpath).send_keys(file_abs) - #download name 下载文件名称操作 - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="download_name", element_position=hijackFiles_switch_downLoadName_posXpath) - #file type 文件类型操作 - file_type = self._get_value_from_data(data, key="file_type", data_index=data_index) - if file_type != no_modify: #修改时不用修改数据直接跳过 - #点击type添加按钮 - self.driver.find_element(By.XPATH, hijackFiles_button_fileType_posXpath).click() - element_type_replace_name = {'gif': 'image/gif', 'jpeg': 'image/jpeg', 'png': 'image/png', 'svg+xml': 'image/svg+xml', 'svg': 'image/svg+xml', - 'exe': 'application/x-msdos-program', 'apk': 'application/vnd.android.package-archive', 'html': 'text/html'} - if file_type not in element_type_replace_name.keys(): - #raise f"文件类型错误。用例数据类型为:{file_type}, 可选择文件类型为:{list(element_type_replace_name.keys())}" - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "please check") - return 0 - element_tablist_type = self.driver.find_element(By.XPATH, hijackFiles_tablist_fileType_posXpath.format(replaceName=element_type_replace_name[file_type])) - self.driver.execute_script("arguments[0].click()", element_tablist_type) # 强制点击 - except Exception as e: - raise - - def _operate_switch(self, data, data_index=0, no_modify="不修改", switch_name="", element_position=""): - """ - 页面中,开关状态操作,。 - :param data: - :param data_index: 0 -1来自上层 - :param no_modify:"不修改" 来自上次调用 - :param operation_type: create modify两个状态, - :param switch_name: 数据中开关名称 - :param elementPosition: 开关的定位 - :return: - """ - try: - #解析输入data数据的数据 - switch_value: str = self._get_value_from_data(data, key=switch_name, data_index=data_index) #提取数据 Mirror Server Response - if switch_value != no_modify: #编辑时,有不修改则不用执行 - #先获取当前页面中开关状态 include_root_current_class : class="el-switch is-checked" 有is-checked当前状态为开,没有is-chedked当前状态为关 - swith_current_class = self.driver.find_element(By.XPATH, element_position).get_attribute("class") - if ("mirror" in switch_value) and ("server" in switch_value) and ("response" in switch_value): #数据中为Mirror Server Response,使用默认名称,不修改; on时需要打开这个开关 - if "is-checked" in swith_current_class: #页面当前为开,不用点击开关 - pass - else: #页面当前为关,需要点击开关 - self.driver.find_element(By.XPATH, f"{element_position}/span[2]").click() - else: ##数据中为其它名称,则需要修改默认名称 - if "is-checked" in swith_current_class: #页面当前为开,需要先关闭开关,在输入值 - self.driver.find_element(By.XPATH, f"{element_position}/span[2]").click() - except Exception as e: - raise - - def _get_value_from_data(self, data, key="", data_index=0): - """ - 从data数据中,根据key提取值 - :param data: - :param key: - :param data_index: 0 -1 - :return: - """ - try: - switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 - return switch_value - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) - raise - - - diff --git a/support/ui_utils/profiles/create_insert_profile.py b/support/ui_utils/profiles/create_insert_profile.py deleted file mode 100644 index 51bcefeb4..000000000 --- a/support/ui_utils/profiles/create_insert_profile.py +++ /dev/null @@ -1,104 +0,0 @@ -import os -import sys -import time -from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * -from datetime import datetime - -class CreateInsertProfile: - def __init__(self, driver): - self.driver = driver - - def create_hijack_file(self, data): - profile_name = data["name"] - if "css" in data["script_type"]: - temp_insertScripts_input_Name_posXpath = insertScripts_input_Name_posXpath - temp_insertScripts_button_oK_posXpath = insertScripts_button_oK_posXpath - else: - temp_insertScripts_input_Name_posXpath = insertScripts_js_input_Name_posXpath - temp_insertScripts_button_oK_posXpath = insertScripts_js_button_oK_posXpath - self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).clear() - self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).send_keys(profile_name) - #页面其它选项操作 - result_code = self._operate_page(data, operation_type="create") - #点击OK - self.driver.find_element(By.XPATH, temp_insertScripts_button_oK_posXpath).click() - if self.driver.is_element_exist_by_value(By.XPATH, insertScripts_button_warningSaveYes_posXpath): - self.driver.find_element(By.XPATH, insertScripts_button_warningSaveYes_posXpath).click() - - def _operate_page(self, data, operation_type="create"): - """ - 除name的其它选项操作,共创建、修改逻辑调用 - :param data: - :param operation_type: create modify。创建 修改时调用 - :return: - """ - if operation_type == "create": - data_index = 0 #新增数据索引 - else: - data_index = -1 #编辑数据索引 - no_modify = "不修改" - #script type脚本类型操作 --先操作文件类型 - - #self._operate_scriptType(data, data_index=data_index) - #File文件上传操作 - file = self._get_value_from_data(data, key="script", data_index=data_index) #解析file文件名称 - if file != no_modify: #判断修改时不修改时跳过操作。 - if file != "": - create_file = data["script"].split("->")[data_index] # 提取上传文件绝对路径 - profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile - file_abs = os.path.join(profile_path, "profile_file", "insert_scripts", create_file) - #上传文件 - if "css" in data["script_type"]: - temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_input_scriptUpLoad_posXpath - else: - temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_js_input_scriptUpLoad_posXpath - self.driver.find_element(By.XPATH, temp_insertScripts_input_scriptUpLoad_posXpath).send_keys(file_abs) - - def _operate_scriptType(self, data, data_index=0, no_modify="不修改"): - """ - 脚本类型操作,。 - :param data: - :param data_index: 0 -1来自上层 - :param no_modify:"不修改" 来自上次调用 - :param operation_type: create modify两个状态, - :return: - """ - #解析输入data数据的数据,脚本类型 - script_type = self._get_value_from_data(data, key="script_type", data_index=data_index) - if script_type != no_modify: #编辑不修改时执行 - if script_type != "": - self.driver.find_element(By.XPATH, insertScripts_input_scriptType_posXpath).click() #点击输入列表框 - if "js" == script_type: #js类型点击 - self.driver.find_element(By.XPATH, insertScripts_dropdown_jsItem_posXpath).click() - #insert position操作 - insert_position = self._get_value_from_data(data, key="insert_position", data_index=data_index) #获取insert position等操作After Page Load - if insert_position != no_modify: - self.driver.find_element(By.XPATH, insertScripts_input_insertPosition_posXpath).click() - if ("after" in insert_position) and ("page" in insert_position) and ("load" in insert_position): #点击 after Page Load选项 - #点击下拉列表选项 - self.driver.find_element(By.XPATH, insertScripts_dropdown_afterPageLoadItem_posXpath).click() - else: #点击 before Page Load选项 - self.driver.find_element(By.XPATH, insertScripts_dropdown_beforePageLoadItem_posXpath).click() - else: #css类型点击列表 - self.driver.find_element(By.XPATH, insertScripts_dropdown_cssItem_posXpath).click() - - def _get_value_from_data(self, data, key="", data_index=0): - """ - 从data数据中,根据key提取值 - :param data: - :param key: - :param data_index: 0 -1 - :return: - """ - try: - switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 - return switch_value - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) - raise - - - - - diff --git a/support/ui_utils/profiles/create_js_file.py b/support/ui_utils/profiles/create_js_file.py new file mode 100644 index 000000000..b8021ece7 --- /dev/null +++ b/support/ui_utils/profiles/create_js_file.py @@ -0,0 +1,104 @@ +import os +import sys +import time +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * +from datetime import datetime + +class CreateJSFile: + def __init__(self, driver): + self.driver = driver + + def create_js_file(self, data): + profile_name = data["name"] + if "css" in data["script_type"]: + temp_insertScripts_input_Name_posXpath = insertScripts_input_Name_posXpath + temp_insertScripts_button_oK_posXpath = insertScripts_button_oK_posXpath + else: + temp_insertScripts_input_Name_posXpath = insertScripts_js_input_Name_posXpath + temp_insertScripts_button_oK_posXpath = insertScripts_js_button_oK_posXpath + self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).clear() + self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).send_keys(profile_name) + #页面其它选项操作 + result_code = self._operate_page(data, operation_type="create") + #点击OK + self.driver.find_element(By.XPATH, temp_insertScripts_button_oK_posXpath).click() + if self.driver.is_element_exist_by_value(By.XPATH, insertScripts_button_warningSaveYes_posXpath): + self.driver.find_element(By.XPATH, insertScripts_button_warningSaveYes_posXpath).click() + + def _operate_page(self, data, operation_type="create"): + """ + 除name的其它选项操作,共创建、修改逻辑调用 + :param data: + :param operation_type: create modify。创建 修改时调用 + :return: + """ + if operation_type == "create": + data_index = 0 #新增数据索引 + else: + data_index = -1 #编辑数据索引 + no_modify = "不修改" + #script type脚本类型操作 --先操作文件类型 + + #self._operate_scriptType(data, data_index=data_index) + #File文件上传操作 + file = self._get_value_from_data(data, key="script", data_index=data_index) #解析file文件名称 + if file != no_modify: #判断修改时不修改时跳过操作。 + if file != "": + create_file = data["script"].split("->")[data_index] # 提取上传文件绝对路径 + profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile + file_abs = os.path.join(profile_path, "profile_file", "insert_scripts", create_file) + #上传文件 + if "css" in data["script_type"]: + temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_input_scriptUpLoad_posXpath + else: + temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_js_input_scriptUpLoad_posXpath + self.driver.find_element(By.XPATH, temp_insertScripts_input_scriptUpLoad_posXpath).send_keys(file_abs) + + def _operate_scriptType(self, data, data_index=0, no_modify="不修改"): + """ + 脚本类型操作,。 + :param data: + :param data_index: 0 -1来自上层 + :param no_modify:"不修改" 来自上次调用 + :param operation_type: create modify两个状态, + :return: + """ + #解析输入data数据的数据,脚本类型 + script_type = self._get_value_from_data(data, key="script_type", data_index=data_index) + if script_type != no_modify: #编辑不修改时执行 + if script_type != "": + self.driver.find_element(By.XPATH, insertScripts_input_scriptType_posXpath).click() #点击输入列表框 + if "js" == script_type: #js类型点击 + self.driver.find_element(By.XPATH, insertScripts_dropdown_jsItem_posXpath).click() + #insert position操作 + insert_position = self._get_value_from_data(data, key="insert_position", data_index=data_index) #获取insert position等操作After Page Load + if insert_position != no_modify: + self.driver.find_element(By.XPATH, insertScripts_input_insertPosition_posXpath).click() + if ("after" in insert_position) and ("page" in insert_position) and ("load" in insert_position): #点击 after Page Load选项 + #点击下拉列表选项 + self.driver.find_element(By.XPATH, insertScripts_dropdown_afterPageLoadItem_posXpath).click() + else: #点击 before Page Load选项 + self.driver.find_element(By.XPATH, insertScripts_dropdown_beforePageLoadItem_posXpath).click() + else: #css类型点击列表 + self.driver.find_element(By.XPATH, insertScripts_dropdown_cssItem_posXpath).click() + + def _get_value_from_data(self, data, key="", data_index=0): + """ + 从data数据中,根据key提取值 + :param data: + :param key: + :param data_index: 0 -1 + :return: + """ + try: + switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 + return switch_value + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) + raise + + + + + diff --git a/support/ui_utils/profiles/create_lua_script.py b/support/ui_utils/profiles/create_lua_script.py new file mode 100644 index 000000000..03b29c452 --- /dev/null +++ b/support/ui_utils/profiles/create_lua_script.py @@ -0,0 +1,69 @@ +import os +import sys +import time +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * +from datetime import datetime + +class CreateLuaScript: + def __init__(self, driver): + self.driver = driver + + def create_lua_script(self, data): + profile_name = data["name"] + self.driver.find_element(By.XPATH, tsgProxyScripts_input_Name_posXpath).clear() + self.driver.find_element(By.XPATH, tsgProxyScripts_input_Name_posXpath).send_keys(profile_name) + self.driver.find_element(By.XPATH, "//label[text()='Name']").click() + #页面其它选项操作 + self._operate_page(data, operation_type="create") + #点击OK + self.driver.find_element(By.XPATH, tsgProxyScripts_button_oK_posXpath).click() + if self.driver.is_element_exist_by_value(By.XPATH,tsgProxyScripts_button_warningSaveYes_posXpath): + self.driver.find_element(By.XPATH, tsgProxyScripts_button_warningSaveYes_posXpath).click() + + def _operate_page(self, data, operation_type="create"): + """ + 除name的其它选项操作,共创建、修改逻辑调用 + :param data: + :param operation_type: create modify。创建 修改时调用 + :return: + """ + if operation_type == "create": + data_index = 0 #新增数据索引 + else: + data_index = -1 #编辑数据索引 + no_modify = "不修改" + #File文件上传操作 + file = self._get_value_from_data(data, key="script", data_index=data_index) #解析file文件名称 + if file != no_modify: #判断修改时不修改时跳过操作。 + if file != "": + #上传文件 + create_file = data["script"].split("->")[data_index] # 提取上传文件绝对路径 + profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile + file_abs = os.path.join(profile_path, "profile_file", "tsg_proxy_scripts", create_file) + #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file_abs) + self.driver.find_element(By.XPATH, tsgProxyScripts_input_runScripts_posXpath).send_keys(file_abs) + elif file == "" and data_index == -1: + element_del = self.driver.find_element(By.XPATH, "//div[@id='runScriptTitleUpload']//i[@class='el-icon-close']") + self.driver.execute_script("arguments[0].click()", element_del) #强制点击 + #maximum execution time 操作 + max_exec_time = self._get_value_from_data(data, key="max_exec_time", data_index=data_index) + if max_exec_time != no_modify: #修改操作时,不修改数据则跳过操作。 + self.driver.find_element(By.XPATH, tsgProxyScripts_input_maximumExecutionTime_posXpath).clear() + #输入数据 + self.driver.find_element(By.XPATH, tsgProxyScripts_input_maximumExecutionTime_posXpath).send_keys(max_exec_time) + + def _get_value_from_data(self, data, key="", data_index=0): + """ + 从data数据中,根据key提取值 + :param data: + :param key: + :param data_index: 0 -1 + :return: + """ + try: + data_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) + raise + return data_value \ No newline at end of file diff --git a/support/ui_utils/profiles/create_profiles.py b/support/ui_utils/profiles/create_profiles.py index ebe60d00b..1b07b7732 100644 --- a/support/ui_utils/profiles/create_profiles.py +++ b/support/ui_utils/profiles/create_profiles.py @@ -2,109 +2,144 @@ import os import sys import time from support.ui_utils.profiles.page_jump import PageJump -from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * -import traceback -from support.ui_utils.profiles.create_statistics_template import CreateStatisticsTemplate -from support.ui_utils.element_position.map_element_position_library import get_profile_element_position -from support.ui_utils.profiles.create_hijack_profile import CreateHijackProfile -from support.ui_utils.profiles.create_insert_profile import CreateInsertProfile -from support.ui_utils.profiles.create_run_script_profile import CreateRunScriptProfile +from datetime import datetime from support.ui_utils.profiles.create_response_page import CreateResponsePage +from support.ui_utils.profiles.create_dns_resource_record import CreateDnsResourceReccord +from support.ui_utils.profiles.create_traffic_mirroring_profile import CreateMonitorProfile from support.ui_utils.profiles.create_ssl_decryption_keyring_profile import CreateSSLDecryptionKeyringProfile from support.ui_utils.profiles.create_ssl_decryption_profile import CreateSSLDecryptionProfile -from support.ui_utils.profiles.create_tcp_proxy_profile import CreateTcpProxyProfile -from support.ui_utils.profiles.create_traffic_mirroring_profile import CreateMonitorProfile -from datetime import datetime +from support.ui_utils.profiles.create_tcp_option_profile import CreateTcpOptionProfile +from support.ui_utils.profiles.create_replacement_file import CreateReplacementFile +from support.ui_utils.profiles.create_js_file import CreateJSFile +from support.ui_utils.profiles.create_css_file import CreateCSSFile +from support.ui_utils.profiles.create_lua_script import CreateLuaScript +from support.ui_utils.profiles.create_sc_profile import CreateServiceFunctionForwarderProfile +from support.ui_utils.profiles.create_statistics_template import CreateStatisticsTemplate +from support.ui_utils.profiles.elemen_position import ElementPosition class CreateProfiles: def __init__(self, driver): self.driver = driver - def create_profiles(self, policy_configuration): - profile_element_position = {} - page_jump_element_position = profile_element_position["page_jump"] - create_element_position = profile_element_position["create"] - try: - # 页面跳转 - page_jump = PageJump(self.driver) - page_jump.jump_sub_profile_page(page_jump_element_position) - # 点击create - self.driver.find_element(By.XPATH, create_element_position["profileListPage_createButton_posXpath"]).click() - if policy_configuration["profile_type"] == "response_page": # response page创建 - self._create_response_page(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "statistics_template": # response page创建 - self._create_statistics_template_page(policy_configuration) - elif policy_configuration["profile_type"] == "hijack_file": # hijack file 创建 - self._create_hijack_file(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "insert_script": # insert script 创建 - self._create_insert_script(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "run_script": # run script 创建 - self._create_run_script(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "ssl_keyring": # ssl decrpytion keyring 创建 - self._create_run_ssl_decryption_keyring(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "ssl_decryption": # ssl decrpytion profile 创建 - self._create_run_ssl_decryption_profile(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "tcp_option": # tcp proxy 创建 - self._create_run_tcp_proxy_profile(policy_configuration["profile_file"]) - elif policy_configuration["profile_type"] == "traffic_mirror": # traffic mirror 创建 - self._create_traffic_mirroring_profile(policy_configuration["profile_file"]) - # 其它 profile创建 + def create_profiles(self, parameter, policy_configuration): + result = "" + if "action_parameter" not in policy_configuration.keys(): return "" - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}") - raise - - def _create_response_page(self, data): - """ - resposne page 创建函数 - """ - Pl = CreateResponsePage(self.driver) - Pl.create_response_page(data) + # 遍历创建 + profile_list = ["html_profile", "resolution", "traffic_mirroring", "keyring_for_trusted", "keyring_for_untrusted", + "decryption_profile", "tcp_option_profile", "replacement_file", "js_file", "css_file", "lua_script", + "profile_chain", "sff_profiles", "template_profile"] + policy_configuration["parameter"] = parameter + for k, v in policy_configuration["action_parameter"].items(): + if k not in profile_list: # 没有则不用执行profile + continue + profile_element_position, key_name = ElementPosition(policy_configuration).get_profile_element_position_library + if len(profile_element_position) == 0: + return "" + try: #页面Profile菜单跳转 + page_jump_element_position = profile_element_position["page_jump"] + page_jump = PageJump(self.driver) + page_jump.jump_sub_profile_page(page_jump_element_position) + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "When jumping profile, the exception error: ", str(e), flush=True) + return "When jumping profile, the exception error: " + str(e) + try: #点击create button + create_element_position = profile_element_position["create"] + self.driver.find_element(By.XPATH, create_element_position["profileListPage_createButton_posXpath"]).click() + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "When clicking create button, the exception error: ", str(e), flush=True) + return "When clicking create button, the exception error: " + str(e) - def _create_hijack_file(self, data): - # 新建hijack_file - Pl = CreateHijackProfile(self.driver) - Pl.create_hijack_file(data) + if key_name == "response_page": + result = self._create_response_page(policy_configuration) + elif key_name == "dns_resource_record": + result = self._create_dns_resource_record(policy_configuration) + elif key_name == "traffic_mirroring_profile": + result = self._create_traffic_mirroring_profile(policy_configuration) + elif key_name == "ssl_decryption_keyring": + result = self._create_run_ssl_decryption_keyring(policy_configuration) + elif key_name == "ssl_decryption_profile": + result = self._create_run_ssl_decryption_profile(policy_configuration) + elif key_name == "tcp_option_profile": + result = self._create_run_tcp_option_profile(policy_configuration) + elif key_name == "replacement_file": + result = self._create_replacement_file(policy_configuration) + elif key_name == "js_file": + result = self._create_js_file(policy_configuration) + elif key_name == "css_file": + result = self._create_cs_file(policy_configuration) + elif key_name == "run_script": + result = self._create_run_script(policy_configuration) + elif key_name == "shaping_profile": + result = "shaping_profile" + elif key_name == "sff_profiles": + result = self._create_sff_script(policy_configuration) + elif key_name == "statistics_template": + result = self._create_statistics_template_page(policy_configuration) + else: + result = "" + return result - def _create_insert_script(self, data): - # 新建hijack_file - Pl = CreateInsertProfile(self.driver) - Pl.create_hijack_file(data) + def _create_response_page(self, policy_configuration): + # resposne page 创建函数 + Pl = CreateResponsePage(self.driver) + return Pl.create_response_page(policy_configuration) - def _create_run_script(self, data): - # 新建 run script - Pl = CreateRunScriptProfile(self.driver) - Pl.create_run_script_file(data) + def _create_dns_resource_record(self, policy_configuration): + # dns resource record 创建函数 + Pl = CreateDnsResourceReccord(self.driver) + return Pl.create_dns_resource_record(policy_configuration) + + def _create_traffic_mirroring_profile(self, policy_configuration): + # 新建 tcp_proxy_profile + Pl = CreateMonitorProfile(self.driver) + return Pl.create_traffic_mirroring_profile(policy_configuration) - def _create_run_ssl_decryption_keyring(self, data): + def _create_run_ssl_decryption_keyring(self, policy_configuration): # 新建 ssl_decryption_keyring Pl = CreateSSLDecryptionKeyringProfile(self.driver) - Pl.create_ssl_decryption_keyring_file(data) + return Pl.create_ssl_decryption_keyring_file(policy_configuration) - def _create_run_ssl_decryption_profile(self, data): + def _create_run_ssl_decryption_profile(self, policy_configuration): # 新建 ssl_decryption_proile Pl = CreateSSLDecryptionProfile(self.driver) - Pl.create_ssl_decryption_file(data) + return Pl.create_ssl_decryption_file(policy_configuration) - def _create_run_tcp_proxy_profile(self, data): - # 新建 tcp_proxy_profile - Pl = CreateTcpProxyProfile(self.driver) - Pl.create_tcp_proxy_profile(data) + def _create_run_tcp_option_profile(self, policy_configuration): + # 新建 tcp_option_profile + Pl = CreateTcpOptionProfile(self.driver) + return Pl.create_tcp_option_profile(policy_configuration) + + def _create_replacement_file(self, policy_configuration): + # 新建replacement file + Pl = CreateReplacementFile(self.driver) + return Pl.create_Replacement_file(policy_configuration) + + def _create_js_file(self, policy_configuration): + # 新建js file + Pl = CreateJSFile(self.driver) + return Pl.create_js_file(policy_configuration) + + def _create_cs_file(self, policy_configuration): + # 新建js file + Pl = CreateCSSFile(self.driver) + return Pl.create_css_file(policy_configuration) + + def _create_run_script(self, policy_configuration): + # 新建 run script + Pl = CreateLuaScript(self.driver) + return Pl.create_lua_script(policy_configuration) + + def _create_sff_script(self, policy_configuration): + # 新建 sff + Pl = CreateServiceFunctionForwarderProfile(self.driver) + return Pl.create_sff_profile(policy_configuration) + + def _create_statistics_template_page(self, policy_configuration): + # 新建 statistics_template + Pl = CreateStatisticsTemplate(self.driver) + return Pl.create_statistics_template(policy_configuration) - def _create_traffic_mirroring_profile(self, data): - # 新建 tcp_proxy_profile - Pl = CreateMonitorProfile(self.driver) - Pl.create_traffic_mirroring_profile(data) - - def _create_statistics_template_page(self, data): - cond_code = 0 - # create_statistics_template = CreateProfile(driver) - if data != None and len(data) >= 0: - create_statistics_template = CreateStatisticsTemplate(self.driver) - element_position_map = get_profile_element_position("statistics_template") - cond_code = create_statistics_template.create(data, element_position_map) - return cond_code diff --git a/support/ui_utils/profiles/create_replacement_file.py b/support/ui_utils/profiles/create_replacement_file.py new file mode 100644 index 000000000..289565b8a --- /dev/null +++ b/support/ui_utils/profiles/create_replacement_file.py @@ -0,0 +1,112 @@ +import os +import sys +import time +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * +from datetime import datetime + +class CreateReplacementFile: + def __init__(self, driver): + self.driver = driver + + def create_Replacement_file(self, data): + try: + profile_name = data["name"] + self.driver.find_element(By.XPATH, hijackFiles_input_Name_posXpath).clear() + self.driver.find_element(By.XPATH, hijackFiles_input_Name_posXpath).send_keys(profile_name) + #self.driver.find_element(By.XPATH, "//label[text()='Name']").click() + #页面其它选项操作 + self._operate_page(data, operation_type="create") + #点击OK + self.driver.find_element(By.XPATH, hijackFiles_button_oK_posXpath).click() + if self.driver.is_element_exist_by_value(By.XPATH, hijackFiles_button_warningSaveYes_posXpath): + self.driver.find_element(By.XPATH, hijackFiles_button_warningSaveYes_posXpath).click() + except Exception as e: + raise + + def _operate_page(self, data, operation_type="create"): + """ + 除name的其它选项操作,共创建、修改逻辑调用 + :param data: + :param operation_type: create modify。创建 修改时调用 + :return: + """ + try: + if operation_type == "create": + data_index = 0 #新增数据索引 + else: + data_index = -1 #编辑数据索引 + no_modify = "不修改" + #解析输入data数据的数据 + #File文件上传操作 + file = self._get_value_from_data(data, key="file", data_index=data_index) #解析file文件名称 + # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file) + if file != no_modify: #判断修改时不修改时跳过操作。 + create_file = data["file"].split("->")[data_index] # 提取上传文件绝对路径 + profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile + file_abs = os.path.join(profile_path, "profile_file", "hijack_file", create_file) + #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file_abs) + self.driver.find_element(By.XPATH, hijackFiles_input_fileUpLoad_posXpath).send_keys(file_abs) + #download name 下载文件名称操作 + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="download_name", element_position=hijackFiles_switch_downLoadName_posXpath) + #file type 文件类型操作 + file_type = self._get_value_from_data(data, key="file_type", data_index=data_index) + if file_type != no_modify: #修改时不用修改数据直接跳过 + #点击type添加按钮 + self.driver.find_element(By.XPATH, hijackFiles_button_fileType_posXpath).click() + element_type_replace_name = {'gif': 'image/gif', 'jpeg': 'image/jpeg', 'png': 'image/png', 'svg+xml': 'image/svg+xml', 'svg': 'image/svg+xml', + 'exe': 'application/x-msdos-program', 'apk': 'application/vnd.android.package-archive', 'html': 'text/html'} + if file_type not in element_type_replace_name.keys(): + #raise f"文件类型错误。用例数据类型为:{file_type}, 可选择文件类型为:{list(element_type_replace_name.keys())}" + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "please check") + return 0 + element_tablist_type = self.driver.find_element(By.XPATH, hijackFiles_tablist_fileType_posXpath.format(replaceName=element_type_replace_name[file_type])) + self.driver.execute_script("arguments[0].click()", element_tablist_type) # 强制点击 + except Exception as e: + raise + + def _operate_switch(self, data, data_index=0, no_modify="不修改", switch_name="", element_position=""): + """ + 页面中,开关状态操作,。 + :param data: + :param data_index: 0 -1来自上层 + :param no_modify:"不修改" 来自上次调用 + :param operation_type: create modify两个状态, + :param switch_name: 数据中开关名称 + :param elementPosition: 开关的定位 + :return: + """ + try: + #解析输入data数据的数据 + switch_value: str = self._get_value_from_data(data, key=switch_name, data_index=data_index) #提取数据 Mirror Server Response + if switch_value != no_modify: #编辑时,有不修改则不用执行 + #先获取当前页面中开关状态 include_root_current_class : class="el-switch is-checked" 有is-checked当前状态为开,没有is-chedked当前状态为关 + swith_current_class = self.driver.find_element(By.XPATH, element_position).get_attribute("class") + if ("mirror" in switch_value) and ("server" in switch_value) and ("response" in switch_value): #数据中为Mirror Server Response,使用默认名称,不修改; on时需要打开这个开关 + if "is-checked" in swith_current_class: #页面当前为开,不用点击开关 + pass + else: #页面当前为关,需要点击开关 + self.driver.find_element(By.XPATH, f"{element_position}/span[2]").click() + else: ##数据中为其它名称,则需要修改默认名称 + if "is-checked" in swith_current_class: #页面当前为开,需要先关闭开关,在输入值 + self.driver.find_element(By.XPATH, f"{element_position}/span[2]").click() + except Exception as e: + raise + + def _get_value_from_data(self, data, key="", data_index=0): + """ + 从data数据中,根据key提取值 + :param data: + :param key: + :param data_index: 0 -1 + :return: + """ + try: + switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 + return switch_value + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) + raise + + + diff --git a/support/ui_utils/profiles/create_response_page.py b/support/ui_utils/profiles/create_response_page.py index 3899473b2..260954759 100644 --- a/support/ui_utils/profiles/create_response_page.py +++ b/support/ui_utils/profiles/create_response_page.py @@ -8,19 +8,19 @@ class CreateResponsePage: def __init__(self, driver): self.driver = driver - def create_response_page(self, data): + def create_response_page(self, policy_configuration): """ resposne page 创建函数 """ # 在create resposne page页面操作 - profile_name = data["name"] + data = policy_configuration["action_parameter"] + profile_name = data["html_profile"]["name"] self.driver.find_element(By.XPATH, responsePage_input_Name_posXpath).clear() self.driver.find_element(By.XPATH, responsePage_input_Name_posXpath).send_keys(profile_name) # 上传文件 - create_file = data["file"].split("->")[0] # 提取上传文件绝对路径 - profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile - file_abs = os.path.join(profile_path, "profile_file", "response_pages", create_file) - + create_file = data["html_profile"]["file_path"] # 提取上传文件绝对路径 + root_path = policy_configuration["parameter"]["root_path"]# ...support/configuration_management/profile + file_abs = os.path.join(root_path, "support" ,"configuration_management", "profile", create_file) self.driver.find_element(By.XPATH, responsePage_input_file_posXpath).send_keys(file_abs) self.driver.find_element(By.XPATH, responsePage_button_oK_posXpath).click() if self.driver.is_element_exist_by_value(By.XPATH, responsePage_button_warningSaveYes_posXpath): diff --git a/support/ui_utils/profiles/create_run_script_profile.py b/support/ui_utils/profiles/create_run_script_profile.py deleted file mode 100644 index 0e0251566..000000000 --- a/support/ui_utils/profiles/create_run_script_profile.py +++ /dev/null @@ -1,69 +0,0 @@ -import os -import sys -import time -from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * -from datetime import datetime - -class CreateRunScriptProfile: - def __init__(self, driver): - self.driver = driver - - def create_run_script_file(self, data): - profile_name = data["name"] - self.driver.find_element(By.XPATH, tsgProxyScripts_input_Name_posXpath).clear() - self.driver.find_element(By.XPATH, tsgProxyScripts_input_Name_posXpath).send_keys(profile_name) - self.driver.find_element(By.XPATH, "//label[text()='Name']").click() - #页面其它选项操作 - self._operate_page(data, operation_type="create") - #点击OK - self.driver.find_element(By.XPATH, tsgProxyScripts_button_oK_posXpath).click() - if self.driver.is_element_exist_by_value(By.XPATH,tsgProxyScripts_button_warningSaveYes_posXpath): - self.driver.find_element(By.XPATH, tsgProxyScripts_button_warningSaveYes_posXpath).click() - - def _operate_page(self, data, operation_type="create"): - """ - 除name的其它选项操作,共创建、修改逻辑调用 - :param data: - :param operation_type: create modify。创建 修改时调用 - :return: - """ - if operation_type == "create": - data_index = 0 #新增数据索引 - else: - data_index = -1 #编辑数据索引 - no_modify = "不修改" - #File文件上传操作 - file = self._get_value_from_data(data, key="script", data_index=data_index) #解析file文件名称 - if file != no_modify: #判断修改时不修改时跳过操作。 - if file != "": - #上传文件 - create_file = data["script"].split("->")[data_index] # 提取上传文件绝对路径 - profile_path = os.path.dirname(os.path.abspath(__file__)) # ...support\ui_utils\profile - file_abs = os.path.join(profile_path, "profile_file", "tsg_proxy_scripts", create_file) - #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], file_abs) - self.driver.find_element(By.XPATH, tsgProxyScripts_input_runScripts_posXpath).send_keys(file_abs) - elif file == "" and data_index == -1: - element_del = self.driver.find_element(By.XPATH, "//div[@id='runScriptTitleUpload']//i[@class='el-icon-close']") - self.driver.execute_script("arguments[0].click()", element_del) #强制点击 - #maximum execution time 操作 - max_exec_time = self._get_value_from_data(data, key="max_exec_time", data_index=data_index) - if max_exec_time != no_modify: #修改操作时,不修改数据则跳过操作。 - self.driver.find_element(By.XPATH, tsgProxyScripts_input_maximumExecutionTime_posXpath).clear() - #输入数据 - self.driver.find_element(By.XPATH, tsgProxyScripts_input_maximumExecutionTime_posXpath).send_keys(max_exec_time) - - def _get_value_from_data(self, data, key="", data_index=0): - """ - 从data数据中,根据key提取值 - :param data: - :param key: - :param data_index: 0 -1 - :return: - """ - try: - data_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) - raise - return data_value \ No newline at end of file diff --git a/support/ui_utils/profiles/create_statistics_template.py b/support/ui_utils/profiles/create_statistics_template.py index aeb836598..6d8c3c045 100644 --- a/support/ui_utils/profiles/create_statistics_template.py +++ b/support/ui_utils/profiles/create_statistics_template.py @@ -20,7 +20,9 @@ class CreateStatisticsTemplate(): self.driver = driver self.actions = ActionChains(self.driver) - def create(self, data, template_element_position): + def create_statistics_template(self, policy_configuration): + data = policy_configuration + template_element_position = {} page_jump_element_position = template_element_position["page_jump"] create_element_position = template_element_position["create"] try: diff --git a/support/ui_utils/profiles/create_tcp_option_profile.py b/support/ui_utils/profiles/create_tcp_option_profile.py new file mode 100644 index 000000000..6b4544703 --- /dev/null +++ b/support/ui_utils/profiles/create_tcp_option_profile.py @@ -0,0 +1,176 @@ +import os +import sys +import time +from selenium.webdriver.common.by import By +from support.ui_utils.element_position.profile_element_position import * +from datetime import datetime + +class CreateTcpOptionProfile: + def __init__(self, driver): + self.driver = driver + + def create_tcp_option_profile(self, data): + profile_name = data["name"] + self.driver.find_element(By.ID, tcpProxyProfiles_input_Name_posId).send_keys(profile_name) + #页面其它选项操作 + result_code = self._operate_page(data, operation_type="create") + #点击OK + self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_oK_posCss).click() + if self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_warningSaveYes_posCss, find_before_wait_time=1).is_displayed(): + self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_warningSaveYes_posCss).click() + + def _operate_page(self, data, operation_type="create"): + """ + 除name的其它选项操作,供创建、修改逻辑调用 + :param data: + :param operation_type: create modify。创建 修改时调用 + :return: + """ + no_modify = "不修改" + if operation_type == "create": + data_index = 0 #新增数据索引 + else: + data_index = -1 #编辑数据索引 + + #解析输入data数据的数据 + #enable TCP Passthrough 开关操作 + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="enable_TCP_passthrough", element_position=tcpProxyProfiles_switch_enableTCPPassthrough_posXpath) + #Client_side 开关操作 override MSS, no delay, keepalive + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_override_MSS", element_position=tcpProxyProfiles_switch_clientOverrideMSS_posXpath) + # Client_side 输入操作 override MSS, ttl, user Timeout + c_overrideMSS_test = self._get_value_from_data(data, key="c_override_MSS", data_index=data_index) + if c_overrideMSS_test == "on" or c_overrideMSS_test == no_modify: + c_overrideMSS = self._get_value_from_data(data, key="c_overrideMSS", data_index=data_index) + if c_overrideMSS != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientOverrideMSS_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientOverrideMSS_posXpath).send_keys(c_overrideMSS) + self.driver.find_element(By.XPATH, "//label[text()='Override MSS']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_no_delay", element_position=tcpProxyProfiles_switch_clientNoDelay_posXpath) + c_ttl = self._get_value_from_data(data, key="c_ttl", data_index=data_index) + if c_ttl != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientTTL_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientTTL_posXpath).send_keys(c_ttl) + self.driver.find_element(By.XPATH, "//label[text()='TTL']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_keepalive", element_position=tcpProxyProfiles_switch_clientKeepalive_posXpath) + c_keepalive_test = self._get_value_from_data(data, key="c_keepalive", data_index=data_index) + if c_keepalive_test == "on": + c_probe_number = self._get_value_from_data(data, key="c_probe_number", data_index=data_index) + if c_probe_number != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientProbeNumber_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientProbeNumber_posXpath).send_keys(c_probe_number) + self.driver.find_element(By.XPATH, "//label[text()='Probe Number']").click() + c_idle_time = self._get_value_from_data(data, key="c_idle_time", data_index=data_index) + if c_idle_time != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientIdleTime_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientIdleTime_posXpath).send_keys(c_idle_time) + self.driver.find_element(By.XPATH, "//label[text()='Idle Time']").click() + c_interval = self._get_value_from_data(data, key="c_interval", data_index=data_index) + if c_interval != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientInterval_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientInterval_posXpath).send_keys(c_interval) + self.driver.find_element(By.XPATH, "//label[text()='Interval']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_UserTimeout_switch", + element_position=tcpProxyProfiles_switch_clientUserTimeout_posXpath) + c_UserTimeout_test = self._get_value_from_data(data, key="c_UserTimeout_switch", data_index=data_index) + if c_UserTimeout_test == "on" or c_overrideMSS_test == no_modify: + c_userTimeout = self._get_value_from_data(data, key="c_userTimeout", data_index=data_index) + if c_userTimeout != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientUserTimeout_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientUserTimeout_posXpath).send_keys(c_userTimeout) + self.driver.find_element(By.XPATH, "//label[text()='User Timeout']").click() + + # Server_side 开关操作 override MSS, no delay, keepalive + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_override_MSS", element_position=tcpProxyProfiles_switch_serverOverrideMSS_posXpath) + # Server_side 输入操作 override MSS, ttl, user Timeout + s_overrideMSS_test = self._get_value_from_data(data, key="s_override_MSS", data_index=data_index) + if s_overrideMSS_test == "on": + s_overrideMSS = self._get_value_from_data(data, key="s_overrideMSS", data_index=data_index) + if s_overrideMSS != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverOverrideMSS_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverOverrideMSS_posXpath).send_keys(s_overrideMSS) + self.driver.find_element(By.XPATH, "//label[text()='Override MSS']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_no_delay", element_position=tcpProxyProfiles_switch_serverNoDelay_posXpath) + s_ttl = self._get_value_from_data(data, key="s_ttl", data_index=data_index) + if s_ttl != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverTTL_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverTTL_posXpath).send_keys(s_ttl) + self.driver.find_element(By.XPATH, "//label[text()='TTL']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_keepalive", element_position=tcpProxyProfiles_switch_serverKeepalive_posXpath) + s_keepalive_test = self._get_value_from_data(data, key="s_keepalive", data_index=data_index) + if s_keepalive_test == "on": + s_probe_number = self._get_value_from_data(data, key="s_probe_number", data_index=data_index) + if s_probe_number != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverProbeNumber_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverProbeNumber_posXpath).send_keys(s_probe_number) + self.driver.find_element(By.XPATH, "//label[text()='Probe Number']").click() + s_idle_time = self._get_value_from_data(data, key="s_idle_time", data_index=data_index) + if s_idle_time != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverIdleTime_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverIdleTime_posXpath).send_keys(s_idle_time) + self.driver.find_element(By.XPATH, "//label[text()='Idle Time']").click() + s_interval = self._get_value_from_data(data, key="s_interval", data_index=data_index) + if s_interval != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverInterval_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverInterval_posXpath).send_keys(s_interval) + self.driver.find_element(By.XPATH, "//label[text()='Interval']").click() + + self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_UserTimeout_switch", + element_position=tcpProxyProfiles_switch_serverUserTimeout_posXpath) + s_UserTimeout_test = self._get_value_from_data(data, key="s_UserTimeout_switch", data_index=data_index) + if s_UserTimeout_test == "on": + s_userTimeout = self._get_value_from_data(data, key="s_userTimeout", data_index=data_index) + if s_userTimeout != no_modify: # 判断不修改时跳过操作 + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverUserTimeout_posXpath).clear() + self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverUserTimeout_posXpath).send_keys(s_userTimeout) + self.driver.find_element(By.XPATH, "//label[text()='User Timeout']").click() + + def _operate_switch(self, data: {}, data_index=0, no_modify="不修改", switch_name="", element_position=""): + """ + 页面中,开关状态操作。 + :param data: + :param data_index: 0 -1来自上层 + :param no_modify:"不修改" 来自上次调用 + :param operation_type: create modify两个状态, + :param switch_name: 数据中开关名称 + :param elementPosition: 开关的定位 + :return: + """ + #解析输入data数据的数据 + switch_value: str = self._get_value_from_data(data, key=switch_name, data_index=data_index) #提取数据 + # print("switch_value=", switch_value) + if switch_value != no_modify: #编辑时,有不修改则不用执行 + #先获取当前页面中开关状态 include_root_current_class : class="el-switch is-checked" 有is-checked当前状态为开,没有is-chedked当前状态为关 + swith_current_class = self.driver.find_element(By.XPATH, element_position).get_attribute("class") + if "on" == switch_value: #数据中为开 + if "is-checked" in swith_current_class: #页面当前为开,不用点击开关 + pass + else: #页面当前为关,需要点击开关 + self.driver.find_element(By.XPATH, element_position).click() + else: #用例输入为关 + if "is-checked" in swith_current_class: #页面当前为开,需要点击开关 + self.driver.find_element(By.XPATH, element_position).click() + else: #页面当前为关,不用点击开关 + pass + + def _get_value_from_data(self, data, key="", data_index=0): + """ + 从data数据中,根据key提取值 + :param data: + :param key: + :param data_index: 0 -1 + :return: + """ + try: + switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 + except Exception as e: + print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) + return switch_value + + + + diff --git a/support/ui_utils/profiles/create_tcp_proxy_profile.py b/support/ui_utils/profiles/create_tcp_proxy_profile.py deleted file mode 100644 index a1adc408f..000000000 --- a/support/ui_utils/profiles/create_tcp_proxy_profile.py +++ /dev/null @@ -1,176 +0,0 @@ -import os -import sys -import time -from selenium.webdriver.common.by import By -from support.ui_utils.element_position.profile_element_position import * -from datetime import datetime - -class CreateTcpProxyProfile: - def __init__(self, driver): - self.driver = driver - - def create_tcp_proxy_profile(self, data): - profile_name = data["name"] - self.driver.find_element(By.ID, tcpProxyProfiles_input_Name_posId).send_keys(profile_name) - #页面其它选项操作 - result_code = self._operate_page(data, operation_type="create") - #点击OK - self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_oK_posCss).click() - if self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_warningSaveYes_posCss, find_before_wait_time=1).is_displayed(): - self.driver.find_element(By.CSS_SELECTOR, tcpProxyProfiles_button_warningSaveYes_posCss).click() - - def _operate_page(self, data, operation_type="create"): - """ - 除name的其它选项操作,供创建、修改逻辑调用 - :param data: - :param operation_type: create modify。创建 修改时调用 - :return: - """ - no_modify = "不修改" - if operation_type == "create": - data_index = 0 #新增数据索引 - else: - data_index = -1 #编辑数据索引 - - #解析输入data数据的数据 - #enable TCP Passthrough 开关操作 - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="enable_TCP_passthrough", element_position=tcpProxyProfiles_switch_enableTCPPassthrough_posXpath) - #Client_side 开关操作 override MSS, no delay, keepalive - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_override_MSS", element_position=tcpProxyProfiles_switch_clientOverrideMSS_posXpath) - # Client_side 输入操作 override MSS, ttl, user Timeout - c_overrideMSS_test = self._get_value_from_data(data, key="c_override_MSS", data_index=data_index) - if c_overrideMSS_test == "on" or c_overrideMSS_test == no_modify: - c_overrideMSS = self._get_value_from_data(data, key="c_overrideMSS", data_index=data_index) - if c_overrideMSS != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientOverrideMSS_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientOverrideMSS_posXpath).send_keys(c_overrideMSS) - self.driver.find_element(By.XPATH, "//label[text()='Override MSS']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_no_delay", element_position=tcpProxyProfiles_switch_clientNoDelay_posXpath) - c_ttl = self._get_value_from_data(data, key="c_ttl", data_index=data_index) - if c_ttl != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientTTL_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientTTL_posXpath).send_keys(c_ttl) - self.driver.find_element(By.XPATH, "//label[text()='TTL']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_keepalive", element_position=tcpProxyProfiles_switch_clientKeepalive_posXpath) - c_keepalive_test = self._get_value_from_data(data, key="c_keepalive", data_index=data_index) - if c_keepalive_test == "on": - c_probe_number = self._get_value_from_data(data, key="c_probe_number", data_index=data_index) - if c_probe_number != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientProbeNumber_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientProbeNumber_posXpath).send_keys(c_probe_number) - self.driver.find_element(By.XPATH, "//label[text()='Probe Number']").click() - c_idle_time = self._get_value_from_data(data, key="c_idle_time", data_index=data_index) - if c_idle_time != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientIdleTime_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientIdleTime_posXpath).send_keys(c_idle_time) - self.driver.find_element(By.XPATH, "//label[text()='Idle Time']").click() - c_interval = self._get_value_from_data(data, key="c_interval", data_index=data_index) - if c_interval != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientInterval_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientInterval_posXpath).send_keys(c_interval) - self.driver.find_element(By.XPATH, "//label[text()='Interval']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="c_UserTimeout_switch", - element_position=tcpProxyProfiles_switch_clientUserTimeout_posXpath) - c_UserTimeout_test = self._get_value_from_data(data, key="c_UserTimeout_switch", data_index=data_index) - if c_UserTimeout_test == "on" or c_overrideMSS_test == no_modify: - c_userTimeout = self._get_value_from_data(data, key="c_userTimeout", data_index=data_index) - if c_userTimeout != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientUserTimeout_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_clientUserTimeout_posXpath).send_keys(c_userTimeout) - self.driver.find_element(By.XPATH, "//label[text()='User Timeout']").click() - - # Server_side 开关操作 override MSS, no delay, keepalive - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_override_MSS", element_position=tcpProxyProfiles_switch_serverOverrideMSS_posXpath) - # Server_side 输入操作 override MSS, ttl, user Timeout - s_overrideMSS_test = self._get_value_from_data(data, key="s_override_MSS", data_index=data_index) - if s_overrideMSS_test == "on": - s_overrideMSS = self._get_value_from_data(data, key="s_overrideMSS", data_index=data_index) - if s_overrideMSS != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverOverrideMSS_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverOverrideMSS_posXpath).send_keys(s_overrideMSS) - self.driver.find_element(By.XPATH, "//label[text()='Override MSS']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_no_delay", element_position=tcpProxyProfiles_switch_serverNoDelay_posXpath) - s_ttl = self._get_value_from_data(data, key="s_ttl", data_index=data_index) - if s_ttl != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverTTL_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverTTL_posXpath).send_keys(s_ttl) - self.driver.find_element(By.XPATH, "//label[text()='TTL']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_keepalive", element_position=tcpProxyProfiles_switch_serverKeepalive_posXpath) - s_keepalive_test = self._get_value_from_data(data, key="s_keepalive", data_index=data_index) - if s_keepalive_test == "on": - s_probe_number = self._get_value_from_data(data, key="s_probe_number", data_index=data_index) - if s_probe_number != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverProbeNumber_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverProbeNumber_posXpath).send_keys(s_probe_number) - self.driver.find_element(By.XPATH, "//label[text()='Probe Number']").click() - s_idle_time = self._get_value_from_data(data, key="s_idle_time", data_index=data_index) - if s_idle_time != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverIdleTime_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverIdleTime_posXpath).send_keys(s_idle_time) - self.driver.find_element(By.XPATH, "//label[text()='Idle Time']").click() - s_interval = self._get_value_from_data(data, key="s_interval", data_index=data_index) - if s_interval != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverInterval_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverInterval_posXpath).send_keys(s_interval) - self.driver.find_element(By.XPATH, "//label[text()='Interval']").click() - - self._operate_switch(data, data_index=data_index, no_modify=no_modify, switch_name="s_UserTimeout_switch", - element_position=tcpProxyProfiles_switch_serverUserTimeout_posXpath) - s_UserTimeout_test = self._get_value_from_data(data, key="s_UserTimeout_switch", data_index=data_index) - if s_UserTimeout_test == "on": - s_userTimeout = self._get_value_from_data(data, key="s_userTimeout", data_index=data_index) - if s_userTimeout != no_modify: # 判断不修改时跳过操作 - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverUserTimeout_posXpath).clear() - self.driver.find_element(By.XPATH, tcpProxyProfiles_input_serverUserTimeout_posXpath).send_keys(s_userTimeout) - self.driver.find_element(By.XPATH, "//label[text()='User Timeout']").click() - - def _operate_switch(self, data: {}, data_index=0, no_modify="不修改", switch_name="", element_position=""): - """ - 页面中,开关状态操作。 - :param data: - :param data_index: 0 -1来自上层 - :param no_modify:"不修改" 来自上次调用 - :param operation_type: create modify两个状态, - :param switch_name: 数据中开关名称 - :param elementPosition: 开关的定位 - :return: - """ - #解析输入data数据的数据 - switch_value: str = self._get_value_from_data(data, key=switch_name, data_index=data_index) #提取数据 - # print("switch_value=", switch_value) - if switch_value != no_modify: #编辑时,有不修改则不用执行 - #先获取当前页面中开关状态 include_root_current_class : class="el-switch is-checked" 有is-checked当前状态为开,没有is-chedked当前状态为关 - swith_current_class = self.driver.find_element(By.XPATH, element_position).get_attribute("class") - if "on" == switch_value: #数据中为开 - if "is-checked" in swith_current_class: #页面当前为开,不用点击开关 - pass - else: #页面当前为关,需要点击开关 - self.driver.find_element(By.XPATH, element_position).click() - else: #用例输入为关 - if "is-checked" in swith_current_class: #页面当前为开,需要点击开关 - self.driver.find_element(By.XPATH, element_position).click() - else: #页面当前为关,不用点击开关 - pass - - def _get_value_from_data(self, data, key="", data_index=0): - """ - 从data数据中,根据key提取值 - :param data: - :param key: - :param data_index: 0 -1 - :return: - """ - try: - switch_value: str = data[key].split("->")[data_index].strip().lower() #用例数据 - except Exception as e: - print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True) - return switch_value - - - - diff --git a/support/ui_utils/profiles/delete_profiles.py b/support/ui_utils/profiles/delete_profiles.py index 98c52158f..e18a5546e 100644 --- a/support/ui_utils/profiles/delete_profiles.py +++ b/support/ui_utils/profiles/delete_profiles.py @@ -9,7 +9,10 @@ class DeleteProfiles: def __init__(self, driver): self.driver = driver - def delete(self, profile_element_position, first_row_checkbox_element, is_del_all=False): + def delete_profiles(self, profiles_tuple): + profile_element_position = {} + first_row_checkbox_element = {} + is_del_all = False delete_element_position = profile_element_position["delete"] try: if is_del_all == True: diff --git a/support/ui_utils/profiles/edit_profiles.py b/support/ui_utils/profiles/edit_profiles.py index eb1dbc4e8..2e811a613 100644 --- a/support/ui_utils/profiles/edit_profiles.py +++ b/support/ui_utils/profiles/edit_profiles.py @@ -4,4 +4,8 @@ import time class EditProfiles: def __init__(self, driver): - self.driver = driver \ No newline at end of file + self.driver = driver + + def edit_profiles(self, policy_configuration): + if "action_parameter" not in policy_configuration.keys(): + return "" \ No newline at end of file diff --git a/support/ui_utils/profiles/elemen_position.py b/support/ui_utils/profiles/elemen_position.py new file mode 100644 index 000000000..1709d0053 --- /dev/null +++ b/support/ui_utils/profiles/elemen_position.py @@ -0,0 +1,49 @@ +# -*- coding: UTF-8 -*- +from support.ui_utils.element_position.map_element_position_library import get_element_position + + +class ElementPosition: + def __init__(self, policy_configuration): + self.policy_configuration = policy_configuration + + @property + def get_profile_element_position_library(self): + """ + 从action_parameter中提取要定位的key_name,再从map_element_position_library到入定位信息 + """ + action_parameter = self.policy_configuration["action_parameter"] + key_name = "" + if "html_profile" in action_parameter: + key_name = "response_page" + elif "resolution" in action_parameter: + # 需要处理 + key_name = "dns_resource_record" + elif "traffic_mirroring" in action_parameter: + key_name = "traffic_mirroring_profile" + elif "keyring_for_trusted" in action_parameter: + key_name = "ssl_decryption_keyring" + elif "keyring_for_untrusted" in action_parameter: + key_name = "ssl_decryption_keyring" + elif "decryption_profile" in action_parameter: + key_name = "ssl_decryption_profile" + elif "tcp_option_profile" in action_parameter: + key_name = "tcp_option_profile" + elif "replacement_file" in action_parameter: + key_name = "replacement_file" + elif "js_file" in action_parameter: + key_name = "js_file" + elif "css_file" in action_parameter: + key_name = "css_file" + elif "lua_script" in action_parameter: + key_name = "run_script" + elif "profile_chain" in action_parameter: + key_name = "shaping_profile" + elif "sff_profiles" in action_parameter: + # 需要处理 + key_name = "sff_profiles" + elif "template_profile" in action_parameter: + key_name = "statistics_template" + else: + return {}, key_name + element_position_library = get_element_position(key_name) + return element_position_library, key_name diff --git a/support/ui_utils/profiles/search_profiles.py b/support/ui_utils/profiles/search_profiles.py index 8f73005e3..b0244ff97 100644 --- a/support/ui_utils/profiles/search_profiles.py +++ b/support/ui_utils/profiles/search_profiles.py @@ -41,3 +41,6 @@ class SearchProfiles: else: return 400, "" + def get_profiles_uuid(self, policy_configuration): + if "action_parameter" not in policy_configuration.keys(): + return [], "" diff --git a/support/ui_utils/ui_client.py b/support/ui_utils/ui_client.py index 06260a4d8..8443dc728 100644 --- a/support/ui_utils/ui_client.py +++ b/support/ui_utils/ui_client.py @@ -53,7 +53,7 @@ class UIClient: def create_profiles(self, policy_configuration): profiles = CreateProfiles(self.driver) - error = profiles.create_profiles(policy_configuration) + error = profiles.create_profiles(self.parameter, policy_configuration) return error def search_profiles(self, policy_configuration, search_type): @@ -64,9 +64,17 @@ class UIClient: def get_profiles_uuid(self, policy_configuration): profiles = SearchProfiles(self.driver) profiles_tuple, error = profiles.get_profiles_uuid(policy_configuration) - # self.profiles_tuple = profiles_tuple return profiles_tuple, error + def edit_profiles(self, policy_configuration): + profiles = EditProfiles(self.driver) + error = profiles.edit_profiles(policy_configuration) + return error + + def delete_profiles(self, profiles_tuple): + profiles = DeleteProfiles(self.driver) + profiles.delete_profiles(profiles_tuple) + def create_rules(self, policy_configuration): self.policy_configuration = policy_configuration rules = CreateRules(self.driver) diff --git a/tests/manipulation/111man_allow_srcip_dstip_http.py b/tests/manipulation/111man_allow_srcip_dstip_http.py index 15f7f8a0d..f87b14a92 100644 --- a/tests/manipulation/111man_allow_srcip_dstip_http.py +++ b/tests/manipulation/111man_allow_srcip_dstip_http.py @@ -10,6 +10,7 @@ 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 +import traceback def run(parameter): @@ -75,6 +76,16 @@ def run(parameter): }], "is_enabled": 1, "log_option": "metadata", + "action_parameter": { + "sub_action": "block", + "manipulation_block": "warning_page", + "code": 403, + "html_profile": { + "name": "test", + "format": "html", + "file_path": "Response-Pages_1.html" + } + } } traffic_generation = { @@ -96,26 +107,26 @@ def run(parameter): if parameter["initiation_method"] == "ui": ui_client = UIClient(parameter) """ - created_object_code = ui_client.create_objects(policy_configuration) - if created_object_code != 200: - return "Fail to create object." - objects_tuple, searched_object_code = ui_client.search_objects(policy_configuration, "name") - if searched_object_code != 200: - return "Fail to search object." + ui_error = ui_client.create_objects(policy_configuration) + if len(ui_error) > 0: + return ui_error + objects_tuple, ui_error = ui_client.get_objects_uuid(policy_configuration) + if len(ui_error) > 0: + return ui_error """ - created_profile_code = ui_client.create_profiles(policy_configuration) - if created_profile_code != 200: - return "Fail to create profile." - profiles_tuple, searched_profile_code = ui_client.search_profiles(policy_configuration) - if searched_profile_code != 200: - return "Fail to search profile." + ui_error = ui_client.create_profiles(policy_configuration) + if len(ui_error )> 0: + return ui_error + profiles_tuple, ui_error = ui_client.get_profiles_uuid(policy_configuration) + if len(ui_error) > 0: + return ui_error """ - created_rule_code = ui_client.create_rules(policy_configuration) - if created_rule_code != 200: - return "Fail to create rule." - rules_tuple, searched_rule_code = ui_client.search_rules(policy_configuration, "name") - if searched_rule_code != 200: - return "Fail to search rule." + ui_error = ui_client.create_rules(policy_configuration) + if len(ui_error) > 0: + return ui_error + rules_tuple, ui_error = ui_client.get_rules_uuid(policy_configuration) + if len(ui_error) > 0: + return ui_error """ elif parameter["initiation_method"] == "api": api_client = APIClient(parameter) @@ -135,7 +146,8 @@ def run(parameter): # 等待下发配置生效 time.sleep(3) - + return 0 + """ # 类实例化 generator = TrafficGenerator() @@ -188,15 +200,17 @@ def run(parameter): test_summary["metric"] = metric_result 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], "When running test case, the exception error: ", str(e), flush=True) + raise return "When running test case, the exception error: " + str(e) finally: # 删除 if parameter["initiation_method"] == "ui": if rules_tuple: - ui_client.delete_rules(parameter, policy_configuration) + ui_client.delete_rules(rules_tuple) if objects_tuple: ui_client.delete_objects(objects_tuple) if profiles_tuple: -- cgit v1.2.3