def test_get_interfaces(self): all_interfaces = [Interface('xe-1/0/1'), Interface('xe-1/0/2')] self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return(all_interfaces) assert_that(self.switch.get_interfaces(), is_(all_interfaces)) assert_that(self.switch.get_interfaces(), is_(all_interfaces))
def node_to_interface(self, interface_node, config): interface = Interface() if interface_node is not None: interface.name = value_of(interface_node.xpath("name")) interface.bond_master = get_bond_master(interface_node) self.fill_interface_from_node(interface, interface_node, config) return interface
def parse_interface(if_data): if regex.match("^\w*Ethernet([^\s]*) is (\w*).*", if_data[0]): i = Interface(name="ethernet {}".format(regex[0]), port_mode=ACCESS, shutdown=regex[1] == "disabled") for line in if_data: if regex.match("Port name is (.*)", line): i.description = regex[0] return i
def test_reset_interface_invalidate_cache(self): self.real_switch_mock.should_receive("get_interface").with_args("eth0").once().\ and_return([Interface('eth0')]) self.switch.get_interface('eth0') self.real_switch_mock.should_receive("reset_interface").with_args('eth0') self.switch.reset_interface('eth0') self.real_switch_mock.should_receive("get_interface").with_args("eth0").once(). \ and_return([Interface('eth0')]) self.switch.get_interface('eth0')
def test_unset_interface_access_vlan(self): self.real_switch_mock.should_receive( "get_interfaces").once().and_return( [Interface('eth0', access_vlan=123)]) self.switch.get_interfaces() self.real_switch_mock.should_receive("unset_interface_access_vlan").once() \ .with_args('eth0') self.switch.unset_interface_access_vlan('eth0') assert_that(self.switch.get_interfaces(), is_([Interface('eth0')]))
def test_set_trunk_mode(self): self.real_switch_mock.should_receive( "get_interfaces").once().and_return([Interface('eth0')]) self.switch.get_interfaces() self.real_switch_mock.should_receive("set_trunk_mode").once() \ .with_args('eth0') self.switch.set_trunk_mode('eth0') assert_that(self.switch.get_interfaces(), is_([Interface('eth0', port_mode=TRUNK)]))
def test_unset_interface_state(self): self.real_switch_mock.should_receive("get_interfaces").once().and_return([Interface('xe-1/0/2')]) self.switch.get_interfaces() self.real_switch_mock.should_receive("unset_interface_state").once().with_args('xe-1/0/2') self.real_switch_mock.should_receive("get_interface").once() \ .with_args('xe-1/0/2').and_return(Interface('xe-1/0/2', shutdown=False)) self.switch.unset_interface_state('xe-1/0/2') assert_that(self.switch.get_interface('xe-1/0/2').shutdown, is_(False))
def test_set_interface_state_on(self): self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2')]) self.switch.get_interfaces() self.real_switch_mock.should_receive("set_interface_state").once() \ .with_args('xe-1/0/2', ON) self.switch.set_interface_state('xe-1/0/2', ON) assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', shutdown=False)]))
def test_set_interface_auto_negotiation_state_on(self): self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2')]) self.switch.get_interfaces() self.real_switch_mock.should_receive("set_interface_auto_negotiation_state").once() \ .with_args('xe-1/0/2', ON) self.switch.set_interface_auto_negotiation_state('xe-1/0/2', ON) assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', auto_negotiation=True)]))
def test_unset_interface_native_vlan(self): self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2', trunk_native_vlan=20)]) self.switch.get_interfaces() self.real_switch_mock.should_receive("unset_interface_native_vlan").once() \ .with_args('xe-1/0/2') self.switch.unset_interface_native_vlan('xe-1/0/2') assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', trunk_native_vlan=None)]))
def test_unset_interface_mtu(self): self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2', mtu=5000)]) self.switch.get_interfaces() self.real_switch_mock.should_receive("unset_interface_mtu").once() \ .with_args('xe-1/0/2') self.switch.unset_interface_mtu('xe-1/0/2') assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', mtu=None)]))
def test_remove_trunk_vlan(self): self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2', trunk_vlans=[1])]) self.switch.get_interfaces() self.real_switch_mock.should_receive("remove_trunk_vlan").once() \ .with_args('xe-1/0/2', 1) self.switch.remove_trunk_vlan('xe-1/0/2', 1) assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', trunk_vlans=[])]))
def test_set_access_mode(self): self.real_switch_mock.should_receive("get_interfaces").once().and_return( [Interface('eth0', trunk_native_vlan=1200, trunk_vlans=[1, 2, 3])]) self.switch.get_interfaces() self.real_switch_mock.should_receive("set_access_mode").once() \ .with_args('eth0') self.switch.set_access_mode('eth0') assert_that( self.switch.get_interfaces(), is_([Interface('eth0', port_mode=ACCESS, trunk_native_vlan=None, trunk_vlans=[])]) )
def test_get_interface(self): interface = Interface('xe-1/0/1') self.real_switch_mock.should_receive("get_interface").with_args("xe-1/0/1").once() \ .and_return(interface) assert_that(self.switch.get_interface('xe-1/0/1'), is_(interface)) assert_that(self.switch.get_interface('xe-1/0/1'), is_(interface))
def test_remove_interface_from_bond(self): self.real_switch_mock.should_receive("get_bonds").once() \ .and_return([Bond(1, members=['xe-1/0/2'])]) self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2', bond_master=1)]) self.switch.get_bonds() self.switch.get_interfaces() self.real_switch_mock.should_receive("remove_interface_from_bond").once() \ .with_args('xe-1/0/2') self.switch.remove_interface_from_bond('xe-1/0/2') assert_that(self.switch.get_bonds(), is_([Bond(1, members=[])])) assert_that(self.switch.get_interfaces(), is_([Interface('xe-1/0/2', bond_master=None)]))
def read_interface(self, interface_name): data = self.get_interface_data(interface_name) interface = Interface(name=interface_name, port_mode=ACCESS, shutdown=False) for line in data: if regex.match("switchport mode \S+", line): interface.port_mode = TRUNK if regex.match("shutdown", line): interface.shutdown = True if regex.match("switchport access vlan (\d+)", line): interface.access_vlan = int(regex[0]) if regex.match("switchport general pvid (\d+)", line): interface.trunk_native_vlan = int(regex[0]) if regex.match("switchport \S+ allowed vlan add (\S+)", line): interface.trunk_vlans += parse_vlan_ranges(regex[0]) return interface
def test_unset_interface_auto_negotiation_state(self): self.real_switch_mock.should_receive("get_interfaces").once()\ .and_return([Interface('xe-1/0/2', auto_negotiation=False)]) self.switch.get_interfaces() self.real_switch_mock.should_receive("unset_interface_auto_negotiation_state").once().with_args('xe-1/0/2') self.switch.unset_interface_auto_negotiation_state('xe-1/0/2') assert_that(self.switch.get_interface('xe-1/0/2').auto_negotiation, is_(None))
def interface(self): warnings.warn('Deprecated: Use directly the members of Bond instead.', DeprecationWarning) return Interface( shutdown=self.shutdown, port_mode=self.port_mode, access_vlan=self.access_vlan, trunk_native_vlan=self.trunk_native_vlan, trunk_vlans=self.trunk_vlans, )
def parse_interfaces(interfaces_data, switchports_data): interfaces = [] for interface_data in interfaces_data.values(): if regex.match("(\w*Ethernet[^\s]*)", interface_data["name"]) or \ regex.match("(Port-channel[^\s]*)", interface_data["name"]): interface = Interface(name=interface_data["name"], shutdown=False) if interface_data["lineProtocolStatus"] == "down": interface.shutdown = True interface.mtu = int(interface_data["mtu"]) interface.auto_negotiation = ON if interface_data["autoNegotiate"] == "on" else OFF if interface.name in switchports_data: patch_switchport(interface, switchports_data[interface.name]["switchportInfo"]) interfaces.append(interface) return interfaces
def node_to_interface(self, interface_node, config): interface = Interface() interface.bond_master = get_bond_master(interface_node) interface.port_mode = self.get_port_mode(interface_node) or ACCESS vlans = list_vlan_members(interface_node, config) if interface.port_mode is ACCESS: interface.access_vlan = first(vlans) else: interface.trunk_vlans = vlans interface.trunk_native_vlan = value_of(interface_node.xpath("unit/family/ethernet-switching/native-vlan-id"), transformer=int) interface.name = value_of(interface_node.xpath("name")) interface.shutdown = first(interface_node.xpath("disable")) is not None return interface
def test_add_interface_to_bond(self): self.real_switch_mock.should_receive("get_bonds").once() \ .and_return([Bond(1)]) self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2')]) self.switch.get_bonds() self.switch.get_interfaces() self.real_switch_mock.should_receive("add_interface_to_bond").once() \ .with_args('xe-1/0/2', 1) self.switch.add_interface_to_bond('xe-1/0/2', 1) self.real_switch_mock.should_receive("get_interfaces").once() \ .and_return([Interface('xe-1/0/2', bond_master=1, port_mode=BOND_MEMBER)]) assert_that(self.switch.get_bonds(), is_([Bond(1, members=['xe-1/0/2'])])) assert_that( self.switch.get_interfaces(), is_([Interface('xe-1/0/2', bond_master=1, port_mode=BOND_MEMBER)]))
def to_api(self, bond): return dict( number=bond.number, link_speed=bond.link_speed, members=bond.members, interface=interface.to_api(Interface( shutdown=bond.shutdown, port_mode=bond.port_mode, access_vlan=bond.access_vlan, trunk_native_vlan=bond.trunk_native_vlan, trunk_vlans=bond.trunk_vlans, mtu=bond.mtu )) )
def get_interfaces(self): interfaces = [] interface_vlans = {} for if_data in split_on_dedent(self.shell.do("show interfaces")): if regex.match("^\w*Ethernet([^\s]*) is (\w*).*", if_data[0]): i = Interface(name="ethernet {}".format(regex[0]), port_mode=ACCESS, shutdown=regex[1] == "disabled") for line in if_data: if regex.match("Port name is (.*)", line): i.description = regex[0] interfaces.append(i) interface_vlans[i.name] = { "object": i, "untagged": None, "tagged": [] } for vlan_data in split_on_bang(self.shell.do("show running-config vlan")): if regex.match("^vlan (\d*)", vlan_data[0]): vlan_id = int(regex[0]) for line in vlan_data: if regex.match(" untagged (.*)", line): for name in _to_real_names(parse_if_ranges(regex[0])): interface_vlans[name]["untagged"] = vlan_id if regex.match(" tagged (.*)", line): for name in _to_real_names(parse_if_ranges(regex[0])): interface_vlans[name]["tagged"].append(vlan_id) for data in interface_vlans.values(): if data["untagged"] is not None and len(data["tagged"]) == 0: data["object"].access_vlan = data["untagged"] elif data["untagged"] is not None and len(data["tagged"]) > 0: data["object"].trunk_native_vlan = data["untagged"] if len(data["tagged"]) > 0: data["object"].port_mode = TRUNK data["object"].trunk_vlans = data["tagged"] return interfaces
def read_interface(self, interface_name): data = self.get_interface_data(interface_name) interface = Interface(name=interface_name, port_mode=ACCESS, shutdown=False) for line in data: if regex.match("switchport mode \S+", line): interface.port_mode = TRUNK if regex.match("shutdown", line): interface.shutdown = True if regex.match("switchport access vlan (\d+)", line): interface.access_vlan = int(regex[0]) if regex.match("switchport general pvid (\d+)", line): interface.trunk_native_vlan = int(regex[0]) if regex.match("switchport \S* allowed vlan (add )?(\S+)", line): interface.trunk_vlans = parse_vlan_ranges(regex[1]) return interface
def to_interface(self): return Interface(name=self.name, shutdown=self.shutdown, port_mode=ACCESS)
def parse_interface(data): if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0]) or regex.match("interface (Port-channel[^\s]*)", data[0])): i = Interface(name=regex[0], shutdown=False) port_mode = access_vlan = native_vlan = trunk_vlans = None for line in data: if regex.match(" switchport mode (.*)", line): port_mode = regex[0] if regex.match(" switchport access vlan (\d*)", line): access_vlan = int(regex[0]) if regex.match(" switchport trunk native vlan (\d*)", line): native_vlan = int(regex[0]) if regex.match(" switchport trunk allowed vlan (.*)", line): trunk_vlans = regex[0] if regex.match(" shutdown", line): i.shutdown = True if not port_mode: i.port_mode = DYNAMIC i.access_vlan = access_vlan i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges( trunk_vlans) if trunk_vlans else [] elif port_mode == 'access': i.port_mode = ACCESS i.access_vlan = access_vlan elif port_mode == 'trunk': i.port_mode = TRUNK i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges( trunk_vlans) if trunk_vlans else [] return i return None
def parse_interface(data): if data and (regex.match("interface (\w*Ethernet[^\s]*)", data[0]) or regex.match("interface (Port-channel[^\s]*)", data[0])): i = Interface(name=regex[0], shutdown=False) port_mode = access_vlan = native_vlan = trunk_vlans = None for line in data: if regex.match(" switchport mode (.*)", line): port_mode = regex[0] if regex.match(" switchport access vlan (\d*)", line): access_vlan = int(regex[0]) if regex.match(" switchport trunk native vlan (\d*)", line): native_vlan = int(regex[0]) if regex.match(" switchport trunk allowed vlan (.*)", line): trunk_vlans = regex[0] if regex.match(" shutdown", line): i.shutdown = True if not port_mode: i.port_mode = DYNAMIC i.access_vlan = access_vlan i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else [] elif port_mode == 'access': i.port_mode = ACCESS i.access_vlan = access_vlan elif port_mode == 'trunk': i.port_mode = TRUNK i.trunk_native_vlan = native_vlan i.trunk_vlans = parse_vlan_ranges(trunk_vlans) if trunk_vlans else [] return i return None
def to_core(self, serialized): params = dict(vars(base_interface.to_core(serialized))) params.update(sub_dict(serialized, 'name', 'bond_master')) return Interface(**params)