Skip to content

Commit f59f2a8

Browse files
Refactor fixture recover_acl_rule and and use it in module cacl/test_cacl_function.py. (#9312)
Description of PR In module cacl/test_cacl_function.py, it will use command acl-loader delete to delete all acl rules, no matter if the testbed has DATAACL rules before test or not. If this testbed has DATAACL rules before, this deletion will cause inconsistent between previous running config and current running config, and will cause unnecessary config reload. In PR #9199 , we have a fixture recover_acl_rule to recovery acl rules. So in this PR, I refactor this fixture and use this fixture to recover acl rules in module cacl/test_cacl_function.py. What is the motivation for this PR? In module cacl/test_cacl_function.py, it will use command acl-loader delete to delete all acl rules, no matter if the testbed has DATAACL rules before test or not. If this testbed has DATAACL rules before, this deletion will cause inconsistent between previous running config and current running config, and will cause unnecessary config reload. In PR #9199 , we have a fixture recover_acl_rule to recovery acl rules. So in this PR, I refactor this fixture and use this fixture to recover acl rules in module cacl/test_cacl_function.py. How did you do it? Move fixture recover_acl_rule to tests/conftest.py and use this fixture to recover acl rules in module cacl/test_cacl_function.py. How did you verify/test it? ``` 07:34:42 conftest.core_dump_and_config_check L1893 INFO | Core dump and config check passed for cacl/test_cacl_function.py ``` Signed-off-by: Yutong Zhang <yutongzhang@microsoft.com>
1 parent 6fce03a commit f59f2a8

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed

tests/cacl/test_cacl_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
SONIC_SSH_REGEX = 'OpenSSH_[\\w\\.]+ Debian'
2020

2121

22-
def test_cacl_function(duthosts, enum_rand_one_per_hwsku_hostname, localhost, creds):
22+
def test_cacl_function(duthosts, enum_rand_one_per_hwsku_hostname, localhost, creds, recover_acl_rule):
2323
"""Test control plane ACL functionality on a SONiC device"""
2424

2525
duthost = duthosts[enum_rand_one_per_hwsku_hostname]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"acl": {
3+
"acl-sets": {
4+
"acl-set": {
5+
"dataacl": {
6+
"acl-entries": {
7+
"acl-entry": {
8+
"1": {
9+
"actions": {
10+
"config": {
11+
"forwarding-action": "ACCEPT"
12+
}
13+
},
14+
"config": {
15+
"sequence-id": 1
16+
},
17+
"l2": {
18+
"config": {
19+
"ethertype": "2048",
20+
"vlan_id": "1000"
21+
}
22+
},
23+
"input_interface": {
24+
"interface_ref":
25+
{
26+
"config": {
27+
"interface": "Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet4,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet56,Ethernet60,Ethernet64,Ethernet68,Ethernet72,Ethernet76,Ethernet8"
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}

tests/conftest.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import getpass
66
import random
77
import re
8+
import tempfile
89

910
import pytest
1011
import yaml
@@ -2259,3 +2260,43 @@ def format_failure(port, failure):
22592260
# HACK: We are using set_do_not_care_scapy but it will be deprecated.
22602261
if not hasattr(Mask, "set_do_not_care_scapy"):
22612262
Mask.set_do_not_care_scapy = Mask.set_do_not_care_packet
2263+
2264+
2265+
@pytest.fixture(scope="module")
2266+
def recover_acl_rule(duthosts, enum_rand_one_per_hwsku_hostname):
2267+
duthost = duthosts[enum_rand_one_per_hwsku_hostname]
2268+
2269+
base_dir = os.path.dirname(os.path.realpath(__file__))
2270+
template_dir = os.path.join(base_dir, "common/templates")
2271+
acl_rules_template = "default_acl_rules.json"
2272+
2273+
dut_tmp_dir = "/tmp"
2274+
dut_conf_file_path = os.path.join(dut_tmp_dir, acl_rules_template)
2275+
2276+
pre_acl_rules = duthost.acl_facts()["ansible_facts"]["ansible_acl_facts"]["DATAACL"]["rules"]
2277+
2278+
yield
2279+
2280+
if pre_acl_rules:
2281+
for key, value in pre_acl_rules.items():
2282+
if key != "DEFAULT_RULE":
2283+
seq_id = key.split('_')[1]
2284+
acl_config = json.loads(open(os.path.join(template_dir, acl_rules_template)).read())
2285+
acl_entry_template = \
2286+
acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]["1"]
2287+
acl_entry_config = acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]
2288+
2289+
acl_entry_config[seq_id] = copy.deepcopy(acl_entry_template)
2290+
acl_entry_config[seq_id]["config"]["sequence-id"] = seq_id
2291+
acl_entry_config[seq_id]["l2"]["config"]["ethertype"] = value["ETHER_TYPE"]
2292+
acl_entry_config[seq_id]["l2"]["config"]["vlan_id"] = value["VLAN_ID"]
2293+
acl_entry_config[seq_id]["input_interface"]["interface_ref"]["config"]["interface"] = value["IN_PORTS"]
2294+
2295+
with tempfile.NamedTemporaryFile(suffix=".json", prefix="acl_config", mode="w") as fp:
2296+
json.dump(acl_config, fp)
2297+
fp.flush()
2298+
logger.info("Generating config for ACL rule, ACL table - DATAACL")
2299+
duthost.template(src=fp.name, dest=dut_conf_file_path, force=True)
2300+
2301+
logger.info("Applying {}".format(dut_conf_file_path))
2302+
duthost.command("acl-loader update full {}".format(dut_conf_file_path))

tests/crm/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def set_polling_interval(duthosts, enum_rand_one_per_hwsku_frontend_hostname):
159159

160160
@pytest.fixture(scope="module")
161161
def collector(duthosts, enum_rand_one_per_hwsku_frontend_hostname):
162-
""" Fixture for sharing variables beatween test cases """
162+
""" Fixture for sharing variables between test cases """
163163
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
164164
data = {}
165165
for asic in duthost.asics:

tests/crm/test_crm.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -860,47 +860,6 @@ def recreate_acl_table(duthost, ports):
860860
duthost.shell_cmds(cmds=cmds)
861861

862862

863-
@pytest.fixture
864-
def recover_acl_rule(duthosts, enum_rand_one_per_hwsku_frontend_hostname, enum_frontend_asic_index, collector):
865-
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
866-
asichost = duthost.asic_instance(enum_frontend_asic_index)
867-
868-
base_dir = os.path.dirname(os.path.realpath(__file__))
869-
template_dir = os.path.join(base_dir, "templates")
870-
acl_rules_template = "acl.json"
871-
872-
dut_tmp_dir = "/tmp-{}".format(asichost.asic_index)
873-
dut_conf_file_path = os.path.join(dut_tmp_dir, acl_rules_template)
874-
875-
pre_acl_rules = duthost.acl_facts()["ansible_facts"]["ansible_acl_facts"]["DATAACL"]["rules"]
876-
877-
yield
878-
879-
if pre_acl_rules:
880-
for key, value in pre_acl_rules.items():
881-
if key != "DEFAULT_RULE":
882-
seq_id = key.split('_')[1]
883-
acl_config = json.loads(open(os.path.join(template_dir, acl_rules_template)).read())
884-
acl_entry_template = \
885-
acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]["1"]
886-
acl_entry_config = acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]
887-
888-
acl_entry_config[seq_id] = copy.deepcopy(acl_entry_template)
889-
acl_entry_config[seq_id]["config"]["sequence-id"] = seq_id
890-
acl_entry_config[seq_id]["l2"]["config"]["ethertype"] = value["ETHER_TYPE"]
891-
acl_entry_config[seq_id]["l2"]["config"]["vlan_id"] = value["VLAN_ID"]
892-
acl_entry_config[seq_id]["input_interface"]["interface_ref"]["config"]["interface"] = value["IN_PORTS"]
893-
894-
with tempfile.NamedTemporaryFile(suffix=".json", prefix="acl_config", mode="w") as fp:
895-
json.dump(acl_config, fp)
896-
fp.flush()
897-
logger.info("Generating config for ACL rule, ACL table - DATAACL")
898-
duthost.template(src=fp.name, dest=dut_conf_file_path, force=True)
899-
900-
logger.info("Applying {}".format(dut_conf_file_path))
901-
duthost.command("acl-loader update full {}".format(dut_conf_file_path))
902-
903-
904863
def test_acl_entry(duthosts, enum_rand_one_per_hwsku_frontend_hostname, enum_frontend_asic_index,
905864
collector, tbinfo, recover_acl_rule):
906865
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]

0 commit comments

Comments
 (0)