Skip to content

Commit

Permalink
Replace cmp in acl_loader with operator.eq (#2328)
Browse files Browse the repository at this point in the history
cmp is deprecated in python3. Use operator.eq instead

Signed-off-by: Zhaohui Sun <zhaohuisun@microsoft.com>
  • Loading branch information
ZhaohuiS authored and yxieca committed Sep 8, 2022
1 parent 4054ebb commit f7d69d4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion acl_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ipaddress
import json
import syslog
import operator

import openconfig_acl
import tabulate
Expand Down Expand Up @@ -758,7 +759,7 @@ def incremental_update(self):
namespace_configdb.mod_entry(self.ACL_RULE, key, None)

for key in existing_controlplane_rules:
if cmp(self.rules_info[key], self.rules_db_info[key]) != 0:
if not operator.eq(self.rules_info[key], self.rules_db_info[key]):
self.configdb.set_entry(self.ACL_RULE, key, self.rules_info[key])
# Program for per-asic namespace corresponding to front asic also if present.
# For control plane ACL it's not needed but to keep all db in sync program everywhere
Expand Down
32 changes: 32 additions & 0 deletions tests/acl_input/incremental_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"acl": {
"acl-sets": {
"acl-set": {
"ntp-acl": {
"acl-entries": {
"acl-entry": {
"1": {
"ip": {
"config": {
"source-ip-address": "20.0.0.12/32"
}
},
"config": {
"sequence-id": 1
},
"actions": {
"config": {
"forwarding-action": "ACCEPT"
}
}
}
}
},
"config": {
"name": "ntp-acl"
}
}
}
}
}
}
32 changes: 32 additions & 0 deletions tests/acl_input/incremental_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"acl": {
"acl-sets": {
"acl-set": {
"ntp-acl": {
"acl-entries": {
"acl-entry": {
"1": {
"ip": {
"config": {
"source-ip-address": "20.0.0.12/32"
}
},
"config": {
"sequence-id": 1
},
"actions": {
"config": {
"forwarding-action": "DROP"
}
}
}
}
},
"config": {
"name": "ntp-acl"
}
}
}
}
}
}
17 changes: 17 additions & 0 deletions tests/acl_loader_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import os
import pytest
from unittest import mock

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
Expand Down Expand Up @@ -200,3 +201,19 @@ def test_icmp_fields_with_non_tcp_protocol(self, acl_loader):
acl_loader.rules_info = {}
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/tcp_bad_protocol_number.json'))
assert not acl_loader.rules_info.get("RULE_1")

def test_incremental_update(self, acl_loader):
acl_loader.rules_info = {}
acl_loader.tables_db_info['NTP_ACL'] = {
"stage": "INGRESS",
"type": "CTRLPLANE"
}
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/incremental_1.json'))
acl_loader.rules_db_info = acl_loader.rules_info
assert acl_loader.rules_info[(('NTP_ACL', 'RULE_1'))]["PACKET_ACTION"] == "ACCEPT"
acl_loader.per_npu_configdb = None
acl_loader.configdb.mod_entry = mock.MagicMock(return_value=True)
acl_loader.configdb.set_entry = mock.MagicMock(return_value=True)
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/incremental_2.json'))
acl_loader.incremental_update()
assert acl_loader.rules_info[(('NTP_ACL', 'RULE_1'))]["PACKET_ACTION"] == "DROP"

0 comments on commit f7d69d4

Please sign in to comment.