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