def test_add_del_ip_dict(self): ifname = self.ifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() (self.ndb.interfaces.create(ifname=ifname, kind='dummy', state='down').add_ip({ 'address': ifaddr1, 'prefixlen': 24 }).add_ip({ 'address': ifaddr2, 'prefixlen': 24 }).commit()) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2)) (self.ndb.interfaces[ifname].del_ip({ 'address': ifaddr2, 'prefixlen': 24 }).del_ip({ 'address': ifaddr1, 'prefixlen': 24 }).commit()) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2))
def test_route_replace_existing(self): # route('replace', ...) should succeed, if route exists require_user('root') naddr = str(self.ipnets[1].network) ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() ifaddr3 = self.ifaddr() self.ip.link('set', index=self.ifaces[0], state='up') self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24) self.ip.route('replace', dst=naddr, mask=24, gateway=ifaddr2, table=100) assert grep('ip route show table 100', pattern='%s/24.*%s' % (naddr, ifaddr2)) self.ip.route('replace', dst=naddr, mask=24, gateway=ifaddr3, table=100) assert not grep('ip route show table 100', pattern='%s/24.*%s' % (naddr, ifaddr2)) assert grep('ip route show table 100', pattern='%s/24.*%s' % (naddr, ifaddr3)) self.ip.flush_routes(table=100) assert not grep('ip route show table 100', pattern='%s/24.*%s' % (naddr, ifaddr3))
def test_flush_routes(self): require_user('root') self.ip.link('set', index=self.ifaces[0], state='up') self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24) self.ip.route('add', prefix='172.16.1.0', mask=24, gateway='172.16.0.1', table=100) self.ip.route('add', prefix='172.16.2.0', mask=24, gateway='172.16.0.1', table=100) assert grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1') assert grep('ip route show table 100', pattern='172.16.2.0/24.*172.16.0.1') self.ip.flush_routes(table=100) assert not grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1') assert not grep('ip route show table 100', pattern='172.16.2.0/24.*172.16.0.1')
def test_route_metrics(self): require_user('root') assert '172.16.0.0/24' not in self.ip.routes.keys() # create a route self.ip.routes.add({ 'dst': '172.16.0.0/24', 'gateway': '127.0.0.1', 'metrics': { 'mtu': 1360 } }).commit() assert grep('ip ro', pattern='172.16.0.0/24.*mtu 1360') # change metrics with self.ip.routes['172.16.0.0/24'] as r: r.metrics.mtu = 1400 assert self.ip.routes['172.16.0.0/24']['metrics']['mtu'] == 1400 assert grep('ip ro', pattern='172.16.0.0/24.*mtu 1400') # delete the route with self.ip.routes['172.16.0.0/24'] as r: r.remove() assert '172.16.0.0/24' not in self.ip.routes.keys() assert not grep('ip ro', pattern='172.16.0.0/24')
def test_basic_route(self): ifaddr = self.ifaddr() router = self.ifaddr() ifname = self.ifname() i = (self .ndb .interfaces .add(ifname=ifname, kind='dummy', state='up')) i.commit() a = (self .ndb .addresses .add(index=i['index'], address=ifaddr, prefixlen=24)) a.commit() r = (self .ndb .routes .add(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))
def test_add_del_ip_dict(self): ifname = self.ifname() ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() (self .ndb .interfaces .create(ifname=ifname, kind='dummy', state='down') .add_ip({'address': ifaddr1, 'prefixlen': 24}) .add_ip({'address': ifaddr2, 'prefixlen': 24}) .commit()) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2)) (self .ndb .interfaces[ifname] .del_ip({'address': ifaddr2, 'prefixlen': 24}) .del_ip({'address': ifaddr1, 'prefixlen': 24}) .commit()) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2))
def test_multipath_ipv4(self): ifname = self.ifname() ifaddr = self.ifaddr() hop1 = self.ifaddr() hop2 = self.ifaddr() (self.ndb.interfaces.create(ifname=ifname, kind='dummy', state='up').ipaddr.create( address=ifaddr, prefixlen=24).commit()) (self.ndb.routes.create( **{ 'dst_len': 24, 'dst': str(self.ipnets[1].network), 'multipath': [{ 'gateway': hop1 }, { 'gateway': hop2 }] }).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' % str(self.ipnets[1])) assert grep('%s ip route show' % self.ssh, pattern='nexthop.*%s.*%s' % (hop1, ifname)) assert grep('%s ip route show' % self.ssh, pattern='nexthop.*%s.*%s' % (hop2, ifname))
def test_update_replace(self): ifaddr = self.ifaddr() router = self.ifaddr() ifname = self.ifname() network = str(self.ipnets[1].network) (self.ndb.interfaces.create(ifname=ifname, kind='dummy', state='up').ipaddr.create( address=ifaddr, prefixlen=24).commit()) (self.ndb.routes.create(dst_len=24, dst=network, priority=10, gateway=router).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.*metric %s' % (network, ifname, 10)) (self.ndb.routes['%s/24' % network].set('priority', 15).commit()) assert not grep('%s ip route show' % self.ssh, pattern='%s.*%s.*metric %s' % (network, ifname, 10)) assert grep('%s ip route show' % self.ssh, pattern='%s.*%s.*metric %s' % (network, ifname, 15))
def test_update_set(self): ifaddr = self.ifaddr() router1 = self.ifaddr() router2 = self.ifaddr() ifname = self.ifname() network = str(self.ipnets[1].network) (self.ndb.interfaces.create(ifname=ifname, kind='dummy', state='up').ipaddr.create( address=ifaddr, prefixlen=24).commit()) (self.ndb.routes.create(dst_len=24, dst=network, gateway=router1).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.*via %s.*%s' % (network, router1, ifname)) (self.ndb.routes['%s/24' % network].set('gateway', router2).commit()) assert not grep('%s ip route show' % self.ssh, pattern='%s.*via %s.*%s' % (network, router1, ifname)) assert grep('%s ip route show' % self.ssh, pattern='%s.*via %s.*%s' % (network, router2, ifname))
def test_add_del_ip_string(self): ifname = self.ifname() ifaddr1 = '%s/24' % (self.ifaddr()) ifaddr2 = '%s/24' % (self.ifaddr()) (self .ndb .interfaces .create(ifname=ifname, kind='dummy', state='down') .add_ip(ifaddr1) .add_ip(ifaddr2) .commit()) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2)) (self .ndb .interfaces[ifname] .del_ip(ifaddr2) .del_ip(ifaddr1) .commit()) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr1)) assert not grep('%s ip -o addr show' % self.ssh, pattern='%s.*%s' % (ifname, ifaddr2))
def test_flush_routes(self): require_user('root') self.ip.link('set', index=self.ifaces[0], state='up') self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24) self.ip.route('add', dst='172.16.1.0', mask=24, gateway='172.16.0.1', table=100) self.ip.route('add', dst='172.16.2.0', mask=24, gateway='172.16.0.1', table=100) assert grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1') assert grep('ip route show table 100', pattern='172.16.2.0/24.*172.16.0.1') self.ip.flush_routes(table=100) assert not grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1') assert not grep('ip route show table 100', pattern='172.16.2.0/24.*172.16.0.1')
def test_multipath_ipv4(self): ifname = self.ifname() ifaddr = self.ifaddr() hop1 = self.ifaddr() hop2 = self.ifaddr() (self .ndb .interfaces .create(ifname=ifname, kind='dummy', state='up') .ipaddr .create(address=ifaddr, prefixlen=24) .commit()) (self .ndb .routes .create(**{'dst_len': 24, 'dst': str(self.ipnets[1].network), 'multipath': [{'gateway': hop1}, {'gateway': hop2}]}) .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' % str(self.ipnets[1])) assert grep('%s ip route show' % self.ssh, pattern='nexthop.*%s.*%s' % (hop1, ifname)) assert grep('%s ip route show' % self.ssh, pattern='nexthop.*%s.*%s' % (hop2, ifname))
def test_basic(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))
def test_veth_simple(self): ifname = uifname() peername = uifname() (self .ndb .interfaces .create(ifname=ifname, peer=peername, kind='veth') .commit()) iflink = self.ndb.interfaces[ifname]['link'] plink = self.ndb.interfaces[peername]['link'] assert iflink == self.ndb.interfaces[peername]['index'] assert plink == self.ndb.interfaces[ifname]['index'] assert grep('%s ip link show' % self.ssh, pattern=ifname) assert grep('%s ip link show' % self.ssh, pattern=peername) (self .ndb .interfaces[ifname] .remove() .commit()) assert not grep('%s ip link show' % self.ssh, pattern=ifname) assert not grep('%s ip link show' % self.ssh, pattern=peername)
def test_veth_spec(self): ifname = uifname() peername = uifname() nsname = str(uuid.uuid4()) (self .ndb .sources .add(netns=nsname)) spec = self.getspec(**{'ifname': ifname, 'kind': 'veth', 'peer': {'ifname': peername, 'address': '00:11:22:33:44:55', 'net_ns_fd': nsname}}) (self .ndb .interfaces .create(**spec) .commit()) (self .ndb .interfaces .wait(target=nsname, ifname=peername)) iflink = (self .ndb .interfaces[self.getspec(ifname=ifname)]['link']) plink = (self .ndb .interfaces[{'target': nsname, 'ifname': peername}]['link']) assert iflink == (self .ndb .interfaces[{'target': nsname, 'ifname': peername}]['index']) assert plink == (self .ndb .interfaces[self.getspec(ifname=ifname)]['index']) assert grep('%s ip link show' % self.ssh, pattern=ifname) assert not grep('%s ip link show' % self.ssh, pattern=peername) (self .ndb .interfaces[self.getspec(ifname=ifname)] .remove() .commit()) assert not grep('%s ip link show' % self.ssh, pattern=ifname) assert not grep('%s ip link show' % self.ssh, pattern=peername) (self .ndb .sources .remove(nsname)) netns.remove(nsname)
def test_vlan_filter_add(self): require_user('root') (bn, bx) = self.create('bridge') (sn, sx) = self.create('dummy') self.ip.link('set', index=sx, master=bx) assert not grep('bridge vlan show', pattern='567') self.ip.vlan_filter('add', index=sx, vlan_info={'vid': 567}) assert grep('bridge vlan show', pattern='567') self.ip.vlan_filter('del', index=sx, vlan_info={'vid': 567}) assert not grep('bridge vlan show', pattern='567')
def test_route_multipath(self): require_user('root') self.ip.route('add', dst='172.16.243.0/24', multipath=[{'gateway': '127.0.0.2'}, {'gateway': '127.0.0.3'}]) assert grep('ip route show', pattern='172.16.243.0/24') assert grep('ip route show', pattern='nexthop.*127.0.0.2') assert grep('ip route show', pattern='nexthop.*127.0.0.3') self.ip.route('del', dst='172.16.243.0', mask=24)
def test_vlan_filter_add(self): require_user('root') (bn, bx) = self.create('bridge') (sn, sx) = self.create('dummy') self.ip.link('set', index=sx, master=bx) assert not grep('bridge vlan show', pattern=' 568') self.ip.vlan_filter('add', index=sx, vlan_info={'vid': 568}) assert grep('bridge vlan show', pattern=' 568') self.ip.vlan_filter('del', index=sx, vlan_info={'vid': 568}) assert not grep('bridge vlan show', pattern=' 568')
def _test_shadow(self, kind): a = self.ip.create(ifname='bala', kind=kind).commit() if a._mode == 'explicit': a.begin() a.shadow().commit() assert 'bala' in self.ip.interfaces assert not grep('ip link', pattern='bala') b = self.ip.create(ifname='bala', kind=kind).commit() assert a == b assert grep('ip link', pattern='bala')
def test_route_multipath(self): require_user('root') naddr = str(self.ipnets[1].network) self.ip.route('add', dst='%s/24' % naddr, multipath=[{'gateway': '127.0.0.2'}, {'gateway': '127.0.0.3'}]) assert grep('ip route show', pattern='%s/24' % naddr) assert grep('ip route show', pattern='nexthop.*127.0.0.2') assert grep('ip route show', pattern='nexthop.*127.0.0.3') self.ip.route('del', dst=naddr, mask=24)
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_shadow(self, kind): ifA = self.get_ifname() a = self.ip.create(ifname=ifA, kind=kind).commit() if a._mode == 'explicit': a.begin() a.shadow().commit() assert ifA in self.ip.interfaces assert not grep('ip link', pattern=ifA) b = self.ip.create(ifname=ifA, kind=kind).commit() assert a == b assert grep('ip link', pattern=ifA)
def test_ovs_kind_aliases(self): require_user('root') ifA = self.get_ifname() ifB = self.get_ifname() self.ip.create(ifname=ifA, kind='ovs-bridge').commit() self.ip.create(ifname=ifB, kind='openvswitch').commit() assert ifA in self.ip.interfaces assert ifB in self.ip.interfaces assert grep('ip link', pattern=ifA) assert grep('ip link', pattern=ifB)
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_set_peer(self): # host A # # private key aPrZzfjeiNuy/oolBFkX4ClU3UjYzVemhK49TfZvMmU= # public key orXRcGaN/vxYm0fupIq/Q0ePQyDviyDRtAxAPNJMrA0= peerB = { 'public_key': 'waDwFcRFnPzGJqgaYoV3P7V3NOji5QNPNjUGuBrwwTI=', 'endpoint_addr': self.ipranges[1][1], 'endpoint_port': 12453, 'persistent_keepalive': 15, 'allowed_ips': ['%s/24' % self.ipranges[0][0], '%s/24' % self.ipranges[1][0]] } self.wg.set(self.wg0if, private_key='aPrZzfjeiNuy/oolBFkX4ClU3UjYzVemhK49TfZvMmU=', listen_port=12452, peer=peerB) # host B # # private key eHqiUofUM6A41mDVSTbBwFyfkDsVW7uEhv9A8romH2A= # public key waDwFcRFnPzGJqgaYoV3P7V3NOji5QNPNjUGuBrwwTI= peerA = { 'public_key': 'orXRcGaN/vxYm0fupIq/Q0ePQyDviyDRtAxAPNJMrA0=', 'endpoint_addr': self.ipranges[1][1], 'endpoint_port': 12452, 'persistent_keepalive': 15, 'allowed_ips': ['%s/24' % self.ipranges[0][0], '%s/24' % self.ipranges[1][0]] } self.wg.set(self.wg1if, private_key='eHqiUofUM6A41mDVSTbBwFyfkDsVW7uEhv9A8romH2A=', listen_port=12453, peer=peerA) assert grep('wg show %s' % self.wg0if, pattern='peer: %s' % peerB['public_key']) assert grep('wg show %s' % self.wg0if, pattern='endpoint: %s:%s' % (peerB['endpoint_addr'], peerB['endpoint_port'])) assert grep('wg show %s' % self.wg0if, pattern='transfer:')
def test_global_create(self): require_user('root') ifA = self.get_ifname() ifB = self.get_ifname() self.ip.create(ifname=ifA, kind='dummy') self.ip.create(ifname=ifB, kind='dummy') self.ip.commit() assert ifA in self.ip.interfaces assert ifB in self.ip.interfaces assert grep('ip link', pattern=ifA) assert grep('ip link', pattern=ifB)
def test_global_create(self): require_user("root") ifA = self.get_ifname() ifB = self.get_ifname() self.ip.create(ifname=ifA, kind="dummy") self.ip.create(ifname=ifB, kind="dummy") self.ip.commit() assert ifA in self.ip.interfaces assert ifB in self.ip.interfaces assert grep("ip link", pattern=ifA) assert grep("ip link", pattern=ifB)
def _test_shadow(self, kind): ifA = self.get_ifname() a = self.ip.create(ifname=ifA, kind=kind).commit() if a._mode == "explicit": a.begin() a.shadow().commit() assert ifA in self.ip.interfaces assert not grep("ip link", pattern=ifA) time.sleep(0.5) b = self.ip.create(ifname=ifA, kind=kind).commit() assert a == b assert grep("ip link", pattern=ifA)
def test_basic_address(self): ifname = self.ifname() i = (self.ndb.interfaces.add(ifname=ifname, kind='dummy', state='up')) i.commit() a = (self.ndb.addresses.add(index=i['index'], address='192.168.153.5', 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='192.168.153.5')
def test_route_onlink_multipath_strflags(self): require_user('root') self.ip.route('add', dst='172.16.245.0/24', multipath=[{'gateway': '10.100.1.1', 'oif': 1, 'flags': ['onlink']}, {'gateway': '10.100.1.2', 'oif': 1, 'flags': RTNH_F_ONLINK}]) assert grep('ip route show', pattern='172.16.245.0/24') assert grep('ip route show', pattern='nexthop.*10.100.1.1.*onlink') assert grep('ip route show', pattern='nexthop.*10.100.1.2.*onlink') self.ip.route('del', dst='172.16.245.0', mask=24)
def test_vlan_filter_add_raw(self): require_user('root') (bn, bx) = self.create('bridge') (sn, sx) = self.create('dummy') self.ip.link('set', index=sx, master=bx) assert not grep('bridge vlan show', pattern=' 567') self.ip.vlan_filter('add', index=sx, af_spec={'attrs': [['IFLA_BRIDGE_VLAN_INFO', {'vid': 567}]]}) assert grep('bridge vlan show', pattern=' 567') self.ip.vlan_filter('del', index=sx, af_spec={'attrs': [['IFLA_BRIDGE_VLAN_INFO', {'vid': 567}]]}) assert not grep('bridge vlan show', pattern=' 567')
def test_routes_multipath_gateway(self): require_user('root') ifR = self.get_ifname() with self.ip.create(ifname=ifR, kind='dummy') as i: i.add_ip('172.16.231.1/24') i.up() r = self.ip.routes.add({ 'dst': '172.16.232.0/24', 'multipath': [{ 'gateway': '172.16.231.2', 'hops': 20 }, { 'gateway': '172.16.231.3', 'hops': 30 }, { 'gateway': '172.16.231.4' }] }) r.commit() assert grep('ip ro', pattern='172.16.232.0/24') assert grep('ip ro', pattern='nexthop.*172.16.231.2.*weight.*21') assert grep('ip ro', pattern='nexthop.*172.16.231.3.*weight.*31') assert grep('ip ro', pattern='nexthop.*172.16.231.4.*weight.*1') with self.ip.routes['172.16.232.0/24'] as r: r.add_nh({'gateway': '172.16.231.5', 'hops': 50}) r.del_nh({'gateway': '172.16.231.2'}) assert grep('ip ro', pattern='172.16.232.0/24') assert grep('ip ro', pattern='nexthop.*172.16.231.5.*weight.*51') assert grep('ip ro', pattern='nexthop.*172.16.231.3.*weight.*31') assert grep('ip ro', pattern='nexthop.*172.16.231.4.*weight.*1')
def test_routes_multipath_gateway(self): require_user("root") ifR = self.get_ifname() with self.ip.create(ifname=ifR, kind="dummy") as i: i.add_ip("172.16.231.1/24") i.up() r = self.ip.routes.add( { "dst": "172.16.232.0/24", "multipath": [ {"gateway": "172.16.231.2", "hops": 20}, {"gateway": "172.16.231.3", "hops": 30}, {"gateway": "172.16.231.4"}, ], } ) r.commit() assert grep("ip ro", pattern="172.16.232.0/24") assert grep("ip ro", pattern="nexthop.*172.16.231.2.*weight.*21") assert grep("ip ro", pattern="nexthop.*172.16.231.3.*weight.*31") assert grep("ip ro", pattern="nexthop.*172.16.231.4.*weight.*1") with self.ip.routes["172.16.232.0/24"] as r: r.add_nh({"gateway": "172.16.231.5", "hops": 50}) r.del_nh({"gateway": "172.16.231.2"}) assert grep("ip ro", pattern="172.16.232.0/24") assert grep("ip ro", pattern="nexthop.*172.16.231.5.*weight.*51") assert grep("ip ro", pattern="nexthop.*172.16.231.3.*weight.*31") assert grep("ip ro", pattern="nexthop.*172.16.231.4.*weight.*1")
def test_routes_multipath_gateway(self): require_user('root') ifR = self.get_ifname() with self.ip.create(ifname=ifR, kind='dummy') as i: i.add_ip('172.16.231.1/24') i.up() r = self.ip.routes.add({'dst': '172.16.232.0/24', 'multipath': [{'gateway': '172.16.231.2', 'hops': 20}, {'gateway': '172.16.231.3', 'hops': 30}, {'gateway': '172.16.231.4'}]}) r.commit() assert grep('ip ro', pattern='172.16.232.0/24') assert grep('ip ro', pattern='nexthop.*172.16.231.2.*weight.*21') assert grep('ip ro', pattern='nexthop.*172.16.231.3.*weight.*31') assert grep('ip ro', pattern='nexthop.*172.16.231.4.*weight.*1') with self.ip.routes['172.16.232.0/24'] as r: r.add_nh({'gateway': '172.16.231.5', 'hops': 50}) r.del_nh({'gateway': '172.16.231.2'}) assert grep('ip ro', pattern='172.16.232.0/24') assert grep('ip ro', pattern='nexthop.*172.16.231.5.*weight.*51') assert grep('ip ro', pattern='nexthop.*172.16.231.3.*weight.*31') assert grep('ip ro', pattern='nexthop.*172.16.231.4.*weight.*1')
def test_route_multipath_helper(self): require_user('root') req = IPRouteRequest({'dst': '172.16.242.0/24', 'multipath': [{'hops': 20, 'ifindex': 1, 'gateway': '127.0.0.2'}, {'hops': 30, 'ifindex': 1, 'gateway': '127.0.0.3'}]}) self.ip.route('add', **req) assert grep('ip route show', pattern='172.16.242.0/24') assert grep('ip route show', pattern='nexthop.*127.0.0.2.*weight 21') assert grep('ip route show', pattern='nexthop.*127.0.0.3.*weight 31') self.ip.route('del', dst='172.16.242.0', mask=24)
def test_basic_address(self): ifaddr = self.ifaddr() ifname = self.ifname() spec_if = self.getspec(ifname=ifname, kind='dummy', state='up') i = (self.ndb.interfaces.create(**spec_if)) i.commit() spec_ad = self.getspec(index=i['index'], address=ifaddr, prefixlen=24) a = (self.ndb.addresses.create(**spec_ad)) 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_metrics_update(self, method): ifaddr = self.ifaddr() gateway1 = self.ifaddr() ifname = self.ifname() i = (self .ndb .interfaces .create(self.getspec(ifname=ifname, kind='dummy', state='up'))) i.commit() a = (self .ndb .addresses .create(self.getspec(index=i['index'], address=ifaddr, prefixlen=24))) a.commit() spec = {'dst_len': 24, 'dst': str(self.ipnets[1].network), 'gateway': gateway1, 'metrics': {'mtu': 1300}} if self.table: spec['table'] = self.table r = (self .ndb .routes .create(self.getspec(**spec))) 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 table %i' % (self.ssh, self.table or 254), pattern='%s.*%s.*%s.*mtu 1300' % (str(self.ipnets[1]), gateway1, ifname)) r['metrics']['mtu'] = 1500 getattr(r, method)() assert grep('%s ip route show table %i' % (self.ssh, self.table or 254), pattern='%s.*%s.*%s.*mtu 1500' % (str(self.ipnets[1]), gateway1, ifname))
def get_usb_sticks(self): disks = [] try: for device in glob.glob("/dev/disk[0-9]"): infos = self.call(["diskutil", "info", device], output=True, log=False)[1] if utils.grep(infos, "Protocol:").split()[1] == "USB": disks.append([ device, " ".join(utils.grep(infos, "Media Name:").split()[2:]) ]) except: return [] return disks
def test_route_replace_not_existing(self): # route('replace', ...) should succeed, if route doesn't exist require_user('root') self.ip.link('set', index=self.ifaces[0], state='up') self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24) self.ip.route('replace', dst='172.16.1.0', mask=24, gateway='172.16.0.1', table=100) assert grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1') self.ip.flush_routes(table=100) assert not grep('ip route show table 100', pattern='172.16.1.0/24.*172.16.0.1')
def test_route_multipath_helper(self): require_user('root') naddr = str(self.ipnets[1].network) req = IPRouteRequest({'dst': '%s/24' % naddr, 'multipath': [{'hops': 20, 'oif': 1, 'gateway': '127.0.0.2'}, {'hops': 30, 'oif': 1, 'gateway': '127.0.0.3'}]}) self.ip.route('add', **req) assert grep('ip route show', pattern='%s/24' % naddr) assert grep('ip route show', pattern='nexthop.*127.0.0.2.*weight 21') assert grep('ip route show', pattern='nexthop.*127.0.0.3.*weight 31') self.ip.route('del', dst=naddr, mask=24)
def test_route_multipath(self): require_user('root') self.ip.route('add', dst='172.16.241.0', mask=24, multipath=[{'hops': 20, 'ifindex': 1, 'attrs': [['RTA_GATEWAY', '127.0.0.2']]}, {'hops': 30, 'ifindex': 1, 'attrs': [['RTA_GATEWAY', '127.0.0.3']]}]) assert grep('ip route show', pattern='172.16.241.0/24') assert grep('ip route show', pattern='nexthop.*127.0.0.2.*weight 21') assert grep('ip route show', pattern='nexthop.*127.0.0.3.*weight 31') self.ip.route('del', dst='172.16.241.0', mask=24)
def test_create_tuntap_fail(self): try: self.ip.create(ifname="fAiL", kind="tuntap", mode="fail").commit() except: assert not grep("ip link", pattern="fAiL") return raise Exception("tuntap create succeded")
def test_route_multipath_raw(self): require_user('root') naddr = str(self.ipnets[1].network) self.ip.route('add', dst=naddr, mask=24, multipath=[{'hops': 20, 'oif': 1, 'attrs': [['RTA_GATEWAY', '127.0.0.2']]}, {'hops': 30, 'oif': 1, 'attrs': [['RTA_GATEWAY', '127.0.0.3']]}]) assert grep('ip route show', pattern='%s/24' % naddr) assert grep('ip route show', pattern='nexthop.*127.0.0.2.*weight 21') assert grep('ip route show', pattern='nexthop.*127.0.0.3.*weight 31') self.ip.route('del', dst=naddr, mask=24)
def test_create_tuntap(self): require_user("root") ifA = self.get_ifname() self.ip.create(ifname=ifA, kind="tuntap", mode="tap", uid=1, gid=1).commit() assert ifA in self.ip.interfaces assert grep("ip link", pattern=ifA)
def test_route_onlink_multipath_strflags(self): require_user('root') naddr = str(self.ipnets[1].network) ifaddr1 = self.ifaddr() ifaddr2 = self.ifaddr() self.ip.route('add', dst='%s/24' % naddr, multipath=[{'gateway': ifaddr1, 'oif': 1, 'flags': ['onlink']}, {'gateway': ifaddr2, 'oif': 1, 'flags': RTNH_F_ONLINK}]) assert grep('ip route show', pattern='%s/24' % naddr) assert grep('ip route show', pattern='nexthop.*%s.*onlink' % ifaddr1) assert grep('ip route show', pattern='nexthop.*%s.*onlink' % ifaddr2) self.ip.route('del', dst=naddr, mask=24)
def test_create_tuntap_fail(self): try: self.ip.create(ifname='fAiL', kind='tuntap', mode='fail').commit() except: assert not grep('ip link', pattern='fAiL') return raise Exception('tuntap create succeded')