def setup(self): require_user('root') self.if_simple = None self.interfaces = self.create_interfaces() self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg))
def setup(self): require_user('root') self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) self.interfaces = []
def setup(self): require_user('root') self.netns = str(uuid.uuid4()) self.ipnets = [allocate_network() for _ in range(3)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.sources = [{'target': 'localhost'}, {'netns': self.netns}] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.sources, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb)))
class Basic(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] def ifaddr(self): return str(self.ipranges[0].pop()) def ifname(self): ret = uifname() self.interfaces.append(ret) return ret def setup(self): require_user('root') self.interfaces = [] self.ipnets = [allocate_network() for _ in range(2)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): try: ipr.link('del', index=ipr.link_lookup(ifname=link)[0]) except Exception: pass self.ndb.close() for net in self.ipnets: free_network(net)
def __init__(self, stdout=None, log=None, sources=None): global HAS_READLINE self.db = NDB(log=log, sources=sources) self.db.config = {'show_format': 'json'} self.stdout = stdout or sys.stdout self.session = Session(self.db, self.stdout, self.set_prompt) self.matches = [] self.isatty = sys.stdin.isatty() self.prompt = '' self.set_prompt() code.InteractiveConsole.__init__(self) if HAS_READLINE: readline.parse_and_bind('tab: complete') readline.set_completer(self.completer) readline.set_completion_display_matches_hook(self.display)
def test_add_del_ip_dict(context): ifname = context.new_ifname ifaddr1 = context.new_ipaddr ifaddr2 = context.new_ipaddr log_spec = ( context.spec.log_spec[0] + '.chaotic', context.spec.log_spec[1], ) with NDB( log=log_spec, sources=[ { 'target': 'localhost', 'kind': 'ChaoticIPRoute', 'success_rate': 0.98, } ], ) as test_ndb: ( test_ndb.interfaces.create( ifname=ifname, kind='dummy', state='down' ) .add_ip({'address': ifaddr1, 'prefixlen': 24}) .add_ip({'address': ifaddr2, 'prefixlen': 24}) .commit() ) assert address_exists(context.netns, ifname=ifname, address=ifaddr1) assert address_exists(context.netns, ifname=ifname, address=ifaddr2) ( test_ndb.interfaces[{'ifname': ifname}] .del_ip({'address': ifaddr2, 'prefixlen': 24}) .del_ip({'address': ifaddr1, 'prefixlen': 24}) .commit() ) assert not address_exists( context.netns, ifname=ifname, address=ifaddr1 ) assert not address_exists( context.netns, ifname=ifname, address=ifaddr2 )
def test_multiple_sources(self): # NB: no 'localhost' record -- important # sources = {'localhost0': IPRoute(), 'localhost1': RemoteIPRoute(), # local mitogen source 'localhost2': RemoteIPRoute()} # one more # check all the views # with NDB(sources=sources) as ndb: assert len(ndb.interfaces.csv()) assert len(ndb.neighbours.csv()) assert len(ndb.addresses.csv()) assert len(ndb.routes.csv()) for source in sources: assert sources[source].closed
def test_no_cleanup(spec): # start and stop the DB, leaving all the data in the DB file NDB(db_provider='sqlite3', db_spec=spec.db_spec, db_cleanup=False, log=spec.log_spec).close() # open the DB file db = sqlite3.connect(spec.db_spec) cursor = db.cursor() cursor.execute('SELECT * FROM interfaces') interfaces = cursor.fetchall() # at least two records: idx 0 and loopback assert len(interfaces) > 1 # all the interfaces must be of the same source, 'localhost' assert set([x[0] for x in interfaces]) == set(('localhost', ))
def test_multiple_sources(self): # NB: no 'localhost' record -- important # sources = [{'target': 'localhost0', 'kind': 'local'}, {'target': 'localhost1', 'kind': 'remote'}, {'target': 'localhost2', 'kind': 'remote'}] # check all the views # with NDB(sources=sources) as ndb: assert len(list(ndb.interfaces.dump())) assert len(list(ndb.neighbours.dump())) assert len(list(ndb.addresses.dump())) assert len(list(ndb.routes.dump())) for source in ndb.sources: assert ndb.sources[source].nl.closed
def _add_namespace(self, name, address, netmaskLength): """ Creates a namespace with the given name, and creates a veth interface with one endpoint inside the namespace which has the given address and netmask length. The peer end of veth interface can be used to connect the namespace to a bridge. """ self._remove_namespace_if_exists(name) netns.create(name) veth_name = "veth0" _remove_interface_if_exists(self.vethPeer) with NDB() as ndb: # # Add netns to the NDB sources # # ndb.interfaces["lo"] is a short form of # ndb.interfaces[{"target": "localhost", "ifname": "lo"}] # # To address interfaces/addresses/routes wthin a netns, use # ndb.interfaces[{"target": netns_name, "ifname": "lo"}] ndb.sources.add(netns=name) # # Create veth (ndb.interfaces.create( ifname=veth_name, kind="veth", peer=self.vethPeer, state="up", ).commit().set(net_ns_fd=name).commit()) # # .interfaces.wait() returns an interface object when # it becomes available on the specified source (ndb.interfaces.wait( target=name, ifname=veth_name).set(state="up").add_ip( "%s/%s" % (address, netmaskLength)).commit()) # (ndb.interfaces[{ "target": name, "ifname": "lo" }].set(state="up").commit())
def test_multiple_sources(self): # NB: no 'localhost' record -- important # nl = { 'localhost0': IPRoute(), 'localhost1': RemoteIPRoute(), # mitogen localhost connection 'localhost2': RemoteIPRoute() } # one more # check all the views # with NDB(nl=nl) as ndb: assert len(ndb.interfaces.csv()) assert len(ndb.neighbours.csv()) assert len(ndb.addresses.csv()) assert len(ndb.routes.csv()) for source in nl: assert nl[source].closed
def teardown(self): with NDB() as indb: if self.nsname: (indb.sources.add(netns=self.nsname)) for link in reversed(self.interfaces): try: (indb.interfaces[link].remove().commit()) except Exception: pass for link in reversed(self.interfaces): try: (indb.interfaces[self.getspec( ifname=link)].remove().commit()) except Exception: pass self.ndb.close() for net in self.ipnets: free_network(net) if self.nsname: netns.remove(self.nsname)
def ensure_vnet_veth_interfaces(config): """ Create en configure the veth interfaces defined in the VNet config Assumes there are veth interfaces present in the config :param dict config: The config generated by get_config() """ logger.info("VNet veth config found, ensuring interfaces") for name, data in config["veths"].items(): # Set STP on the master if required if "stp" in data: logger.info("{} STP on VNet interface {}".format("Enabling" if data["stp"] else "Disabling", data["bridge"])) state = 1 if data["stp"] else 0 nbd = NDB(log=False) with nbd.interfaces[data["bridge"]] as bridge: bridge.set("br_stp_state", state) if not check_if_interface_exists(name): create_veth_interface(name, data) # Always configure a VNet veth interface to make sure it is connected to its master bridge configure_veth_interface(name, data) configure_vnet_interface(name)
def test_multiple_sources(context): ''' NDB should work with multiple netlink sources Check that it actually works: * with multiple sources of different kind * without the default "localhost" RTNL source ''' nsname = context.new_nsname # # NB: no 'localhost' record -- important ! sources = [{ 'target': 'localhost0', 'kind': 'local' }, { 'target': 'localhost1', 'kind': 'netns', 'netns': nsname }, { 'target': 'localhost2', 'kind': 'local' }] ndb = None # # check that all the view has length > 0 # that means that the sources are working with NDB(sources=sources) as ndb: assert len(list(ndb.interfaces.dump())) assert len(list(ndb.neighbours.dump())) assert len(list(ndb.addresses.dump())) assert len(list(ndb.routes.dump())) # here NDB() gets closed # # # the `ndb` variable still references the closed # NDB() object from the code block above, check # that all the sources are closed too for source in ndb.sources: assert ndb.sources[source].nl.closed
def show_vnet_interface_status(config): """ Shows the VNet interface status to the user :param dict config: The config generated by get_config() """ logger.info("Listing VNet interface statuses") header = ["Name", "Status", "L2_addr", "Sniffer", "STP", "Used by"] statuses = [] ip = IPRoute() ndb = NDB(log=False) for ifname in get_vnet_interface_names_from_config(config): used_by = get_machines_by_vnet_interface_name(config, ifname) dev = ip.link_lookup(ifname=ifname) if not dev: # Link does not exist statuses.append([ifname, "NA", "NA", "NA", "NA", ", ".join(used_by)]) else: # Get the link info sniffer = check_if_sniffer_exists(ifname) with ndb.interfaces[ifname] as info: statuses.append([ifname, info["state"], info["address"], sniffer, bool(info["br_stp_state"]), ", ".join(used_by)]) print(tabulate(statuses, headers=header, tablefmt="pretty"))
def setup(self): require_user('root') self.ipnets = [] self.wg = WireGuard() self.log_id = uifname() self.ndb = NDB(log='../ndb-%s-%s.log' % (os.getpid(), self.log_id), rtnl_debug=True) self.netif = uifname() self.wg0if = uifname() self.wg1if = uifname() self.ifnames = [self.netif, self.wg0if, self.wg1if] self.ipnets.append(allocate_network()) self.ipnets.append(allocate_network()) self.ipranges = [[str(x) for x in net] for net in self.ipnets] # create the "network" interface (self.ndb.interfaces.create(ifname=self.netif, kind='dummy').set( 'state', 'up').add_ip('%s/24' % (self.ipranges[0][1])).commit()) # create one peer (self.ndb.interfaces.create(ifname=self.wg0if, kind='wireguard').set( 'state', 'up').add_ip('%s/24' % (self.ipranges[1][1])).commit()) # create another peer (self.ndb.interfaces.create(ifname=self.wg1if, kind='wireguard').set( 'state', 'up').add_ip('%s/24' % (self.ipranges[1][2])).commit())
def test_basic(self): ifname = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() ifaddr3 = self.ifaddr() (self.ndb.interfaces.create( target=self.netns, ifname=ifname, kind='dummy').ipaddr.create(address=ifaddr1, prefixlen=24).create( address=ifaddr2, prefixlen=24).create(address=ifaddr3, prefixlen=24).commit()) with NDB(sources=[{ 'target': 'localhost', 'netns': self.netns, 'kind': 'netns' }]) as ndb: if_idx = ndb.interfaces[ifname]['index'] addr1_idx = ndb.addresses['%s/24' % ifaddr1]['index'] addr2_idx = ndb.addresses['%s/24' % ifaddr2]['index'] addr3_idx = ndb.addresses['%s/24' % ifaddr3]['index'] assert if_idx == addr1_idx == addr2_idx == addr3_idx
class TestNetNS(object): db_provider = 'sqlite3' db_spec = ':memory:' def setup(self): require_user('root') self.netns = str(uuid.uuid4()) self.ipnets = [allocate_network() for _ in range(3)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.sources = [{'target': 'localhost'}, {'netns': self.netns}] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.sources, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) def ifaddr(self, r=0): return str(self.ipranges[r].pop()) def teardown(self): for net in self.ipnets: free_network(net) self.ndb.close() netns.remove(self.netns) def test_basic(self): ifname = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() ifaddr3 = self.ifaddr() (self .ndb .interfaces .create(target=self.netns, ifname=ifname, kind='dummy') .ipaddr .create(address=ifaddr1, prefixlen=24) .create(address=ifaddr2, prefixlen=24) .create(address=ifaddr3, prefixlen=24) .commit()) with NDB(sources=[{'target': 'localhost', 'netns': self.netns, 'kind': 'netns'}]) as ndb: if_idx = ndb.interfaces[ifname]['index'] addr1_idx = ndb.addresses['%s/24' % ifaddr1]['index'] addr2_idx = ndb.addresses['%s/24' % ifaddr2]['index'] addr3_idx = ndb.addresses['%s/24' % ifaddr3]['index'] assert if_idx == addr1_idx == addr2_idx == addr3_idx def _assert_test_view(self, ifname, ifaddr): with NDB(sources=[{'target': 'localhost', 'netns': self.netns, 'kind': 'netns'}]) as ndb: (if_idx, if_state, if_addr, if_flags) = ndb.interfaces[ifname].fields('index', 'state', 'address', 'flags') addr_idx = ndb.addresses['%s/24' % ifaddr]['index'] assert if_idx == addr_idx assert if_state == 'up' assert if_flags & 1 assert if_addr == '00:11:22:33:44:55' def test_view_constraints_pipeline(self): ifname = uifname() ifaddr = self.ifaddr() (self .ndb .interfaces .constraint('target', self.netns) .create(ifname=ifname, kind='dummy') .set('address', '00:11:22:33:44:55') .set('state', 'up') .ipaddr .create(address=ifaddr, prefixlen=24) .commit()) self._assert_test_view(ifname, ifaddr) def test_view_constraints_cmanager(self): ifname = uifname() ifaddr = self.ifaddr() with self.ndb.interfaces as view: view.constraints['target'] = self.netns with view.create(ifname=ifname, kind='dummy') as interface: interface['address'] = '00:11:22:33:44:55' interface['state'] = 'up' with interface.ipaddr as aview: with aview.create(address=ifaddr, prefixlen=24): pass self._assert_test_view(ifname, ifaddr) def test_move(self): ifname = uifname() ifaddr = self.ifaddr() # create the interfaces (self .ndb .interfaces .create(ifname=ifname, kind='dummy') .commit()) # move it to a netns (self .ndb .interfaces[ifname] .set('net_ns_fd', self.netns) .commit()) # setup the interface only when it is moved (self .ndb .interfaces .wait(target=self.netns, ifname=ifname) .set('state', 'up') .set('address', '00:11:22:33:44:55') .ipaddr .create(address=ifaddr, prefixlen=24) .commit()) self._assert_test_view(ifname, ifaddr)
class TestRollback(TestBase): def setup(self): require_user('root') self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) self.interfaces = [] def test_simple_deps(self): # register NDB handler to wait for the interface self.if_simple = uifname() ifaddr = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) # # simple dummy interface with one address and # one dependent route # (self .interfaces .append(self .ndb .interfaces .create(ifname=self.if_simple, kind='dummy') .set('state', 'up') .commit()['index'])) (self .ndb .addresses .create(address=ifaddr, prefixlen=24, index=self.interfaces[-1]) .commit()) (self .ndb .routes .create(dst=dst, dst_len=24, gateway=router) .commit()) iface = self.ndb.interfaces[self.if_simple] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert not grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) def test_bridge_deps(self): self.if_br0 = uifname() self.if_br0p0 = uifname() self.if_br0p1 = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) (self .interfaces .append(self .ndb .interfaces .create(ifname=self.if_br0, kind='bridge', state='up') .commit()['index'])) (self .interfaces .append(self .ndb .interfaces .create(ifname=self.if_br0p0, kind='dummy', state='up', master=self.ndb.interfaces[self.if_br0]['index']) .commit()['index'])) (self .interfaces .append(self .ndb .interfaces .create(ifname=self.if_br0p1, kind='dummy', state='up', master=self.ndb.interfaces[self.if_br0]['index']) .commit()['index'])) (self .ndb .interfaces[self.if_br0] .ipaddr .create(address=ifaddr1, prefixlen=24) .commit()) (self .ndb .interfaces[self.if_br0] .ipaddr .create(address=ifaddr2, prefixlen=24) .commit()) (self .ndb .routes .create(dst=dst, dst_len=24, gateway=router) .commit()) master = self.ndb.interfaces[self.if_br0]['index'] self.ndb.interfaces.wait(ifname=self.if_br0p0, master=master) self.ndb.interfaces.wait(ifname=self.if_br0p1, master=master) self.ndb.addresses.wait(address=ifaddr1) self.ndb.addresses.wait(address=ifaddr2) self.ndb.routes.wait(dst=dst, gateway=router) iface = self.ndb.interfaces[self.if_br0] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert not grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) def test_vlan_deps(self): if_host = uifname() if_vlan = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) (self .interfaces .append(self .ndb .interfaces .create(ifname=if_host, kind='dummy', state='up') .commit()['index'])) (self .interfaces .append(self .ndb .interfaces .create(ifname=if_vlan, kind='vlan', link=self.interfaces[-1], state='up', vlan_id=1001) .commit()['index'])) (self .ndb .addresses .create(address=ifaddr1, prefixlen=24, index=self.interfaces[-1]) .commit()) (self .ndb .addresses .create(address=ifaddr2, prefixlen=24, index=self.interfaces[-1]) .commit()) (self .ndb .routes .create(dst=dst, dst_len=24, gateway=router) .commit()) iface = self.ndb.interfaces[if_host] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=if_host) assert grep('%s ip link show' % self.ssh, pattern=if_vlan) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=if_vlan) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=if_host) assert not grep('%s ip link show' % self.ssh, pattern=if_vlan) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert not grep('%s ip route show' % self.ssh, pattern=if_vlan) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert not grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=if_host) assert grep('%s ip link show' % self.ssh, pattern=if_vlan) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=if_vlan) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan)
class TestBase(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' def link_wait(self, ifname): with self.nl_class(**self.nl_kwarg) as ipr: for _ in range(5): try: return ipr.link_lookup(ifname=ifname)[0] except: time.sleep(0.1) raise Exception('link setup error') def create_interfaces(self): # dummy interface if_dummy = uifname() if_vlan_stag = uifname() if_vlan_ctag = uifname() if_bridge = uifname() if_port = uifname() ret = [] with self.nl_class(**self.nl_kwarg) as ipr: ipr.link('add', ifname=if_dummy, kind='dummy') ret.append(self.link_wait(if_dummy)) ipr.link('add', ifname=if_vlan_stag, link=ret[-1], vlan_id=101, vlan_protocol=0x88a8, kind='vlan') ret.append(self.link_wait(if_vlan_stag)) ipr.link('add', ifname=if_vlan_ctag, link=ret[-1], vlan_id=1001, vlan_protocol=0x8100, kind='vlan') ret.append(self.link_wait(if_vlan_ctag)) ipr.link('add', ifname=if_port, kind='dummy') ret.append(self.link_wait(if_port)) ipr.link('add', ifname=if_bridge, kind='bridge') ret.append(self.link_wait(if_bridge)) ipr.link('set', index=ret[-2], master=ret[-1]) return ret def setup(self): require_user('root') self.if_simple = None self.interfaces = self.create_interfaces() self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg)) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=link) self.ndb.close() def fetch(self, request, values=[]): with self.ndb.schema.db_lock: return (self.ndb.schema.execute(request, values).fetchall())
class TestBase(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' def link_wait(self, ifname): with self.nl_class(**self.nl_kwarg) as ipr: for _ in range(5): try: return ipr.link_lookup(ifname=ifname)[0] except: time.sleep(0.1) raise Exception('link setup error') def create_interfaces(self): # dummy interface if_dummy = uifname() if_vlan_stag = uifname() if_vlan_ctag = uifname() if_bridge = uifname() if_port = uifname() ret = [] with self.nl_class(**self.nl_kwarg) as ipr: ipr.link('add', ifname=if_dummy, kind='dummy') ret.append(self.link_wait(if_dummy)) ipr.link('add', ifname=if_vlan_stag, link=ret[-1], vlan_id=101, vlan_protocol=0x88a8, kind='vlan') ret.append(self.link_wait(if_vlan_stag)) ipr.link('add', ifname=if_vlan_ctag, link=ret[-1], vlan_id=1001, vlan_protocol=0x8100, kind='vlan') ret.append(self.link_wait(if_vlan_ctag)) ipr.link('add', ifname=if_port, kind='dummy') ret.append(self.link_wait(if_port)) ipr.link('add', ifname=if_bridge, kind='bridge') ret.append(self.link_wait(if_bridge)) ipr.link('set', index=ret[-2], master=ret[-1]) return ret def setup(self): require_user('root') self.if_simple = None self.interfaces = self.create_interfaces() self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg)) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=link) self.ndb.close() def fetch(self, request, values=[]): with self.ndb.schema.db_lock: return (self .ndb .schema .execute(request, values) .fetchall())
class TestPreSet(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] def create_interfaces(self): # dummy interface if_dummy = uifname() if_vlan_stag = uifname() if_vlan_ctag = uifname() if_bridge = uifname() if_port = uifname() if_addr1 = self.ifaddr() if_addr2 = self.ifaddr() ret = [] ret.append(self .ndb .interfaces .create(ifname=if_dummy, kind='dummy') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_vlan_stag, link=self.ndb.interfaces[if_dummy]['index'], vlan_id=101, vlan_protocol=0x88a8, kind='vlan') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_vlan_ctag, link=self.ndb.interfaces[if_vlan_stag]['index'], vlan_id=1001, vlan_protocol=0x8100, kind='vlan') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_bridge, kind='bridge') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_port, master=self.ndb.interfaces[if_bridge]['index'], kind='dummy') .commit()['index']) (self .ndb .interfaces[if_bridge] .ipaddr .create(address=if_addr1, prefixlen=24) .commit()) (self .ndb .interfaces[if_bridge] .ipaddr .create(address=if_addr2, prefixlen=24) .commit()) self.if_bridge = if_bridge return ret def ifaddr(self, r=0): return str(self.ipranges[r].pop()) def setup(self): require_user('root') self.log_id = str(uuid.uuid4()) self.if_simple = None self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, log='../ndb-%s-%s.log' % (os.getpid(), self.log_id), rtnl_debug=True) self.interfaces = self.create_interfaces() def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=link) self.ndb.close() for net in self.ipnets: free_network(net) def fetch(self, request, values=[]): return (self .ndb .schema .fetch(request, values))
class Basic(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] nsname = None def ifaddr(self): return str(self.ipranges[0].pop()) def ifname(self): ret = uifname() self.interfaces.append(ret) return ret def getspec(self, **kwarg): spec = dict(kwarg) if self.nsname is not None: spec['target'] = self.nsname return spec def setup(self): require_user('root') self.interfaces = [] self.log_id = str(uuid.uuid4()) self.ipnets = [allocate_network() for _ in range(2)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, log='../ndb-%s-%s.log' % (os.getpid(), self.log_id), rtnl_debug=True) if self.nsname: netns.create(self.nsname) (self .ndb .sources .add(netns=self.nsname)) def teardown(self): with NDB() as indb: if self.nsname: (indb .sources .add(netns=self.nsname)) for link in reversed(self.interfaces): try: (indb .interfaces[link] .remove() .commit()) except Exception: pass for link in reversed(self.interfaces): try: (indb .interfaces[self.getspec(ifname=link)] .remove() .commit()) except Exception: pass self.ndb.close() for net in self.ipnets: free_network(net) if self.nsname: netns.remove(self.nsname)
class TestRollback(TestBase): def setup(self): require_user('root') self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, rtnl_log=True, sources=self.nl_class(**self.nl_kwarg)) def test_simple_deps(self): # register NDB handler to wait for the interface self.if_simple = uifname() ifaddr = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) with self.nl_class(**self.nl_kwarg) as ipr: self.interfaces = [] # # simple dummy interface with one address and # one dependent route # ipr.link('add', ifname=self.if_simple, kind='dummy') self.ndb.wait({'interfaces': [{'ifname': self.if_simple}]}) (self .interfaces .append(self .ndb .interfaces[self.if_simple]['index'])) ipr.link('set', index=self.interfaces[-1], state='up') ipr.addr('add', index=self.interfaces[-1], address=ifaddr, prefixlen=24) ipr.route('add', dst=dst, dst_len=24, gateway=router) self.ndb.wait({'addresses': [{'address': ifaddr}], 'routes': [{'dst': dst}]}) iface = self.ndb.interfaces[self.if_simple] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert not grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern=self.if_simple) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) def test_bridge_deps(self): self.if_br0 = uifname() self.if_br0p0 = uifname() self.if_br0p1 = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) with self.nl_class(**self.nl_kwarg) as ipr: self.interfaces = [] ipr.link('add', ifname=self.if_br0, kind='bridge') ipr.link('add', ifname=self.if_br0p0, kind='dummy') ipr.link('add', ifname=self.if_br0p1, kind='dummy') self.ndb.wait({'interfaces': [{'ifname': self.if_br0}, {'ifname': self.if_br0p0}, {'ifname': self.if_br0p1}]}) self.interfaces.append(self.ndb.interfaces[self.if_br0]['index']) self.interfaces.append(self.ndb.interfaces[self.if_br0p0]['index']) self.interfaces.append(self.ndb.interfaces[self.if_br0p1]['index']) ipr.link('set', index=self.interfaces[-3], state='up') ipr.addr('add', index=self.interfaces[-3], address=ifaddr1, prefixlen=24) ipr.addr('add', index=self.interfaces[-3], address=ifaddr2, prefixlen=24) ipr.route('add', dst=dst, dst_len=24, gateway=router) ipr.link('set', index=self.interfaces[-1], state='up', master=self.interfaces[-3]) ipr.link('set', index=self.interfaces[-2], state='up', master=self.interfaces[-3]) master = self.ndb.interfaces[self.if_br0]['index'] self.ndb.wait({'interfaces': [{'ifname': self.if_br0p0, 'master': master}, {'ifname': self.if_br0p1, 'master': master}], 'addresses': [{'address': ifaddr1}, {'address': ifaddr2}], 'routes': [{'dst': dst}]}) iface = self.ndb.interfaces[self.if_br0] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert not grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=self.if_br0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p0) assert grep('%s ip link show' % self.ssh, pattern=self.if_br0p1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=self.if_br0) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) def test_vlan_deps(self): if_host = uifname() if_vlan = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() router = self.ifaddr() dst = str(self.ipnets[1].network) with self.nl_class(**self.nl_kwarg) as ipr: self.interfaces = [] ipr.link('add', ifname=if_host, kind='dummy') self.ndb.wait({'interfaces': [{'ifname': if_host}]}) self.interfaces.append(self.ndb.interfaces[if_host]['index']) ipr.link('set', index=self.interfaces[-1], state='up') ipr.link('add', ifname=if_vlan, kind='vlan', link=self.interfaces[-1], vlan_id=1001) self.ndb.wait({'interfaces': [{'ifname': if_vlan}]}) self.interfaces.append(self.ndb.interfaces[if_vlan]['index']) ipr.link('set', index=self.interfaces[-1], state='up') ipr.addr('add', index=self.interfaces[-1], address=ifaddr1, prefixlen=24) ipr.addr('add', index=self.interfaces[-1], address=ifaddr2, prefixlen=24) ipr.route('add', dst=dst, dst_len=24, gateway=router) self.ndb.wait({'addresses': [{'address': ifaddr1}, {'address': ifaddr2}], 'routes': [{'dst': dst}]}) iface = self.ndb.interfaces[if_host] # check everything is in place assert grep('%s ip link show' % self.ssh, pattern=if_host) assert grep('%s ip link show' % self.ssh, pattern=if_vlan) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=if_vlan) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan) # remove the interface iface.remove() iface.commit() # check there is no interface, no route assert not grep('%s ip link show' % self.ssh, pattern=if_host) assert not grep('%s ip link show' % self.ssh, pattern=if_vlan) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert not grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert not grep('%s ip route show' % self.ssh, pattern=if_vlan) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert not grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan) # revert the changes using the implicit last_save iface.rollback() assert grep('%s ip link show' % self.ssh, pattern=if_host) assert grep('%s ip link show' % self.ssh, pattern=if_vlan) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr1) assert grep('%s ip addr show' % self.ssh, pattern=ifaddr2) assert grep('%s ip route show' % self.ssh, pattern=if_vlan) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (dst, router)) assert grep('%s cat /proc/net/vlan/config' % self.ssh, pattern=if_vlan)
class Console(code.InteractiveConsole): def __init__(self, stdout=None, log=None, sources=None): global HAS_READLINE self.db = NDB(log=log, sources=sources) self.db.config = {'show_format': 'json'} self.stdout = stdout or sys.stdout self.session = Session(self.db, self.stdout, self.set_prompt) self.matches = [] self.isatty = sys.stdin.isatty() self.prompt = '' self.set_prompt() code.InteractiveConsole.__init__(self) if HAS_READLINE: readline.parse_and_bind('tab: complete') readline.set_completer(self.completer) readline.set_completion_display_matches_hook(self.display) def close(self): self.db.close() def help(self): self.session.lprint("Built-in commands: \n" "exit\t-- exit cli\n" "ls\t-- list current namespace\n" ".\t-- print the current object\n" ".. or Ctrl-D\t-- one level up\n") def set_prompt(self, prompt=None): if self.isatty: if prompt is not None: self.prompt = '%s > ' % (prompt) else: self.prompt = '%s > ' % (self.session.ptr.__class__.__name__) self.prompt = '%s@%s : %s' % (getpass.getuser(), (socket .gethostname() .split('.')[0]), self.prompt) def loadrc(self, fname): with open(fname, 'r') as f: self.session.handle(f.read()) def interact(self, readfunc=None): if self.isatty and readfunc is None: self.session.lprint("pyroute2 cli prototype") if readfunc is None: readfunc = self.raw_input indent = 0 while True: try: text = readfunc(self.prompt) except EOFError: if self.session.stack: self.session.lprint() self.session.stack_pop() continue else: self.close() break except Exception: self.close() break try: indent = self.session.handle(text, indent) except: self.showtraceback() continue def completer(self, text, state): if state == 0: d = [x for x in dir(self.session.ptr) if x.startswith(text)] if isinstance(self.session.ptr, dict): keys = [str(y) for y in self.session.ptr.keys()] d.extend([x for x in keys if x.startswith(text)]) self.matches = d try: return self.matches[state] except: pass def display(self, line, matches, length): self.session.lprint() self.session.lprint(matches) self.session.lprint('%s%s' % (self.prompt, line), end='')
class Console(code.InteractiveConsole): def __init__(self, stdout=None, log=None, sources=None): global HAS_READLINE self.db = NDB(log=log, sources=sources) self.db.config = {'show_format': 'json'} self.stdout = stdout or sys.stdout self.session = Session(self.db, self.stdout, self.set_prompt) self.matches = [] self.isatty = sys.stdin.isatty() self.prompt = '' self.set_prompt() code.InteractiveConsole.__init__(self) if HAS_READLINE: readline.parse_and_bind('tab: complete') readline.set_completer(self.completer) readline.set_completion_display_matches_hook(self.display) def close(self): self.db.close() def help(self): self.session.lprint("Built-in commands: \n" "exit\t-- exit cli\n" "ls\t-- list current namespace\n" ".\t-- print the current object\n" ".. or Ctrl-D\t-- one level up\n") def set_prompt(self, prompt=None): if self.isatty: if prompt is not None: self.prompt = '%s > ' % (prompt) else: self.prompt = '%s > ' % (self.session.ptr.__class__.__name__) self.prompt = '%s@%s : %s' % (getpass.getuser(), (socket.gethostname().split('.')[0]), self.prompt) def loadrc(self, fname): with open(fname, 'r') as f: self.session.handle(f.read()) def interact(self, readfunc=None): if self.isatty and readfunc is None: self.session.lprint("pyroute2 cli prototype") if readfunc is None: readfunc = self.raw_input indent = 0 while True: try: text = readfunc(self.prompt) except EOFError: if self.session.stack: self.session.lprint() self.session.stack_pop() continue else: self.close() break except Exception: self.close() break try: indent = self.session.handle(text, indent) except: self.showtraceback() continue def completer(self, text, state): if state == 0: d = [x for x in dir(self.session.ptr) if x.startswith(text)] if isinstance(self.session.ptr, dict): keys = [str(y) for y in self.session.ptr.keys()] d.extend([x for x in keys if x.startswith(text)]) self.matches = d try: return self.matches[state] except: pass def display(self, line, matches, length): self.session.lprint() self.session.lprint(matches) self.session.lprint('%s%s' % (self.prompt, line), end='')
class TestCreate(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' def ifname(self): ret = uifname() self.interfaces.append(ret) return ret def setup(self): require_user('root') self.interfaces = [] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg)) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=ipr.link_lookup(ifname=link)[0]) self.ndb.close() def test_context_manager(self): ifname = uifname() address = '00:11:22:36:47:58' ifobj = (self .ndb .interfaces .add(ifname=ifname, kind='dummy')) with ifobj: pass assert grep('%s ip link show' % self.ssh, pattern=ifname) with ifobj: ifobj['state'] = 'up' ifobj['address'] = address assert grep('%s ip link show' % self.ssh, pattern=address) assert self.ndb.interfaces[ifname]['state'] == 'up' def test_fail(self): ifname = uifname() kind = uifname() ifobj = (self .ndb .interfaces .add(ifname=ifname, kind=kind)) save = dict(ifobj) try: ifobj.commit() except NetlinkError as e: assert e.code == 95 # Operation not supported assert save == dict(ifobj) assert ifobj.scope == 'invalid' def test_dummy(self): ifname = self.ifname() (self .ndb .interfaces .add(ifname=ifname, kind='dummy', address='00:11:22:33:44:55') .commit()) assert grep('%s ip link show' % self.ssh, pattern=ifname) assert self.ndb.interfaces[ifname]['address'] == '00:11:22:33:44:55'
class Console(code.InteractiveConsole): def __init__(self, stdout=None): global HAS_READLINE self.db = NDB() self.ptr = self.db self.ptrname = None self.stack = [] self.matches = [] self.isatty = sys.stdin.isatty() self.prompt = '' self.stdout = stdout or sys.stdout self.set_prompt() code.InteractiveConsole.__init__(self) if HAS_READLINE: readline.parse_and_bind('tab: complete') readline.set_completer(self.completer) readline.set_completion_display_matches_hook(self.display) def close(self): self.db.close() def write(self, text=''): self.lprint(text) def pprint(self, text=''): pprint(text, stream=self.stdout) self.stdout.flush() def lprint(self, text='', end='\n'): print(text, file=self.stdout, end=end) self.stdout.flush() def help(self): self.lprint("Built-in commands: \n" "pdb\t-- run pdb (if installed)\n" "exit\t-- exit cli\n" "ls\t-- list current namespace\n" ".\t-- print the current object\n" ".. or ;\t-- one level up\n") def set_prompt(self, prompt=None): if self.isatty: if prompt is not None: self.prompt = '%s > ' % (prompt) else: self.prompt = '%s > ' % (self.ptr.__class__.__name__) self.prompt = '%s@%s : %s' % (getpass.getuser(), (socket .gethostname() .split('.')[0]), self.prompt) def handle_statement(self, stmt): obj = None if stmt.name == 'pdb': if HAS_PDB: pdb.set_trace() else: self.lprint('pdb is not available') elif stmt.name == 'exit': raise SystemExit() elif stmt.name == 'ls': self.lprint(dir(self.ptr)) elif stmt.name == 'help': self.help() elif stmt.name == '.': self.lprint(repr(self.ptr)) elif stmt.name == '..': if self.stack: self.ptr, self.ptrname = self.stack.pop() self.set_prompt(self.ptrname) else: if stmt.kind == t_dict: obj = self.ptr[stmt.kwarg] else: if isinstance(self.ptr, dict): try: obj = self.ptr.get(stmt.name, None) except Exception: pass if obj is None: obj = getattr(self.ptr, stmt.name, None) if obj is None: if isinstance(self.ptr, dict): self.ptr[stmt.name] = stmt.argv[0] return else: raise KeyError() if hasattr(obj, '__call__'): try: ret = obj(*stmt.argv, **stmt.kwarg) if hasattr(obj, '__cptr__'): obj = ret else: if hasattr(ret, 'generator') or hasattr(ret, 'next'): for line in ret: self.pprint(line) return except: self.showtraceback() return if isinstance(obj, (basestring, int, float)): if stmt.argv: self.ptr[stmt.name] = stmt.argv[0] else: self.pprint(obj) else: self.stack.append((self.ptr, self.ptrname)) self.ptr = obj self.ptrname = stmt.name self.set_prompt(stmt.name) def interact(self, readfunc=None): if readfunc is None: readfunc = self.raw_input if self.isatty: self.lprint("pyroute2 cli prototype") indent = 0 while True: try: text = readfunc(self.prompt) except: self.lprint() self.close() break try: parser = Parser(text) except: self.showtraceback() continue for sentence in parser.sentences: if sentence.indent < indent: if self.stack: self.ptr, self.ptrname = self.stack.pop() indent = sentence.indent for stmt in sentence.statements: try: self.handle_statement(stmt) except SystemExit: self.close() return except KeyError: self.lprint('object not found') except: self.showtraceback() def completer(self, text, state): if state == 0: d = [x for x in dir(self.ptr) if x.startswith(text)] if isinstance(self.ptr, dict): keys = [str(y) for y in self.ptr.keys()] d.extend([x for x in keys if x.startswith(text)]) self.matches = d try: return self.matches[state] except: pass def display(self, line, matches, length): self.lprint() self.lprint(matches) self.lprint('%s%s' % (self.prompt, line), end='')
def toggle_stp(self, name, value): # 0 is off > 0 is on with NDB(log="off") as ndb: with ndb.interfaces[name] as br: br['br_stp_state'] = value
def stp(self): with NDB(log="off") as ndb: with ndb.interfaces[self.name] as br: return bool(br['br_stp_state'])
class TestBase(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] def create_interfaces(self): # dummy interface if_dummy = uifname() if_vlan_stag = uifname() if_vlan_ctag = uifname() if_bridge = uifname() if_port = uifname() if_addr1 = self.ifaddr() if_addr2 = self.ifaddr() ret = [] with self.nl_class(**self.nl_kwarg) as ipr: ipr.link('add', ifname=if_dummy, kind='dummy') self.ndb.wait({'interfaces': [{'ifname': if_dummy}]}) ret.append(self.ndb.interfaces[if_dummy]['index']) ipr.link('add', ifname=if_vlan_stag, link=self.ndb.interfaces[if_dummy]['index'], vlan_id=101, vlan_protocol=0x88a8, kind='vlan') self.ndb.wait({'interfaces': [{'ifname': if_vlan_stag}]}) ret.append(self.ndb.interfaces[if_vlan_stag]['index']) ipr.link('add', ifname=if_vlan_ctag, link=self.ndb.interfaces[if_vlan_stag]['index'], vlan_id=1001, vlan_protocol=0x8100, kind='vlan') self.ndb.wait({'interfaces': [{'ifname': if_vlan_ctag}]}) ret.append(self.ndb.interfaces[if_vlan_ctag]['index']) ipr.link('add', ifname=if_port, kind='dummy') self.ndb.wait({'interfaces': [{'ifname': if_port}]}) ret.append(self.ndb.interfaces[if_port]['index']) ipr.link('add', ifname=if_bridge, kind='bridge') self.ndb.wait({'interfaces': [{'ifname': if_bridge}]}) ipr.link('set', index=self.ndb.interfaces[if_port]['index'], master=self.ndb.interfaces[if_bridge]['index']) ret.append(self.ndb.interfaces[if_bridge]['index']) ipr.addr('add', index=self.ndb.interfaces[if_bridge]['index'], address=if_addr1, prefixlen=24) ipr.addr('add', index=self.ndb.interfaces[if_bridge]['index'], address=if_addr2, prefixlen=24) self.ndb.wait({'addresses': [{'address': if_addr1}, {'address': if_addr2}]}) self.if_bridge = if_bridge return ret def ifaddr(self, r=0): return str(self.ipranges[r].pop()) def setup(self): require_user('root') self.if_simple = None self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, rtnl_log=True, sources=self.nl_class(**self.nl_kwarg)) self.interfaces = self.create_interfaces() def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=link) self.ndb.close() for net in self.ipnets: free_network(net) def fetch(self, request, values=[]): with self.ndb.schema.db_lock: return (self .ndb .schema .execute(request, values) .fetchall())
class TestCreate(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] def ifaddr(self): return str(self.ipranges[0].pop()) def ifname(self): ret = uifname() self.interfaces.append(ret) return ret def setup(self): require_user('root') self.interfaces = [] self.ipnets = [allocate_network() for _ in range(2)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=ipr.link_lookup(ifname=link)[0]) self.ndb.close() for net in self.ipnets: free_network(net) def test_context_manager(self): ifname = uifname() address = '00:11:22:36:47:58' ifobj = (self .ndb .interfaces .create(ifname=ifname, kind='dummy')) with ifobj: pass assert grep('%s ip link show' % self.ssh, pattern=ifname) with ifobj: ifobj['state'] = 'up' ifobj['address'] = address assert grep('%s ip link show' % self.ssh, pattern=address) assert self.ndb.interfaces[ifname]['state'] == 'up' with ifobj: ifobj.remove() def test_fail(self): ifname = uifname() kind = uifname() ifobj = (self .ndb .interfaces .create(ifname=ifname, kind=kind)) save = dict(ifobj) try: ifobj.commit() except NetlinkError as e: assert e.code == 95 # Operation not supported assert save == dict(ifobj) assert ifobj.state == 'invalid' def test_dummy(self): ifname = self.ifname() (self .ndb .interfaces .create(ifname=ifname, kind='dummy', address='00:11:22:33:44:55') .commit()) assert grep('%s ip link show' % self.ssh, pattern=ifname) assert self.ndb.interfaces[ifname]['address'] == '00:11:22:33:44:55' def test_bridge(self): bridge = self.ifname() brport = self.ifname() (self .ndb .interfaces .create(ifname=bridge, kind='bridge') .commit()) (self .ndb .interfaces .create(ifname=brport, kind='dummy') .set('master', self.ndb.interfaces[bridge]['index']) .commit()) assert grep('%s ip link show' % self.ssh, pattern=bridge) assert grep('%s ip link show' % self.ssh, pattern='%s.*%s' % (brport, bridge)) def test_vrf(self): vrf = self.ifname() (self .ndb .interfaces .create(ifname=vrf, kind='vrf') .set('vrf_table', 42) .commit()) assert grep('%s ip link show' % self.ssh, pattern=vrf) def test_vlan(self): host = self.ifname() vlan = self.ifname() (self .ndb .interfaces .create(ifname=host, kind='dummy') .commit()) (self .ndb .interfaces .create(ifname=vlan, kind='vlan') .set('link', self.ndb.interfaces[host]['index']) .set('vlan_id', 101) .commit()) assert grep('%s ip link show' % self.ssh, pattern=vlan) def test_vxlan(self): host = self.ifname() vxlan = self.ifname() (self .ndb .interfaces .create(ifname=host, kind='dummy') .commit()) (self .ndb .interfaces .create(ifname=vxlan, kind='vxlan') .set('vxlan_link', self.ndb.interfaces[host]['index']) .set('vxlan_id', 101) .set('vxlan_group', '239.1.1.1') .set('vxlan_ttl', 16) .commit()) assert grep('%s ip link show' % self.ssh, pattern=vxlan) def test_basic_address(self): ifaddr = self.ifaddr() ifname = self.ifname() i = (self .ndb .interfaces .create(ifname=ifname, kind='dummy', state='up')) i.commit() a = (self .ndb .addresses .create(index=i['index'], address=ifaddr, prefixlen=24)) a.commit() assert grep('%s ip link show' % self.ssh, pattern=ifname) assert grep('%s ip addr show dev %s' % (self.ssh, ifname), pattern=ifaddr) def test_basic_route(self): ifaddr = self.ifaddr() router = self.ifaddr() ifname = self.ifname() i = (self .ndb .interfaces .create(ifname=ifname, kind='dummy', state='up')) i.commit() a = (self .ndb .addresses .create(index=i['index'], address=ifaddr, prefixlen=24)) a.commit() r = (self .ndb .routes .create(dst_len=24, dst=str(self.ipnets[1].network), gateway=router)) r.commit() assert grep('%s ip link show' % self.ssh, pattern=ifname) assert grep('%s ip addr show dev %s' % (self.ssh, ifname), pattern=ifaddr) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s' % (str(self.ipnets[1]), ifname))
from pyroute2 import NDB ndb = NDB(log='on') for record in ndb.interfaces.summary(): print(record.ifname, record.address, record.state) for record in ndb.addresses.summary(): print(record._as_dict()) (ndb.interfaces.create(ifname='br1', kind='bridge') # create a bridge .add_port('eth0') # add ports .add_port('eth1') # .add_ip('10.0.0.1/24') # add addresses .add_ip('192.168.0.1/24') # .set('br_stp_state', 1) # set STP on .set('state', 'up') # bring the interface up .commit())
class NDBContextManager(object): ''' This class is used to manage fixture contexts. * create log spec * create NDB with specified parameters * provide methods to register interfaces * automatically remove registered interfaces ''' def __init__(self, request, tmpdir, **kwarg): self.spec = SpecContextManager(request, tmpdir) self.netns = None # # the cleanup registry self.interfaces = {} self.namespaces = {} if 'log' not in kwarg: kwarg['log'] = self.spec.log_spec if 'rtnl_debug' not in kwarg: kwarg['rtnl_debug'] = True kind = 'local' self.table = None kwarg['db_provider'] = 'sqlite3' kwarg['db_spec'] = ':memory:' if hasattr(request, 'param'): if isinstance(request.param, ContextParams): kind = request.param.target self.table = request.param.table kwarg['db_provider'] = request.param.db_provider kwarg['db_spec'] = request.param.db_spec elif isinstance(request.param, (tuple, list)): kind, self.table = request.param else: kind = request.param if kind == 'local': sources = [{'target': 'localhost', 'kind': 'local'}] elif kind == 'netns': self.netns = self.new_nsname sources = [{ 'target': 'localhost', 'kind': 'netns', 'netns': self.netns }] else: sources = None if sources is not None: kwarg['sources'] = sources # # select the DB to work on db_name = os.environ.get('PYROUTE2_TEST_DBNAME') if isinstance(db_name, basestring) and len(db_name): kwarg['db_provider'] = 'psycopg2' kwarg['db_spec'] = {'dbname': db_name} # # this instance is to be tested, so do NOT use it # in utility methods self.ndb = NDB(**kwarg) self.ipr = self.ndb.sources['localhost'].nl.clone() # # IPAM self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] def register(self, ifname=None, netns=None): ''' Register an interface in `self.interfaces`. If no interface name specified, create a random one. All the saved interfaces will be removed on `teardown()` ''' if ifname is None: ifname = uifname() self.interfaces[ifname] = netns return ifname def register_netns(self, netns=None): ''' Register netns in `self.namespaces`. If no netns name is specified, create a random one. All the save namespaces will be removed on `teardown()` ''' if netns is None: netns = str(uuid.uuid4()) self.namespaces[netns] = None return netns def get_ipaddr(self, r=0): ''' Returns an ip address from the specified range. ''' return str(self.ipranges[r].pop()) @property def new_ifname(self): ''' Returns a new unique ifname and registers it to be cleaned up on `self.teardown()` ''' return self.register() @property def new_ipaddr(self): ''' Returns a new ipaddr from the configured range ''' return self.get_ipaddr() @property def new_nsname(self): ''' Returns a new unique nsname and registers it to be removed on `self.teardown()` ''' return self.register_netns() def teardown(self): ''' 1. close the test NDB 2. remove the registered interfaces, ignore not existing ''' self.ndb.close() self.ipr.close() for (ifname, nsname) in self.interfaces.items(): try: ipr = None # # spawn ipr to remove the interface if nsname is not None: ipr = NetNS(nsname) else: ipr = IPRoute() # # lookup the interface index index = list(ipr.link_lookup(ifname=ifname)) if len(index): index = index[0] else: # # ignore not existing interfaces continue # # try to remove it ipr.link('del', index=index) except NetlinkError as e: # # ignore if removed (t.ex. by another process) if e.code != errno.ENODEV: raise finally: if ipr is not None: ipr.close() for nsname in self.namespaces: netns.remove(nsname) for net in self.ipnets: free_network(net)
class TestCreate(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' def ifname(self): ret = uifname() self.interfaces.append(ret) return ret def setup(self): require_user('root') self.interfaces = [] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg)) def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=ipr.link_lookup(ifname=link)[0]) self.ndb.close() def test_context_manager(self): ifname = uifname() address = '00:11:22:36:47:58' ifobj = (self.ndb.interfaces.add(ifname=ifname, kind='dummy')) with ifobj: pass assert grep('%s ip link show' % self.ssh, pattern=ifname) with ifobj: ifobj['state'] = 'up' ifobj['address'] = address assert grep('%s ip link show' % self.ssh, pattern=address) assert self.ndb.interfaces[ifname]['state'] == 'up' def test_fail(self): ifname = uifname() kind = uifname() ifobj = (self.ndb.interfaces.add(ifname=ifname, kind=kind)) save = dict(ifobj) try: ifobj.commit() except NetlinkError as e: assert e.code == 95 # Operation not supported assert save == dict(ifobj) assert ifobj.scope == 'invalid' def test_dummy(self): ifname = self.ifname() (self.ndb.interfaces.add(ifname=ifname, kind='dummy', address='00:11:22:33:44:55').commit()) assert grep('%s ip link show' % self.ssh, pattern=ifname) assert self.ndb.interfaces[ifname]['address'] == '00:11:22:33:44:55'
class TestBase(object): db_provider = 'sqlite3' db_spec = ':memory:' nl_class = IPRoute nl_kwarg = {} ssh = '' ipnets = [] ipranges = [] def create_interfaces(self): # dummy interface if_dummy = uifname() if_vlan_stag = uifname() if_vlan_ctag = uifname() if_bridge = uifname() if_port = uifname() if_addr1 = self.ifaddr() if_addr2 = self.ifaddr() ret = [] ret.append(self .ndb .interfaces .create(ifname=if_dummy, kind='dummy') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_vlan_stag, link=self.ndb.interfaces[if_dummy]['index'], vlan_id=101, vlan_protocol=0x88a8, kind='vlan') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_vlan_ctag, link=self.ndb.interfaces[if_vlan_stag]['index'], vlan_id=1001, vlan_protocol=0x8100, kind='vlan') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_bridge, kind='bridge') .commit()['index']) ret.append(self .ndb .interfaces .create(ifname=if_port, master=self.ndb.interfaces[if_bridge]['index'], kind='dummy') .commit()['index']) (self .ndb .interfaces[if_bridge] .ipaddr .create(address=if_addr1, prefixlen=24) .commit()) (self .ndb .interfaces[if_bridge] .ipaddr .create(address=if_addr2, prefixlen=24) .commit()) self.if_bridge = if_bridge return ret def ifaddr(self, r=0): return str(self.ipranges[r].pop()) def setup(self): require_user('root') self.if_simple = None self.ipnets = [allocate_network() for _ in range(5)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, debug=True) self.ndb.log('../ndb-%s-%s.log' % (os.getpid(), id(self.ndb))) self.interfaces = self.create_interfaces() def teardown(self): with self.nl_class(**self.nl_kwarg) as ipr: for link in reversed(self.interfaces): ipr.link('del', index=link) self.ndb.close() for net in self.ipnets: free_network(net) def fetch(self, request, values=[]): return (self .ndb .schema .fetch(request, values))
def setup(self): require_user('root') self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg))
class TestNetNS(object): db_provider = 'sqlite3' db_spec = ':memory:' def setup(self): require_user('root') self.log_id = str(uuid.uuid4()) self.netns = str(uuid.uuid4()) self.ipnets = [allocate_network() for _ in range(3)] self.ipranges = [[str(x) for x in net] for net in self.ipnets] self.sources = [{'target': 'localhost'}, {'netns': self.netns}, {'target': 'localhost/netns', 'kind': 'nsmanager'}] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.sources, log='../ndb-%s-%s.log' % (os.getpid(), self.log_id), rtnl_debug=True, auto_netns=True) def ifaddr(self, r=0): return str(self.ipranges[r].pop()) def teardown(self): for net in self.ipnets: free_network(net) self.ndb.close() netns.remove(self.netns) def test_nsmanager(self): assert self.ndb.netns.count() > 0 def test_auto_netns(self): newns = str(uuid.uuid4()) assert self.ndb.interfaces.count() > 0 assert len(tuple(self .ndb .interfaces .summary(match={'target': 'netns/%s' % newns}))) == 0 netns.create(newns) self.ndb.interfaces.wait(**{'target': 'netns/%s' % newns}) netns.remove(newns) def test_basic(self): ifname = uifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() ifaddr3 = self.ifaddr() (self .ndb .interfaces .create(target=self.netns, ifname=ifname, kind='dummy') .ipaddr .create(address=ifaddr1, prefixlen=24) .create(address=ifaddr2, prefixlen=24) .create(address=ifaddr3, prefixlen=24) .commit()) with NDB(sources=[{'target': 'localhost', 'netns': self.netns, 'kind': 'netns'}]) as ndb: if_idx = ndb.interfaces[ifname]['index'] addr1_idx = ndb.addresses['%s/24' % ifaddr1]['index'] addr2_idx = ndb.addresses['%s/24' % ifaddr2]['index'] addr3_idx = ndb.addresses['%s/24' % ifaddr3]['index'] assert if_idx == addr1_idx == addr2_idx == addr3_idx def _assert_test_view(self, ifname, ifaddr): with NDB(sources=[{'target': 'localhost', 'netns': self.netns, 'kind': 'netns'}]) as ndb: (if_idx, if_state, if_addr, if_flags) = ndb.interfaces[ifname].fields('index', 'state', 'address', 'flags') addr_idx = ndb.addresses['%s/24' % ifaddr]['index'] assert if_idx == addr_idx assert if_state == 'up' assert if_flags & 1 assert if_addr == '00:11:22:33:44:55' def test_view_constraints_pipeline(self): ifname = uifname() ifaddr = self.ifaddr() (self .ndb .interfaces .constraint('target', self.netns) .create(ifname=ifname, kind='dummy') .set('address', '00:11:22:33:44:55') .set('state', 'up') .ipaddr .create(address=ifaddr, prefixlen=24) .commit()) self._assert_test_view(ifname, ifaddr) def test_view_constraints_cmanager(self): ifname = uifname() ifaddr = self.ifaddr() with self.ndb.interfaces as view: view.constraints['target'] = self.netns with view.create(ifname=ifname, kind='dummy') as interface: interface['address'] = '00:11:22:33:44:55' interface['state'] = 'up' with interface.ipaddr as aview: with aview.create(address=ifaddr, prefixlen=24): pass self._assert_test_view(ifname, ifaddr) def test_move(self): ifname = uifname() ifaddr = self.ifaddr() # create the interfaces (self .ndb .interfaces .create(ifname=ifname, kind='dummy') .commit()) # move it to a netns (self .ndb .interfaces[ifname] .set('net_ns_fd', self.netns) .commit()) # setup the interface only when it is moved (self .ndb .interfaces .wait(target=self.netns, ifname=ifname) .set('state', 'up') .set('address', '00:11:22:33:44:55') .ipaddr .create(address=ifaddr, prefixlen=24) .commit()) self._assert_test_view(ifname, ifaddr)
def setup(self): require_user('root') self.interfaces = [] self.ndb = NDB(db_provider=self.db_provider, db_spec=self.db_spec, sources=self.nl_class(**self.nl_kwarg))
f = open("./backend.txt", "r") lines = f.readlines() backend_url = lines[0].rstrip("\n") backend_username = lines[1].rstrip("\n") backend_password = lines[2].rstrip("\n") f.close() f = open("./techonline-servers.txt", "r") lines = f.readlines() tos_url = lines[0].rstrip("\n") tos_username = lines[1].rstrip("\n") tos_password = lines[2].rstrip("\n") f.close() ndb = NDB(log='on') def setup_interface(interface_name, vlan_id): interface = (ndb.interfaces.create(ifname=interface_name, kind='vlan', link='ens19', vlan_id=vlan_id).set('state', 'up').commit()) return interface def cleanup_interface(interface): try: interface.remove().commit() return True
def teardown(self): with NDB() as ndb: for i in self.ifnames: (ndb.interfaces[i].remove().commit()) for net in self.ipnets: free_network(net)