|
2 | 2 | import logging |
3 | 3 | import time |
4 | 4 | import ipaddress |
| 5 | +import json |
| 6 | +import re |
5 | 7 | from six.moves.urllib.parse import urlparse |
6 | 8 | from tests.common.helpers.assertions import pytest_assert |
7 | 9 | from tests.common import reboot |
@@ -117,3 +119,51 @@ def check_reboot_cause(duthost, expected_cause): |
117 | 119 | reboot_cause = get_reboot_cause(duthost) |
118 | 120 | logging.info("Checking cause from dut {} to expected {}".format(reboot_cause, expected_cause)) |
119 | 121 | return reboot_cause == expected_cause |
| 122 | + |
| 123 | + |
| 124 | +def check_copp_config(duthost): |
| 125 | + logging.info("Comparing CoPP configuration from copp_cfg.json to COPP_TABLE") |
| 126 | + copp_tables = json.loads(duthost.shell("sonic-db-dump -n APPL_DB -k COPP_TABLE* -y")["stdout"]) |
| 127 | + copp_cfg = json.loads(duthost.shell("cat /etc/sonic/copp_cfg.json")["stdout"]) |
| 128 | + feature_status = duthost.shell("show feature status")["stdout"] |
| 129 | + copp_tables_formatted = get_copp_table_formatted_dict(copp_tables) |
| 130 | + copp_cfg_formatted = get_copp_cfg_formatted_dict(copp_cfg, feature_status) |
| 131 | + pytest_assert(copp_tables_formatted == copp_cfg_formatted, |
| 132 | + "There is a difference between CoPP config and CoPP tables. CoPP config: {}\nCoPP tables:" |
| 133 | + " {}".format(copp_tables_formatted, copp_cfg_formatted)) |
| 134 | + |
| 135 | + |
| 136 | +def get_copp_table_formatted_dict(copp_tables): |
| 137 | + """ |
| 138 | + Format the copp tables output to "copp_group":{"values"} only |
| 139 | + """ |
| 140 | + formatted_dict = {} |
| 141 | + for queue_group, queue_group_value in copp_tables.items(): |
| 142 | + new_queue_group = queue_group.replace("COPP_TABLE:", "") |
| 143 | + formatted_dict.update({new_queue_group: queue_group_value["value"]}) |
| 144 | + logging.debug("Formatted copp tables dictionary: {}".format(formatted_dict)) |
| 145 | + return formatted_dict |
| 146 | + |
| 147 | + |
| 148 | +def get_copp_cfg_formatted_dict(copp_cfg, feature_status): |
| 149 | + """ |
| 150 | + Format the copp_cfg.json output to compare with copp tables |
| 151 | + """ |
| 152 | + formatted_dict = {} |
| 153 | + for trap_name, trap_value in copp_cfg["COPP_TRAP"].items(): |
| 154 | + pattern = r"{}\s+enabled".format(trap_name) |
| 155 | + trap_enabled = re.search(pattern, feature_status) |
| 156 | + if trap_value.get("always_enabled", "") or trap_enabled: |
| 157 | + trap_group = trap_value["trap_group"] |
| 158 | + if trap_group in formatted_dict: |
| 159 | + exist_trap_ids = formatted_dict[trap_group]["trap_ids"].split(",") |
| 160 | + additional_trap_ids = trap_value["trap_ids"].split(",") |
| 161 | + trap_ids = exist_trap_ids + additional_trap_ids |
| 162 | + trap_ids.sort() |
| 163 | + formatted_dict[trap_group].update({"trap_ids": ",".join(trap_ids)}) |
| 164 | + else: |
| 165 | + formatted_dict.update({trap_group: copp_cfg["COPP_GROUP"][trap_group]}) |
| 166 | + formatted_dict[trap_group].update({"trap_ids": trap_value["trap_ids"]}) |
| 167 | + formatted_dict.update({"default": copp_cfg["COPP_GROUP"]["default"]}) |
| 168 | + logging.debug("Formatted copp_cfg.json dictionary: {}".format(formatted_dict)) |
| 169 | + return formatted_dict |
0 commit comments