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