]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
tests: raise timeout on test_runner.RunNode.test_introducer from 120s to 240s, since...
[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(common_util.SignalMixin, unittest.TestCase,
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(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
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     test_introducer.timeout = 240 # This hit the 120-second timeout on "François Lenny-armv5tel"
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 sixty 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