Skip to content

Commit

Permalink
[cisco] Wrap port names on show vlan
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mathieu Mitchell committed Jan 26, 2017
1 parent d697504 commit 0dccad5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 36 additions & 2 deletions fake_switches/cisco/command_processor/enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down Expand Up @@ -221,13 +228,15 @@ def show_version(self):
port_count=len(self.switch_configuration.get_physical_ports()),
))


def strip_leading_slash(dest_file):
return dest_file[1:]


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,
Expand Down Expand Up @@ -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"
Expand All @@ -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])
Expand Down
8 changes: 6 additions & 2 deletions tests/cisco/test_cisco_switch_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 0dccad5

Please sign in to comment.