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