def test_description_absent(self): """ Configure any interface to have no description. Attempt the task exactly once. Restore the original description to the interface. """ for device_name in inventory_connected(): for interface_name in interface_names(device_name): original_info = interface_configuration_tuple( device_name, interface_name) interface_configuration_update(device_name, interface_name, None, original_info.address, original_info.netmask, original_info.active, original_info.shutdown) modified_info = interface_configuration_tuple( device_name, interface_name) self.assertIsNone(modified_info.description) interface_configuration_update(device_name, interface_name, original_info.description, original_info.address, original_info.netmask, original_info.active, original_info.shutdown) return self.fail( "Expected (at least) one connected device with (at least) one interface." )
def cdp_enable_device(device_name): ''' Description: This function will iterate all the devices to enable the cdp. ''' for interface_name in interface_names(device_name): cdp_enable_interface(device_name, interface_name)
def main(): ''' Select a device/interface and demonstrate.''' print(plain(doc(interface_configuration_tuple))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue return demonstrate(device_name, interface_name) print("There are no suitable network devices. Demonstration cancelled.")
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Avoid modifying the management interface. if interface_name == mgmt_name: continue demonstrate(device_name, interface_name) return EX_OK print("There are no suitable network devices. Demonstration cancelled.") return EX_TEMPFAIL
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Avoid modifying the management interface. if interface_name == mgmt_name: continue demonstrate(device_name, interface_name) return os.EX_OK print("There are no suitable network devices. Demonstration cancelled.") return os.EX_TEMPFAIL
def test_interface_configuration_tuple(self): device_names = inventory_connected() self.assertTrue(device_names, "Expected one or more connected devices.") interface_count = 0 for device_name in device_names: for interface_name in interface_names(device_name): info = interface_configuration_tuple(device_name, interface_name) self.assertEqual(info.name, interface_name) self.assertTrue(info.description is None or isinstance(info.description, str)) self.assertTrue(info.address is None or isinstance(info.address, str)) self.assertTrue(info.netmask is None or isinstance(info.netmask, str)) interface_count+=1 self.assertNotEqual(0, interface_count, 'Expected one or more interfaces.')
def sample_interface(): 'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.' for device_name in inventory_connected(): print device_name management_name = management_interface(device_name) print management_name if management_name == None: continue # Uncertain which interface is for management. for interface_name in interface_names(device_name): print interface_name if interface_name == management_name: continue return (device_name, interface_name) raise LookupError("Unable to find a suitable sample device and interface in the inventory.")
def sample_interface(): 'Return a tuple of (device_name, interface_name) if possible; otherwise raise LookupError.' for device_name in inventory_connected(): print device_name management_name = management_interface(device_name) print management_name if management_name == None: continue # Uncertain which interface is for management. for interface_name in interface_names(device_name): print interface_name if interface_name == management_name: continue return (device_name, interface_name) raise LookupError( "Unable to find a suitable sample device and interface in the inventory." )
def match(device_name, interface_network): """ Discover matching interface on a different device.""" for other_device in inventory_mounted(): if other_device == device_name: continue for interface_name in interface_names(other_device): interface_config = interface_configuration_tuple(other_device, interface_name) if interface_config.address is None: # Skip network interface with unassigned IP address. continue other_network = to_ip_network(interface_config.address, interface_config.netmask) if other_network == interface_network: print('Match %s/%s/%s to %s/%s' % (device_name, interface_config.address, interface_config.netmask, \ other_device, interface_network)) return interface_config.address return None
def test_description_absent(self): """ Configure any interface to have no description. Attempt the task exactly once. Restore the original description to the interface. """ for device_name in inventory_connected(): for interface_name in interface_names(device_name): original_info = interface_configuration_tuple(device_name, interface_name) interface_configuration_update(device_name, interface_name, None, original_info.address, original_info.netmask, original_info.active, original_info.shutdown) modified_info = interface_configuration_tuple(device_name, interface_name) self.assertIsNone(modified_info.description) interface_configuration_update(device_name, interface_name, original_info.description, original_info.address, original_info.netmask, original_info.active, original_info.shutdown) return self.fail("Expected (at least) one connected device with (at least) one interface.")
def sample_destination(device_name): """ Determine a suitable destination for a static route from the specified network device. """ # Implementation: use 'next subnet' of any data interface on the specified device. mgmt_name = management_interface(device_name) interface_list = interface_names(device_name) for interface_name in interface_list: if interface_name == mgmt_name: # Do not configure static routes on the control plane. continue config = interface_configuration_tuple(device_name, interface_name) if config.address is None: # Skip network interface with unassigned IP address. continue interface_network = to_ip_network(config.address, config.netmask) destination_network = next_subnet(interface_network) return destination_network return None
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue interface_config = interface_configuration_tuple(device_name, interface_name) if not interface_config.address: continue if not interface_config.shutdown: continue demonstrate(device_name, interface_config) return EX_OK print("There are no suitable network devices/interfaces. Demonstration cancelled.") return EX_TEMPFAIL
def main(): print(plain(doc(interface_configuration_update))) for device_name in inventory_connected(): mgmt_name = management_interface(device_name) for interface_name in interface_names(device_name): # Choose interface on 'data plane' not 'control plane'. if interface_name == mgmt_name: continue interface_config = interface_configuration_tuple(device_name, interface_name) if not interface_config.address: continue if interface_config.shutdown: continue demonstrate(device_name, interface_config) return os.EX_OK print("There are no suitable network devices/interfaces. Demonstration cancelled.") return os.EX_TEMPFAIL
def test_interface_configuration_tuple(self): device_names = inventory_connected() self.assertTrue(device_names, "Expected one or more connected devices.") interface_count = 0 for device_name in device_names: for interface_name in interface_names(device_name): info = interface_configuration_tuple(device_name, interface_name) self.assertEqual(info.name, interface_name) self.assertTrue(info.description is None or isinstance(info.description, str)) self.assertTrue(info.address is None or isinstance(info.address, str)) self.assertTrue(info.netmask is None or isinstance(info.netmask, str)) interface_count += 1 self.assertNotEqual(0, interface_count, 'Expected one or more interfaces.')
def demonstrate(device_name): ''' Apply function 'static_route_create' to the specified device for a new destination.''' mgmt_name = management_interface(device_name) interface_list = interface_names(device_name) for interface_name in interface_list: if interface_name == mgmt_name: # Do not configure static routes on the control plane. continue config = interface_configuration_tuple(device_name, interface_name) if config.address is None: # Skip network interface with unassigned IP address. continue print() interface_network = to_ip_network(config.address, config.netmask) next_hop_address = match(device_name, interface_network) if next_hop_address is None: print('No end-point for %s/%s/%s/%s' % (device_name, interface_name, config.address, config.netmask)) continue destination_network = static_route_fixture.new_destination(device_name, interface_network) print('static_route_create(%s, %s, %s)' % (device_name, destination_network, next_hop_address)) static_route_create(device_name, destination_network, next_hop_address) return True return False
def demonstrate(device_name): ''' Apply function 'interface_names' to the specified device.''' print('interface_names(' + device_name, end=')\n') print(interface_names(device_name))
def test_interface_names(self): device_names = inventory_connected() self.assertTrue(device_names, "Expected one or more connected devices.") for device_name in device_names: interfaces = interface_names(device_name) self.assertGreater(len(interfaces), 1, 'Expected multiple interfaces, got %d' % len(interfaces))
def test_interface_names(self): device_names = inventory_connected() self.assertTrue(device_names, "Expected one or more connected devices.") for device_name in device_names: interfaces = interface_names(device_name) self.assertGreater(len(interfaces), 1, "Expected multiple interfaces, got %d" % len(interfaces))
def cdp_enable_device(device_name): for interface_name in interface_names(device_name): cdp_enable_interface(device_name, interface_name)