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