def test_multithread(self): require_user('root') parallel_count = 5 test_count = 10 ns_names = ['testns%i' % i for i in range(parallel_count)] success = [True] for ns_name in ns_names: NetNS(ns_name) for _t in range(test_count): threads = [ Thread(target=_ns_worker, args=(netnsmod._get_netnspath(ns_name), i, success)) for i, ns_name in enumerate(ns_names) ] for thread in threads: thread.start() for thread in threads: thread.join() for ns_name in ns_names: netnsmod.remove(ns_name) assert success[0]
def test_multithread(self): require_user('root') parallel_count = 5 test_count = 10 ns_names = ['testns%i' % i for i in range(parallel_count)] success = [True] for ns_name in ns_names: NetNS(ns_name) for _t in range(test_count): threads = [ Thread(target=_ns_worker, args=(netnsmod._get_netnspath(ns_name), i, success)) for i, ns_name in enumerate(ns_names)] for thread in threads: thread.start() for thread in threads: thread.join() for ns_name in ns_names: netnsmod.remove(ns_name) assert success[0]
def adjust_spec(cls, spec): if isinstance(spec, dict): ret_spec = spec else: ret_spec = {'target': 'localhost/netns'} if isinstance(spec, basestring): ret_spec['path'] = spec ret_spec['path'] = netns._get_netnspath(ret_spec['path']) return ret_spec
def create(self, path): netnspath = netns._get_netnspath(path) try: netns.create(netnspath, self.libc) except OSError as e: raise NetlinkError(e.errno) info = self.ipr._dump_one_ns(netnspath, set()) info['header']['type'] = RTM_NEWNETNS info['event'] = 'RTM_NEWNETNS' del info['value'] return info,
def spec_normalize(spec): if isinstance(spec, basestring): ret = {'path': spec} else: ret = dict(spec) path = netns._get_netnspath(ret['path']) # on Python3 _get_netnspath() returns bytes, not str, so # we have to decode it here in order to avoid issues with # cache keys and DB inserts if hasattr(path, 'decode'): path = path.decode('utf-8') ret['path'] = path return ret
def netns(self, cmd, *argv, **kwarg): path = kwarg.get('path', kwarg.get('NSINFO_PATH')) if path is None: raise ValueError('netns spec is required') netnspath = netns._get_netnspath(path) if cmd == 'add': return self.create(netnspath) elif cmd == 'del': return self.remove(netnspath) elif cmd not in ('get', 'set'): raise ValueError('method not supported') for item in self.dump(): if item.get_attr('NSINFO_PATH') == netnspath: return (item, ) return tuple()
def remove(self, path): netnspath = netns._get_netnspath(path) info = None try: info = self.ipr._dump_one_ns(netnspath, set()) except SkipInode: raise NetlinkError(errno.EEXIST) info['header']['type'] = RTM_DELNETNS info['event'] = 'RTM_DELNETNS' del info['value'] try: netns.remove(netnspath, self.libc) except OSError as e: raise NetlinkError(e.errno) return info,
def defaults(cls, spec): ret = dict(spec) defaults = {} if 'hostname' in spec: defaults['kind'] = 'remote' defaults['protocol'] = 'ssh' defaults['target'] = spec['hostname'] if 'netns' in spec: defaults['kind'] = 'netns' defaults['target'] = spec['netns'] ret['netns'] = netns._get_netnspath(spec['netns']) for key in defaults: if key not in ret: ret[key] = defaults[key] return ret
def adjust_spec(cls, spec, context): if isinstance(spec, dict): ret_spec = spec else: ret_spec = {'target': 'localhost/netns'} if isinstance(spec, basestring): ret_spec['path'] = spec elif isinstance(spec, Record): ret_spec.update(spec._as_dict()) path = netns._get_netnspath(ret_spec['path']) # on Python3 _get_netnspath() returns bytes, not str, so # we have to decode it here in order to avoid issues with # cache keys and DB inserts if hasattr(path, 'decode'): path = path.decode('utf-8') ret_spec['path'] = path return ret_spec
def __setitem__(self, key, value): if self.state == 'system': raise ValueError('attempt to change a readonly object') if key == 'path': value = netns._get_netnspath(value) return super(NetNS, self).__setitem__(key, value)