Commit f882fb20 authored by Peter V. Saveliev's avatar Peter V. Saveliev

netns: custom netns path, complete

parent 1bf5f444
......@@ -111,12 +111,16 @@ def _get_netnspath(name):
return netnspath
def listnetns():
def listnetns(nspath=None):
'''
List available network namespaces.
'''
if nspath:
nsdir = nspath
else:
nsdir = NETNS_RUN_DIR
try:
return os.listdir(NETNS_RUN_DIR)
return os.listdir(nsdir)
except OSError as e:
if e.errno == errno.ENOENT:
return []
......@@ -183,7 +187,7 @@ def setns(netns, flags=os.O_CREAT, libc=None):
libc = libc or ctypes.CDLL('libc.so.6', use_errno=True)
netnspath = _get_netnspath(netns)
if netns in listnetns():
if os.path.basename(netns) in listnetns(os.path.dirname(netns)):
if flags & (os.O_CREAT | os.O_EXCL) == (os.O_CREAT | os.O_EXCL):
raise OSError(errno.EEXIST, 'netns exists', netns)
else:
......
......@@ -243,19 +243,17 @@ class TestNetNS(object):
def test_create_from_path(self):
ns_dir = tempfile.mkdtemp()
netns_run_dir = netnsmod.NETNS_RUN_DIR
netnsmod.NETNS_RUN_DIR = ns_dir
# Create namespace
ns_name = str(uuid4())
temp_ns = NetNS(ns_name)
temp_ns.close()
nspath = '%s/%s' % (ns_dir, ns_name)
temp_ns = NetNS(nspath)
temp_ns.close()
fd = open(nspath)
self._test_create(nspath, fd.fileno())
fd.close()
netnsmod.remove(ns_name)
netnsmod.remove(nspath)
assert ns_name not in netnsmod.listnetns()
netnsmod.NETNS_RUN_DIR = netns_run_dir
assert ns_name not in netnsmod.listnetns(ns_dir)
def test_rename_plus_ipv6(self):
require_user('root')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment