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