Remove some more test code related to start/restart/stop

This commit is contained in:
Jean-Paul Calderone 2020-12-09 07:24:51 -05:00
parent d346c90c6e
commit 5b0190b9a1
3 changed files with 46 additions and 95 deletions

View File

@ -20,14 +20,14 @@ from allmydata.scripts.common_http import socket_error
import allmydata.scripts.common_http import allmydata.scripts.common_http
# Test that the scripts can be imported. # Test that the scripts can be imported.
from allmydata.scripts import create_node, debug, tahoe_start, tahoe_restart, \ from allmydata.scripts import create_node, debug, \
tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, \ tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, \
tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen, \ tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen, \
tahoe_stop, tahoe_daemonize, tahoe_run tahoe_run
_hush_pyflakes = [create_node, debug, tahoe_start, tahoe_restart, tahoe_stop, _hush_pyflakes = [create_node, debug,
tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls,
tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen, tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen,
tahoe_daemonize, tahoe_run] tahoe_run]
from allmydata.scripts import common from allmydata.scripts import common
from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases, get_alias, \ from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases, get_alias, \
@ -626,18 +626,6 @@ class Help(unittest.TestCase):
help = str(cli.ListAliasesOptions()) help = str(cli.ListAliasesOptions())
self.failUnlessIn("[options]", help) self.failUnlessIn("[options]", help)
def test_start(self):
help = str(tahoe_start.StartOptions())
self.failUnlessIn("[options] [NODEDIR [twistd-options]]", help)
def test_stop(self):
help = str(tahoe_stop.StopOptions())
self.failUnlessIn("[options] [NODEDIR]", help)
def test_restart(self):
help = str(tahoe_restart.RestartOptions())
self.failUnlessIn("[options] [NODEDIR [twistd-options]]", help)
def test_run(self): def test_run(self):
help = str(tahoe_run.RunOptions()) help = str(tahoe_run.RunOptions())
self.failUnlessIn("[options] [NODEDIR [twistd-options]]", help) self.failUnlessIn("[options] [NODEDIR [twistd-options]]", help)
@ -1269,79 +1257,58 @@ class Options(ReallyEqualMixin, unittest.TestCase):
self.failUnlessIn(allmydata.__full_version__, stdout.getvalue()) self.failUnlessIn(allmydata.__full_version__, stdout.getvalue())
# but "tahoe SUBCOMMAND --version" should be rejected # but "tahoe SUBCOMMAND --version" should be rejected
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--version"]) ["run", "--version"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--version-and-path"]) ["run", "--version-and-path"])
def test_quiet(self): def test_quiet(self):
# accepted as an overall option, but not on subcommands # accepted as an overall option, but not on subcommands
o = self.parse(["--quiet", "start"]) o = self.parse(["--quiet", "run"])
self.failUnless(o.parent["quiet"]) self.failUnless(o.parent["quiet"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--quiet"]) ["run", "--quiet"])
def test_basedir(self): def test_basedir(self):
# accept a --node-directory option before the verb, or a --basedir # accept a --node-directory option before the verb, or a --basedir
# option after, or a basedir argument after, but none in the wrong # option after, or a basedir argument after, but none in the wrong
# place, and not more than one of the three. # place, and not more than one of the three.
o = self.parse(["start"]) o = self.parse(["run"])
self.failUnlessReallyEqual(o["basedir"], os.path.join(fileutil.abspath_expanduser_unicode(u"~"), self.failUnlessReallyEqual(o["basedir"], os.path.join(fileutil.abspath_expanduser_unicode(u"~"),
u".tahoe")) u".tahoe"))
o = self.parse(["start", "here"]) o = self.parse(["run", "here"])
self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here")) self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here"))
o = self.parse(["start", "--basedir", "there"]) o = self.parse(["run", "--basedir", "there"])
self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there"))
o = self.parse(["--node-directory", "there", "start"]) o = self.parse(["--node-directory", "there", "run"])
self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there")) self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"there"))
o = self.parse(["start", "here", "--nodaemon"]) o = self.parse(["run", "here", "--nodaemon"])
self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here")) self.failUnlessReallyEqual(o["basedir"], fileutil.abspath_expanduser_unicode(u"here"))
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["--basedir", "there", "start"]) ["--basedir", "there", "run"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--node-directory", "there"]) ["run", "--node-directory", "there"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["--node-directory=there", ["--node-directory=there",
"start", "--basedir=here"]) "run", "--basedir=here"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--basedir=here", "anywhere"]) ["run", "--basedir=here", "anywhere"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["--node-directory=there", ["--node-directory=there",
"start", "anywhere"]) "run", "anywhere"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["--node-directory=there", ["--node-directory=there",
"start", "--basedir=here", "anywhere"]) "run", "--basedir=here", "anywhere"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["--node-directory=there", "start", "--nodaemon"]) ["--node-directory=there", "run", "--nodaemon"])
self.failUnlessRaises(usage.UsageError, self.parse, self.failUnlessRaises(usage.UsageError, self.parse,
["start", "--basedir=here", "--nodaemon"]) ["run", "--basedir=here", "--nodaemon"])
class Stop(unittest.TestCase): class Run(unittest.TestCase):
def test_non_numeric_pid(self):
"""
If the pidfile exists but does not contain a numeric value, a complaint to
this effect is written to stderr and the non-success result is
returned.
"""
basedir = FilePath(self.mktemp().decode("ascii"))
basedir.makedirs()
basedir.child(u"twistd.pid").setContent(b"foo")
config = tahoe_stop.StopOptions()
config.stdout = StringIO()
config.stderr = StringIO()
config['basedir'] = basedir.path
result_code = tahoe_stop.stop(config)
self.assertEqual(2, result_code)
self.assertIn("invalid PID file", config.stderr.getvalue())
class Start(unittest.TestCase):
@patch('allmydata.scripts.run_common.os.chdir') @patch('allmydata.scripts.run_common.os.chdir')
@patch('allmydata.scripts.run_common.twistd') @patch('allmydata.scripts.run_common.twistd')
@ -1355,13 +1322,13 @@ class Start(unittest.TestCase):
basedir.child(u"twistd.pid").setContent(b"foo") basedir.child(u"twistd.pid").setContent(b"foo")
basedir.child(u"tahoe-client.tac").setContent(b"") basedir.child(u"tahoe-client.tac").setContent(b"")
config = tahoe_daemonize.DaemonizeOptions() config = tahoe_run.RunOptions()
config.stdout = StringIO() config.stdout = StringIO()
config.stderr = StringIO() config.stderr = StringIO()
config['basedir'] = basedir.path config['basedir'] = basedir.path
config.twistd_args = [] config.twistd_args = []
result_code = tahoe_daemonize.daemonize(config) result_code = tahoe_run.run(config)
self.assertIn("invalid PID file", config.stderr.getvalue()) self.assertIn("invalid PID file", config.stderr.getvalue())
self.assertTrue(len(mock_twistd.mock_calls), 1) self.assertTrue(len(mock_twistd.mock_calls), 1)
self.assertEqual(mock_twistd.mock_calls[0][0], 'runApp') self.assertEqual(mock_twistd.mock_calls[0][0], 'runApp')

