def _setup_tub(self, ign): # we can't get a dynamically-assigned portnum until our Tub is # running, which means after startService. l = self.tub.getListeners()[0] portnum = l.getPortnum() # record which port we're listening on, so we can grab the same one # next time fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="") location = self.get_config("node", "tub.location", "AUTO") # Replace the location "AUTO", if present, with the detected local addresses. split_location = location.split(",") if "AUTO" in split_location: d = iputil.get_local_addresses_async() def _add_local(local_addresses): while "AUTO" in split_location: split_location.remove("AUTO") split_location.extend( ["%s:%d" % (addr, portnum) for addr in local_addresses]) return ",".join(split_location) d.addCallback(_add_local) else: d = defer.succeed(location) def _got_location(location): self.log("Tub location set to %s" % (location, )) self.tub.setLocation(location) return self.tub d.addCallback(_got_location) return d
def test_list_async(self): d = iputil.get_local_addresses_async() def _check(addresses): self.failUnlessIn("127.0.0.1", addresses) self.failIfIn("0.0.0.0", addresses) d.addCallbacks(_check) return d
def _setup_tub(self, ign): # we can't get a dynamically-assigned portnum until our Tub is # running, which means after startService. l = self.tub.getListeners()[0] portnum = l.getPortnum() # record which port we're listening on, so we can grab the same one # next time fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="") location = self.get_config("node", "tub.location", "AUTO") # Replace the location "AUTO", if present, with the detected local addresses. split_location = location.split(",") if "AUTO" in split_location: d = iputil.get_local_addresses_async() def _add_local(local_addresses): while "AUTO" in split_location: split_location.remove("AUTO") split_location.extend(["%s:%d" % (addr, portnum) for addr in local_addresses]) return ",".join(split_location) d.addCallback(_add_local) else: d = defer.succeed(location) def _got_location(location): self.log("Tub location set to %s" % (location,)) self.tub.setLocation(location) return self.tub d.addCallback(_got_location) return d
def test_list_async(self): d = iputil.get_local_addresses_async() def _check(addresses): self.failUnless(len(addresses) >= 1) # always have localhost self.failUnless("127.0.0.1" in addresses, addresses) self.failIf("0.0.0.0" in addresses, addresses) d.addCallbacks(_check) return d
def _test_list_async_mock(self, command, output, expected): ns = Namespace() ns.first = True def call_Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0): if ns.first: ns.first = False e = OSError("EINTR") e.errno = errno.EINTR raise e elif os.path.basename(args[0]) == command: return FakeProcess(output, "") else: e = OSError("[Errno 2] No such file or directory") e.errno = errno.ENOENT raise e self.patch(subprocess, 'Popen', call_Popen) self.patch(os.path, 'isfile', lambda x: True) def call_get_local_ip_for(target): if target in ("localhost", "127.0.0.1"): return "127.0.0.1" else: return "192.168.0.10" self.patch(iputil, 'get_local_ip_for', call_get_local_ip_for) def call_which(name): return [name] self.patch(iputil, 'which', call_which) d = iputil.get_local_addresses_async() def _check(addresses): self.failUnlessEquals(set(addresses), set(expected)) d.addCallbacks(_check) return d
def _startService(self): precondition(reactor.running) self.log("Node._startService") service.MultiService.startService(self) d = defer.succeed(None) d.addCallback(lambda res: iputil.get_local_addresses_async()) d.addCallback(self._setup_tub) def _ready(res): self.log("%s running" % self.NODETYPE) self._tub_ready_observerlist.fire(self) return self d.addCallback(_ready) d.addErrback(self._service_startup_failed)