From 2043a7ffecf6e09226cd7a6461e2bb4da1551148 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Thu, 10 Feb 2022 09:01:47 +0000 Subject: [PATCH 1/4] monitor config test --- .../test_monitor_config.py | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 tests/generic_config_updater/test_monitor_config.py diff --git a/tests/generic_config_updater/test_monitor_config.py b/tests/generic_config_updater/test_monitor_config.py new file mode 100644 index 00000000000..001bfbcad3a --- /dev/null +++ b/tests/generic_config_updater/test_monitor_config.py @@ -0,0 +1,135 @@ +import logging +import pytest + +from tests.generic_config_updater.gu_utils import apply_patch, expect_op_success, expect_res_success +from tests.generic_config_updater.gu_utils import generate_tmpfile, delete_tmpfile +from tests.generic_config_updater.gu_utils import create_checkpoint, delete_checkpoint, rollback_or_reload + +logger = logging.getLogger(__name__) + +MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP_TEST" +MONITOR_CONFIG_ACL_RULE = "RULE_1" +MONITOR_CONFIG_MIRROR_SESSION = "mirror_session_dscp_test" + +@pytest.fixture(scope='module') +def get_valid_acl_ports(cfg_facts): + """ Get valid acl ports + """ + ports = set() + portchannel_members = set() + + portchannel_member_dict = cfg_facts.get('PORTCHANNEL_MEMBER', {}) + for po, po_members in portchannel_member_dict.items(): + ports.add(po) + for po_member in po_members: + portchannel_members.add(po_member) + + port_dict = cfg_facts.get('PORT', {}) + for key in port_dict: + if key not in portchannel_members: + ports.add(key) + + return list(ports) + +@pytest.fixture(autouse=True) +def setup_env(duthosts, rand_one_dut_hostname): + """ + Setup/teardown fixture for syslog config + + Args: + duthosts: list of DUTs. + rand_selected_dut: The fixture returns a randomly selected DuT. + """ + duthost = duthosts[rand_one_dut_hostname] + create_checkpoint(duthost) + + yield + + try: + logger.info("Rolled back to original checkpoint") + rollback_or_reload(duthost) + + finally: + delete_checkpoint(duthost) + +def test_monitor_config_tc1_add_config(duthost): + """ Test to add everflow always on config + This config contains 3 parts: ACL_TABLE, ACL_RULE, MIRROR_SESSION + + admin@vlab-01:~$ show acl table EVERFLOW_DSCP_TEST + Name Type Binding Description Stage + ------------------ ----------- --------- ------------------ ------- + EVERFLOW_DSCP_TEST MIRROR_DSCP Ethernet0 EVERFLOW_DSCP_TEST ingress + ... + + admin@vlab-01:~$ show acl rule EVERFLOW_DSCP_TEST RULE_1 + Table Rule Priority Action Match + ------------------ ------ ---------- ---------------------------------------- ------- + EVERFLOW_DSCP_TEST RULE_1 9999 MIRROR INGRESS: mirror_session_dscp_test DSCP: 5 + + admin@vlab-01:~$ show mirror_session mirror_session_dscp_test + ERSPAN Sessions + Name Status SRC IP DST IP GRE DSCP TTL Queue Policer Monitor Port SRC Port Direction + ------------------------ -------- -------- -------- ----- ------ ----- ------- --------- -------------- ---------- ----------- + mirror_session_dscp_test active 1.1.1.1 2.2.2.2 5 32 Ethernet112 + ... + + """ + + + json_patch = [ + { + "op": "add", + "path": "/ACL_TABLE/{}".format(MONITOR_CONFIG_ACL_TABLE), + "value": { + "policy_desc": "{}".format(MONITOR_CONFIG_ACL_TABLE), + "ports": get_valid_acl_ports, + "stage": "ingress", + "type": "MIRROR_DSCP" + } + }, + { + "op": "add", + "path": "/ACL_RULE", + "value": { + "{}|{}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE): { + "DSCP": "5", + "MIRROR_INGRESS_ACTION": "{}".format(MONITOR_CONFIG_MIRROR_SESSION), + "PRIORITY": "9999" + } + } + }, + { + "op": "add", + "path": "/MIRROR_SESSION", + "value": { + "{}".format(MONITOR_CONFIG_MIRROR_SESSION): { + "dscp": "5", + "dst_ip": "2.2.2.2", + "src_ip": "1.1.1.1", + "ttl": "32", + "type": "ERSPAN" + } + } + } + ] + + tmpfile = generate_tmpfile(duthost) + logger.info("tmpfile {}".format(tmpfile)) + + try: + output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile) + expect_op_success(duthost, output) + + table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) + expect_res_success(duthost, table, [MONITOR_CONFIG_ACL_TABLE], []) + + rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) + expect_res_success(duthost, rule, + [MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION], []) + + mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) + expect_res_success(duthost, mirror_session, [MONITOR_CONFIG_MIRROR_SESSION], []) + + finally: + delete_tmpfile(duthost, tmpfile) From 142555807cb9857fc2f7acb4672e0eb41b395bd8 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Mon, 14 Feb 2022 06:14:41 +0000 Subject: [PATCH 2/4] 1. resolve comment 2. remove yang ignored flag --- tests/generic_config_updater/gu_utils.py | 2 +- .../test_monitor_config.py | 115 +++++++++++++++--- 2 files changed, 98 insertions(+), 19 deletions(-) diff --git a/tests/generic_config_updater/gu_utils.py b/tests/generic_config_updater/gu_utils.py index c6a2b378973..0488301d373 100644 --- a/tests/generic_config_updater/gu_utils.py +++ b/tests/generic_config_updater/gu_utils.py @@ -10,7 +10,7 @@ CONTAINER_SERVICES_LIST = ["swss", "syncd", "radv", "lldp", "dhcp_relay", "teamd", "bgp", "pmon", "telemetry", "acms"] DEFAULT_CHECKPOINT_NAME = "test" -YANG_IGNORED_OPTIONS = "-i /FEATURE -i /QUEUE -i /SCHEDULER" +YANG_IGNORED_OPTIONS = "" def generate_tmpfile(duthost): """Generate temp file diff --git a/tests/generic_config_updater/test_monitor_config.py b/tests/generic_config_updater/test_monitor_config.py index 001bfbcad3a..bdbaf166a24 100644 --- a/tests/generic_config_updater/test_monitor_config.py +++ b/tests/generic_config_updater/test_monitor_config.py @@ -1,19 +1,23 @@ import logging import pytest +from tests.common.helpers.assertions import pytest_assert from tests.generic_config_updater.gu_utils import apply_patch, expect_op_success, expect_res_success from tests.generic_config_updater.gu_utils import generate_tmpfile, delete_tmpfile -from tests.generic_config_updater.gu_utils import create_checkpoint, delete_checkpoint, rollback_or_reload +from tests.generic_config_updater.gu_utils import create_checkpoint, delete_checkpoint, rollback, rollback_or_reload logger = logging.getLogger(__name__) -MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP_TEST" -MONITOR_CONFIG_ACL_RULE = "RULE_1" +MONITOR_CONFIG_TEST_CP = "monitor_config_test" +MONITOR_CONFIG_INITIAL_CP = "monitor_config_initial" +MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP_TEST" +MONITOR_CONFIG_ACL_RULE = "RULE_1" MONITOR_CONFIG_MIRROR_SESSION = "mirror_session_dscp_test" +MONITOR_CONFIG_POLICER = "everflow_policer_test" @pytest.fixture(scope='module') def get_valid_acl_ports(cfg_facts): - """ Get valid acl ports + """ Get valid acl ports that could be added to ACL table """ ports = set() portchannel_members = set() @@ -52,9 +56,9 @@ def setup_env(duthosts, rand_one_dut_hostname): finally: delete_checkpoint(duthost) -def test_monitor_config_tc1_add_config(duthost): - """ Test to add everflow always on config - This config contains 3 parts: ACL_TABLE, ACL_RULE, MIRROR_SESSION +def verify_monitor_config(duthost): + """ + This config contains 4 parts: ACL_TABLE, ACL_RULE, POLICER, MIRROR_SESSION admin@vlab-01:~$ show acl table EVERFLOW_DSCP_TEST Name Type Binding Description Stage @@ -67,16 +71,53 @@ def test_monitor_config_tc1_add_config(duthost): ------------------ ------ ---------- ---------------------------------------- ------- EVERFLOW_DSCP_TEST RULE_1 9999 MIRROR INGRESS: mirror_session_dscp_test DSCP: 5 + admin@vlab-01:~/everflow$ show policer everflow_static_policer + Name Type Mode CIR CBS + ----------------------- ------ ------ -------- -------- + everflow_policer_test bytes sr_tcm 12500000 12500000 + admin@vlab-01:~$ show mirror_session mirror_session_dscp_test ERSPAN Sessions - Name Status SRC IP DST IP GRE DSCP TTL Queue Policer Monitor Port SRC Port Direction - ------------------------ -------- -------- -------- ----- ------ ----- ------- --------- -------------- ---------- ----------- - mirror_session_dscp_test active 1.1.1.1 2.2.2.2 5 32 Ethernet112 + Name Status SRC IP DST IP GRE DSCP TTL Queue Policer Monitor Port SRC Port Direction + ------------------------ -------- -------- -------- ----- ------ ----- ------- ----------------------- -------------- ---------- ----------- + mirror_session_dscp_test active 1.1.1.1 2.2.2.2 5 32 everflow_policer_test ... + """ + table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) + expect_res_success(duthost, table, [MONITOR_CONFIG_ACL_TABLE], []) + + rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) + expect_res_success(duthost, rule, [ + MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION], []) + + policer = duthost.shell("show policer {}".format(MONITOR_CONFIG_POLICER)) + expect_res_success(duthost, policer, [MONITOR_CONFIG_POLICER], []) + + mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) + expect_res_success(duthost, mirror_session, [ + MONITOR_CONFIG_MIRROR_SESSION, MONITOR_CONFIG_POLICER], []) +def verify_no_monitor_config(duthost): """ + Clean up monitor config in ACL_TABLE, ACL_RULE, POLICER, MIRROR_SESSION + """ + table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) + expect_res_success(duthost, table, [], [MONITOR_CONFIG_ACL_TABLE]) + + rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) + expect_res_success(duthost, rule, [], [ + MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION]) + + policer = duthost.shell("show policer {}".format(MONITOR_CONFIG_POLICER)) + expect_res_success(duthost, policer, [], [MONITOR_CONFIG_POLICER]) + mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) + expect_res_success(duthost, mirror_session, [], [ + MONITOR_CONFIG_MIRROR_SESSION, MONITOR_CONFIG_POLICER]) +def monitor_config_add_config(duthost, get_valid_acl_ports): + """ Test to add everflow always on config + """ json_patch = [ { "op": "add", @@ -106,11 +147,25 @@ def test_monitor_config_tc1_add_config(duthost): "{}".format(MONITOR_CONFIG_MIRROR_SESSION): { "dscp": "5", "dst_ip": "2.2.2.2", + "policer": "{}".format(MONITOR_CONFIG_POLICER), "src_ip": "1.1.1.1", "ttl": "32", "type": "ERSPAN" } } + }, + { + "op": "add", + "path": "/POLICER", + "value": { + "{}".format(MONITOR_CONFIG_POLICER): { + "meter_type": "bytes", + "mode": "sr_tcm", + "cir": "12500000", + "cbs": "12500000", + "red_packet_action": "drop" + } + } } ] @@ -121,15 +176,39 @@ def test_monitor_config_tc1_add_config(duthost): output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile) expect_op_success(duthost, output) - table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) - expect_res_success(duthost, table, [MONITOR_CONFIG_ACL_TABLE], []) + verify_monitor_config(duthost) + finally: + delete_tmpfile(duthost, tmpfile) + +def test_monitor_config_tc1_suite(duthost, get_valid_acl_ports): + """ Test enable/disable EverflowAlwaysOn config + """ + # Step 1: Create checkpoint at initial state + create_checkpoint(duthost, MONITOR_CONFIG_INITIAL_CP) + + # Step 2: Add EverflowAlwaysOn config to duthost + monitor_config_add_config(duthost, get_valid_acl_ports) - rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) - expect_res_success(duthost, rule, - [MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION], []) + # Step 3: Create checkpoint that containing desired EverflowAlwaysOn config + create_checkpoint(duthost, MONITOR_CONFIG_TEST_CP) - mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) - expect_res_success(duthost, mirror_session, [MONITOR_CONFIG_MIRROR_SESSION], []) + try: + # Step 4: Rollback to initial state disabling monitor config + output = rollback(duthost, MONITOR_CONFIG_INITIAL_CP) + pytest_assert( + not output['rc'] and "Config rolled back successfull" in output['stdout'], + "config rollback to {} failed.".format(MONITOR_CONFIG_INITIAL_CP) + ) + verify_no_monitor_config(duthost) + + # Step 5: Rollback to EverflowAlwaysOn config and verify + output = rollback(duthost, MONITOR_CONFIG_TEST_CP) + pytest_assert( + not output['rc'] and "Config rolled back successfull" in output['stdout'], + "config rollback to {} failed.".format(MONITOR_CONFIG_TEST_CP) + ) + verify_monitor_config(duthost) finally: - delete_tmpfile(duthost, tmpfile) + delete_checkpoint(duthost, MONITOR_CONFIG_INITIAL_CP) + delete_checkpoint(duthost, MONITOR_CONFIG_TEST_CP) From fb2f48244816abf89fe9b63e9e0c740566836b53 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Wed, 16 Feb 2022 06:37:05 +0000 Subject: [PATCH 3/4] Add comment for valid acl ports --- tests/generic_config_updater/test_monitor_config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/generic_config_updater/test_monitor_config.py b/tests/generic_config_updater/test_monitor_config.py index bdbaf166a24..d91da8b5022 100644 --- a/tests/generic_config_updater/test_monitor_config.py +++ b/tests/generic_config_updater/test_monitor_config.py @@ -18,6 +18,7 @@ @pytest.fixture(scope='module') def get_valid_acl_ports(cfg_facts): """ Get valid acl ports that could be added to ACL table + valid ports refers to the portchannels and ports not belongs portchannel """ ports = set() portchannel_members = set() From 170da8b0adf578ba15bda3fdbeb911c6801a5feb Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Fri, 25 Feb 2022 07:47:59 +0000 Subject: [PATCH 4/4] Clean up monitor config before test --- .../test_monitor_config.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/generic_config_updater/test_monitor_config.py b/tests/generic_config_updater/test_monitor_config.py index d91da8b5022..7f686f05730 100644 --- a/tests/generic_config_updater/test_monitor_config.py +++ b/tests/generic_config_updater/test_monitor_config.py @@ -10,10 +10,10 @@ MONITOR_CONFIG_TEST_CP = "monitor_config_test" MONITOR_CONFIG_INITIAL_CP = "monitor_config_initial" -MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP_TEST" +MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP" MONITOR_CONFIG_ACL_RULE = "RULE_1" -MONITOR_CONFIG_MIRROR_SESSION = "mirror_session_dscp_test" -MONITOR_CONFIG_POLICER = "everflow_policer_test" +MONITOR_CONFIG_MIRROR_SESSION = "mirror_session_dscp" +MONITOR_CONFIG_POLICER = "policer_dscp" @pytest.fixture(scope='module') def get_valid_acl_ports(cfg_facts): @@ -36,6 +36,22 @@ def get_valid_acl_ports(cfg_facts): return list(ports) +def bgp_monitor_config_cleanup(duthost): + """ Test requires no monitor config + Clean up current monitor config if existed + """ + cmds = [] + cmds.append('sonic-db-cli CONFIG_DB del "ACL_TABLE|{}"'.format(MONITOR_CONFIG_ACL_TABLE)) + cmds.append('sonic-db-cli CONFIG_DB del "ACL_RULE|{}|{}"'.format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) + cmds.append('sonic-db-cli CONFIG_DB del "MIRROR_SESSION|{}"'.format(MONITOR_CONFIG_MIRROR_SESSION)) + cmds.append('sonic-db-cli CONFIG_DB del "POLICER|everflow_static_policer"'.format(MONITOR_CONFIG_POLICER)) + + output = duthost.shell_cmds(cmds=cmds)['results'] + for res in output: + pytest_assert(not res['rc'], + "bgp monitor config cleanup failed." + ) + @pytest.fixture(autouse=True) def setup_env(duthosts, rand_one_dut_hostname): """ @@ -184,7 +200,8 @@ def monitor_config_add_config(duthost, get_valid_acl_ports): def test_monitor_config_tc1_suite(duthost, get_valid_acl_ports): """ Test enable/disable EverflowAlwaysOn config """ - # Step 1: Create checkpoint at initial state + # Step 1: Create checkpoint at initial state where no monitor config exist + bgp_monitor_config_cleanup(duthost) create_checkpoint(duthost, MONITOR_CONFIG_INITIAL_CP) # Step 2: Add EverflowAlwaysOn config to duthost