View File

@ -14,6 +14,10 @@ from errno import ENOENT
import attr import attr
from eliot import (
log_call,
)
from twisted.internet.error import ( from twisted.internet.error import (
ProcessDone, ProcessDone,
ProcessTerminated, ProcessTerminated,
@ -42,11 +46,9 @@ from twisted.internet.task import (
from ..client import ( from ..client import (
_Client, _Client,
) )
from ..scripts.tahoe_stop import (
COULD_NOT_STOP,
)
from ..util.eliotutil import ( from ..util.eliotutil import (
inline_callbacks, inline_callbacks,
log_call_deferred,
) )
class Expect(Protocol, object): class Expect(Protocol, object):
@ -156,6 +158,7 @@ class CLINodeAPI(object):
env=os.environ, env=os.environ,
) )
@log_call(action_type="test:cli-api:run", include_args=["extra_tahoe_args"])
def run(self, protocol, extra_tahoe_args=()): def run(self, protocol, extra_tahoe_args=()):
""" """
Start the node running. Start the node running.
@ -176,28 +179,21 @@ class CLINodeAPI(object):
if ENOENT != e.errno: if ENOENT != e.errno:
raise raise
@log_call_deferred(action_type="test:cli-api:stop")
def stop(self, protocol): def stop(self, protocol):
self._execute( return self.stop_and_wait()
protocol,
[u"stop", self.basedir.asTextMode().path],
)
@log_call_deferred(action_type="test:cli-api:stop-and-wait")
@inline_callbacks @inline_callbacks
def stop_and_wait(self): def stop_and_wait(self):
if platform.isWindows(): if self.process is not None:
# On Windows there is no PID file and no "tahoe stop". while True:
if self.process is not None: try:
while True: self.process.signalProcess("TERM")
try: except ProcessExitedAlready:
self.process.signalProcess("TERM") break
except ProcessExitedAlready: else:
break yield deferLater(self.reactor, 0.1, lambda: None)
else:
yield deferLater(self.reactor, 0.1, lambda: None)
else:
protocol, ended = wait_for_exit()
self.stop(protocol)
yield ended
def active(self): def active(self):
# By writing this file, we get two minutes before the client will # By writing this file, we get two minutes before the client will
@ -208,8 +204,6 @@ class CLINodeAPI(object):
def _check_cleanup_reason(self, reason): def _check_cleanup_reason(self, reason):
# Let it fail because the process has already exited. # Let it fail because the process has already exited.
reason.trap(ProcessTerminated) reason.trap(ProcessTerminated)
if reason.value.exitCode != COULD_NOT_STOP:
return reason
return None return None
def cleanup(self): def cleanup(self):

View File

@ -34,6 +34,7 @@ from ._twisted_9607 import (
) )
from ..util.eliotutil import ( from ..util.eliotutil import (
inline_callbacks, inline_callbacks,
log_call_deferred,
) )
def get_root_from_file(src): def get_root_from_file(src):
@ -54,6 +55,7 @@ rootdir = get_root_from_file(srcfile)
class RunBinTahoeMixin(object): class RunBinTahoeMixin(object):
@log_call_deferred(action_type="run-bin-tahoe")
def run_bintahoe(self, args, stdin=None, python_options=[], env=None): def run_bintahoe(self, args, stdin=None, python_options=[], env=None):
command = sys.executable command = sys.executable
argv = python_options + ["-m", "allmydata.scripts.runner"] + args argv = python_options + ["-m", "allmydata.scripts.runner"] + args
@ -335,7 +337,7 @@ class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
@inline_callbacks @inline_callbacks
def test_client(self): def test_client(self):
""" """
Test many things. Test too many things.
0) Verify that "tahoe create-node" takes a --webport option and writes 0) Verify that "tahoe create-node" takes a --webport option and writes
the value to the configuration file. the value to the configuration file.
@ -343,9 +345,9 @@ class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
1) Verify that "tahoe run" writes a pid file and a node url file (on POSIX). 1) Verify that "tahoe run" writes a pid file and a node url file (on POSIX).
2) Verify that the storage furl file has a stable value across a 2) Verify that the storage furl file has a stable value across a
"tahoe run" / "tahoe stop" / "tahoe run" sequence. "tahoe run" / stop / "tahoe run" sequence.
3) Verify that the pid file is removed after "tahoe stop" succeeds (on POSIX). 3) Verify that the pid file is removed after SIGTERM (on POSIX).
""" """
basedir = self.workdir("test_client") basedir = self.workdir("test_client")
c1 = os.path.join(basedir, "c1") c1 = os.path.join(basedir, "c1")
@ -449,18 +451,6 @@ class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
"does not look like a directory at all" "does not look like a directory at all"
) )
def test_stop_bad_directory(self):
"""
If ``tahoe run`` is pointed at a directory where no node is running, it
reports an error and exits.
"""
return self._bad_directory_test(
u"test_stop_bad_directory",
"tahoe stop",
lambda tahoe, p: tahoe.stop(p),
"does not look like a running node directory",
)
@inline_callbacks @inline_callbacks
def _bad_directory_test(self, workdir, description, operation, expected_message): def _bad_directory_test(self, workdir, description, operation, expected_message):
""" """