|
| 1 | +import logging |
| 2 | +import pytest |
| 3 | + |
| 4 | +from tests.common.helpers.assertions import pytest_assert |
| 5 | +from tests.generic_config_updater.gu_utils import apply_patch, expect_op_success, expect_res_success |
| 6 | +from tests.generic_config_updater.gu_utils import generate_tmpfile, delete_tmpfile |
| 7 | +from tests.generic_config_updater.gu_utils import create_checkpoint, delete_checkpoint, rollback, rollback_or_reload |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | +MONITOR_CONFIG_TEST_CP = "monitor_config_test" |
| 12 | +MONITOR_CONFIG_INITIAL_CP = "monitor_config_initial" |
| 13 | +MONITOR_CONFIG_ACL_TABLE = "EVERFLOW_DSCP" |
| 14 | +MONITOR_CONFIG_ACL_RULE = "RULE_1" |
| 15 | +MONITOR_CONFIG_MIRROR_SESSION = "mirror_session_dscp" |
| 16 | +MONITOR_CONFIG_POLICER = "policer_dscp" |
| 17 | + |
| 18 | +@pytest.fixture(scope='module') |
| 19 | +def get_valid_acl_ports(cfg_facts): |
| 20 | + """ Get valid acl ports that could be added to ACL table |
| 21 | + valid ports refers to the portchannels and ports not belongs portchannel |
| 22 | + """ |
| 23 | + ports = set() |
| 24 | + portchannel_members = set() |
| 25 | + |
| 26 | + portchannel_member_dict = cfg_facts.get('PORTCHANNEL_MEMBER', {}) |
| 27 | + for po, po_members in portchannel_member_dict.items(): |
| 28 | + ports.add(po) |
| 29 | + for po_member in po_members: |
| 30 | + portchannel_members.add(po_member) |
| 31 | + |
| 32 | + port_dict = cfg_facts.get('PORT', {}) |
| 33 | + for key in port_dict: |
| 34 | + if key not in portchannel_members: |
| 35 | + ports.add(key) |
| 36 | + |
| 37 | + return list(ports) |
| 38 | + |
| 39 | +def bgp_monitor_config_cleanup(duthost): |
| 40 | + """ Test requires no monitor config |
| 41 | + Clean up current monitor config if existed |
| 42 | + """ |
| 43 | + cmds = [] |
| 44 | + cmds.append('sonic-db-cli CONFIG_DB del "ACL_TABLE|{}"'.format(MONITOR_CONFIG_ACL_TABLE)) |
| 45 | + cmds.append('sonic-db-cli CONFIG_DB del "ACL_RULE|{}|{}"'.format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) |
| 46 | + cmds.append('sonic-db-cli CONFIG_DB del "MIRROR_SESSION|{}"'.format(MONITOR_CONFIG_MIRROR_SESSION)) |
| 47 | + cmds.append('sonic-db-cli CONFIG_DB del "POLICER|everflow_static_policer"'.format(MONITOR_CONFIG_POLICER)) |
| 48 | + |
| 49 | + output = duthost.shell_cmds(cmds=cmds)['results'] |
| 50 | + for res in output: |
| 51 | + pytest_assert(not res['rc'], |
| 52 | + "bgp monitor config cleanup failed." |
| 53 | + ) |
| 54 | + |
| 55 | +@pytest.fixture(autouse=True) |
| 56 | +def setup_env(duthosts, rand_one_dut_hostname): |
| 57 | + """ |
| 58 | + Setup/teardown fixture for syslog config |
| 59 | +
|
| 60 | + Args: |
| 61 | + duthosts: list of DUTs. |
| 62 | + rand_selected_dut: The fixture returns a randomly selected DuT. |
| 63 | + """ |
| 64 | + duthost = duthosts[rand_one_dut_hostname] |
| 65 | + create_checkpoint(duthost) |
| 66 | + |
| 67 | + yield |
| 68 | + |
| 69 | + try: |
| 70 | + logger.info("Rolled back to original checkpoint") |
| 71 | + rollback_or_reload(duthost) |
| 72 | + |
| 73 | + finally: |
| 74 | + delete_checkpoint(duthost) |
| 75 | + |
| 76 | +def verify_monitor_config(duthost): |
| 77 | + """ |
| 78 | + This config contains 4 parts: ACL_TABLE, ACL_RULE, POLICER, MIRROR_SESSION |
| 79 | +
|
| 80 | + admin@vlab-01:~$ show acl table EVERFLOW_DSCP_TEST |
| 81 | + Name Type Binding Description Stage |
| 82 | + ------------------ ----------- --------- ------------------ ------- |
| 83 | + EVERFLOW_DSCP_TEST MIRROR_DSCP Ethernet0 EVERFLOW_DSCP_TEST ingress |
| 84 | + ... |
| 85 | + |
| 86 | + admin@vlab-01:~$ show acl rule EVERFLOW_DSCP_TEST RULE_1 |
| 87 | + Table Rule Priority Action Match |
| 88 | + ------------------ ------ ---------- ---------------------------------------- ------- |
| 89 | + EVERFLOW_DSCP_TEST RULE_1 9999 MIRROR INGRESS: mirror_session_dscp_test DSCP: 5 |
| 90 | +
|
| 91 | + admin@vlab-01:~/everflow$ show policer everflow_static_policer |
| 92 | + Name Type Mode CIR CBS |
| 93 | + ----------------------- ------ ------ -------- -------- |
| 94 | + everflow_policer_test bytes sr_tcm 12500000 12500000 |
| 95 | +
|
| 96 | + admin@vlab-01:~$ show mirror_session mirror_session_dscp_test |
| 97 | + ERSPAN Sessions |
| 98 | + Name Status SRC IP DST IP GRE DSCP TTL Queue Policer Monitor Port SRC Port Direction |
| 99 | + ------------------------ -------- -------- -------- ----- ------ ----- ------- ----------------------- -------------- ---------- ----------- |
| 100 | + mirror_session_dscp_test active 1.1.1.1 2.2.2.2 5 32 everflow_policer_test |
| 101 | + ... |
| 102 | + """ |
| 103 | + table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) |
| 104 | + expect_res_success(duthost, table, [MONITOR_CONFIG_ACL_TABLE], []) |
| 105 | + |
| 106 | + rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) |
| 107 | + expect_res_success(duthost, rule, [ |
| 108 | + MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION], []) |
| 109 | + |
| 110 | + policer = duthost.shell("show policer {}".format(MONITOR_CONFIG_POLICER)) |
| 111 | + expect_res_success(duthost, policer, [MONITOR_CONFIG_POLICER], []) |
| 112 | + |
| 113 | + mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) |
| 114 | + expect_res_success(duthost, mirror_session, [ |
| 115 | + MONITOR_CONFIG_MIRROR_SESSION, MONITOR_CONFIG_POLICER], []) |
| 116 | + |
| 117 | +def verify_no_monitor_config(duthost): |
| 118 | + """ |
| 119 | + Clean up monitor config in ACL_TABLE, ACL_RULE, POLICER, MIRROR_SESSION |
| 120 | + """ |
| 121 | + table = duthost.shell("show acl table {}".format(MONITOR_CONFIG_ACL_TABLE)) |
| 122 | + expect_res_success(duthost, table, [], [MONITOR_CONFIG_ACL_TABLE]) |
| 123 | + |
| 124 | + rule = duthost.shell("show acl rule {} {}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE)) |
| 125 | + expect_res_success(duthost, rule, [], [ |
| 126 | + MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE, MONITOR_CONFIG_MIRROR_SESSION]) |
| 127 | + |
| 128 | + policer = duthost.shell("show policer {}".format(MONITOR_CONFIG_POLICER)) |
| 129 | + expect_res_success(duthost, policer, [], [MONITOR_CONFIG_POLICER]) |
| 130 | + |
| 131 | + mirror_session = duthost.shell("show mirror_session {}".format(MONITOR_CONFIG_MIRROR_SESSION)) |
| 132 | + expect_res_success(duthost, mirror_session, [], [ |
| 133 | + MONITOR_CONFIG_MIRROR_SESSION, MONITOR_CONFIG_POLICER]) |
| 134 | + |
| 135 | +def monitor_config_add_config(duthost, get_valid_acl_ports): |
| 136 | + """ Test to add everflow always on config |
| 137 | + """ |
| 138 | + json_patch = [ |
| 139 | + { |
| 140 | + "op": "add", |
| 141 | + "path": "/ACL_TABLE/{}".format(MONITOR_CONFIG_ACL_TABLE), |
| 142 | + "value": { |
| 143 | + "policy_desc": "{}".format(MONITOR_CONFIG_ACL_TABLE), |
| 144 | + "ports": get_valid_acl_ports, |
| 145 | + "stage": "ingress", |
| 146 | + "type": "MIRROR_DSCP" |
| 147 | + } |
| 148 | + }, |
| 149 | + { |
| 150 | + "op": "add", |
| 151 | + "path": "/ACL_RULE", |
| 152 | + "value": { |
| 153 | + "{}|{}".format(MONITOR_CONFIG_ACL_TABLE, MONITOR_CONFIG_ACL_RULE): { |
| 154 | + "DSCP": "5", |
| 155 | + "MIRROR_INGRESS_ACTION": "{}".format(MONITOR_CONFIG_MIRROR_SESSION), |
| 156 | + "PRIORITY": "9999" |
| 157 | + } |
| 158 | + } |
| 159 | + }, |
| 160 | + { |
| 161 | + "op": "add", |
| 162 | + "path": "/MIRROR_SESSION", |
| 163 | + "value": { |
| 164 | + "{}".format(MONITOR_CONFIG_MIRROR_SESSION): { |
| 165 | + "dscp": "5", |
| 166 | + "dst_ip": "2.2.2.2", |
| 167 | + "policer": "{}".format(MONITOR_CONFIG_POLICER), |
| 168 | + "src_ip": "1.1.1.1", |
| 169 | + "ttl": "32", |
| 170 | + "type": "ERSPAN" |
| 171 | + } |
| 172 | + } |
| 173 | + }, |
| 174 | + { |
| 175 | + "op": "add", |
| 176 | + "path": "/POLICER", |
| 177 | + "value": { |
| 178 | + "{}".format(MONITOR_CONFIG_POLICER): { |
| 179 | + "meter_type": "bytes", |
| 180 | + "mode": "sr_tcm", |
| 181 | + "cir": "12500000", |
| 182 | + "cbs": "12500000", |
| 183 | + "red_packet_action": "drop" |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + ] |
| 188 | + |
| 189 | + tmpfile = generate_tmpfile(duthost) |
| 190 | + logger.info("tmpfile {}".format(tmpfile)) |
| 191 | + |
| 192 | + try: |
| 193 | + output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile) |
| 194 | + expect_op_success(duthost, output) |
| 195 | + |
| 196 | + verify_monitor_config(duthost) |
| 197 | + finally: |
| 198 | + delete_tmpfile(duthost, tmpfile) |
| 199 | + |
| 200 | +def test_monitor_config_tc1_suite(duthost, get_valid_acl_ports): |
| 201 | + """ Test enable/disable EverflowAlwaysOn config |
| 202 | + """ |
| 203 | + # Step 1: Create checkpoint at initial state where no monitor config exist |
| 204 | + bgp_monitor_config_cleanup(duthost) |
| 205 | + create_checkpoint(duthost, MONITOR_CONFIG_INITIAL_CP) |
| 206 | + |
| 207 | + # Step 2: Add EverflowAlwaysOn config to duthost |
| 208 | + monitor_config_add_config(duthost, get_valid_acl_ports) |
| 209 | + |
| 210 | + # Step 3: Create checkpoint that containing desired EverflowAlwaysOn config |
| 211 | + create_checkpoint(duthost, MONITOR_CONFIG_TEST_CP) |
| 212 | + |
| 213 | + try: |
| 214 | + # Step 4: Rollback to initial state disabling monitor config |
| 215 | + output = rollback(duthost, MONITOR_CONFIG_INITIAL_CP) |
| 216 | + pytest_assert( |
| 217 | + not output['rc'] and "Config rolled back successfull" in output['stdout'], |
| 218 | + "config rollback to {} failed.".format(MONITOR_CONFIG_INITIAL_CP) |
| 219 | + ) |
| 220 | + verify_no_monitor_config(duthost) |
| 221 | + |
| 222 | + # Step 5: Rollback to EverflowAlwaysOn config and verify |
| 223 | + output = rollback(duthost, MONITOR_CONFIG_TEST_CP) |
| 224 | + pytest_assert( |
| 225 | + not output['rc'] and "Config rolled back successfull" in output['stdout'], |
| 226 | + "config rollback to {} failed.".format(MONITOR_CONFIG_TEST_CP) |
| 227 | + ) |
| 228 | + verify_monitor_config(duthost) |
| 229 | + |
| 230 | + finally: |
| 231 | + delete_checkpoint(duthost, MONITOR_CONFIG_INITIAL_CP) |
| 232 | + delete_checkpoint(duthost, MONITOR_CONFIG_TEST_CP) |
0 commit comments