Skip to content

Commit 750d1db

Browse files
dbarashinvdyxieca
authored andcommitted
Convert IPv6 addresses to lowercase in apply-patch (#2299)
Fixes sonic-net/sonic-buildimage#11622 - What I did Convert IPv6 addresses to lowercase in apply-patch for remove op - How I did it python regex on 'remove' op in JSON patch input file - How to verify it Manual test of created bug, Unit test
1 parent 555947e commit 750d1db

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

config/main.py

+14
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,20 @@ def apply_patch(ctx, patch_file_path, format, dry_run, ignore_non_yang_tables, i
12831283
patch_as_json = json.loads(text)
12841284
patch = jsonpatch.JsonPatch(patch_as_json)
12851285

1286+
# convert IPv6 addresses to lowercase
1287+
for patch_line in patch:
1288+
if 'remove' == patch_line['op']:
1289+
match = re.search(r"(?P<prefix>/INTERFACE/\w+\|)(?P<ipv6_address>([a-fA-F0-9]{0,4}[:~]|::){1,7}[a-fA-F0-9]{0,4})"
1290+
"(?P<suffix>.*)", str.format(patch_line['path']))
1291+
if match:
1292+
prefix = match.group('prefix')
1293+
ipv6_address_str = match.group('ipv6_address')
1294+
suffix = match.group('suffix')
1295+
ipv6_address_str = ipv6_address_str.lower()
1296+
click.secho("converted ipv6 address to lowercase {} with prefix {} in value: {}"
1297+
.format(ipv6_address_str, prefix, patch_line['path']))
1298+
patch_line['path'] = prefix + ipv6_address_str + suffix
1299+
12861300
config_format = ConfigFormat[format.upper()]
12871301
GenericUpdater().apply_patch(patch, config_format, verbose, dry_run, ignore_non_yang_tables, ignore_path)
12881302

tests/ip_config_input/patch_ipv6.test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"path": "/INTERFACE/Ethernet12|FC00::1~1126",
4+
"op": "remove"
5+
}
6+
]

tests/ip_config_test.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import jsonpatch
13
import os
24
import traceback
35
from unittest import mock
@@ -8,6 +10,9 @@
810
import show.main as show
911
from utilities_common.db import Db
1012

13+
test_path = os.path.dirname(os.path.abspath(__file__))
14+
ip_config_input_path = os.path.join(test_path, "ip_config_input")
15+
1116
ERROR_MSG = "Error: IP address is not valid"
1217

1318
class TestConfigIP(object):
@@ -157,6 +162,22 @@ def test_add_del_interface_shortened_ipv6_with_leading_zeros(self):
157162
assert result.exit_code != 0
158163
assert ('Ethernet68', '3000::1/64') not in db.cfgdb.get_table('INTERFACE')
159164

165+
def test_remove_interface_case_sensitive_mock_ipv6_w_apply_patch(self):
166+
runner = CliRunner()
167+
any_patch_as_json = [{"op": "remove", "path": "/INTERFACE/Ethernet12|FC00::1~1126"}]
168+
any_patch = jsonpatch.JsonPatch(any_patch_as_json)
169+
any_patch_as_text = json.dumps(any_patch_as_json)
170+
ipv6_patch_file = os.path.join(ip_config_input_path, 'patch_ipv6.test')
171+
172+
# config apply-patch patch
173+
mock_generic_updater = mock.Mock()
174+
with mock.patch('config.main.GenericUpdater', return_value=mock_generic_updater):
175+
with mock.patch('builtins.open', mock.mock_open(read_data=any_patch_as_text)):
176+
result = runner.invoke(config.config.commands["apply-patch"], [ipv6_patch_file], catch_exceptions=False)
177+
print(result.exit_code, result.output)
178+
assert "converted ipv6 address to lowercase fc00::1~1126 with prefix /INTERFACE/Ethernet12| in value: /INTERFACE/Ethernet12|FC00::1~1126" in result.output
179+
180+
160181
@classmethod
161182
def teardown_class(cls):
162183
os.environ['UTILITIES_UNIT_TESTING'] = "0"

0 commit comments

Comments
 (0)