def test_pushns(self): require_user('root') foo = str(uuid4()) ifA = uifname() with IPRoute() as ipr: ipr.link('add', ifname=ifA, kind='dummy') netnsmod.pushns(foo) with IPRoute() as ipr: assert ifA not in [ x.get_attr('IFLA_IFNAME') for x in ipr.link('dump') ] netnsmod.popns() with IPRoute() as ipr: assert ifA in [x.get_attr('IFLA_IFNAME') for x in ipr.link('dump')] ipr.link('del', index=ipr.link_lookup(ifname=ifA)[0]) netnsmod.remove(foo)
def test_pushns(self): require_user('root') foo = str(uuid4()) ifA = uifname() with IPRoute() as ipr: ipr.link('add', ifname=ifA, kind='dummy') netnsmod.pushns(foo) with IPRoute() as ipr: assert ifA not in [x.get_attr('IFLA_IFNAME') for x in ipr.link('dump')] netnsmod.popns() with IPRoute() as ipr: assert ifA in [x.get_attr('IFLA_IFNAME') for x in ipr.link('dump')] ipr.link('del', index=ipr.link_lookup(ifname=ifA)[0]) netnsmod.remove(foo)
def test_ns_pids(self): def waiting_child(fd): while True: if not os.read(fd, 32): exit(0) require_user('root') foo = str(uuid4()) netnsmod.create(foo) netnsmod.pushns(foo) foo_pid, foo_fd = os.forkpty() if not foo_pid: waiting_child(foo_fd) netnsmod.popns() pids = netnsmod.ns_pids() try: assert pids[foo] == [foo_pid] finally: os.close(foo_fd) netnsmod.remove(foo)
def __exit__(self, exc_type, exc_val, exc_tb): return netns.popns()