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