Skip to content

Commit 3af8ba4

Browse files
authored
Replace cmp in acl_loader with operator.eq (sonic-net#2328)
cmp is deprecated in python3. Use operator.eq instead Signed-off-by: Zhaohui Sun <[email protected]>
1 parent 899ba12 commit 3af8ba4

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

acl_loader/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ipaddress
55
import json
66
import syslog
7+
import operator
78

89
import openconfig_acl
910
import tabulate
@@ -758,7 +759,7 @@ def incremental_update(self):
758759
namespace_configdb.mod_entry(self.ACL_RULE, key, None)
759760

760761
for key in existing_controlplane_rules:
761-
if cmp(self.rules_info[key], self.rules_db_info[key]) != 0:
762+
if not operator.eq(self.rules_info[key], self.rules_db_info[key]):
762763
self.configdb.set_entry(self.ACL_RULE, key, self.rules_info[key])
763764
# Program for per-asic namespace corresponding to front asic also if present.
764765
# For control plane ACL it's not needed but to keep all db in sync program everywhere

tests/acl_input/incremental_1.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"acl": {
3+
"acl-sets": {
4+
"acl-set": {
5+
"ntp-acl": {
6+
"acl-entries": {
7+
"acl-entry": {
8+
"1": {
9+
"ip": {
10+
"config": {
11+
"source-ip-address": "20.0.0.12/32"
12+
}
13+
},
14+
"config": {
15+
"sequence-id": 1
16+
},
17+
"actions": {
18+
"config": {
19+
"forwarding-action": "ACCEPT"
20+
}
21+
}
22+
}
23+
}
24+
},
25+
"config": {
26+
"name": "ntp-acl"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}

tests/acl_input/incremental_2.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"acl": {
3+
"acl-sets": {
4+
"acl-set": {
5+
"ntp-acl": {
6+
"acl-entries": {
7+
"acl-entry": {
8+
"1": {
9+
"ip": {
10+
"config": {
11+
"source-ip-address": "20.0.0.12/32"
12+
}
13+
},
14+
"config": {
15+
"sequence-id": 1
16+
},
17+
"actions": {
18+
"config": {
19+
"forwarding-action": "DROP"
20+
}
21+
}
22+
}
23+
}
24+
},
25+
"config": {
26+
"name": "ntp-acl"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}

tests/acl_loader_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import pytest
4+
from unittest import mock
45

56
test_path = os.path.dirname(os.path.abspath(__file__))
67
modules_path = os.path.dirname(test_path)
@@ -200,3 +201,19 @@ def test_icmp_fields_with_non_tcp_protocol(self, acl_loader):
200201
acl_loader.rules_info = {}
201202
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/tcp_bad_protocol_number.json'))
202203
assert not acl_loader.rules_info.get("RULE_1")
204+
205+
def test_incremental_update(self, acl_loader):
206+
acl_loader.rules_info = {}
207+
acl_loader.tables_db_info['NTP_ACL'] = {
208+
"stage": "INGRESS",
209+
"type": "CTRLPLANE"
210+
}
211+
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/incremental_1.json'))
212+
acl_loader.rules_db_info = acl_loader.rules_info
213+
assert acl_loader.rules_info[(('NTP_ACL', 'RULE_1'))]["PACKET_ACTION"] == "ACCEPT"
214+
acl_loader.per_npu_configdb = None
215+
acl_loader.configdb.mod_entry = mock.MagicMock(return_value=True)
216+
acl_loader.configdb.set_entry = mock.MagicMock(return_value=True)
217+
acl_loader.load_rules_from_file(os.path.join(test_path, 'acl_input/incremental_2.json'))
218+
acl_loader.incremental_update()
219+
assert acl_loader.rules_info[(('NTP_ACL', 'RULE_1'))]["PACKET_ACTION"] == "DROP"

0 commit comments

Comments
 (0)