From 47150c954049ac7fc34add6cd4058a271416eee3 Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Wed, 6 Oct 2021 14:02:25 +0300 Subject: [PATCH 1/6] Add portchannel support Signed-off-by: d-dashkov --- config/main.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/config/main.py b/config/main.py index 90531997ee..3afd8646fc 100644 --- a/config/main.py +++ b/config/main.py @@ -890,11 +890,13 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True): try: ipaddress.ip_network(ip_prefix) if 'nexthop' in config_entry: - nh = config_entry['nexthop'].split(',') - for ip in nh: - ipaddress.ip_address(ip) + nh_list = config_entry['nexthop'].split(',') + for nh in nh_list: + # Nexthop to portchannel + if not nh.startswith('PortChannel'): + ipaddress.ip_address(nh) except ValueError: - ctx.fail("ip address is not valid.") + ctx.fail("ip address is not valid.") if not vrf_name == "": key = vrf_name + "|" + ip_prefix From 82fa6bf7fbb564fd453574b300185667e3f24772 Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Wed, 6 Oct 2021 14:13:51 +0300 Subject: [PATCH 2/6] Add portchannel check Signed-off-by: d-dashkov --- config/main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index 3afd8646fc..c79141e7ff 100644 --- a/config/main.py +++ b/config/main.py @@ -893,7 +893,11 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True): nh_list = config_entry['nexthop'].split(',') for nh in nh_list: # Nexthop to portchannel - if not nh.startswith('PortChannel'): + if nh.startswith('PortChannel'): + config_db = ctx.obj['config_db'] + if not nh in config_db.get_keys('PORTCHANNEL'): + ctx.fail("portchannel doest not exist.") + else: ipaddress.ip_address(nh) except ValueError: ctx.fail("ip address is not valid.") From d7a4d5b3680ebbfcc834e07cab34e6abfa0a78b2 Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Wed, 6 Oct 2021 14:20:56 +0300 Subject: [PATCH 3/6] Fix tabulation Signed-off-by: d-dashkov --- config/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/main.py b/config/main.py index c79141e7ff..1c38212217 100644 --- a/config/main.py +++ b/config/main.py @@ -900,7 +900,7 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True): else: ipaddress.ip_address(nh) except ValueError: - ctx.fail("ip address is not valid.") + ctx.fail("ip address is not valid.") if not vrf_name == "": key = vrf_name + "|" + ip_prefix From 8117378db1a31eb477bf982ac949b87157756724 Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Wed, 6 Oct 2021 19:03:55 +0300 Subject: [PATCH 4/6] Add test Signed-off-by: d-dashkov --- tests/static_routes_test.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/static_routes_test.py b/tests/static_routes_test.py index c354cb97c4..9f0944c687 100644 --- a/tests/static_routes_test.py +++ b/tests/static_routes_test.py @@ -51,6 +51,32 @@ def test_simple_static_route(self): print(result.exit_code, result.output) assert not '1.2.3.4/32' in db.cfgdb.get_table('STATIC_ROUTE') + def test_portchannel_static_route(self): + db = Db() + runner = CliRunner() + obj = {'config_db':db.cfgdb} + + # config portchannel add PortChannel0101 + result = runner.invoke(config.config.commands["portchannel"].commands["add"], ["PortChannel0101"], obj=obj) + print(result.exit_code, result.output) + + # config route add prefix 1.2.3.4/32 nexthop PortChannel0101 + result = runner.invoke(config.config.commands["route"].commands["add"], \ + ["prefix", "1.2.3.4/32", "nexthop", "PortChannel0101"], obj=obj) + print(result.exit_code, result.output) + assert ('1.2.3.4/32') in db.cfgdb.get_table('STATIC_ROUTE') + assert db.cfgdb.get_entry('STATIC_ROUTE', '1.2.3.4/32') == {'nexthop': 'PortChannel0101', 'blackhole': 'false', 'distance': '0', 'ifname': '', 'nexthop-vrf': ''} + + # config route del prefix 1.2.3.4/32 nexthop PortChannel0101 + result = runner.invoke(config.config.commands["route"].commands["del"], \ + ["prefix", "1.2.3.4/32", "nexthop", "PortChannel0101"], obj=obj) + print(result.exit_code, result.output) + assert not '1.2.3.4/32' in db.cfgdb.get_table('STATIC_ROUTE') + + # config portchannel del PortChannel0101 + result = runner.invoke(config.config.commands["portchannel"].commands["del"], ["PortChannel0101"], obj=obj) + print(result.exit_code, result.output) + def test_static_route_invalid_prefix_ip(self): db = Db() runner = CliRunner() From 96079bfdc39de4786f81758b37b70b6fbe4302be Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Thu, 7 Oct 2021 10:40:45 +0300 Subject: [PATCH 5/6] Change test and fix spelling mistake Signed-off-by: d-dashkov --- config/main.py | 2 +- tests/static_routes_test.py | 22 +++++----------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/config/main.py b/config/main.py index 1c38212217..b778296cbe 100644 --- a/config/main.py +++ b/config/main.py @@ -896,7 +896,7 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True): if nh.startswith('PortChannel'): config_db = ctx.obj['config_db'] if not nh in config_db.get_keys('PORTCHANNEL'): - ctx.fail("portchannel doest not exist.") + ctx.fail("portchannel does not exist.") else: ipaddress.ip_address(nh) except ValueError: diff --git a/tests/static_routes_test.py b/tests/static_routes_test.py index 9f0944c687..34f08a821d 100644 --- a/tests/static_routes_test.py +++ b/tests/static_routes_test.py @@ -25,6 +25,9 @@ ERROR_INVALID_IP = ''' Error: ip address is not valid. ''' +ERROR_INVALID_PORTCHANNEL = ''' +Error: portchannel doest not exist. +''' class TestStaticRoutes(object): @@ -51,31 +54,16 @@ def test_simple_static_route(self): print(result.exit_code, result.output) assert not '1.2.3.4/32' in db.cfgdb.get_table('STATIC_ROUTE') - def test_portchannel_static_route(self): + def test_invalid_portchannel_static_route(self): db = Db() runner = CliRunner() obj = {'config_db':db.cfgdb} - # config portchannel add PortChannel0101 - result = runner.invoke(config.config.commands["portchannel"].commands["add"], ["PortChannel0101"], obj=obj) - print(result.exit_code, result.output) - # config route add prefix 1.2.3.4/32 nexthop PortChannel0101 result = runner.invoke(config.config.commands["route"].commands["add"], \ ["prefix", "1.2.3.4/32", "nexthop", "PortChannel0101"], obj=obj) print(result.exit_code, result.output) - assert ('1.2.3.4/32') in db.cfgdb.get_table('STATIC_ROUTE') - assert db.cfgdb.get_entry('STATIC_ROUTE', '1.2.3.4/32') == {'nexthop': 'PortChannel0101', 'blackhole': 'false', 'distance': '0', 'ifname': '', 'nexthop-vrf': ''} - - # config route del prefix 1.2.3.4/32 nexthop PortChannel0101 - result = runner.invoke(config.config.commands["route"].commands["del"], \ - ["prefix", "1.2.3.4/32", "nexthop", "PortChannel0101"], obj=obj) - print(result.exit_code, result.output) - assert not '1.2.3.4/32' in db.cfgdb.get_table('STATIC_ROUTE') - - # config portchannel del PortChannel0101 - result = runner.invoke(config.config.commands["portchannel"].commands["del"], ["PortChannel0101"], obj=obj) - print(result.exit_code, result.output) + assert ERROR_INVALID_PORTCHANNEL in result.output def test_static_route_invalid_prefix_ip(self): db = Db() From e8b087b011642030210c5e79b83177ef0d983c62 Mon Sep 17 00:00:00 2001 From: d-dashkov Date: Wed, 20 Oct 2021 09:15:23 +0300 Subject: [PATCH 6/6] Fix typo in test Signed-off-by: d-dashkov --- tests/static_routes_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/static_routes_test.py b/tests/static_routes_test.py index 34f08a821d..5704844aef 100644 --- a/tests/static_routes_test.py +++ b/tests/static_routes_test.py @@ -26,7 +26,7 @@ Error: ip address is not valid. ''' ERROR_INVALID_PORTCHANNEL = ''' -Error: portchannel doest not exist. +Error: portchannel does not exist. '''