2016-10-06 05:03:35 +00:00
|
|
|
import sys
|
2016-08-22 23:36:56 +00:00
|
|
|
import time
|
2016-10-06 05:03:35 +00:00
|
|
|
from os import mkdir
|
|
|
|
from os.path import exists, join
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
2017-07-26 15:29:15 +00:00
|
|
|
from twisted.internet.defer import Deferred, succeed
|
2016-10-06 05:03:35 +00:00
|
|
|
from twisted.internet.protocol import ProcessProtocol
|
|
|
|
from twisted.internet.error import ProcessExitedAlready, ProcessDone
|
|
|
|
|
2017-07-26 14:49:43 +00:00
|
|
|
from allmydata.util.configutil import (
|
|
|
|
get_config,
|
|
|
|
set_config,
|
|
|
|
write_config,
|
|
|
|
)
|
|
|
|
|
2016-10-06 05:03:35 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
class _ProcessExitedProtocol(ProcessProtocol):
|
|
|
|
"""
|
|
|
|
Internal helper that .callback()s on self.done when the process
|
|
|
|
exits (for any reason).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.done = Deferred()
|
|
|
|
|
|
|
|
def processEnded(self, reason):
|
|
|
|
self.done.callback(None)
|
|
|
|
|
|
|
|
|
|
|
|
class _CollectOutputProtocol(ProcessProtocol):
|
|
|
|
"""
|
|
|
|
Internal helper. Collects all output (stdout + stderr) into
|
|
|
|
self.output, and callback's on done with all of it after the
|
|
|
|
process exits (for any reason).
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.done = Deferred()
|
|
|
|
self.output = StringIO()
|
|
|
|
|
|
|
|
def processEnded(self, reason):
|
|
|
|
if not self.done.called:
|
|
|
|
self.done.callback(self.output.getvalue())
|
|
|
|
|
|
|
|
def processExited(self, reason):
|
|
|
|
if not isinstance(reason.value, ProcessDone):
|
|
|
|
self.done.errback(reason)
|
|
|
|
|
|
|
|
def outReceived(self, data):
|
|
|
|
self.output.write(data)
|
|
|
|
|
|
|
|
def errReceived(self, data):
|
2017-09-27 23:11:36 +00:00
|
|
|
print("ERR: {}".format(data))
|
2016-10-06 05:03:35 +00:00
|
|
|
self.output.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
class _DumpOutputProtocol(ProcessProtocol):
|
|
|
|
"""
|
|
|
|
Internal helper.
|
|
|
|
"""
|
|
|
|
def __init__(self, f):
|
|
|
|
self.done = Deferred()
|
|
|
|
self._out = f if f is not None else sys.stdout
|
|
|
|
|
|
|
|
def processEnded(self, reason):
|
|
|
|
if not self.done.called:
|
|
|
|
self.done.callback(None)
|
|
|
|
|
|
|
|
def processExited(self, reason):
|
|
|
|
if not isinstance(reason.value, ProcessDone):
|
|
|
|
self.done.errback(reason)
|
|
|
|
|
|
|
|
def outReceived(self, data):
|
|
|
|
self._out.write(data)
|
|
|
|
|
|
|
|
def errReceived(self, data):
|
|
|
|
self._out.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
class _MagicTextProtocol(ProcessProtocol):
|
|
|
|
"""
|
|
|
|
Internal helper. Monitors all stdout looking for a magic string,
|
|
|
|
and then .callback()s on self.done and .errback's if the process exits
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, magic_text):
|
|
|
|
self.magic_seen = Deferred()
|
|
|
|
self.exited = Deferred()
|
|
|
|
self._magic_text = magic_text
|
|
|
|
self._output = StringIO()
|
|
|
|
|
|
|
|
def processEnded(self, reason):
|
|
|
|
self.exited.callback(None)
|
|
|
|
|
|
|
|
def outReceived(self, data):
|
|
|
|
sys.stdout.write(data)
|
|
|
|
self._output.write(data)
|
|
|
|
if not self.magic_seen.called and self._magic_text in self._output.getvalue():
|
|
|
|
print("Saw '{}' in the logs".format(self._magic_text))
|
2017-09-27 23:11:36 +00:00
|
|
|
self.magic_seen.callback(self)
|
2016-10-06 05:03:35 +00:00
|
|
|
|
|
|
|
def errReceived(self, data):
|
|
|
|
sys.stdout.write(data)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_node(reactor, node_dir, request, magic_text):
|
|
|
|
if magic_text is None:
|
|
|
|
magic_text = "client running"
|
|
|
|
protocol = _MagicTextProtocol(magic_text)
|
|
|
|
|
|
|
|
# on windows, "tahoe start" means: run forever in the foreground,
|
|
|
|
# but on linux it means daemonize. "tahoe run" is consistent
|
|
|
|
# between platforms.
|
|
|
|
process = reactor.spawnProcess(
|
|
|
|
protocol,
|
|
|
|
sys.executable,
|
|
|
|
(
|
|
|
|
sys.executable, '-m', 'allmydata.scripts.runner',
|
|
|
|
'run',
|
|
|
|
node_dir,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
process.exited = protocol.exited
|
|
|
|
|
|
|
|
def cleanup():
|
|
|
|
try:
|
|
|
|
process.signalProcess('TERM')
|
|
|
|
pytest.blockon(protocol.exited)
|
|
|
|
except ProcessExitedAlready:
|
|
|
|
pass
|
|
|
|
request.addfinalizer(cleanup)
|
|
|
|
|
|
|
|
# we return the 'process' ITransport instance
|
|
|
|
# XXX abusing the Deferred; should use .when_magic_seen() or something?
|
2017-09-27 23:11:36 +00:00
|
|
|
|
|
|
|
def got_proto(proto):
|
|
|
|
process._protocol = proto
|
|
|
|
process._node_dir = node_dir
|
|
|
|
return process
|
|
|
|
protocol.magic_seen.addCallback(got_proto)
|
2016-10-06 05:03:35 +00:00
|
|
|
return protocol.magic_seen
|
|
|
|
|
|
|
|
|
2017-02-14 23:36:57 +00:00
|
|
|
def _create_node(reactor, request, temp_dir, introducer_furl, flog_gatherer, name, web_port,
|
|
|
|
storage=True,
|
|
|
|
magic_text=None,
|
|
|
|
needed=2,
|
|
|
|
happy=3,
|
|
|
|
total=4):
|
2016-10-06 05:03:35 +00:00
|
|
|
"""
|
|
|
|
Helper to create a single node, run it and return the instance
|
|
|
|
spawnProcess returned (ITransport)
|
|
|
|
"""
|
|
|
|
node_dir = join(temp_dir, name)
|
|
|
|
if web_port is None:
|
|
|
|
web_port = ''
|
2017-07-26 15:29:15 +00:00
|
|
|
if exists(node_dir):
|
|
|
|
created_d = succeed(None)
|
|
|
|
else:
|
2016-10-06 05:03:35 +00:00
|
|
|
print("creating", node_dir)
|
|
|
|
mkdir(node_dir)
|
|
|
|
done_proto = _ProcessExitedProtocol()
|
|
|
|
args = [
|
|
|
|
sys.executable, '-m', 'allmydata.scripts.runner',
|
|
|
|
'create-node',
|
|
|
|
'--nickname', name,
|
|
|
|
'--introducer', introducer_furl,
|
|
|
|
'--hostname', 'localhost',
|
|
|
|
'--listen', 'tcp',
|
2017-07-26 14:49:43 +00:00
|
|
|
'--webport', web_port,
|
|
|
|
'--shares-needed', unicode(needed),
|
|
|
|
'--shares-happy', unicode(happy),
|
|
|
|
'--shares-total', unicode(total),
|
2016-10-06 05:03:35 +00:00
|
|
|
]
|
|
|
|
if not storage:
|
|
|
|
args.append('--no-storage')
|
|
|
|
args.append(node_dir)
|
|
|
|
|
|
|
|
reactor.spawnProcess(
|
|
|
|
done_proto,
|
|
|
|
sys.executable,
|
|
|
|
args,
|
|
|
|
)
|
2017-02-14 23:36:57 +00:00
|
|
|
created_d = done_proto.done
|
2016-10-06 05:03:35 +00:00
|
|
|
|
2017-02-14 23:36:57 +00:00
|
|
|
def created(_):
|
2017-07-26 14:49:43 +00:00
|
|
|
config_path = join(node_dir, 'tahoe.cfg')
|
|
|
|
config = get_config(config_path)
|
|
|
|
set_config(config, 'node', 'log_gatherer.furl', flog_gatherer)
|
|
|
|
write_config(config_path, config)
|
2017-02-14 23:36:57 +00:00
|
|
|
created_d.addCallback(created)
|
|
|
|
|
|
|
|
d = Deferred()
|
|
|
|
d.callback(None)
|
|
|
|
d.addCallback(lambda _: created_d)
|
|
|
|
d.addCallback(lambda _: _run_node(reactor, node_dir, request, magic_text))
|
|
|
|
return d
|
2016-08-22 23:36:56 +00:00
|
|
|
|
|
|
|
|
2018-03-27 22:11:40 +00:00
|
|
|
def await_file_contents(path, contents, timeout=15, error_if=None):
|
|
|
|
"""
|
|
|
|
wait up to `timeout` seconds for the file at `path` to have the
|
|
|
|
exact content `contents.
|
|
|
|
|
|
|
|
:param error_if: if specified, a list of additional paths; if any
|
|
|
|
of these paths appear an Exception is raised.
|
|
|
|
"""
|
2016-08-22 23:36:56 +00:00
|
|
|
start_time = time.time()
|
|
|
|
while time.time() - start_time < timeout:
|
|
|
|
print(" waiting for '{}'".format(path))
|
2018-03-27 22:11:40 +00:00
|
|
|
if error_if and any([exists(p) for p in error_if]):
|
|
|
|
raise Exception(
|
|
|
|
"While waiting for '{}', unwanted files appeared: {}".format(
|
|
|
|
path,
|
|
|
|
', '.join([p for p in error_if if exists(p)]),
|
|
|
|
)
|
|
|
|
)
|
2016-08-22 23:36:56 +00:00
|
|
|
if exists(path):
|
2016-09-15 15:52:40 +00:00
|
|
|
try:
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
current = f.read()
|
|
|
|
except IOError:
|
|
|
|
print("IOError; trying again")
|
|
|
|
else:
|
|
|
|
if current == contents:
|
|
|
|
return True
|
|
|
|
print(" file contents still mismatched")
|
|
|
|
print(" wanted: {}".format(contents.replace('\n', ' ')))
|
|
|
|
print(" got: {}".format(current.replace('\n', ' ')))
|
2016-08-22 23:36:56 +00:00
|
|
|
time.sleep(1)
|
|
|
|
if exists(path):
|
|
|
|
raise Exception("Contents of '{}' mismatched after {}s".format(path, timeout))
|
|
|
|
raise Exception("Didn't find '{}' after {}s".format(path, timeout))
|
|
|
|
|
|
|
|
|
2018-04-20 23:33:14 +00:00
|
|
|
def await_files_exist(paths, timeout=15, await_all=False):
|
|
|
|
"""
|
|
|
|
wait up to `timeout` seconds for any of the paths to exist; when
|
|
|
|
any exist, a list of all found filenames is returned. Otherwise,
|
|
|
|
an Exception is raised
|
|
|
|
"""
|
|
|
|
found = []
|
|
|
|
start_time = time.time()
|
|
|
|
while not found and time.time() - start_time < 15.0:
|
|
|
|
print(" waiting for: {}".format(' '.join(paths)))
|
|
|
|
found = []
|
|
|
|
for path in paths:
|
|
|
|
if exists(path):
|
|
|
|
print("FOUND {}".format(path))
|
|
|
|
found.append(path)
|
|
|
|
if await_all:
|
|
|
|
if len(found) == len(paths):
|
|
|
|
return found
|
|
|
|
else:
|
|
|
|
if found:
|
|
|
|
return found
|
|
|
|
time.sleep(1)
|
2018-04-22 07:07:22 +00:00
|
|
|
if await_all:
|
|
|
|
raise Exception("Didn't find {} after {}s".format(' and '.join(paths), timeout))
|
|
|
|
raise Exception("Didn't find {} after {}s".format(' or '.join(paths), timeout))
|
2018-04-20 23:33:14 +00:00
|
|
|
|
|
|
|
|
2016-08-22 23:36:56 +00:00
|
|
|
def await_file_vanishes(path, timeout=10):
|
|
|
|
start_time = time.time()
|
|
|
|
while time.time() - start_time < timeout:
|
|
|
|
print(" waiting for '{}' to vanish".format(path))
|
|
|
|
if not exists(path):
|
|
|
|
return
|
|
|
|
time.sleep(1)
|
|
|
|
raise Exception("'{}' still exists after {}s".format(path, timeout))
|