diff --git a/config/main.py b/config/main.py index 26e7cc9554..2ebc4f077a 100644 --- a/config/main.py +++ b/config/main.py @@ -6013,6 +6013,853 @@ def disable_use_link_local_only(ctx, interface_name): interface_dict = db.get_table(interface_type) set_ipv6_link_local_only_on_interface(db, interface_dict, interface_type, interface_name, "disable") + +def is_vaild_intf_ip_addr(ip_addr) -> bool: + """Check whether the ip address is valid""" + try: + ip_address = ipaddress.ip_interface(ip_addr) + except ValueError as err: + click.echo("IP address {} is not valid: {}".format(ip_addr, err)) + return False + + if ip_address.version == 6: + if ip_address.is_unspecified: + click.echo("IPv6 address {} is unspecified".format(str(ip_address))) + return False + elif ip_address.version == 4: + if str(ip_address.ip) == "0.0.0.0": + click.echo("IPv4 address {} is Zero".format(str(ip_address))) + return False + + if ip_address.is_multicast: + click.echo("IP address {} is multicast".format(str(ip_address))) + return False + + ip = ip_address.ip + if ip.is_loopback: + click.echo("IP address {} is loopback address".format(str(ip_address))) + return False + + return True + + +# +# 'vrrp' subgroup ('config interface vrrp ...') +# +@interface.group(cls=clicommon.AbbreviationGroup) +@click.pass_context +def vrrp(ctx): + """Vrrp configuration""" + pass + + +# +# ip subgroup ('config interface vrrp ip ...') +# +@vrrp.group(cls=clicommon.AbbreviationGroup, name='ip') +@click.pass_context +def ip(ctx): + """vrrp ip configuration """ + pass + + +@ip.command('add') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("ip_addr", metavar="", required=True) +@click.pass_context +def add_vrrp_ip(ctx, interface_name, vrrp_id, ip_addr): + """Add IPv4 address to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + if not is_vaild_intf_ip_addr(ip_addr): + ctx.abort() + if check_vrrp_ip_exist(config_db, ip_addr): + ctx.abort() + + if "/" not in ip_addr: + ctx.fail("IP address {} is missing a mask. Such as xx.xx.xx.xx/yy or xx:xx::xx/yy".format(str(ip_addr))) + + # check vip exist + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + address_list = [] + if vrrp_entry: + # update vrrp + if "vip" in vrrp_entry: + address_list = vrrp_entry.get("vip") + # add ip address + if len(address_list) >= 4: + ctx.fail("The vrrp instance {} has already configured 4 IP addresses".format(vrrp_id)) + + else: + # create new vrrp + vrrp_entry = {} + vrrp_keys = config_db.get_keys("VRRP") + if len(vrrp_keys) >= 254: + ctx.fail("Has already configured 254 vrrp instances") + intf_cfg = 0 + for key in vrrp_keys: + if key[1] == str(vrrp_id): + ctx.fail("The vrrp instance {} has already configured!".format(vrrp_id)) + if key[0] == interface_name: + intf_cfg += 1 + if intf_cfg >= 16: + ctx.fail("{} has already configured 16 vrrp instances!".format(interface_name)) + vrrp_entry["vid"] = vrrp_id + + address_list.append(ip_addr) + vrrp_entry['vip'] = address_list + + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +@ip.command('remove') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("ip_addr", metavar="", required=True) +@click.pass_context +def remove_vrrp_ip(ctx, interface_name, vrrp_id, ip_addr): + """Remove IPv4 address to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + try: + ipaddress.ip_interface(ip_addr) + except ValueError as err: + ctx.fail("IP address is not valid: {}".format(err)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("{} is not configured on the vrrp instance {}!".format(ip_addr, vrrp_id)) + + address_list = vrrp_entry.get("vip") + # address_list = vrrp_entry.get("vip") + if not address_list: + ctx.fail("{} is not configured on the vrrp instance {}!".format(ip_addr, vrrp_id)) + + # del ip address + if ip_addr in address_list: + address_list.remove(ip_addr) + else: + ctx.fail("{} is not configured on the vrrp instance {}!".format(ip_addr, vrrp_id)) + vrrp_entry['vip'] = address_list + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +# +# track interface subgroup ('config interface vrrp track_interface ...') +# +@vrrp.group(cls=clicommon.AbbreviationGroup, name='track_interface') +@click.pass_context +def track_interface(ctx): + """ vrrp track_interface configuration """ + pass + + +@track_interface.command('add') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("track_interface", metavar="", required=True) +@click.argument('priority_increment', metavar='', required=True, type=click.IntRange(10, 50), + default=20) +@click.pass_context +def add_track_interface(ctx, interface_name, vrrp_id, track_interface, priority_increment): + """add track_interface to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + track_interface = interface_alias_to_name(config_db, track_interface) + if interface_name is None: + ctx.fail("'interface_name' is None!") + if track_interface is None: + ctx.fail("'track_interface' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + table_name_t = get_interface_table_name(track_interface) + if table_name_t == "" or table_name_t == "LOOPBACK_INTERFACE": + ctx.fail("'track_interface' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if track_interface not in config_db.get_table(table_name_t): + ctx.fail("Router Interface '{}' not found".format(track_interface)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + track_entry = config_db.get_entry("VRRP_TRACK", (interface_name, str(vrrp_id), track_interface)) + if track_entry: + track_entry['priority_increment'] = priority_increment + else: + track_entry = {} + track_entry["priority_increment"] = priority_increment + + vrrp_track_keys = config_db.get_keys("VRRP_TRACK") + if vrrp_track_keys: + track_key = (interface_name, str(vrrp_id)) + count = 0 + for item in vrrp_track_keys: + subtuple1 = item[:2] + if subtuple1 == track_key: + count += 1 + + if count >= 8: + ctx.fail("The Vrrpv instance {} has already configured 8 track interfaces".format(vrrp_id)) + + config_db.set_entry("VRRP_TRACK", (interface_name, str(vrrp_id), track_interface), track_entry) + + +@track_interface.command('remove') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("track_interface", metavar="", required=True) +@click.pass_context +def remove_track_interface(ctx, interface_name, vrrp_id, track_interface): + """Remove track_interface to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + track_interface = interface_alias_to_name(config_db, track_interface) + if interface_name is None: + ctx.fail("'interface_name' is None!") + if track_interface is None: + ctx.fail("'track_interface' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + table_name_t = get_interface_table_name(track_interface) + if table_name_t == "" or table_name_t == "LOOPBACK_INTERFACE": + ctx.fail("'track_interface' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + track_entry = config_db.get_entry("VRRP_TRACK", (interface_name, str(vrrp_id), track_interface)) + if not track_entry: + ctx.fail("{} is not configured on the vrrp instance {}!".format(track_interface, vrrp_id)) + config_db.set_entry('VRRP_TRACK', (interface_name, str(vrrp_id), track_interface), None) + + +# +# 'vrrp' subcommand ('config interface vrrp priority ...') +# +@vrrp.command("priority") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('priority', metavar='', required=True, type=click.IntRange(1, 254), default=100) +@click.pass_context +def priority(ctx, interface_name, vrrp_id, priority): + """config priority to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp_entry['priority'] = priority + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +# +# 'vrrp' subcommand ('config interface vrrp adv_interval ...') +# +@vrrp.command("adv_interval") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('interval', metavar='', required=True, type=click.IntRange(1, 255), default=1) +@click.pass_context +def adv_interval(ctx, interface_name, vrrp_id, interval): + """config adv_interval to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp_entry['adv_interval'] = interval + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +# +# 'vrrp' subcommand ('config interface vrrp pre_empt ...') +# +@vrrp.command("pre_empt") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('mode', metavar='', required=True, type=click.Choice(["enabled", "disabled"])) +@click.pass_context +def pre_empt(ctx, interface_name, vrrp_id, mode): + """Config pre_empt mode to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp_entry['preempt'] = mode + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +# +# 'vrrp' subcommand ('config interface vrrp version...') +# +@vrrp.command("version") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('version', metavar='', required=True, type=click.Choice(["2", "3"]), default=3) +@click.pass_context +def version(ctx, interface_name, vrrp_id, version): + """Config vrrp packet version to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp_entry['version'] = version + config_db.set_entry("VRRP", (interface_name, str(vrrp_id)), vrrp_entry) + + +# +# 'vrrp' subcommand +# +@vrrp.command("add") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.pass_context +def add_vrrp(ctx, interface_name, vrrp_id): + """Add vrrp instance to the interface""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if vrrp_entry: + ctx.fail("{} has already configured the vrrp instance {}!".format(interface_name, vrrp_id)) + else: + vrrp_keys = config_db.get_keys("VRRP") + if len(vrrp_keys) >= 254: + ctx.fail("Has already configured 254 vrrp instances!") + intf_cfg = 0 + for key in vrrp_keys: + if key[1] == str(vrrp_id): + ctx.fail("The vrrp instance {} has already configured!".format(vrrp_id)) + if key[0] == interface_name: + intf_cfg += 1 + if intf_cfg >= 16: + ctx.fail("{} has already configured 16 vrrp instances!".format(interface_name)) + + config_db.set_entry('VRRP', (interface_name, str(vrrp_id)), {"vid": vrrp_id}) + + +@vrrp.command("remove") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.pass_context +def remove_vrrp(ctx, interface_name, vrrp_id): + """Remove vrrp instance to the interface""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp_entry = config_db.get_entry("VRRP", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("{} dose not configured the vrrp instance {}!".format(interface_name, vrrp_id)) + config_db.set_entry('VRRP', (interface_name, str(vrrp_id)), None) + + +# +# 'vrrp6' subgroup ('config interface vrrp6 ...') +# +@interface.group(cls=clicommon.AbbreviationGroup) +@click.pass_context +def vrrp6(ctx): + """Vrrpv6 configuration""" + pass + + +# +# ip subgroup ('config interface vrrp6 ipv6 ...') +# +@vrrp6.group(cls=clicommon.AbbreviationGroup, name='ipv6') +@click.pass_context +def ipv6(ctx): + """Vrrpv6 ipv6 configuration """ + pass + + +@ipv6.command('add') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("ipv6_addr", metavar="", required=True) +@click.pass_context +def add_vrrp6_ipv6(ctx, interface_name, vrrp_id, ipv6_addr): + """Add IPv6 address to the Vrrpv6 instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + if not is_vaild_intf_ip_addr(ipv6_addr): + ctx.abort() + if check_vrrp_ip_exist(config_db, ipv6_addr): + ctx.abort() + + if "/" not in ipv6_addr: + ctx.fail("IPv6 address {} is missing a mask. Such as xx:xx::xx/yy".format(str(ipv6_addr))) + + # check vip exist + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + address_list = [] + if vrrp6_entry: + # update vrrp + if "vip" in vrrp6_entry: + address_list = vrrp6_entry.get("vip") + # add ip address + if len(address_list) >= 4: + ctx.fail("The vrrp instance {} has already configured 4 IPv6 addresses".format(vrrp_id)) + + else: + # create new vrrp + vrrp6_entry = {} + vrrp6_keys = config_db.get_keys("VRRP6") + if len(vrrp6_keys) >= 254: + ctx.fail("Has already configured 254 Vrrpv6 instances.") + intf_cfg = 0 + for key in vrrp6_keys: + if key[1] == str(vrrp_id): + ctx.fail("The Vrrpv6 instance {} has already configured!".format(vrrp_id)) + if key[0] == interface_name: + intf_cfg += 1 + if intf_cfg >= 16: + ctx.fail("{} has already configured 16 Vrrpv6 instances!".format(interface_name)) + vrrp6_entry["vid"] = vrrp_id + + address_list.append(ipv6_addr) + vrrp6_entry['vip'] = address_list + + config_db.set_entry("VRRP6", (interface_name, str(vrrp_id)), vrrp6_entry) + + +@ipv6.command('remove') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("ipv6_addr", metavar="", required=True) +@click.pass_context +def remove_vrrp_ipv6(ctx, interface_name, vrrp_id, ipv6_addr): + """Remove IPv6 address to the Vrrpv6 instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + try: + ipaddress.ip_interface(ipv6_addr) + except ValueError as err: + ctx.fail("IPv6 address is not valid: {}".format(err)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp6_entry: + ctx.fail("{} is not configured on the Vrrpv6 instance {}!".format(ipv6_addr, vrrp_id)) + + address_list = vrrp6_entry.get("vip") + # address_list = vrrp6_entry.get("vip") + if not address_list: + ctx.fail("{} is not configured on the Vrrpv6 instance {}!".format(ipv6_addr, vrrp_id)) + + # del ip address + if ipv6_addr in address_list: + address_list.remove(ipv6_addr) + else: + ctx.fail("{} is not configured on the Vrrpv6 instance {}!".format(ipv6_addr, vrrp_id)) + vrrp6_entry['vip'] = address_list + config_db.set_entry("VRRP6", (interface_name, str(vrrp_id)), vrrp6_entry) + + +def check_vrrp_ip_exist(config_db, ip_addr) -> bool: + addr_type = ipaddress.ip_interface(ip_addr).version + vrrp_table = "VRRP" if addr_type == 4 else "VRRP6" + vrrp_keys = config_db.get_keys(vrrp_table) + for vrrp_key in vrrp_keys: + vrrp_entry = config_db.get_entry(vrrp_table, vrrp_key) + if "vip" not in vrrp_entry: + continue + if ip_addr in vrrp_entry["vip"]: + click.echo("{} has already configured on the {} vrrp instance {}!".format(ip_addr, vrrp_key[0], + vrrp_key[1])) + return True + return False + + +# +# track interface subgroup ('config interface vrrp6 track_interface ...') +# +@vrrp6.group(cls=clicommon.AbbreviationGroup, name='track_interface') +@click.pass_context +def vrrp6_track_interface(ctx): + """ Vrrpv6 track_interface configuration """ + pass + + +@vrrp6_track_interface.command('add') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("track_interface", metavar="", required=True) +@click.argument('priority_increment', metavar='', required=True, type=click.IntRange(10, 50), + default=20) +@click.pass_context +def add_track_interface_v6(ctx, interface_name, vrrp_id, track_interface, priority_increment): + """add track_interface to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + track_interface = interface_alias_to_name(config_db, track_interface) + if interface_name is None: + ctx.fail("'interface_name' is None!") + if track_interface is None: + ctx.fail("'track_interface' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + table_name_t = get_interface_table_name(track_interface) + if table_name_t == "" or table_name_t == "LOOPBACK_INTERFACE": + ctx.fail("'track_interface' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if track_interface not in config_db.get_table(table_name_t): + ctx.fail("Router Interface '{}' not found".format(track_interface)) + + vrrp_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp6 instance {} not found on interface {}".format(vrrp_id, interface_name)) + + # track_intf_key = track_interface + "|weight|" + str(weight) + vrrp6_track_keys = config_db.get_keys("VRRP6_TRACK") + if vrrp6_track_keys: + track_key = (interface_name, str(vrrp_id)) + count = 0 + for item in vrrp6_track_keys: + subtuple1 = item[:2] + if subtuple1 == track_key: + count += 1 + + if count >= 8: + ctx.fail("The Vrrpv6 instance {} has already configured 8 track interfaces".format(vrrp_id)) + + # create a new entry + track6_entry = {} + track6_entry["priority_increment"] = priority_increment + config_db.set_entry("VRRP6_TRACK", (interface_name, str(vrrp_id), track_interface), track6_entry) + + +@vrrp6_track_interface.command('remove') +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument("track_interface", metavar="", required=True) +@click.pass_context +def remove_track_interface_v6(ctx, interface_name, vrrp_id, track_interface): + """Remove track_interface to the vrrp instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + track_interface = interface_alias_to_name(config_db, track_interface) + if interface_name is None: + ctx.fail("'interface_name' is None!") + if track_interface is None: + ctx.fail("'track_interface' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + table_name_t = get_interface_table_name(track_interface) + if table_name_t == "" or table_name_t == "LOOPBACK_INTERFACE": + ctx.fail("'track_interface' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + + vrrp_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp_entry: + ctx.fail("vrrp6 instance {} not found on interface {}".format(vrrp_id, interface_name)) + + track6_entry = config_db.get_entry("VRRP6_TRACK", (interface_name, str(vrrp_id), track_interface)) + if not track6_entry: + ctx.fail("{} is not configured on the vrrp6 instance {}!".format(track_interface, vrrp_id)) + config_db.set_entry('VRRP6_TRACK', (interface_name, str(vrrp_id), track_interface), None) + + +# +# 'vrrp6' subcommand ('config interface vrrp6 priority ...') +# +@vrrp6.command("priority") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('priority', metavar='', required=True, type=click.IntRange(1, 254), default=100) +@click.pass_context +def priority_v6(ctx, interface_name, vrrp_id, priority): + """config priority to the Vrrpv6 instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp6_entry: + ctx.fail("Vrrpv6 instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp6_entry['priority'] = priority + config_db.set_entry("VRRP6", (interface_name, str(vrrp_id)), vrrp6_entry) + + +# +# 'vrrp' subcommand ('config interface vrrp6 adv_interval ...') +# +@vrrp6.command("adv_interval") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('interval', metavar='', required=True, type=click.IntRange(1, 255), default=1000) +@click.pass_context +def adv_interval_v6(ctx, interface_name, vrrp_id, interval): + """config adv_interval to the Vrrpv6 instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp6_entry: + ctx.fail("Vrrpv6 instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp6_entry['adv_interval'] = interval + config_db.set_entry("VRRP6", (interface_name, str(vrrp_id)), vrrp6_entry) + + +# +# 'vrrp' subcommand ('config interface vrrp6 pre_empt ...') +# +@vrrp6.command("pre_empt") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.argument('mode', metavar='', required=True, type=click.Choice(["enabled", "disabled"])) +@click.pass_context +def pre_empt_v6(ctx, interface_name, vrrp_id, mode): + """Config pre_empt mode to the Vrrpv6 instance""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp6_entry: + ctx.fail("Vrrpv6 instance {} not found on interface {}".format(vrrp_id, interface_name)) + + vrrp6_entry['preempt'] = mode + config_db.set_entry("VRRP6", (interface_name, str(vrrp_id)), vrrp6_entry) + + +# +# 'vrrp6' subcommand +# +@vrrp6.command("add") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.pass_context +def add_vrrp_v6(ctx, interface_name, vrrp_id): + """Add Vrrpv6 instance to the interface""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if vrrp6_entry: + ctx.fail("{} has already configured the Vrrpv6 instance {}!".format(interface_name, vrrp_id)) + else: + vrrp6_keys = config_db.get_keys("VRRP6") + if len(vrrp6_keys) >= 254: + ctx.fail("Has already configured 254 Vrrpv6 instances!") + intf_cfg = 0 + for key in vrrp6_keys: + if key[1] == str(vrrp_id): + ctx.fail("The Vrrpv6 instance {} has already configured!".format(vrrp_id)) + if key[0] == interface_name: + intf_cfg += 1 + if intf_cfg >= 16: + ctx.fail("{} has already configured 16 Vrrpv6 instances!".format(interface_name)) + + config_db.set_entry('VRRP6', (interface_name, str(vrrp_id)), {"vid": vrrp_id}) + + +@vrrp6.command("remove") +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrrp_id', metavar='', required=True, type=click.IntRange(1, 255)) +@click.pass_context +def remove_vrrp_v6(ctx, interface_name, vrrp_id): + """Remove Vrrpv6 instance to the interface""" + config_db = ctx.obj["config_db"] + + if clicommon.get_interface_naming_mode() == "alias": + interface_name = interface_alias_to_name(config_db, interface_name) + if interface_name is None: + ctx.fail("'interface_name' is None!") + + table_name = get_interface_table_name(interface_name) + if table_name == "" or table_name == "LOOPBACK_INTERFACE": + ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]") + if interface_name not in config_db.get_table(table_name): + ctx.fail("Router Interface '{}' not found".format(interface_name)) + + vrrp6_entry = config_db.get_entry("VRRP6", (interface_name, str(vrrp_id))) + if not vrrp6_entry: + ctx.fail("{} dose not configured the Vrrpv6 instance {}!".format(interface_name, vrrp_id)) + config_db.set_entry('VRRP6', (interface_name, str(vrrp_id)), None) + + # # 'vrf' group ('config vrf ...') # diff --git a/show/main.py b/show/main.py index 370798d219..0230da3ffd 100755 --- a/show/main.py +++ b/show/main.py @@ -1145,6 +1145,111 @@ def route_map(route_map_name, verbose): cmd[-1] += ' {}'.format(route_map_name) run_command(cmd, display_cmd=verbose) + +# +# 'vrrp' group ("show vrrp ...") +# +@cli.group(cls=clicommon.AliasedGroup, invoke_without_command="true") +@click.pass_context +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp(ctx, verbose): + """Show vrrp commands""" + if ctx.invoked_subcommand is not None: + return + + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp'] + run_command(cmd, display_cmd=verbose) + + +# 'interface' command +@vrrp.command('interface') +@click.pass_context +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrid', metavar='', required=False) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp_interface(ctx, interface_name, vrid, verbose): + """show vrrp interface """ + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp'] + if vrid is not None: + cmd[-1] += ' interface {} {}'.format(interface_name, vrid) + else: + cmd[-1] += ' interface {}'.format(interface_name) + run_command(cmd, display_cmd=verbose) + + +# 'vrid' command +@vrrp.command('vrid') +@click.pass_context +@click.argument('vrid', metavar='', required=True) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp_vrid(ctx, vrid, verbose): + """show vrrp vrid """ + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp {}'.format(vrid)] + run_command(cmd, display_cmd=verbose) + + +# 'summary' command +@vrrp.command('summary') +@click.pass_context +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp_summary(ctx, verbose): + """show vrrp summary""" + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp summary'] + run_command(cmd, display_cmd=verbose) + + +# +# 'vrrp6' group ("show vrrp6 ...") +# +@cli.group(cls=clicommon.AliasedGroup, invoke_without_command="true") +@click.pass_context +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp6(ctx, verbose): + """Show vrrp6 commands""" + if ctx.invoked_subcommand is not None: + return + + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp6'] + run_command(cmd, display_cmd=verbose) + + +# 'interface' command +@vrrp6.command('interface') +@click.pass_context +@click.argument('interface_name', metavar='', required=True) +@click.argument('vrid', metavar='', required=False) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp6_interface(ctx, interface_name, vrid, verbose): + """show vrrp6 interface """ + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp6'] + if vrid is not None: + cmd[-1] += ' interface {} {}'.format(interface_name, vrid) + else: + cmd[-1] += ' interface {}'.format(interface_name) + run_command(cmd, display_cmd=verbose) + + +# 'vrid' command +@vrrp6.command('vrid') +@click.pass_context +@click.argument('vrid', metavar='', required=True) +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp6_vrid(ctx, vrid, verbose): + """show vrrp6 vrid """ + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp6 {}'.format(vrid)] + run_command(cmd, display_cmd=verbose) + + +# 'summary' command +@vrrp6.command('summary') +@click.pass_context +@click.option('--verbose', is_flag=True, help="Enable verbose output") +def vrrp6_summary(ctx, verbose): + """show vrrp6 summary""" + cmd = ['sudo', constants.RVTYSH_COMMAND, '-c', 'show vrrp6 summary'] + run_command(cmd, display_cmd=verbose) + + # # 'ip' group ("show ip ...") # diff --git a/tests/vrrp_test.py b/tests/vrrp_test.py new file mode 100644 index 0000000000..bd33738fa5 --- /dev/null +++ b/tests/vrrp_test.py @@ -0,0 +1,1499 @@ +import os +from unittest import mock + +from click.testing import CliRunner + +import config.main as config +from utilities_common.db import Db +import utilities_common.bgp_util as bgp_util + + +class TestConfigVRRP(object): + _old_run_bgp_command = None + + @classmethod + def setup_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "1" + cls._old_run_bgp_command = bgp_util.run_bgp_command + bgp_util.run_bgp_command = mock.MagicMock( + return_value=cls.mock_run_bgp_command()) + print("SETUP") + + ''' Tests for VRRPv4 and VRRPv6 ''' + + def mock_run_bgp_command(): + return "" + + def test_add_del_vrrp_instance_without_vip(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet63 9.9.9.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet63", "9.9.9.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '9.9.9.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp remove Ethernet63 7 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet63", "7"], obj=obj) + print(result.exit_code, result.output) + assert "Ethernet63 dose not configured the vrrp instance 7!" in result.output + assert result.exit_code != 0 + + # config int vrrp add Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["add"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + + # config int vrrp add Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["add"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert "Ethernet64 has already configured the vrrp instance 8!" in result.output + assert result.exit_code != 0 + + # config int vrrp add Ethernet63 7 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["add"], + ["Ethernet63", "7"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '7') in db.cfgdb.get_table('VRRP') + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int vrrp remove Ethernet63 7 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet63", "7"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '7') not in db.cfgdb.get_table('VRRP') + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["add"], + ["Ethernt64", "8"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["add"], + ["Ethernet2", "7"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernt64", "8"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet2", "7"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet63 9.9.9.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet63", "9.9.9.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet63', '9.9.9.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_add_del_vrrp6_instance_without_vip(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 100::64/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "100::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '100::64/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet63 99::64/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet63", "99::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '99::64/64') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp6 add Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["add"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + + # config int vrrp6 add Ethernet63 7 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["add"], + ["Ethernet63", "7"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '7') in db.cfgdb.get_table('VRRP6') + + # config int vrrp6 add Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["add"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert "Ethernet64 has already configured the Vrrpv6 instance 8!" in result.output + assert result.exit_code != 0 + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["add"], + ["Ethernt64", "8"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["add"], + ["Ethernet2", "7"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int vrrp6 remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # config int ip remove Ethernet64 100::64/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "100::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '100::64/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet63 99::64/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet63", "99::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet63', '99::64/64') not in db.cfgdb.get_table('INTERFACE') + + def test_add_del_vrrp_instance(self): + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet63 9.9.9.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet63", "9.9.9.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '9.9.9.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet62 8.8.8.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet62", "8.8.8.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet62', '8.8.8.1/24') in db.cfgdb.get_table('INTERFACE') + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernt64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet2", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # config int vrrp ip add Ethernet64 8 10.10.10.16/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.16/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24', '10.10.10.16/24'] + + # config int vrrp ip add Ethernet62 7 8.8.8.16/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "8.8.8.16/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet62', '7') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet62', '7']['vip'] == ['8.8.8.16/24'] + + # config int vrrp ip add Ethernet62 7 8.8.8.16/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "8.8.8.16/24"], obj=obj) + print(result.exit_code, result.output) + assert "8.8.8.16/24 has already configured" in result.output + assert result.exit_code != 0 + + # config int vrrp ip add Ethernet62 7 0.0.0.0 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "0.0.0.0"], obj=obj) + print(result.exit_code, result.output) + assert "IPv4 address 0.0.0.0/32 is Zero" in result.output + assert result.exit_code != 0 + + # config int vrrp ip add Ethernet62 7 777.256.1.1/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "777.256.1.1/24"], obj=obj) + print(result.exit_code, result.output) + assert "IP address 777.256.1.1/24 is not valid" in result.output + assert result.exit_code != 0 + + # config int vrrp ip add Ethernet62 7 224.0.0.41/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "224.0.0.41/24"], obj=obj) + print(result.exit_code, result.output) + assert "IP address 224.0.0.41/24 is multicast" in result.output + assert result.exit_code != 0 + + # config int vrrp ip add Ethernet62 7 6.6.6.6 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet62", "7", "6.6.6.6"], obj=obj) + print(result.exit_code, result.output) + assert "IP address 6.6.6.6 is missing a mask." in result.output + assert result.exit_code != 0 + + # config int vrrp ip remove Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.16/24'] + + # config int vrrp ip remove Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert "10.10.10.8/24 is not configured on the vrrp instance" in result.output + assert result.exit_code != 0 + + # config int vrrp ip remove Ethernet64 8 10.10.10.888/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet64", "8", "10.10.10.888/24"], obj=obj) + print(result.exit_code, result.output) + assert "IP address is not valid:" in result.output + assert result.exit_code != 0 + + # config int vrrp ip remove Ethernet64 8 10.10.10.16/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet64", "8", "10.10.10.16/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == [''] + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernt64", "8", "10.10.10.16/24"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet2", "8", "10.10.10.16/24"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int vrrp remove Ethernet63 9 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["remove"], + ["Ethernet63", "9", "10.10.10.16/24"], obj=obj) + print(result.exit_code, result.output) + assert "10.10.10.16/24 is not configured on the vrrp instance" in result.output + assert result.exit_code != 0 + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet63 9.9.9.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet63", "9.9.9.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet63', '9.9.9.1/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet62 8.8.8.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet62", "8.8.8.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet62', '8.8.8.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_add_del_vrrp6_instance(self): + runner = CliRunner() + db = Db() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 100::1/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "100::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '100::1/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet63 99::1/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet63", "99::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet63', '99::1/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet62 88::1/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet62", "88::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet62', '88::1/64') in db.cfgdb.get_table('INTERFACE') + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernt64", "8", "100::8/64"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet2", "8", "100::8/64"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 add Ethernet64 8 100::8/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "100::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['100::8/64'] + + # config int vrrp6 ipv6 add Ethernet64 8 100::16/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "100::16/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['100::8/64', '100::16/64'] + + # config int vrrp6 ipv6 add Ethernet62 7 88::16/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet62", "7", "88::16/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet62', '7') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet62', '7']['vip'] == ['88::16/64'] + + # config int vrrp6 ipv6 add Ethernet62 7 88::16/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet62", "7", "88::16/64"], obj=obj) + print(result.exit_code, result.output) + assert "88::16/64 has already configured" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 add Ethernet62 7 :: + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet62", "7", "::"], obj=obj) + print(result.exit_code, result.output) + assert "IPv6 address ::/128 is unspecified" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 add Ethernet62 7 785h::12/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet62", "7", "785h::12/64"], obj=obj) + print(result.exit_code, result.output) + assert "IP address 785h::12/64 is not valid" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 add Ethernet62 7 88::2 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet62", "7", "88::2"], obj=obj) + print(result.exit_code, result.output) + assert "IPv6 address 88::2 is missing a mask." in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 remove Ethernet64 8 100::8/64 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet64", "8", "100::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['100::16/64'] + + # config int vrrp6 ipv6 remove Ethernet64 8 100::8/64 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet64", "8", "100::8/64"], obj=obj) + print(result.exit_code, result.output) + assert "100::8/64 is not configured on the Vrrpv6 instance 8!" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 remove Ethernet64 8 100::16/64 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet64", "8", "100::16/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == [''] + + # config int vrrp6 remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # check interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernt64", "8", "100::16/64"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet2", "8", "100::16/64"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config int vrrp remove Ethernet63 9 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet63", "9", "100::16/64"], obj=obj) + print(result.exit_code, result.output) + assert "100::16/64 is not configured on the Vrrpv6 instance 9" in result.output + assert result.exit_code != 0 + + # config int vrrp6 ipv6 remove Ethernet64 8 88cg::2/64 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["remove"], + ["Ethernet64", "8", "88cg::2/64"], obj=obj) + print(result.exit_code, result.output) + assert "IPv6 address is not valid:" in result.output + assert result.exit_code != 0 + + # config int ip remove Ethernet64 100::1/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "100::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '100::1/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet63 99::1/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet63", "99::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet63', '99::1/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet62 88::1/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet62", "88::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet62', '88::1/64') not in db.cfgdb.get_table('INTERFACE') + + def test_add_del_vrrp_instance_track_intf(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet5 10.10.10.5/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet5", "10.10.10.5/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet5', '10.10.10.5/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet6 10.10.10.6/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet6", "10.10.10.6/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet6', '10.10.10.6/24') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet7 10.10.10.7/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet7", "10.10.10.7/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet7', '10.10.10.7/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # check interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernt64", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet2", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernt5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "'track_interface' is not valid." in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet2", "20"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config interface vrrp track_interface add Ethernet64 8 Ethernet5 20 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet5') in db.cfgdb.get_table('VRRP_TRACK') + assert db.cfgdb.get_table('VRRP_TRACK')['Ethernet64', '8', 'Ethernet5']['priority_increment'] == '20' + + # config interface vrrp track_interface add Ethernet64 8 Ethernet6 30 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet6", "30"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet6') in db.cfgdb.get_table('VRRP_TRACK') + assert db.cfgdb.get_table('VRRP_TRACK')['Ethernet64', '8', 'Ethernet6']['priority_increment'] == '30' + + # config interface vrrp track_interface add Ethernet64 8 Ethernet6 25 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet6", "25"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet6') in db.cfgdb.get_table('VRRP_TRACK') + assert db.cfgdb.get_table('VRRP_TRACK')['Ethernet64', '8', 'Ethernet6']['priority_increment'] == '25' + + # config interface vrrp track_interface add Ethernet64 8 Ethernet7 80 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet7", "80"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config interface vrrp track_interface add Ethernet7 7 Ethernet5 40 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["add"], + ["Ethernet7", "7", "Ethernet5", "40"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 7 not found on interface Ethernet7" in result.output + assert result.exit_code != 0 + + # config interface vrrp track_interface remove Ethernet64 8 Ethernet6 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet6"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet6') not in db.cfgdb.get_table('VRRP_TRACK') + + # config interface vrrp track_interface remove Ethernet64 8 Ethernet5 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet5') not in db.cfgdb.get_table('VRRP_TRACK') + + # check interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernt64", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet2", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernt5"], obj=obj) + print(result.exit_code, result.output) + assert "'track_interface' is not valid." in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet2"], obj=obj) + print(result.exit_code, result.output) + assert "Ethernet2 is not configured on the vrrp instance 8" in result.output + assert result.exit_code != 0 + + # config interface vrrp track_interface remove Ethernet7 7 Ethernet5 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp"].commands["track_interface"].commands["remove"], + ["Ethernet7", "7", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 7 not found on interface Ethernet7" in result.output + assert result.exit_code != 0 + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int ip remove Ethernet7 10.10.10.7/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet7", "10.10.10.7/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet7', '10.10.10.7/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet6 10.10.10.6/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet6", "10.10.10.6/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet6', '10.10.10.6/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet5 10.10.10.5/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet5", "10.10.10.5/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet5', '10.10.10.5/24') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_add_del_vrrp6_instance_track_intf(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 100::64/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "100::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '100::64/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet5 100::5/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet5", "100::5/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet5', '100::5/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet6 100::6/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet6", "100::6/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet6', '100::6/64') in db.cfgdb.get_table('INTERFACE') + + # config int ip add Ethernet7 100::7/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet7", "100::7/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet7', '100::7/64') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp6 ipv6 add Ethernet64 8 100::1/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "100::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['100::1/64'] + + # check interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernt64", "8", "Ethernet", "20"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet2", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernt5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "'track_interface' is not valid." in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet2", "20"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # config interface vrrp6 track_interface add Ethernet7 8 Ethernet5 20 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet7", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp6 instance 8 not found on interface Ethernet7" in result.output + assert result.exit_code != 0 + + # config interface vrrp6 track_interface add Ethernet64 8 Ethernet5 20 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet5", "20"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet5') in db.cfgdb.get_table('VRRP6_TRACK') + assert db.cfgdb.get_table('VRRP6_TRACK')['Ethernet64', '8', 'Ethernet5']['priority_increment'] == '20' + + # config interface vrrp6 track_interface add Ethernet64 8 Ethernet6 30 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet6", "30"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet6') in db.cfgdb.get_table('VRRP6_TRACK') + assert db.cfgdb.get_table('VRRP6_TRACK')['Ethernet64', '8', 'Ethernet6']['priority_increment'] == '30' + + # config interface vrrp6 track_interface add Ethernet64 8 Ethernet7 80 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["add"], + ["Ethernet64", "8", "Ethernet7", "80"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config interface vrrp6 track_interface remove Ethernet64 8 Ethernet6 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet6"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet6') not in db.cfgdb.get_table('VRRP6_TRACK') + + # config interface vrrp6 track_interface remove Ethernet64 8 Ethernet5 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8', 'Ethernet5') not in db.cfgdb.get_table('VRRP6_TRACK') + + # config interface vrrp6 track_interface remove Ethernet7 8 Ethernet5 + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet7", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp6 instance 8 not found on interface Ethernet7" in result.output + assert result.exit_code != 0 + + # check interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernt64", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet2", "8", "Ethernet5"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernt5"], obj=obj) + print(result.exit_code, result.output) + assert "'track_interface' is not valid." in result.output + assert result.exit_code != 0 + + # check track_interface_name is valid + result = runner.invoke( + config.config.commands["interface"].commands["vrrp6"].commands["track_interface"].commands["remove"], + ["Ethernet64", "8", "Ethernet2"], obj=obj) + print(result.exit_code, result.output) + assert "Ethernet2 is not configured on the vrrp6 instance 8" in result.output + assert result.exit_code != 0 + + # config int vrrp6 remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # config int ip remove Ethernet7 100::7/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet7", "100::7/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet7', '100::7/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet6 100::6/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet6", "100::6/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet6', '100::6/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet5 100::5/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet5", "100::5/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet5', '100::5/64') not in db.cfgdb.get_table('INTERFACE') + + # config int ip remove Ethernet64 100::64/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "100::64/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '100::64/64') not in db.cfgdb.get_table('INTERFACE') + + def test_enable_disable_vrrp_instance_preempt(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["pre_empt"], + ["Ethernt64", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["pre_empt"], + ["Ethernet2", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["pre_empt"], + ["Ethernet64", "9", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp vrrp pre_empt Ethernet64 8 disabled + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["pre_empt"], + ["Ethernet64", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['preempt'] == 'disabled' + + # config interface vrrp vrrp pre_empt Ethernet64 8 enabled + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["pre_empt"], + ["Ethernet64", "8", "enabled"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['preempt'] == 'enabled' + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_enable_disable_vrrp6_instance_preempt(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10::8/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10::8/64') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp6 ipv6 add Ethernet64 8 10::1/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "10::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['10::1/64'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["pre_empt"], + ["Ethernt64", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["pre_empt"], + ["Ethernet2", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp6 instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["pre_empt"], + ["Ethernet64", "9", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert "Vrrpv6 instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp6 pre_empt Ethernet64 8 disabled + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["pre_empt"], + ["Ethernet64", "8", "disabled"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['preempt'] == 'disabled' + + # config interface vrrp vrrp pre_empt Ethernet64 8 enabled + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["pre_empt"], + ["Ethernet64", "8", "enabled"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['preempt'] == 'enabled' + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # config int ip remove Ethernet64 10::8/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10::8/64') not in db.cfgdb.get_table('INTERFACE') + + def test_config_vrrp_instance_adv_interval(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["adv_interval"], + ["Ethernt64", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["adv_interval"], + ["Ethernet2", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["adv_interval"], + ["Ethernet64", "9", "2"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp vrrp adv_interval Ethernet64 8 2 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["adv_interval"], + ["Ethernet64", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['adv_interval'] == '2' + + # config interface vrrp vrrp adv_interval Ethernet64 8 500 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["adv_interval"], + ["Ethernet64", "8", "500"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_config_vrrp6_instance_adv_interval(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10::8/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10::8/64') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp6 ipv6 add Ethernet64 8 10::1/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "10::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['10::1/64'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["adv_interval"], + ["Ethernt64", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["adv_interval"], + ["Ethernet2", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["adv_interval"], + ["Ethernet64", "9", "2"], obj=obj) + print(result.exit_code, result.output) + assert "Vrrpv6 instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp6 adv_interval Ethernet64 8 2 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["adv_interval"], + ["Ethernet64", "8", "2"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['adv_interval'] == '2' + + # config interface vrrp6 adv_interval Ethernet64 8 500 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["adv_interval"], + ["Ethernet64", "8", "500"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config int vrrp6 remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # config int ip remove Ethernet64 10::8/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10::8/64') not in db.cfgdb.get_table('INTERFACE') + + def test_config_vrrp_instance_priority(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["priority"], + ["Ethernt64", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["priority"], + ["Ethernet2", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["priority"], + ["Ethernet64", "9", "150"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp priority Ethernet64 8 150 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["priority"], + ["Ethernet64", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['priority'] == '150' + + # config interface vrrp priority Ethernet64 8 256 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["priority"], + ["Ethernet64", "8", "256"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE') + + def test_config_vrrp6_instance_priority(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10::8/64 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10::8/64') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp6 ipv6 add Ethernet64 8 10::1/64 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["ipv6"].commands["add"], + ["Ethernet64", "8", "10::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['vip'] == ['10::1/64'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["priority"], + ["Ethernt64", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["priority"], + ["Ethernet2", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["priority"], + ["Ethernet64", "9", "150"], obj=obj) + print(result.exit_code, result.output) + assert "Vrrpv6 instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp6 priority Ethernet64 8 150 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["priority"], + ["Ethernet64", "8", "150"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP6') + assert db.cfgdb.get_table('VRRP6')['Ethernet64', '8']['priority'] == '150' + + # config interface vrrp priority Ethernet64 8 256 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["priority"], + ["Ethernet64", "8", "256"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config int vrrp6 remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp6"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP6') + + # config int ip remove Ethernet64 10::8/64 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10::8/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10::8/64') not in db.cfgdb.get_table('INTERFACE') + + def test_config_vrrp_instance_version(self): + db = Db() + runner = CliRunner() + obj = {'config_db': db.cfgdb} + + # config int ip add Ethernet64 10.10.10.1/24 + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '10.10.10.1/24') in db.cfgdb.get_table('INTERFACE') + + # config int vrrp ip add Ethernet64 8 10.10.10.8/24 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["ip"].commands["add"], + ["Ethernet64", "8", "10.10.10.8/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['vip'] == ['10.10.10.8/24'] + + # check interface_name is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["version"], + ["Ethernt64", "8", "3"], obj=obj) + print(result.exit_code, result.output) + assert "'interface_name' is not valid" in result.output + assert result.exit_code != 0 + + # check interface is Router interface + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["version"], + ["Ethernet2", "8", "3"], obj=obj) + print(result.exit_code, result.output) + assert "Router Interface 'Ethernet2' not found" in result.output + assert result.exit_code != 0 + + # check the vrrp instance is valid + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["version"], + ["Ethernet64", "9", "3"], obj=obj) + print(result.exit_code, result.output) + assert "vrrp instance 9 not found on interface Ethernet64" in result.output + assert result.exit_code != 0 + + # config interface vrrp version Ethernet64 8 3 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["version"], + ["Ethernet64", "8", "3"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') in db.cfgdb.get_table('VRRP') + assert db.cfgdb.get_table('VRRP')['Ethernet64', '8']['version'] == '3' + + # config interface vrrp version Ethernet64 8 1 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["version"], + ["Ethernet64", "8", "1"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # config int vrrp remove Ethernet64 8 + result = runner.invoke(config.config.commands["interface"].commands["vrrp"].commands["remove"], + ["Ethernet64", "8"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert ('Ethernet64', '8') not in db.cfgdb.get_table('VRRP') + + # config int ip remove Ethernet64 10.10.10.1/24 + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Ethernet64", "10.10.10.1/24"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code == 0 + assert mock_run_command.call_count == 1 + assert ('Ethernet64', '10.10.10.1/24') not in db.cfgdb.get_table('INTERFACE')