]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
remove --multiple/-m option from all CLI commands: closes #1262
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_runner.py
1 # -*- coding: utf-8 -*-
2
3 from twisted.trial import unittest
4
5 from twisted.python import usage, runtime
6 from twisted.internet import utils
7 import os.path, re, sys
8 from cStringIO import StringIO
9 from allmydata.util import fileutil, pollmixin
10 from allmydata.util.encodingutil import unicode_to_argv, unicode_to_output, get_filesystem_encoding
11 from allmydata.scripts import runner
12
13 from allmydata.test import common_util
14 import allmydata
15
16 timeout = 240
17
18 srcfile = allmydata.__file__
19 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
20
21 rootdir = os.path.dirname(srcdir)
22 if os.path.basename(srcdir) == 'site-packages':
23     if re.search(r'python.+\..+', os.path.basename(rootdir)):
24         rootdir = os.path.dirname(rootdir)
25     rootdir = os.path.dirname(rootdir)
26 elif os.path.basename(rootdir) == 'src':
27     rootdir = os.path.dirname(rootdir)
28
29 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
30 if sys.platform == "win32":
31     bintahoe += ".pyscript"
32     if not os.path.exists(bintahoe):
33        alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
34        if os.path.exists(alt_bintahoe):
35            bintahoe = alt_bintahoe
36
37
38 class SkipMixin:
39     def skip_if_cannot_run_bintahoe(self):
40         if "cygwin" in sys.platform.lower():
41             raise unittest.SkipTest("We don't know how to make this test work on cygwin: spawnProcess seems to hang forever. We don't know if 'bin/tahoe start' can be run on cygwin.")
42         if not os.path.exists(bintahoe):
43             raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location (%s), and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of Tahoe. Perhaps running 'setup.py build' again will help." % (bintahoe,))
44
45     def skip_if_cannot_daemonize(self):
46         self.skip_if_cannot_run_bintahoe()
47         if runtime.platformType == "win32":
48             # twistd on windows doesn't daemonize. cygwin should work normally.
49             raise unittest.SkipTest("twistd does not fork under windows")
50
51
52 class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
53     def test_the_right_code(self):
54         cwd = os.path.normcase(os.path.realpath("."))
55         root_from_cwd = os.path.dirname(cwd)
56         if os.path.basename(root_from_cwd) == 'src':
57             root_from_cwd = os.path.dirname(root_from_cwd)
58
59         same = (root_from_cwd == rootdir)
60         if not same:
61             try:
62                 same = os.path.samefile(root_from_cwd, rootdir)
63             except AttributeError, e:
64                 e  # hush pyflakes
65
66         if not same:
67             msg = ("We seem to be testing the code at %r,\n"
68                    "(according to the source filename %r),\n"
69                    "but expected to be testing the code at %r.\n"
70                    % (rootdir, srcfile, root_from_cwd))
71
72             root_from_cwdu = os.path.normcase(os.path.normpath(os.getcwdu()))
73             if os.path.basename(root_from_cwdu) == u'src':
74                 root_from_cwdu = os.path.dirname(root_from_cwdu)
75
76             if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
77                 msg += ("However, this may be a false alarm because the current directory path\n"
78                         "is not representable in the filesystem encoding. Please run the tests\n"
79                         "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
80                 raise unittest.SkipTest(msg)
81             else:
82                 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
83                 self.fail(msg)
84
85     def test_path(self):
86         self.skip_if_cannot_run_bintahoe()
87         d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
88         def _cb(res):
89             out, err, rc_or_sig = res
90             self.failUnlessEqual(rc_or_sig, 0, str(res))
91
92             # Fail unless the allmydata-tahoe package is *this* version *and*
93             # was loaded from *this* source directory.
94
95             verstr = str(allmydata.__version__)
96
97             self.failIfEqual(verstr, "unknown",
98                              "We don't know our version, because this distribution didn't come "
99                              "with a _version.py and 'setup.py darcsver' hasn't been run.")
100
101             # The Python "rational version numbering" convention
102             # disallows "-r$REV" but allows ".post$REV"
103             # instead. Eventually we'll probably move to that. When we
104             # do, this test won't go red:
105
106             ix = verstr.rfind('-r')
107             if ix != -1:
108                 altverstr = verstr[:ix] + '.post' + verstr[ix+2:]
109             else:
110                 ix = verstr.rfind('.post')
111                 if ix != -1:
112                     altverstr = verstr[:ix] + '-r' + verstr[ix+5:]
113                 else:
114                     altverstr = verstr
115
116             required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, verstr, srcdir)
117             alt_required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, altverstr, srcdir)
118
119             self.failUnless(out.startswith(required_ver_and_path) or out.startswith(alt_required_ver_and_path), (out, err, rc_or_sig, required_ver_and_path))
120         d.addCallback(_cb)
121         return d
122
123     def test_unicode_arguments_and_output(self):
124         self.skip_if_cannot_run_bintahoe()
125
126         tricky = u"\u2621"
127         try:
128             tricky_arg = unicode_to_argv(tricky, mangle=True)
129             tricky_out = unicode_to_output(tricky)
130         except UnicodeEncodeError:
131             raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
132
133         d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ)
134         def _cb(res):
135             out, err, rc_or_sig = res
136             self.failUnlessEqual(rc_or_sig, 1, str(res))
137             self.failUnlessIn("Unknown command: "+tricky_out, out)
138         d.addCallback(_cb)
139         return d
140
141     def test_run_with_python_options(self):
142         self.skip_if_cannot_run_bintahoe()
143
144         # -t is a harmless option that warns about tabs.
145         d = utils.getProcessOutputAndValue(sys.executable, args=['-t', bintahoe, '--version'],
146                                            env=os.environ)
147         def _cb(res):
148             out, err, rc_or_sig = res
149             self.failUnlessEqual(rc_or_sig, 0, str(res))
150             self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
151         d.addCallback(_cb)
152         return d
153
154     def test_version_no_noise(self):
155         self.skip_if_cannot_run_bintahoe()
156         import pkg_resources
157         try:
158             pkg_resources.require("Twisted>=9.0.0")
159         except pkg_resources.VersionConflict:
160             raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
161
162         d = utils.getProcessOutputAndValue(bintahoe, args=["--version"], env=os.environ)
163         def _cb(res):
164             out, err, rc_or_sig = res
165             self.failUnlessEqual(rc_or_sig, 0, str(res))
166             self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
167             self.failIfIn("DeprecationWarning", out, str(res))
168             errlines = err.split("\n")
169             self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
170                                                                   and "from pkg_resources import load_entry_point" not in line)], str(res))
171             if err != "":
172                 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
173         d.addCallback(_cb)
174         return d
175
176
177 class CreateNode(unittest.TestCase):
178     # exercise "tahoe create-node", create-introducer,
179     # create-key-generator, and create-stats-gatherer, by calling the
180     # corresponding code as a subroutine.
181
182     def workdir(self, name):
183         basedir = os.path.join("test_runner", "CreateNode", name)
184         fileutil.make_dirs(basedir)
185         return basedir
186
187     def run_tahoe(self, argv):
188         out,err = StringIO(), StringIO()
189         rc = runner.runner(argv, stdout=out, stderr=err)
190         return rc, out.getvalue(), err.getvalue()
191
192     def do_create(self, kind):
193         basedir = self.workdir("test_" + kind)
194         command = "create-" + kind
195         is_client = kind in ("node", "client")
196         tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
197
198         n1 = os.path.join(basedir, command + "-n1")
199         argv = ["--quiet", command, "--basedir", n1]
200         rc, out, err = self.run_tahoe(argv)
201         self.failUnlessEqual(err, "")
202         self.failUnlessEqual(out, "")
203         self.failUnlessEqual(rc, 0)
204         self.failUnless(os.path.exists(n1))
205         self.failUnless(os.path.exists(os.path.join(n1, tac)))
206
207         if is_client:
208             # tahoe.cfg should exist, and should have storage enabled for
209             # 'create-node', and disabled for 'create-client'.
210             tahoe_cfg = os.path.join(n1, "tahoe.cfg")
211             self.failUnless(os.path.exists(tahoe_cfg))
212             content = open(tahoe_cfg).read()
213             if kind == "client":
214                 self.failUnless("\n[storage]\nenabled = false\n" in content)
215             else:
216                 self.failUnless("\n[storage]\nenabled = true\n" in content)
217
218         # creating the node a second time should be rejected
219         rc, out, err = self.run_tahoe(argv)
220         self.failIfEqual(rc, 0, str((out, err, rc)))
221         self.failUnlessEqual(out, "")
222         self.failUnless("is not empty." in err)
223
224         # Fail if there is a non-empty line that doesn't end with a
225         # punctuation mark.
226         for line in err.splitlines():
227             self.failIf(re.search("[\S][^\.!?]$", line), (line,))
228
229         # test that the non --basedir form works too
230         n2 = os.path.join(basedir, command + "-n2")
231         argv = ["--quiet", command, n2]
232         rc, out, err = self.run_tahoe(argv)
233         self.failUnlessEqual(err, "")
234         self.failUnlessEqual(out, "")
235         self.failUnlessEqual(rc, 0)
236         self.failUnless(os.path.exists(n2))
237         self.failUnless(os.path.exists(os.path.join(n2, tac)))
238
239         # test the --node-directory form
240         n3 = os.path.join(basedir, command + "-n3")
241         argv = ["--quiet", command, "--node-directory", n3]
242         rc, out, err = self.run_tahoe(argv)
243         self.failUnlessEqual(err, "")
244         self.failUnlessEqual(out, "")
245         self.failUnlessEqual(rc, 0)
246         self.failUnless(os.path.exists(n3))
247         self.failUnless(os.path.exists(os.path.join(n3, tac)))
248
249         # make sure it rejects too many arguments
250         argv = [command, "basedir", "extraarg"]
251         self.failUnlessRaises(usage.UsageError,
252                               runner.runner, argv,
253                               run_by_human=False)
254
255         # when creating a non-client, there is no default for the basedir
256         if not is_client:
257             argv = [command]
258             self.failUnlessRaises(usage.UsageError,
259                                   runner.runner, argv,
260                                   run_by_human=False)
261
262
263     def test_node(self):
264         self.do_create("node")
265
266     def test_client(self):
267         # create-client should behave like create-node --no-storage.
268         self.do_create("client")
269
270     def test_introducer(self):
271         self.do_create("introducer")
272
273     def test_key_generator(self):
274         self.do_create("key-generator")
275
276     def test_stats_gatherer(self):
277         self.do_create("stats-gatherer")
278
279     def test_subcommands(self):
280         # no arguments should trigger a command listing, via UsageError
281         self.failUnlessRaises(usage.UsageError,
282                               runner.runner,
283                               [],
284                               run_by_human=False)
285
286
287 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
288               SkipMixin):
289     # exercise "tahoe start", for both introducer, client node, and
290     # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
291     # get us figleaf-based line-level coverage, but it does a better job of
292     # confirming that the user can actually run "./bin/tahoe start" and
293     # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
294     # the like correctly.
295
296     # This doesn't work on cygwin (it hangs forever), so we skip this test
297     # when we're on cygwin. It is likely that "tahoe start" itself doesn't
298     # work on cygwin: twisted seems unable to provide a version of
299     # spawnProcess which really works there.
300
301     def workdir(self, name):
302         basedir = os.path.join("test_runner", "RunNode", name)
303         fileutil.make_dirs(basedir)
304         return basedir
305
306     def test_introducer(self):
307         self.skip_if_cannot_daemonize()
308         basedir = self.workdir("test_introducer")
309         c1 = os.path.join(basedir, "c1")
310         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
311         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
312         INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
313
314         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
315         def _cb(res):
316             out, err, rc_or_sig = res
317             self.failUnlessEqual(rc_or_sig, 0)
318             # by writing this file, we get ten seconds before the node will
319             # exit. This insures that even if the test fails (and the 'stop'
320             # command doesn't work), the client should still terminate.
321             open(HOTLINE_FILE, "w").write("")
322             # now it's safe to start the node
323         d.addCallback(_cb)
324
325         def _then_start_the_node(res):
326             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
327         d.addCallback(_then_start_the_node)
328
329         def _cb2(res):
330             out, err, rc_or_sig = res
331
332             open(HOTLINE_FILE, "w").write("")
333             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
334             self.failUnlessEqual(rc_or_sig, 0, errstr)
335             self.failUnlessEqual(out, "", errstr)
336             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
337
338             # the parent (twistd) has exited. However, twistd writes the pid
339             # from the child, not the parent, so we can't expect twistd.pid
340             # to exist quite yet.
341
342             # the node is running, but it might not have made it past the
343             # first reactor turn yet, and if we kill it too early, it won't
344             # remove the twistd.pid file. So wait until it does something
345             # that we know it won't do until after the first turn.
346         d.addCallback(_cb2)
347
348         def _node_has_started():
349             return os.path.exists(INTRODUCER_FURL_FILE)
350         d.addCallback(lambda res: self.poll(_node_has_started))
351
352         def _started(res):
353             open(HOTLINE_FILE, "w").write("")
354             self.failUnless(os.path.exists(TWISTD_PID_FILE))
355             # rm this so we can detect when the second incarnation is ready
356             os.unlink(INTRODUCER_FURL_FILE)
357             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
358         d.addCallback(_started)
359
360         def _then(res):
361             out, err, rc_or_sig = res
362             open(HOTLINE_FILE, "w").write("")
363             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
364             self.failUnlessEqual(rc_or_sig, 0, errstr)
365             self.failUnlessEqual(out, "", errstr)
366             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
367         d.addCallback(_then)
368
369         # again, the second incarnation of the node might not be ready yet,
370         # so poll until it is
371         d.addCallback(lambda res: self.poll(_node_has_started))
372
373         # now we can kill it. TODO: On a slow machine, the node might kill
374         # itself before we get a chance too, especially if spawning the
375         # 'tahoe stop' command takes a while.
376         def _stop(res):
377             open(HOTLINE_FILE, "w").write("")
378             self.failUnless(os.path.exists(TWISTD_PID_FILE))
379
380             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
381         d.addCallback(_stop)
382
383         def _after_stopping(res):
384             out, err, rc_or_sig = res
385             open(HOTLINE_FILE, "w").write("")
386             # the parent has exited by now
387             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
388             self.failUnlessEqual(rc_or_sig, 0, errstr)
389             self.failUnlessEqual(out, "", errstr)
390             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
391             # the parent was supposed to poll and wait until it sees
392             # twistd.pid go away before it exits, so twistd.pid should be
393             # gone by now.
394             self.failIf(os.path.exists(TWISTD_PID_FILE))
395         d.addCallback(_after_stopping)
396
397         def _remove_hotline(res):
398             os.unlink(HOTLINE_FILE)
399             return res
400         d.addBoth(_remove_hotline)
401         return d
402     test_introducer.timeout = 480 # This hit the 120-second timeout on "François Lenny-armv5tel", then it hit a 240-second timeout on our feisty2.5 buildslave: http://allmydata.org/buildbot/builders/feisty2.5/builds/2381/steps/test/logs/test.log
403
404     def test_client_no_noise(self):
405         self.skip_if_cannot_daemonize()
406         import pkg_resources
407         try:
408             pkg_resources.require("Twisted>=9.0.0")
409         except pkg_resources.VersionConflict:
410             raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
411         basedir = self.workdir("test_client_no_noise")
412         c1 = os.path.join(basedir, "c1")
413         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
414         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
415         PORTNUMFILE = os.path.join(c1, "client.port")
416
417         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
418         def _cb(res):
419             out, err, rc_or_sig = res
420             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
421             assert rc_or_sig == 0, errstr
422             self.failUnlessEqual(rc_or_sig, 0)
423             # By writing this file, we get forty seconds before the client will exit. This insures
424             # that even if the 'stop' command doesn't work (and the test fails), the client should
425             # still terminate.
426             open(HOTLINE_FILE, "w").write("")
427             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
428             # now it's safe to start the node
429         d.addCallback(_cb)
430
431         def _start(res):
432             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
433         d.addCallback(_start)
434
435         def _cb2(res):
436             out, err, rc_or_sig = res
437             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
438             open(HOTLINE_FILE, "w").write("")
439             self.failUnlessEqual(rc_or_sig, 0, errstr)
440             self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
441             errlines = err.split("\n")
442             self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
443                                                                   and "from pkg_resources import load_entry_point" not in line)], errstr)
444             if err != "":
445                 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
446
447             # the parent (twistd) has exited. However, twistd writes the pid
448             # from the child, not the parent, so we can't expect twistd.pid
449             # to exist quite yet.
450
451             # the node is running, but it might not have made it past the
452             # first reactor turn yet, and if we kill it too early, it won't
453             # remove the twistd.pid file. So wait until it does something
454             # that we know it won't do until after the first turn.
455         d.addCallback(_cb2)
456
457         def _node_has_started():
458             return os.path.exists(PORTNUMFILE)
459         d.addCallback(lambda res: self.poll(_node_has_started))
460
461         # now we can kill it. TODO: On a slow machine, the node might kill
462         # itself before we get a chance too, especially if spawning the
463         # 'tahoe stop' command takes a while.
464         def _stop(res):
465             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
466             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
467         d.addCallback(_stop)
468         return d
469
470     def test_client(self):
471         self.skip_if_cannot_daemonize()
472         basedir = self.workdir("test_client")
473         c1 = os.path.join(basedir, "c1")
474         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
475         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
476         PORTNUMFILE = os.path.join(c1, "client.port")
477
478         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
479         def _cb(res):
480             out, err, rc_or_sig = res
481             self.failUnlessEqual(rc_or_sig, 0)
482             # By writing this file, we get sixty seconds before the client will exit. This insures
483             # that even if the 'stop' command doesn't work (and the test fails), the client should
484             # still terminate.
485             open(HOTLINE_FILE, "w").write("")
486             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
487             # now it's safe to start the node
488         d.addCallback(_cb)
489
490         def _start(res):
491             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
492         d.addCallback(_start)
493
494         def _cb2(res):
495             out, err, rc_or_sig = res
496             open(HOTLINE_FILE, "w").write("")
497             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
498             self.failUnlessEqual(rc_or_sig, 0, errstr)
499             self.failUnlessEqual(out, "", errstr)
500             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
501
502             # the parent (twistd) has exited. However, twistd writes the pid
503             # from the child, not the parent, so we can't expect twistd.pid
504             # to exist quite yet.
505
506             # the node is running, but it might not have made it past the
507             # first reactor turn yet, and if we kill it too early, it won't
508             # remove the twistd.pid file. So wait until it does something
509             # that we know it won't do until after the first turn.
510         d.addCallback(_cb2)
511
512         def _node_has_started():
513             return os.path.exists(PORTNUMFILE)
514         d.addCallback(lambda res: self.poll(_node_has_started))
515
516         def _started(res):
517             open(HOTLINE_FILE, "w").write("")
518             self.failUnless(os.path.exists(TWISTD_PID_FILE))
519             # rm this so we can detect when the second incarnation is ready
520             os.unlink(PORTNUMFILE)
521
522             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
523         d.addCallback(_started)
524
525         def _cb3(res):
526             out, err, rc_or_sig = res
527
528             open(HOTLINE_FILE, "w").write("")
529             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
530             self.failUnlessEqual(rc_or_sig, 0, errstr)
531             self.failUnlessEqual(out, "", errstr)
532             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
533         d.addCallback(_cb3)
534
535         # again, the second incarnation of the node might not be ready yet,
536         # so poll until it is
537         d.addCallback(lambda res: self.poll(_node_has_started))
538
539         # now we can kill it. TODO: On a slow machine, the node might kill
540         # itself before we get a chance too, especially if spawning the
541         # 'tahoe stop' command takes a while.
542         def _stop(res):
543             open(HOTLINE_FILE, "w").write("")
544             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
545             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
546         d.addCallback(_stop)
547
548         def _cb4(res):
549             out, err, rc_or_sig = res
550
551             open(HOTLINE_FILE, "w").write("")
552             # the parent has exited by now
553             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
554             self.failUnlessEqual(rc_or_sig, 0, errstr)
555             self.failUnlessEqual(out, "", errstr)
556             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
557             # the parent was supposed to poll and wait until it sees
558             # twistd.pid go away before it exits, so twistd.pid should be
559             # gone by now.
560             self.failIf(os.path.exists(TWISTD_PID_FILE))
561         d.addCallback(_cb4)
562         def _remove_hotline(res):
563             os.unlink(HOTLINE_FILE)
564             return res
565         d.addBoth(_remove_hotline)
566         return d
567
568     def test_baddir(self):
569         self.skip_if_cannot_daemonize()
570         basedir = self.workdir("test_baddir")
571         fileutil.make_dirs(basedir)
572
573         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
574         def _cb(res):
575             out, err, rc_or_sig = res
576             self.failUnlessEqual(rc_or_sig, 1)
577             self.failUnless("does not look like a node directory" in err, err)
578         d.addCallback(_cb)
579
580         def _then_stop_it(res):
581             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
582         d.addCallback(_then_stop_it)
583
584         def _cb2(res):
585             out, err, rc_or_sig = res
586             self.failUnlessEqual(rc_or_sig, 2)
587             self.failUnless("does not look like a running node directory" in err)
588         d.addCallback(_cb2)
589
590         def _then_start_in_bogus_basedir(res):
591             not_a_dir = os.path.join(basedir, "bogus")
592             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
593         d.addCallback(_then_start_in_bogus_basedir)
594
595         def _cb3(res):
596             out, err, rc_or_sig = res
597             self.failUnlessEqual(rc_or_sig, 1)
598             self.failUnless("does not look like a directory at all" in err, err)
599         d.addCallback(_cb3)
600         return d
601
602     def test_keygen(self):
603         self.skip_if_cannot_daemonize()
604         basedir = self.workdir("test_keygen")
605         c1 = os.path.join(basedir, "c1")
606         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
607         KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
608
609         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
610         def _cb(res):
611             out, err, rc_or_sig = res
612             self.failUnlessEqual(rc_or_sig, 0)
613         d.addCallback(_cb)
614
615         def _start(res):
616             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
617         d.addCallback(_start)
618
619         def _cb2(res):
620             out, err, rc_or_sig = res
621             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
622             self.failUnlessEqual(rc_or_sig, 0, errstr)
623             self.failUnlessEqual(out, "", errstr)
624             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
625
626             # the parent (twistd) has exited. However, twistd writes the pid
627             # from the child, not the parent, so we can't expect twistd.pid
628             # to exist quite yet.
629
630             # the node is running, but it might not have made it past the
631             # first reactor turn yet, and if we kill it too early, it won't
632             # remove the twistd.pid file. So wait until it does something
633             # that we know it won't do until after the first turn.
634         d.addCallback(_cb2)
635
636         def _node_has_started():
637             return os.path.exists(KEYGEN_FURL_FILE)
638         d.addCallback(lambda res: self.poll(_node_has_started))
639
640         def _started(res):
641             self.failUnless(os.path.exists(TWISTD_PID_FILE))
642             # rm this so we can detect when the second incarnation is ready
643             os.unlink(KEYGEN_FURL_FILE)
644             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
645         d.addCallback(_started)
646
647         def _cb3(res):
648             out, err, rc_or_sig = res
649             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
650             self.failUnlessEqual(rc_or_sig, 0, errstr)
651             self.failUnlessEqual(out, "", errstr)
652             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
653         d.addCallback(_cb3)
654
655         # again, the second incarnation of the node might not be ready yet,
656         # so poll until it is
657         d.addCallback(lambda res: self.poll(_node_has_started))
658
659         # now we can kill it. TODO: On a slow machine, the node might kill
660         # itself before we get a chance too, especially if spawning the
661         # 'tahoe stop' command takes a while.
662         def _stop(res):
663             self.failUnless(os.path.exists(TWISTD_PID_FILE))
664             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
665         d.addCallback(_stop)
666
667         def _cb4(res):
668             out, err, rc_or_sig = res
669             # the parent has exited by now
670             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
671             self.failUnlessEqual(rc_or_sig, 0, errstr)
672             self.failUnlessEqual(out, "", errstr)
673             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
674             # the parent was supposed to poll and wait until it sees
675             # twistd.pid go away before it exits, so twistd.pid should be
676             # gone by now.
677             self.failIf(os.path.exists(TWISTD_PID_FILE))
678         d.addCallback(_cb4)
679         return d