Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/cacl/test_cacl_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
SONIC_SSH_REGEX = 'OpenSSH_[\\w\\.]+ Debian'


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

duthost = duthosts[enum_rand_one_per_hwsku_hostname]
Expand Down
38 changes: 38 additions & 0 deletions tests/common/templates/default_acl_rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"acl": {
"acl-sets": {
"acl-set": {
"dataacl": {
"acl-entries": {
"acl-entry": {
"1": {
"actions": {
"config": {
"forwarding-action": "ACCEPT"
}
},
"config": {
"sequence-id": 1
},
"l2": {
"config": {
"ethertype": "2048",
"vlan_id": "1000"
}
},
"input_interface": {
"interface_ref":
{
"config": {
"interface": "Ethernet12,Ethernet16,Ethernet20,Ethernet24,Ethernet28,Ethernet32,Ethernet36,Ethernet4,Ethernet40,Ethernet44,Ethernet48,Ethernet52,Ethernet56,Ethernet60,Ethernet64,Ethernet68,Ethernet72,Ethernet76,Ethernet8"
}
}
}
}
}
}
}
}
}
}
}
41 changes: 41 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import getpass
import random
import re
import tempfile

import pytest
import yaml
Expand Down Expand Up @@ -2256,3 +2257,43 @@ def format_failure(port, failure):
# HACK: We are using set_do_not_care_scapy but it will be deprecated.
if not hasattr(Mask, "set_do_not_care_scapy"):
Mask.set_do_not_care_scapy = Mask.set_do_not_care_packet


@pytest.fixture(scope="module")
def recover_acl_rule(duthosts, enum_rand_one_per_hwsku_hostname):
duthost = duthosts[enum_rand_one_per_hwsku_hostname]

base_dir = os.path.dirname(os.path.realpath(__file__))
template_dir = os.path.join(base_dir, "common/templates")
acl_rules_template = "default_acl_rules.json"

dut_tmp_dir = "/tmp"
dut_conf_file_path = os.path.join(dut_tmp_dir, acl_rules_template)

pre_acl_rules = duthost.acl_facts()["ansible_facts"]["ansible_acl_facts"]["DATAACL"]["rules"]

yield

if pre_acl_rules:
for key, value in pre_acl_rules.items():
if key != "DEFAULT_RULE":
seq_id = key.split('_')[1]
acl_config = json.loads(open(os.path.join(template_dir, acl_rules_template)).read())
acl_entry_template = \
acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]["1"]
acl_entry_config = acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]

acl_entry_config[seq_id] = copy.deepcopy(acl_entry_template)
acl_entry_config[seq_id]["config"]["sequence-id"] = seq_id
acl_entry_config[seq_id]["l2"]["config"]["ethertype"] = value["ETHER_TYPE"]
acl_entry_config[seq_id]["l2"]["config"]["vlan_id"] = value["VLAN_ID"]
acl_entry_config[seq_id]["input_interface"]["interface_ref"]["config"]["interface"] = value["IN_PORTS"]

with tempfile.NamedTemporaryFile(suffix=".json", prefix="acl_config", mode="w") as fp:
json.dump(acl_config, fp)
fp.flush()
logger.info("Generating config for ACL rule, ACL table - DATAACL")
duthost.template(src=fp.name, dest=dut_conf_file_path, force=True)

logger.info("Applying {}".format(dut_conf_file_path))
duthost.command("acl-loader update full {}".format(dut_conf_file_path))
2 changes: 1 addition & 1 deletion tests/crm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def set_polling_interval(duthosts, enum_rand_one_per_hwsku_frontend_hostname):

@pytest.fixture(scope="module")
def collector(duthosts, enum_rand_one_per_hwsku_frontend_hostname):
""" Fixture for sharing variables beatween test cases """
""" Fixture for sharing variables between test cases """
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
data = {}
for asic in duthost.asics:
Expand Down
41 changes: 0 additions & 41 deletions tests/crm/test_crm.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,47 +860,6 @@ def recreate_acl_table(duthost, ports):
duthost.shell_cmds(cmds=cmds)


@pytest.fixture
def recover_acl_rule(duthosts, enum_rand_one_per_hwsku_frontend_hostname, enum_frontend_asic_index, collector):
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
asichost = duthost.asic_instance(enum_frontend_asic_index)

base_dir = os.path.dirname(os.path.realpath(__file__))
template_dir = os.path.join(base_dir, "templates")
acl_rules_template = "acl.json"

dut_tmp_dir = "/tmp-{}".format(asichost.asic_index)
dut_conf_file_path = os.path.join(dut_tmp_dir, acl_rules_template)

pre_acl_rules = duthost.acl_facts()["ansible_facts"]["ansible_acl_facts"]["DATAACL"]["rules"]

yield

if pre_acl_rules:
for key, value in pre_acl_rules.items():
if key != "DEFAULT_RULE":
seq_id = key.split('_')[1]
acl_config = json.loads(open(os.path.join(template_dir, acl_rules_template)).read())
acl_entry_template = \
acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]["1"]
acl_entry_config = acl_config["acl"]["acl-sets"]["acl-set"]["dataacl"]["acl-entries"]["acl-entry"]

acl_entry_config[seq_id] = copy.deepcopy(acl_entry_template)
acl_entry_config[seq_id]["config"]["sequence-id"] = seq_id
acl_entry_config[seq_id]["l2"]["config"]["ethertype"] = value["ETHER_TYPE"]
acl_entry_config[seq_id]["l2"]["config"]["vlan_id"] = value["VLAN_ID"]
acl_entry_config[seq_id]["input_interface"]["interface_ref"]["config"]["interface"] = value["IN_PORTS"]

with tempfile.NamedTemporaryFile(suffix=".json", prefix="acl_config", mode="w") as fp:
json.dump(acl_config, fp)
fp.flush()
logger.info("Generating config for ACL rule, ACL table - DATAACL")
duthost.template(src=fp.name, dest=dut_conf_file_path, force=True)

logger.info("Applying {}".format(dut_conf_file_path))
duthost.command("acl-loader update full {}".format(dut_conf_file_path))


def test_acl_entry(duthosts, enum_rand_one_per_hwsku_frontend_hostname, enum_frontend_asic_index,
collector, tbinfo, recover_acl_rule):
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
Expand Down