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