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