1 # -*- coding: utf-8 -*-
3 from twisted.trial import unittest
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
13 from allmydata.test import common_util
18 srcfile = allmydata.__file__
19 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
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)
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
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,))
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")
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)
55 same = (root_from_cwd == rootdir)
58 same = os.path.samefile(root_from_cwd, rootdir)
59 except AttributeError, e:
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)
74 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
78 self.skip_if_cannot_run_bintahoe()
79 d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
81 out, err, rc_or_sig = res
82 self.failUnlessEqual(rc_or_sig, 0, str(res))
84 # Fail unless the allmydata-tahoe package is *this* version *and*
85 # was loaded from *this* source directory.
87 verstr = str(allmydata.__version__)
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.")
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:
98 ix = verstr.rfind('-r')
100 altverstr = verstr[:ix] + '.post' + verstr[ix+2:]
102 ix = verstr.rfind('.post')
104 altverstr = verstr[:ix] + '-r' + verstr[ix+5:]
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)
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))
115 def test_unicode_arguments_and_output(self):
116 self.skip_if_cannot_run_bintahoe()
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.")
125 d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ)
127 out, err, rc_or_sig = res
128 self.failUnlessEqual(rc_or_sig, 1, str(res))
129 self.failUnlessIn("Unknown command: "+tricky_out, out)
133 def test_run_with_python_options(self):
134 self.skip_if_cannot_run_bintahoe()
136 # -t is a harmless option that warns about tabs.
137 d = utils.getProcessOutputAndValue(sys.executable, args=['-t', bintahoe, '--version'],
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))
146 def test_version_no_noise(self):
147 self.skip_if_cannot_run_bintahoe()
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")
154 d = utils.getProcessOutputAndValue(bintahoe, args=["--version"], env=os.environ)
156 out, err, rc_or_sig = res
157 self.failUnlessEqual(rc_or_sig, 0, str(res))
158 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
159 self.failIfIn("DeprecationWarning", out, str(res))
160 errlines = err.split("\n")
161 self.failIf([True for line in errlines if line != "" and ("UserWarning: Unbuilt egg for setuptools" not in line)], str(res))
163 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
168 class CreateNode(unittest.TestCase):
169 # exercise "tahoe create-node", create-introducer,
170 # create-key-generator, and create-stats-gatherer, by calling the
171 # corresponding code as a subroutine.
173 def workdir(self, name):
174 basedir = os.path.join("test_runner", "CreateNode", name)
175 fileutil.make_dirs(basedir)
178 def run_tahoe(self, argv):
179 out,err = StringIO(), StringIO()
180 rc = runner.runner(argv, stdout=out, stderr=err)
181 return rc, out.getvalue(), err.getvalue()
183 def do_create(self, kind):
184 basedir = self.workdir("test_" + kind)
185 command = "create-" + kind
186 is_client = kind in ("node", "client")
187 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
189 n1 = os.path.join(basedir, command + "-n1")
190 argv = ["--quiet", command, "--basedir", n1]
191 rc, out, err = self.run_tahoe(argv)
192 self.failUnlessEqual(err, "")
193 self.failUnlessEqual(out, "")
194 self.failUnlessEqual(rc, 0)
195 self.failUnless(os.path.exists(n1))
196 self.failUnless(os.path.exists(os.path.join(n1, tac)))
199 # tahoe.cfg should exist, and should have storage enabled for
200 # 'create-node', and disabled for 'create-client'.
201 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
202 self.failUnless(os.path.exists(tahoe_cfg))
203 content = open(tahoe_cfg).read()
205 self.failUnless("\n[storage]\nenabled = false\n" in content)
207 self.failUnless("\n[storage]\nenabled = true\n" in content)
209 # creating the node a second time should be rejected
210 rc, out, err = self.run_tahoe(argv)
211 self.failIfEqual(rc, 0, str((out, err, rc)))
212 self.failUnlessEqual(out, "")
213 self.failUnless("is not empty." in err)
215 # Fail if there is a non-empty line that doesn't end with a
217 for line in err.splitlines():
218 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
220 # test that the non --basedir form works too
221 n2 = os.path.join(basedir, command + "-n2")
222 argv = ["--quiet", command, n2]
223 rc, out, err = self.run_tahoe(argv)
224 self.failUnlessEqual(err, "")
225 self.failUnlessEqual(out, "")
226 self.failUnlessEqual(rc, 0)
227 self.failUnless(os.path.exists(n2))
228 self.failUnless(os.path.exists(os.path.join(n2, tac)))
230 # test the --node-directory form
231 n3 = os.path.join(basedir, command + "-n3")
232 argv = ["--quiet", command, "--node-directory", n3]
233 rc, out, err = self.run_tahoe(argv)
234 self.failUnlessEqual(err, "")
235 self.failUnlessEqual(out, "")
236 self.failUnlessEqual(rc, 0)
237 self.failUnless(os.path.exists(n3))
238 self.failUnless(os.path.exists(os.path.join(n3, tac)))
240 # test the --multiple form
241 n4 = os.path.join(basedir, command + "-n4")
242 n5 = os.path.join(basedir, command + "-n5")
243 argv = ["--quiet", command, "--multiple", n4, n5]
244 rc, out, err = self.run_tahoe(argv)
245 self.failUnlessEqual(err, "")
246 self.failUnlessEqual(out, "")
247 self.failUnlessEqual(rc, 0)
248 self.failUnless(os.path.exists(n4))
249 self.failUnless(os.path.exists(os.path.join(n4, tac)))
250 self.failUnless(os.path.exists(n5))
251 self.failUnless(os.path.exists(os.path.join(n5, tac)))
253 # make sure it rejects too many arguments without --multiple
254 argv = [command, "basedir", "extraarg"]
255 self.failUnlessRaises(usage.UsageError,
259 # when creating a non-client, there is no default for the basedir
262 self.failUnlessRaises(usage.UsageError,
268 self.do_create("node")
270 def test_client(self):
271 # create-client should behave like create-node --no-storage.
272 self.do_create("client")
274 def test_introducer(self):
275 self.do_create("introducer")
277 def test_key_generator(self):
278 self.do_create("key-generator")
280 def test_stats_gatherer(self):
281 self.do_create("stats-gatherer")
283 def test_subcommands(self):
284 # no arguments should trigger a command listing, via UsageError
285 self.failUnlessRaises(usage.UsageError,
291 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
293 # exercise "tahoe start", for both introducer, client node, and
294 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
295 # get us figleaf-based line-level coverage, but it does a better job of
296 # confirming that the user can actually run "./bin/tahoe start" and
297 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
298 # the like correctly.
300 # This doesn't work on cygwin (it hangs forever), so we skip this test
301 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
302 # work on cygwin: twisted seems unable to provide a version of
303 # spawnProcess which really works there.
305 def workdir(self, name):
306 basedir = os.path.join("test_runner", "RunNode", name)
307 fileutil.make_dirs(basedir)
310 def test_introducer(self):
311 self.skip_if_cannot_daemonize()
312 basedir = self.workdir("test_introducer")
313 c1 = os.path.join(basedir, "c1")
314 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
315 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
316 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
318 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
320 out, err, rc_or_sig = res
321 self.failUnlessEqual(rc_or_sig, 0)
322 # by writing this file, we get ten seconds before the node will
323 # exit. This insures that even if the test fails (and the 'stop'
324 # command doesn't work), the client should still terminate.
325 open(HOTLINE_FILE, "w").write("")
326 # now it's safe to start the node
329 def _then_start_the_node(res):
330 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
331 d.addCallback(_then_start_the_node)
334 out, err, rc_or_sig = res
336 open(HOTLINE_FILE, "w").write("")
337 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
338 self.failUnlessEqual(rc_or_sig, 0, errstr)
339 self.failUnlessEqual(out, "", errstr)
340 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
342 # the parent (twistd) has exited. However, twistd writes the pid
343 # from the child, not the parent, so we can't expect twistd.pid
344 # to exist quite yet.
346 # the node is running, but it might not have made it past the
347 # first reactor turn yet, and if we kill it too early, it won't
348 # remove the twistd.pid file. So wait until it does something
349 # that we know it won't do until after the first turn.
352 def _node_has_started():
353 return os.path.exists(INTRODUCER_FURL_FILE)
354 d.addCallback(lambda res: self.poll(_node_has_started))
357 open(HOTLINE_FILE, "w").write("")
358 self.failUnless(os.path.exists(TWISTD_PID_FILE))
359 # rm this so we can detect when the second incarnation is ready
360 os.unlink(INTRODUCER_FURL_FILE)
361 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
362 d.addCallback(_started)
365 out, err, rc_or_sig = res
366 open(HOTLINE_FILE, "w").write("")
367 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
368 self.failUnlessEqual(rc_or_sig, 0, errstr)
369 self.failUnlessEqual(out, "", errstr)
370 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
373 # again, the second incarnation of the node might not be ready yet,
374 # so poll until it is
375 d.addCallback(lambda res: self.poll(_node_has_started))
377 # now we can kill it. TODO: On a slow machine, the node might kill
378 # itself before we get a chance too, especially if spawning the
379 # 'tahoe stop' command takes a while.
381 open(HOTLINE_FILE, "w").write("")
382 self.failUnless(os.path.exists(TWISTD_PID_FILE))
384 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
387 def _after_stopping(res):
388 out, err, rc_or_sig = res
389 open(HOTLINE_FILE, "w").write("")
390 # the parent has exited by now
391 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
392 self.failUnlessEqual(rc_or_sig, 0, errstr)
393 self.failUnlessEqual(out, "", errstr)
394 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
395 # the parent was supposed to poll and wait until it sees
396 # twistd.pid go away before it exits, so twistd.pid should be
398 self.failIf(os.path.exists(TWISTD_PID_FILE))
399 d.addCallback(_after_stopping)
401 def _remove_hotline(res):
402 os.unlink(HOTLINE_FILE)
404 d.addBoth(_remove_hotline)
406 test_introducer.timeout = 480 # This hit the 120-second timeout on "François Lenny-armv5tel", then it hit a 240-second timeout on our feisty2.5 buildslave: http://allmydata.org/buildbot/builders/feisty2.5/builds/2381/steps/test/logs/test.log
408 def test_client_no_noise(self):
409 self.skip_if_cannot_daemonize()
412 pkg_resources.require("Twisted>=9.0.0")
413 except pkg_resources.VersionConflict:
414 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
415 basedir = self.workdir("test_client_no_noise")
416 c1 = os.path.join(basedir, "c1")
417 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
418 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
419 PORTNUMFILE = os.path.join(c1, "client.port")
421 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
423 out, err, rc_or_sig = res
424 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
425 assert rc_or_sig == 0, errstr
426 self.failUnlessEqual(rc_or_sig, 0)
427 # By writing this file, we get forty seconds before the client will exit. This insures
428 # that even if the 'stop' command doesn't work (and the test fails), the client should
430 open(HOTLINE_FILE, "w").write("")
431 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
432 # now it's safe to start the node
436 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
437 d.addCallback(_start)
440 out, err, rc_or_sig = res
441 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
442 open(HOTLINE_FILE, "w").write("")
443 self.failUnlessEqual(rc_or_sig, 0, errstr)
444 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
445 errlines = err.split("\n")
446 self.failIf([True for line in errlines if line != "" and ("UserWarning: Unbuilt egg for setuptools" not in line)], errstr)
448 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
450 # the parent (twistd) has exited. However, twistd writes the pid
451 # from the child, not the parent, so we can't expect twistd.pid
452 # to exist quite yet.
454 # the node is running, but it might not have made it past the
455 # first reactor turn yet, and if we kill it too early, it won't
456 # remove the twistd.pid file. So wait until it does something
457 # that we know it won't do until after the first turn.
460 def _node_has_started():
461 return os.path.exists(PORTNUMFILE)
462 d.addCallback(lambda res: self.poll(_node_has_started))
464 # now we can kill it. TODO: On a slow machine, the node might kill
465 # itself before we get a chance too, especially if spawning the
466 # 'tahoe stop' command takes a while.
468 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
469 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
473 def test_client(self):
474 self.skip_if_cannot_daemonize()
475 basedir = self.workdir("test_client")
476 c1 = os.path.join(basedir, "c1")
477 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
478 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
479 PORTNUMFILE = os.path.join(c1, "client.port")
481 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
483 out, err, rc_or_sig = res
484 self.failUnlessEqual(rc_or_sig, 0)
485 # By writing this file, we get sixty seconds before the client will exit. This insures
486 # that even if the 'stop' command doesn't work (and the test fails), the client should
488 open(HOTLINE_FILE, "w").write("")
489 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
490 # now it's safe to start the node
494 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
495 d.addCallback(_start)
498 out, err, rc_or_sig = res
499 open(HOTLINE_FILE, "w").write("")
500 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
501 self.failUnlessEqual(rc_or_sig, 0, errstr)
502 self.failUnlessEqual(out, "", errstr)
503 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
505 # the parent (twistd) has exited. However, twistd writes the pid
506 # from the child, not the parent, so we can't expect twistd.pid
507 # to exist quite yet.
509 # the node is running, but it might not have made it past the
510 # first reactor turn yet, and if we kill it too early, it won't
511 # remove the twistd.pid file. So wait until it does something
512 # that we know it won't do until after the first turn.
515 def _node_has_started():
516 return os.path.exists(PORTNUMFILE)
517 d.addCallback(lambda res: self.poll(_node_has_started))
520 open(HOTLINE_FILE, "w").write("")
521 self.failUnless(os.path.exists(TWISTD_PID_FILE))
522 # rm this so we can detect when the second incarnation is ready
523 os.unlink(PORTNUMFILE)
525 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
526 d.addCallback(_started)
529 out, err, rc_or_sig = res
531 open(HOTLINE_FILE, "w").write("")
532 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
533 self.failUnlessEqual(rc_or_sig, 0, errstr)
534 self.failUnlessEqual(out, "", errstr)
535 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
538 # again, the second incarnation of the node might not be ready yet,
539 # so poll until it is
540 d.addCallback(lambda res: self.poll(_node_has_started))
542 # now we can kill it. TODO: On a slow machine, the node might kill
543 # itself before we get a chance too, especially if spawning the
544 # 'tahoe stop' command takes a while.
546 open(HOTLINE_FILE, "w").write("")
547 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
548 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
552 out, err, rc_or_sig = res
554 open(HOTLINE_FILE, "w").write("")
555 # the parent has exited by now
556 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
557 self.failUnlessEqual(rc_or_sig, 0, errstr)
558 self.failUnlessEqual(out, "", errstr)
559 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
560 # the parent was supposed to poll and wait until it sees
561 # twistd.pid go away before it exits, so twistd.pid should be
563 self.failIf(os.path.exists(TWISTD_PID_FILE))
565 def _remove_hotline(res):
566 os.unlink(HOTLINE_FILE)
568 d.addBoth(_remove_hotline)
571 def test_baddir(self):
572 self.skip_if_cannot_daemonize()
573 basedir = self.workdir("test_baddir")
574 fileutil.make_dirs(basedir)
576 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
578 out, err, rc_or_sig = res
579 self.failUnlessEqual(rc_or_sig, 1)
580 self.failUnless("does not look like a node directory" in err, err)
583 def _then_stop_it(res):
584 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
585 d.addCallback(_then_stop_it)
588 out, err, rc_or_sig = res
589 self.failUnlessEqual(rc_or_sig, 2)
590 self.failUnless("does not look like a running node directory" in err)
593 def _then_start_in_bogus_basedir(res):
594 not_a_dir = os.path.join(basedir, "bogus")
595 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
596 d.addCallback(_then_start_in_bogus_basedir)
599 out, err, rc_or_sig = res
600 self.failUnlessEqual(rc_or_sig, 1)
601 self.failUnless("does not look like a directory at all" in err, err)
605 def test_keygen(self):
606 self.skip_if_cannot_daemonize()
607 basedir = self.workdir("test_keygen")
608 c1 = os.path.join(basedir, "c1")
609 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
610 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
612 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
614 out, err, rc_or_sig = res
615 self.failUnlessEqual(rc_or_sig, 0)
619 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
620 d.addCallback(_start)
623 out, err, rc_or_sig = res
624 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
625 self.failUnlessEqual(rc_or_sig, 0, errstr)
626 self.failUnlessEqual(out, "", errstr)
627 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
629 # the parent (twistd) has exited. However, twistd writes the pid
630 # from the child, not the parent, so we can't expect twistd.pid
631 # to exist quite yet.
633 # the node is running, but it might not have made it past the
634 # first reactor turn yet, and if we kill it too early, it won't
635 # remove the twistd.pid file. So wait until it does something
636 # that we know it won't do until after the first turn.
639 def _node_has_started():
640 return os.path.exists(KEYGEN_FURL_FILE)
641 d.addCallback(lambda res: self.poll(_node_has_started))
644 self.failUnless(os.path.exists(TWISTD_PID_FILE))
645 # rm this so we can detect when the second incarnation is ready
646 os.unlink(KEYGEN_FURL_FILE)
647 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
648 d.addCallback(_started)
651 out, err, rc_or_sig = res
652 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
653 self.failUnlessEqual(rc_or_sig, 0, errstr)
654 self.failUnlessEqual(out, "", errstr)
655 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
658 # again, the second incarnation of the node might not be ready yet,
659 # so poll until it is
660 d.addCallback(lambda res: self.poll(_node_has_started))
662 # now we can kill it. TODO: On a slow machine, the node might kill
663 # itself before we get a chance too, especially if spawning the
664 # 'tahoe stop' command takes a while.
666 self.failUnless(os.path.exists(TWISTD_PID_FILE))
667 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
671 out, err, rc_or_sig = res
672 # the parent has exited by now
673 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
674 self.failUnlessEqual(rc_or_sig, 0, errstr)
675 self.failUnlessEqual(out, "", errstr)
676 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
677 # the parent was supposed to poll and wait until it sees
678 # twistd.pid go away before it exits, so twistd.pid should be
680 self.failIf(os.path.exists(TWISTD_PID_FILE))