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