From 0dccad538afb8b758aec627e28f8fc749597030f Mon Sep 17 00:00:00 2001 From: Mathieu Mitchell Date: Thu, 26 Jan 2017 06:38:04 -0500 Subject: [PATCH] [cisco] Wrap port names on show vlan Currently, port names were written all on one line, as if the software connecting had defined a very long terminal. This is not always the case. Fake-switch should expose possible cases, such as port names wrapping on other lines. This commit adds this (missing) feature to the show vlan command. This is a follow up of #90. Partial-bug: #97 --- .../cisco/command_processor/enabled.py | 38 ++++++++++++++++++- tests/cisco/test_cisco_switch_protocol.py | 8 +++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/fake_switches/cisco/command_processor/enabled.py b/fake_switches/cisco/command_processor/enabled.py index b4c8b962..78a60272 100644 --- a/fake_switches/cisco/command_processor/enabled.py +++ b/fake_switches/cisco/command_processor/enabled.py @@ -72,11 +72,18 @@ def do_show(self, *args): for vlan in sorted(self.switch_configuration.vlans, key=lambda v: v.number): ports = [port.get_subname(length=2) for port in self.switch_configuration.get_physical_ports() if port.access_vlan == vlan.number or (vlan.number == 1 and port.access_vlan is None)] + formatted_membership = [] + if ports: + ports_membership = [" {}".format(l) for l in get_port_groups(ports, max_line_length=30)] + formatted_membership.append(ports_membership.pop(0)) + for remaining_line in ports_membership: + formatted_membership.append(' ' * 44 + remaining_line) + self.write_line("%-4s %-32s %s%s" % ( vlan.number, - vlan_name(vlan) if vlan_name(vlan) else "VLAN%s" % vlan.number, + vlan_display_name(vlan), "active", - (" " + ", ".join(ports)) if ports else "" + '\n'.join(formatted_membership) )) if len(args) == 1: self.write_line("") @@ -221,6 +228,7 @@ def show_version(self): port_count=len(self.switch_configuration.get_physical_ports()), )) + def strip_leading_slash(dest_file): return dest_file[1:] @@ -228,6 +236,7 @@ def strip_leading_slash(dest_file): def build_static_routes(route): return "ip route {0} {1} {2}".format(route.destination, route.mask, route.next_hop) + def build_running_vlan(vlan): data = [ "vlan %s" % vlan.number, @@ -303,6 +312,10 @@ def vlan_name(vlan): return vlan.name or ("default" if vlan.number == 1 else None) +def vlan_display_name(vlan): + return vlan_name(vlan) or "VLAN%s" % vlan.number + + def to_vlan_ranges(vlans): if len(vlans) == 0: return "none" @@ -312,6 +325,27 @@ def to_vlan_ranges(vlans): return ",".join([to_range_string(r) for r in ranges]) +def get_port_groups(ports, max_line_length): + delimiter = ', ' + new_lines = [] + + while len(ports) > 0: + line = [] + line_length = 0 + while line_length < max_line_length: + try: + next_port = ports[0] + if len(delimiter.join(line + [next_port])) > max_line_length: + break + line.append(ports.pop(0)) + except IndexError: + break + line_length = len(delimiter.join(line)) + new_lines.append(delimiter.join(line)) + + return new_lines + + def to_range_string(array_range): if len(array_range) < 3: return ",".join([str(n) for n in array_range]) diff --git a/tests/cisco/test_cisco_switch_protocol.py b/tests/cisco/test_cisco_switch_protocol.py index 477c521a..9569a6a0 100644 --- a/tests/cisco/test_cisco_switch_protocol.py +++ b/tests/cisco/test_cisco_switch_protocol.py @@ -194,7 +194,9 @@ def test_show_vlan_brief(self, t): t.readln("") t.readln("VLAN Name Status Ports") t.readln("---- -------------------------------- --------- -------------------------------") - t.readln("1 default active Fa0/2, Fa0/3, Fa0/4, Fa0/5, Fa0/6, Fa0/7, Fa0/8, Fa0/9, Fa0/10, Fa0/11, Fa0/12") + t.readln("1 default active Fa0/2, Fa0/3, Fa0/4, Fa0/5") + t.readln(" Fa0/6, Fa0/7, Fa0/8, Fa0/9") + t.readln(" Fa0/10, Fa0/11, Fa0/12") t.readln("123 VLAN123 active Fa0/1") t.readln("2222 your-name-is-way-too-long-for-th active") t.readln("3333 some-name active") @@ -218,7 +220,9 @@ def test_show_vlan(self, t): t.readln("") t.readln("VLAN Name Status Ports") t.readln("---- -------------------------------- --------- -------------------------------") - t.readln("1 default active Fa0/2, Fa0/3, Fa0/4, Fa0/5, Fa0/6, Fa0/7, Fa0/8, Fa0/9, Fa0/10, Fa0/11, Fa0/12") + t.readln("1 default active Fa0/2, Fa0/3, Fa0/4, Fa0/5") + t.readln(" Fa0/6, Fa0/7, Fa0/8, Fa0/9") + t.readln(" Fa0/10, Fa0/11, Fa0/12") t.readln("123 VLAN123 active Fa0/1") t.readln("2222 your-name-is-way-too-long-for-th active") t.readln("3333 some-name active")