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