]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
test_runner.py: also allow 'from pkg_resources import load_entry_point' as noise...
[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
162                                                                   and "from pkg_resources import load_entry_point" not in line)], errstr)
163             if err != "":
164                 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
165         d.addCallback(_cb)
166         return d
167
168
169 class CreateNode(unittest.TestCase):
170     # exercise "tahoe create-node", create-introducer,
171     # create-key-generator, and create-stats-gatherer, by calling the
172     # corresponding code as a subroutine.
173
174     def workdir(self, name):
175         basedir = os.path.join("test_runner", "CreateNode", name)
176         fileutil.make_dirs(basedir)
177         return basedir
178
179     def run_tahoe(self, argv):
180         out,err = StringIO(), StringIO()
181         rc = runner.runner(argv, stdout=out, stderr=err)
182         return rc, out.getvalue(), err.getvalue()
183
184     def do_create(self, kind):
185         basedir = self.workdir("test_" + kind)
186         command = "create-" + kind
187         is_client = kind in ("node", "client")
188         tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
189
190         n1 = os.path.join(basedir, command + "-n1")
191         argv = ["--quiet", command, "--basedir", n1]
192         rc, out, err = self.run_tahoe(argv)
193         self.failUnlessEqual(err, "")
194         self.failUnlessEqual(out, "")
195         self.failUnlessEqual(rc, 0)
196         self.failUnless(os.path.exists(n1))
197         self.failUnless(os.path.exists(os.path.join(n1, tac)))
198
199         if is_client:
200             # tahoe.cfg should exist, and should have storage enabled for
201             # 'create-node', and disabled for 'create-client'.
202             tahoe_cfg = os.path.join(n1, "tahoe.cfg")
203             self.failUnless(os.path.exists(tahoe_cfg))
204             content = open(tahoe_cfg).read()
205             if kind == "client":
206                 self.failUnless("\n[storage]\nenabled = false\n" in content)
207             else:
208                 self.failUnless("\n[storage]\nenabled = true\n" in content)
209
210         # creating the node a second time should be rejected
211         rc, out, err = self.run_tahoe(argv)
212         self.failIfEqual(rc, 0, str((out, err, rc)))
213         self.failUnlessEqual(out, "")
214         self.failUnless("is not empty." in err)
215
216         # Fail if there is a non-empty line that doesn't end with a
217         # punctuation mark.
218         for line in err.splitlines():
219             self.failIf(re.search("[\S][^\.!?]$", line), (line,))
220
221         # test that the non --basedir form works too
222         n2 = os.path.join(basedir, command + "-n2")
223         argv = ["--quiet", command, n2]
224         rc, out, err = self.run_tahoe(argv)
225         self.failUnlessEqual(err, "")
226         self.failUnlessEqual(out, "")
227         self.failUnlessEqual(rc, 0)
228         self.failUnless(os.path.exists(n2))
229         self.failUnless(os.path.exists(os.path.join(n2, tac)))
230
231         # test the --node-directory form
232         n3 = os.path.join(basedir, command + "-n3")
233         argv = ["--quiet", command, "--node-directory", n3]
234         rc, out, err = self.run_tahoe(argv)
235         self.failUnlessEqual(err, "")
236         self.failUnlessEqual(out, "")
237         self.failUnlessEqual(rc, 0)
238         self.failUnless(os.path.exists(n3))
239         self.failUnless(os.path.exists(os.path.join(n3, tac)))
240
241         # test the --multiple form
242         n4 = os.path.join(basedir, command + "-n4")
243         n5 = os.path.join(basedir, command + "-n5")
244         argv = ["--quiet", command, "--multiple", n4, n5]
245         rc, out, err = self.run_tahoe(argv)
246         self.failUnlessEqual(err, "")
247         self.failUnlessEqual(out, "")
248         self.failUnlessEqual(rc, 0)
249         self.failUnless(os.path.exists(n4))
250         self.failUnless(os.path.exists(os.path.join(n4, tac)))
251         self.failUnless(os.path.exists(n5))
252         self.failUnless(os.path.exists(os.path.join(n5, tac)))
253
254         # make sure it rejects too many arguments without --multiple
255         argv = [command, "basedir", "extraarg"]
256         self.failUnlessRaises(usage.UsageError,
257                               runner.runner, argv,
258                               run_by_human=False)
259
260         # when creating a non-client, there is no default for the basedir
261         if not is_client:
262             argv = [command]
263             self.failUnlessRaises(usage.UsageError,
264                                   runner.runner, argv,
265                                   run_by_human=False)
266
267
268     def test_node(self):
269         self.do_create("node")
270
271     def test_client(self):
272         # create-client should behave like create-node --no-storage.
273         self.do_create("client")
274
275     def test_introducer(self):
276         self.do_create("introducer")
277
278     def test_key_generator(self):
279         self.do_create("key-generator")
280
281     def test_stats_gatherer(self):
282         self.do_create("stats-gatherer")
283
284     def test_subcommands(self):
285         # no arguments should trigger a command listing, via UsageError
286         self.failUnlessRaises(usage.UsageError,
287                               runner.runner,
288                               [],
289                               run_by_human=False)
290
291
292 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
293               SkipMixin):
294     # exercise "tahoe start", for both introducer, client node, and
295     # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
296     # get us figleaf-based line-level coverage, but it does a better job of
297     # confirming that the user can actually run "./bin/tahoe start" and
298     # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
299     # the like correctly.
300
301     # This doesn't work on cygwin (it hangs forever), so we skip this test
302     # when we're on cygwin. It is likely that "tahoe start" itself doesn't
303     # work on cygwin: twisted seems unable to provide a version of
304     # spawnProcess which really works there.
305
306     def workdir(self, name):
307         basedir = os.path.join("test_runner", "RunNode", name)
308         fileutil.make_dirs(basedir)
309         return basedir
310
311     def test_introducer(self):
312         self.skip_if_cannot_daemonize()
313         basedir = self.workdir("test_introducer")
314         c1 = os.path.join(basedir, "c1")
315         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
316         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
317         INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
318
319         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
320         def _cb(res):
321             out, err, rc_or_sig = res
322             self.failUnlessEqual(rc_or_sig, 0)
323             # by writing this file, we get ten seconds before the node will
324             # exit. This insures that even if the test fails (and the 'stop'
325             # command doesn't work), the client should still terminate.
326             open(HOTLINE_FILE, "w").write("")
327             # now it's safe to start the node
328         d.addCallback(_cb)
329
330         def _then_start_the_node(res):
331             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
332         d.addCallback(_then_start_the_node)
333
334         def _cb2(res):
335             out, err, rc_or_sig = res
336
337             open(HOTLINE_FILE, "w").write("")
338             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
339             self.failUnlessEqual(rc_or_sig, 0, errstr)
340             self.failUnlessEqual(out, "", errstr)
341             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
342
343             # the parent (twistd) has exited. However, twistd writes the pid
344             # from the child, not the parent, so we can't expect twistd.pid
345             # to exist quite yet.
346
347             # the node is running, but it might not have made it past the
348             # first reactor turn yet, and if we kill it too early, it won't
349             # remove the twistd.pid file. So wait until it does something
350             # that we know it won't do until after the first turn.
351         d.addCallback(_cb2)
352
353         def _node_has_started():
354             return os.path.exists(INTRODUCER_FURL_FILE)
355         d.addCallback(lambda res: self.poll(_node_has_started))
356
357         def _started(res):
358             open(HOTLINE_FILE, "w").write("")
359             self.failUnless(os.path.exists(TWISTD_PID_FILE))
360             # rm this so we can detect when the second incarnation is ready
361             os.unlink(INTRODUCER_FURL_FILE)
362             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
363         d.addCallback(_started)
364
365         def _then(res):
366             out, err, rc_or_sig = res
367             open(HOTLINE_FILE, "w").write("")
368             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
369             self.failUnlessEqual(rc_or_sig, 0, errstr)
370             self.failUnlessEqual(out, "", errstr)
371             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
372         d.addCallback(_then)
373
374         # again, the second incarnation of the node might not be ready yet,
375         # so poll until it is
376         d.addCallback(lambda res: self.poll(_node_has_started))
377
378         # now we can kill it. TODO: On a slow machine, the node might kill
379         # itself before we get a chance too, especially if spawning the
380         # 'tahoe stop' command takes a while.
381         def _stop(res):
382             open(HOTLINE_FILE, "w").write("")
383             self.failUnless(os.path.exists(TWISTD_PID_FILE))
384
385             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
386         d.addCallback(_stop)
387
388         def _after_stopping(res):
389             out, err, rc_or_sig = res
390             open(HOTLINE_FILE, "w").write("")
391             # the parent has exited by now
392             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
393             self.failUnlessEqual(rc_or_sig, 0, errstr)
394             self.failUnlessEqual(out, "", errstr)
395             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
396             # the parent was supposed to poll and wait until it sees
397             # twistd.pid go away before it exits, so twistd.pid should be
398             # gone by now.
399             self.failIf(os.path.exists(TWISTD_PID_FILE))
400         d.addCallback(_after_stopping)
401
402         def _remove_hotline(res):
403             os.unlink(HOTLINE_FILE)
404             return res
405         d.addBoth(_remove_hotline)
406         return d
407     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
408
409     def test_client_no_noise(self):
410         self.skip_if_cannot_daemonize()
411         import pkg_resources
412         try:
413             pkg_resources.require("Twisted>=9.0.0")
414         except pkg_resources.VersionConflict:
415             raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
416         basedir = self.workdir("test_client_no_noise")
417         c1 = os.path.join(basedir, "c1")
418         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
419         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
420         PORTNUMFILE = os.path.join(c1, "client.port")
421
422         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
423         def _cb(res):
424             out, err, rc_or_sig = res
425             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
426             assert rc_or_sig == 0, errstr
427             self.failUnlessEqual(rc_or_sig, 0)
428             # By writing this file, we get forty seconds before the client will exit. This insures
429             # that even if the 'stop' command doesn't work (and the test fails), the client should
430             # still terminate.
431             open(HOTLINE_FILE, "w").write("")
432             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
433             # now it's safe to start the node
434         d.addCallback(_cb)
435
436         def _start(res):
437             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
438         d.addCallback(_start)
439
440         def _cb2(res):
441             out, err, rc_or_sig = res
442             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
443             open(HOTLINE_FILE, "w").write("")
444             self.failUnlessEqual(rc_or_sig, 0, errstr)
445             self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
446             errlines = err.split("\n")
447             self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
448                                                                   and "from pkg_resources import load_entry_point" not in line)], errstr)
449             if err != "":
450                 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
451
452             # the parent (twistd) has exited. However, twistd writes the pid
453             # from the child, not the parent, so we can't expect twistd.pid
454             # to exist quite yet.
455
456             # the node is running, but it might not have made it past the
457             # first reactor turn yet, and if we kill it too early, it won't
458             # remove the twistd.pid file. So wait until it does something
459             # that we know it won't do until after the first turn.
460         d.addCallback(_cb2)
461
462         def _node_has_started():
463             return os.path.exists(PORTNUMFILE)
464         d.addCallback(lambda res: self.poll(_node_has_started))
465
466         # now we can kill it. TODO: On a slow machine, the node might kill
467         # itself before we get a chance too, especially if spawning the
468         # 'tahoe stop' command takes a while.
469         def _stop(res):
470             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
471             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
472         d.addCallback(_stop)
473         return d
474
475     def test_client(self):
476         self.skip_if_cannot_daemonize()
477         basedir = self.workdir("test_client")
478         c1 = os.path.join(basedir, "c1")
479         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
480         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
481         PORTNUMFILE = os.path.join(c1, "client.port")
482
483         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
484         def _cb(res):
485             out, err, rc_or_sig = res
486             self.failUnlessEqual(rc_or_sig, 0)
487             # By writing this file, we get sixty seconds before the client will exit. This insures
488             # that even if the 'stop' command doesn't work (and the test fails), the client should
489             # still terminate.
490             open(HOTLINE_FILE, "w").write("")
491             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
492             # now it's safe to start the node
493         d.addCallback(_cb)
494
495         def _start(res):
496             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
497         d.addCallback(_start)
498
499         def _cb2(res):
500             out, err, rc_or_sig = res
501             open(HOTLINE_FILE, "w").write("")
502             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
503             self.failUnlessEqual(rc_or_sig, 0, errstr)
504             self.failUnlessEqual(out, "", errstr)
505             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
506
507             # the parent (twistd) has exited. However, twistd writes the pid
508             # from the child, not the parent, so we can't expect twistd.pid
509             # to exist quite yet.
510
511             # the node is running, but it might not have made it past the
512             # first reactor turn yet, and if we kill it too early, it won't
513             # remove the twistd.pid file. So wait until it does something
514             # that we know it won't do until after the first turn.
515         d.addCallback(_cb2)
516
517         def _node_has_started():
518             return os.path.exists(PORTNUMFILE)
519         d.addCallback(lambda res: self.poll(_node_has_started))
520
521         def _started(res):
522             open(HOTLINE_FILE, "w").write("")
523             self.failUnless(os.path.exists(TWISTD_PID_FILE))
524             # rm this so we can detect when the second incarnation is ready
525             os.unlink(PORTNUMFILE)
526
527             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
528         d.addCallback(_started)
529
530         def _cb3(res):
531             out, err, rc_or_sig = res
532
533             open(HOTLINE_FILE, "w").write("")
534             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
535             self.failUnlessEqual(rc_or_sig, 0, errstr)
536             self.failUnlessEqual(out, "", errstr)
537             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
538         d.addCallback(_cb3)
539
540         # again, the second incarnation of the node might not be ready yet,
541         # so poll until it is
542         d.addCallback(lambda res: self.poll(_node_has_started))
543
544         # now we can kill it. TODO: On a slow machine, the node might kill
545         # itself before we get a chance too, especially if spawning the
546         # 'tahoe stop' command takes a while.
547         def _stop(res):
548             open(HOTLINE_FILE, "w").write("")
549             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
550             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
551         d.addCallback(_stop)
552
553         def _cb4(res):
554             out, err, rc_or_sig = res
555
556             open(HOTLINE_FILE, "w").write("")
557             # the parent has exited by now
558             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
559             self.failUnlessEqual(rc_or_sig, 0, errstr)
560             self.failUnlessEqual(out, "", errstr)
561             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
562             # the parent was supposed to poll and wait until it sees
563             # twistd.pid go away before it exits, so twistd.pid should be
564             # gone by now.
565             self.failIf(os.path.exists(TWISTD_PID_FILE))
566         d.addCallback(_cb4)
567         def _remove_hotline(res):
568             os.unlink(HOTLINE_FILE)
569             return res
570         d.addBoth(_remove_hotline)
571         return d
572
573     def test_baddir(self):
574         self.skip_if_cannot_daemonize()
575         basedir = self.workdir("test_baddir")
576         fileutil.make_dirs(basedir)
577
578         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
579         def _cb(res):
580             out, err, rc_or_sig = res
581             self.failUnlessEqual(rc_or_sig, 1)
582             self.failUnless("does not look like a node directory" in err, err)
583         d.addCallback(_cb)
584
585         def _then_stop_it(res):
586             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
587         d.addCallback(_then_stop_it)
588
589         def _cb2(res):
590             out, err, rc_or_sig = res
591             self.failUnlessEqual(rc_or_sig, 2)
592             self.failUnless("does not look like a running node directory" in err)
593         d.addCallback(_cb2)
594
595         def _then_start_in_bogus_basedir(res):
596             not_a_dir = os.path.join(basedir, "bogus")
597             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
598         d.addCallback(_then_start_in_bogus_basedir)
599
600         def _cb3(res):
601             out, err, rc_or_sig = res
602             self.failUnlessEqual(rc_or_sig, 1)
603             self.failUnless("does not look like a directory at all" in err, err)
604         d.addCallback(_cb3)
605         return d
606
607     def test_keygen(self):
608         self.skip_if_cannot_daemonize()
609         basedir = self.workdir("test_keygen")
610         c1 = os.path.join(basedir, "c1")
611         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
612         KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
613
614         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
615         def _cb(res):
616             out, err, rc_or_sig = res
617             self.failUnlessEqual(rc_or_sig, 0)
618         d.addCallback(_cb)
619
620         def _start(res):
621             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
622         d.addCallback(_start)
623
624         def _cb2(res):
625             out, err, rc_or_sig = res
626             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
627             self.failUnlessEqual(rc_or_sig, 0, errstr)
628             self.failUnlessEqual(out, "", errstr)
629             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
630
631             # the parent (twistd) has exited. However, twistd writes the pid
632             # from the child, not the parent, so we can't expect twistd.pid
633             # to exist quite yet.
634
635             # the node is running, but it might not have made it past the
636             # first reactor turn yet, and if we kill it too early, it won't
637             # remove the twistd.pid file. So wait until it does something
638             # that we know it won't do until after the first turn.
639         d.addCallback(_cb2)
640
641         def _node_has_started():
642             return os.path.exists(KEYGEN_FURL_FILE)
643         d.addCallback(lambda res: self.poll(_node_has_started))
644
645         def _started(res):
646             self.failUnless(os.path.exists(TWISTD_PID_FILE))
647             # rm this so we can detect when the second incarnation is ready
648             os.unlink(KEYGEN_FURL_FILE)
649             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
650         d.addCallback(_started)
651
652         def _cb3(res):
653             out, err, rc_or_sig = res
654             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
655             self.failUnlessEqual(rc_or_sig, 0, errstr)
656             self.failUnlessEqual(out, "", errstr)
657             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
658         d.addCallback(_cb3)
659
660         # again, the second incarnation of the node might not be ready yet,
661         # so poll until it is
662         d.addCallback(lambda res: self.poll(_node_has_started))
663
664         # now we can kill it. TODO: On a slow machine, the node might kill
665         # itself before we get a chance too, especially if spawning the
666         # 'tahoe stop' command takes a while.
667         def _stop(res):
668             self.failUnless(os.path.exists(TWISTD_PID_FILE))
669             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
670         d.addCallback(_stop)
671
672         def _cb4(res):
673             out, err, rc_or_sig = res
674             # the parent has exited by now
675             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
676             self.failUnlessEqual(rc_or_sig, 0, errstr)
677             self.failUnlessEqual(out, "", errstr)
678             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
679             # the parent was supposed to poll and wait until it sees
680             # twistd.pid go away before it exits, so twistd.pid should be
681             # gone by now.
682             self.failIf(os.path.exists(TWISTD_PID_FILE))
683         d.addCallback(_cb4)
684         return d