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
162 and "from pkg_resources import load_entry_point" not in line)], errstr)
164 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
169 class CreateNode(unittest.TestCase):
170 # exercise "tahoe create-node", create-introducer,
171 # create-key-generator, and create-stats-gatherer, by calling the
172 # corresponding code as a subroutine.
174 def workdir(self, name):
175 basedir = os.path.join("test_runner", "CreateNode", name)
176 fileutil.make_dirs(basedir)
179 def run_tahoe(self, argv):
180 out,err = StringIO(), StringIO()
181 rc = runner.runner(argv, stdout=out, stderr=err)
182 return rc, out.getvalue(), err.getvalue()
184 def do_create(self, kind):
185 basedir = self.workdir("test_" + kind)
186 command = "create-" + kind
187 is_client = kind in ("node", "client")
188 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
190 n1 = os.path.join(basedir, command + "-n1")
191 argv = ["--quiet", command, "--basedir", n1]
192 rc, out, err = self.run_tahoe(argv)
193 self.failUnlessEqual(err, "")
194 self.failUnlessEqual(out, "")
195 self.failUnlessEqual(rc, 0)
196 self.failUnless(os.path.exists(n1))
197 self.failUnless(os.path.exists(os.path.join(n1, tac)))
200 # tahoe.cfg should exist, and should have storage enabled for
201 # 'create-node', and disabled for 'create-client'.
202 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
203 self.failUnless(os.path.exists(tahoe_cfg))
204 content = open(tahoe_cfg).read()
206 self.failUnless("\n[storage]\nenabled = false\n" in content)
208 self.failUnless("\n[storage]\nenabled = true\n" in content)
210 # creating the node a second time should be rejected
211 rc, out, err = self.run_tahoe(argv)
212 self.failIfEqual(rc, 0, str((out, err, rc)))
213 self.failUnlessEqual(out, "")
214 self.failUnless("is not empty." in err)
216 # Fail if there is a non-empty line that doesn't end with a
218 for line in err.splitlines():
219 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
221 # test that the non --basedir form works too
222 n2 = os.path.join(basedir, command + "-n2")
223 argv = ["--quiet", command, n2]
224 rc, out, err = self.run_tahoe(argv)
225 self.failUnlessEqual(err, "")
226 self.failUnlessEqual(out, "")
227 self.failUnlessEqual(rc, 0)
228 self.failUnless(os.path.exists(n2))
229 self.failUnless(os.path.exists(os.path.join(n2, tac)))
231 # test the --node-directory form
232 n3 = os.path.join(basedir, command + "-n3")
233 argv = ["--quiet", command, "--node-directory", n3]
234 rc, out, err = self.run_tahoe(argv)
235 self.failUnlessEqual(err, "")
236 self.failUnlessEqual(out, "")
237 self.failUnlessEqual(rc, 0)
238 self.failUnless(os.path.exists(n3))
239 self.failUnless(os.path.exists(os.path.join(n3, tac)))
241 # test the --multiple form
242 n4 = os.path.join(basedir, command + "-n4")
243 n5 = os.path.join(basedir, command + "-n5")
244 argv = ["--quiet", command, "--multiple", n4, n5]
245 rc, out, err = self.run_tahoe(argv)
246 self.failUnlessEqual(err, "")
247 self.failUnlessEqual(out, "")
248 self.failUnlessEqual(rc, 0)
249 self.failUnless(os.path.exists(n4))
250 self.failUnless(os.path.exists(os.path.join(n4, tac)))
251 self.failUnless(os.path.exists(n5))
252 self.failUnless(os.path.exists(os.path.join(n5, tac)))
254 # make sure it rejects too many arguments without --multiple
255 argv = [command, "basedir", "extraarg"]
256 self.failUnlessRaises(usage.UsageError,
260 # when creating a non-client, there is no default for the basedir
263 self.failUnlessRaises(usage.UsageError,
269 self.do_create("node")
271 def test_client(self):
272 # create-client should behave like create-node --no-storage.
273 self.do_create("client")
275 def test_introducer(self):
276 self.do_create("introducer")
278 def test_key_generator(self):
279 self.do_create("key-generator")
281 def test_stats_gatherer(self):
282 self.do_create("stats-gatherer")
284 def test_subcommands(self):
285 # no arguments should trigger a command listing, via UsageError
286 self.failUnlessRaises(usage.UsageError,
292 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
294 # exercise "tahoe start", for both introducer, client node, and
295 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
296 # get us figleaf-based line-level coverage, but it does a better job of
297 # confirming that the user can actually run "./bin/tahoe start" and
298 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
299 # the like correctly.
301 # This doesn't work on cygwin (it hangs forever), so we skip this test
302 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
303 # work on cygwin: twisted seems unable to provide a version of
304 # spawnProcess which really works there.
306 def workdir(self, name):
307 basedir = os.path.join("test_runner", "RunNode", name)
308 fileutil.make_dirs(basedir)
311 def test_introducer(self):
312 self.skip_if_cannot_daemonize()
313 basedir = self.workdir("test_introducer")
314 c1 = os.path.join(basedir, "c1")
315 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
316 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
317 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
319 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
321 out, err, rc_or_sig = res
322 self.failUnlessEqual(rc_or_sig, 0)
323 # by writing this file, we get ten seconds before the node will
324 # exit. This insures that even if the test fails (and the 'stop'
325 # command doesn't work), the client should still terminate.
326 open(HOTLINE_FILE, "w").write("")
327 # now it's safe to start the node
330 def _then_start_the_node(res):
331 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
332 d.addCallback(_then_start_the_node)
335 out, err, rc_or_sig = res
337 open(HOTLINE_FILE, "w").write("")
338 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
339 self.failUnlessEqual(rc_or_sig, 0, errstr)
340 self.failUnlessEqual(out, "", errstr)
341 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
343 # the parent (twistd) has exited. However, twistd writes the pid
344 # from the child, not the parent, so we can't expect twistd.pid
345 # to exist quite yet.
347 # the node is running, but it might not have made it past the
348 # first reactor turn yet, and if we kill it too early, it won't
349 # remove the twistd.pid file. So wait until it does something
350 # that we know it won't do until after the first turn.
353 def _node_has_started():
354 return os.path.exists(INTRODUCER_FURL_FILE)
355 d.addCallback(lambda res: self.poll(_node_has_started))
358 open(HOTLINE_FILE, "w").write("")
359 self.failUnless(os.path.exists(TWISTD_PID_FILE))
360 # rm this so we can detect when the second incarnation is ready
361 os.unlink(INTRODUCER_FURL_FILE)
362 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
363 d.addCallback(_started)
366 out, err, rc_or_sig = res
367 open(HOTLINE_FILE, "w").write("")
368 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
369 self.failUnlessEqual(rc_or_sig, 0, errstr)
370 self.failUnlessEqual(out, "", errstr)
371 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
374 # again, the second incarnation of the node might not be ready yet,
375 # so poll until it is
376 d.addCallback(lambda res: self.poll(_node_has_started))
378 # now we can kill it. TODO: On a slow machine, the node might kill
379 # itself before we get a chance too, especially if spawning the
380 # 'tahoe stop' command takes a while.
382 open(HOTLINE_FILE, "w").write("")
383 self.failUnless(os.path.exists(TWISTD_PID_FILE))
385 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
388 def _after_stopping(res):
389 out, err, rc_or_sig = res
390 open(HOTLINE_FILE, "w").write("")
391 # the parent has exited by now
392 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
393 self.failUnlessEqual(rc_or_sig, 0, errstr)
394 self.failUnlessEqual(out, "", errstr)
395 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
396 # the parent was supposed to poll and wait until it sees
397 # twistd.pid go away before it exits, so twistd.pid should be
399 self.failIf(os.path.exists(TWISTD_PID_FILE))
400 d.addCallback(_after_stopping)
402 def _remove_hotline(res):
403 os.unlink(HOTLINE_FILE)
405 d.addBoth(_remove_hotline)
407 test_introducer.timeout = 480 # This hit the 120-second timeout on "François Lenny-armv5tel", then it hit a 240-second timeout on our feisty2.5 buildslave: http://allmydata.org/buildbot/builders/feisty2.5/builds/2381/steps/test/logs/test.log
409 def test_client_no_noise(self):
410 self.skip_if_cannot_daemonize()
413 pkg_resources.require("Twisted>=9.0.0")
414 except pkg_resources.VersionConflict:
415 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
416 basedir = self.workdir("test_client_no_noise")
417 c1 = os.path.join(basedir, "c1")
418 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
419 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
420 PORTNUMFILE = os.path.join(c1, "client.port")
422 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
424 out, err, rc_or_sig = res
425 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
426 assert rc_or_sig == 0, errstr
427 self.failUnlessEqual(rc_or_sig, 0)
428 # By writing this file, we get forty seconds before the client will exit. This insures
429 # that even if the 'stop' command doesn't work (and the test fails), the client should
431 open(HOTLINE_FILE, "w").write("")
432 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
433 # now it's safe to start the node
437 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
438 d.addCallback(_start)
441 out, err, rc_or_sig = res
442 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
443 open(HOTLINE_FILE, "w").write("")
444 self.failUnlessEqual(rc_or_sig, 0, errstr)
445 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
446 errlines = err.split("\n")
447 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
448 and "from pkg_resources import load_entry_point" not in line)], errstr)
450 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
452 # the parent (twistd) has exited. However, twistd writes the pid
453 # from the child, not the parent, so we can't expect twistd.pid
454 # to exist quite yet.
456 # the node is running, but it might not have made it past the
457 # first reactor turn yet, and if we kill it too early, it won't
458 # remove the twistd.pid file. So wait until it does something
459 # that we know it won't do until after the first turn.
462 def _node_has_started():
463 return os.path.exists(PORTNUMFILE)
464 d.addCallback(lambda res: self.poll(_node_has_started))
466 # now we can kill it. TODO: On a slow machine, the node might kill
467 # itself before we get a chance too, especially if spawning the
468 # 'tahoe stop' command takes a while.
470 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
471 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
475 def test_client(self):
476 self.skip_if_cannot_daemonize()
477 basedir = self.workdir("test_client")
478 c1 = os.path.join(basedir, "c1")
479 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
480 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
481 PORTNUMFILE = os.path.join(c1, "client.port")
483 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
485 out, err, rc_or_sig = res
486 self.failUnlessEqual(rc_or_sig, 0)
487 # By writing this file, we get sixty seconds before the client will exit. This insures
488 # that even if the 'stop' command doesn't work (and the test fails), the client should
490 open(HOTLINE_FILE, "w").write("")
491 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
492 # now it's safe to start the node
496 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
497 d.addCallback(_start)
500 out, err, rc_or_sig = res
501 open(HOTLINE_FILE, "w").write("")
502 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
503 self.failUnlessEqual(rc_or_sig, 0, errstr)
504 self.failUnlessEqual(out, "", errstr)
505 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
507 # the parent (twistd) has exited. However, twistd writes the pid
508 # from the child, not the parent, so we can't expect twistd.pid
509 # to exist quite yet.
511 # the node is running, but it might not have made it past the
512 # first reactor turn yet, and if we kill it too early, it won't
513 # remove the twistd.pid file. So wait until it does something
514 # that we know it won't do until after the first turn.
517 def _node_has_started():
518 return os.path.exists(PORTNUMFILE)
519 d.addCallback(lambda res: self.poll(_node_has_started))
522 open(HOTLINE_FILE, "w").write("")
523 self.failUnless(os.path.exists(TWISTD_PID_FILE))
524 # rm this so we can detect when the second incarnation is ready
525 os.unlink(PORTNUMFILE)
527 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
528 d.addCallback(_started)
531 out, err, rc_or_sig = res
533 open(HOTLINE_FILE, "w").write("")
534 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
535 self.failUnlessEqual(rc_or_sig, 0, errstr)
536 self.failUnlessEqual(out, "", errstr)
537 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
540 # again, the second incarnation of the node might not be ready yet,
541 # so poll until it is
542 d.addCallback(lambda res: self.poll(_node_has_started))
544 # now we can kill it. TODO: On a slow machine, the node might kill
545 # itself before we get a chance too, especially if spawning the
546 # 'tahoe stop' command takes a while.
548 open(HOTLINE_FILE, "w").write("")
549 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
550 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
554 out, err, rc_or_sig = res
556 open(HOTLINE_FILE, "w").write("")
557 # the parent has exited by now
558 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
559 self.failUnlessEqual(rc_or_sig, 0, errstr)
560 self.failUnlessEqual(out, "", errstr)
561 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
562 # the parent was supposed to poll and wait until it sees
563 # twistd.pid go away before it exits, so twistd.pid should be
565 self.failIf(os.path.exists(TWISTD_PID_FILE))
567 def _remove_hotline(res):
568 os.unlink(HOTLINE_FILE)
570 d.addBoth(_remove_hotline)
573 def test_baddir(self):
574 self.skip_if_cannot_daemonize()
575 basedir = self.workdir("test_baddir")
576 fileutil.make_dirs(basedir)
578 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
580 out, err, rc_or_sig = res
581 self.failUnlessEqual(rc_or_sig, 1)
582 self.failUnless("does not look like a node directory" in err, err)
585 def _then_stop_it(res):
586 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
587 d.addCallback(_then_stop_it)
590 out, err, rc_or_sig = res
591 self.failUnlessEqual(rc_or_sig, 2)
592 self.failUnless("does not look like a running node directory" in err)
595 def _then_start_in_bogus_basedir(res):
596 not_a_dir = os.path.join(basedir, "bogus")
597 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
598 d.addCallback(_then_start_in_bogus_basedir)
601 out, err, rc_or_sig = res
602 self.failUnlessEqual(rc_or_sig, 1)
603 self.failUnless("does not look like a directory at all" in err, err)
607 def test_keygen(self):
608 self.skip_if_cannot_daemonize()
609 basedir = self.workdir("test_keygen")
610 c1 = os.path.join(basedir, "c1")
611 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
612 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
614 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
616 out, err, rc_or_sig = res
617 self.failUnlessEqual(rc_or_sig, 0)
621 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
622 d.addCallback(_start)
625 out, err, rc_or_sig = res
626 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
627 self.failUnlessEqual(rc_or_sig, 0, errstr)
628 self.failUnlessEqual(out, "", errstr)
629 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
631 # the parent (twistd) has exited. However, twistd writes the pid
632 # from the child, not the parent, so we can't expect twistd.pid
633 # to exist quite yet.
635 # the node is running, but it might not have made it past the
636 # first reactor turn yet, and if we kill it too early, it won't
637 # remove the twistd.pid file. So wait until it does something
638 # that we know it won't do until after the first turn.
641 def _node_has_started():
642 return os.path.exists(KEYGEN_FURL_FILE)
643 d.addCallback(lambda res: self.poll(_node_has_started))
646 self.failUnless(os.path.exists(TWISTD_PID_FILE))
647 # rm this so we can detect when the second incarnation is ready
648 os.unlink(KEYGEN_FURL_FILE)
649 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
650 d.addCallback(_started)
653 out, err, rc_or_sig = res
654 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
655 self.failUnlessEqual(rc_or_sig, 0, errstr)
656 self.failUnlessEqual(out, "", errstr)
657 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
660 # again, the second incarnation of the node might not be ready yet,
661 # so poll until it is
662 d.addCallback(lambda res: self.poll(_node_has_started))
664 # now we can kill it. TODO: On a slow machine, the node might kill
665 # itself before we get a chance too, especially if spawning the
666 # 'tahoe stop' command takes a while.
668 self.failUnless(os.path.exists(TWISTD_PID_FILE))
669 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
673 out, err, rc_or_sig = res
674 # the parent has exited by now
675 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
676 self.failUnlessEqual(rc_or_sig, 0, errstr)
677 self.failUnlessEqual(out, "", errstr)
678 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
679 # the parent was supposed to poll and wait until it sees
680 # twistd.pid go away before it exits, so twistd.pid should be
682 self.failIf(os.path.exists(TWISTD_PID_FILE))