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)
26 elif os.path.basename(rootdir) == 'src':
27 rootdir = os.path.dirname(rootdir)
29 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
30 if sys.platform == "win32":
31 bintahoe += ".pyscript"
32 if not os.path.exists(bintahoe):
33 alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
34 if os.path.exists(alt_bintahoe):
35 bintahoe = alt_bintahoe
39 def skip_if_cannot_run_bintahoe(self):
40 if "cygwin" in sys.platform.lower():
41 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.")
42 if not os.path.exists(bintahoe):
43 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,))
45 def skip_if_cannot_daemonize(self):
46 self.skip_if_cannot_run_bintahoe()
47 if runtime.platformType == "win32":
48 # twistd on windows doesn't daemonize. cygwin should work normally.
49 raise unittest.SkipTest("twistd does not fork under windows")
52 class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
53 def test_the_right_code(self):
54 cwd = os.path.normcase(os.path.realpath("."))
55 root_from_cwd = os.path.dirname(cwd)
56 if os.path.basename(root_from_cwd) == 'src':
57 root_from_cwd = os.path.dirname(root_from_cwd)
59 same = (root_from_cwd == rootdir)
62 same = os.path.samefile(root_from_cwd, rootdir)
63 except AttributeError, e:
67 msg = ("We seem to be testing the code at %r,\n"
68 "(according to the source filename %r),\n"
69 "but expected to be testing the code at %r.\n"
70 % (rootdir, srcfile, root_from_cwd))
72 root_from_cwdu = os.path.normcase(os.path.normpath(os.getcwdu()))
73 if os.path.basename(root_from_cwdu) == u'src':
74 root_from_cwdu = os.path.dirname(root_from_cwdu)
76 if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
77 msg += ("However, this may be a false alarm because the current directory path\n"
78 "is not representable in the filesystem encoding. Please run the tests\n"
79 "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
80 raise unittest.SkipTest(msg)
82 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
86 self.skip_if_cannot_run_bintahoe()
87 d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
89 out, err, rc_or_sig = res
90 self.failUnlessEqual(rc_or_sig, 0, str(res))
92 # Fail unless the allmydata-tahoe package is *this* version *and*
93 # was loaded from *this* source directory.
95 verstr = str(allmydata.__version__)
97 self.failIfEqual(verstr, "unknown",
98 "We don't know our version, because this distribution didn't come "
99 "with a _version.py and 'setup.py darcsver' hasn't been run.")
101 # The Python "rational version numbering" convention
102 # disallows "-r$REV" but allows ".post$REV"
103 # instead. Eventually we'll probably move to that. When we
104 # do, this test won't go red:
106 ix = verstr.rfind('-r')
108 altverstr = verstr[:ix] + '.post' + verstr[ix+2:]
110 ix = verstr.rfind('.post')
112 altverstr = verstr[:ix] + '-r' + verstr[ix+5:]
116 required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, verstr, srcdir)
117 alt_required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, altverstr, srcdir)
119 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))
123 def test_unicode_arguments_and_output(self):
124 self.skip_if_cannot_run_bintahoe()
128 tricky_arg = unicode_to_argv(tricky, mangle=True)
129 tricky_out = unicode_to_output(tricky)
130 except UnicodeEncodeError:
131 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
133 d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ)
135 out, err, rc_or_sig = res
136 self.failUnlessEqual(rc_or_sig, 1, str(res))
137 self.failUnlessIn("Unknown command: "+tricky_out, out)
141 def test_run_with_python_options(self):
142 self.skip_if_cannot_run_bintahoe()
144 # -t is a harmless option that warns about tabs.
145 d = utils.getProcessOutputAndValue(sys.executable, args=['-t', bintahoe, '--version'],
148 out, err, rc_or_sig = res
149 self.failUnlessEqual(rc_or_sig, 0, str(res))
150 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
154 def test_version_no_noise(self):
155 self.skip_if_cannot_run_bintahoe()
158 pkg_resources.require("Twisted>=9.0.0")
159 except pkg_resources.VersionConflict:
160 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
162 d = utils.getProcessOutputAndValue(bintahoe, args=["--version"], env=os.environ)
164 out, err, rc_or_sig = res
165 self.failUnlessEqual(rc_or_sig, 0, str(res))
166 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
167 self.failIfIn("DeprecationWarning", out, str(res))
168 errlines = err.split("\n")
169 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
170 and "from pkg_resources import load_entry_point" not in line)], str(res))
172 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
177 class CreateNode(unittest.TestCase):
178 # exercise "tahoe create-node", create-introducer,
179 # create-key-generator, and create-stats-gatherer, by calling the
180 # corresponding code as a subroutine.
182 def workdir(self, name):
183 basedir = os.path.join("test_runner", "CreateNode", name)
184 fileutil.make_dirs(basedir)
187 def run_tahoe(self, argv):
188 out,err = StringIO(), StringIO()
189 rc = runner.runner(argv, stdout=out, stderr=err)
190 return rc, out.getvalue(), err.getvalue()
192 def do_create(self, kind):
193 basedir = self.workdir("test_" + kind)
194 command = "create-" + kind
195 is_client = kind in ("node", "client")
196 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
198 n1 = os.path.join(basedir, command + "-n1")
199 argv = ["--quiet", command, "--basedir", n1]
200 rc, out, err = self.run_tahoe(argv)
201 self.failUnlessEqual(err, "")
202 self.failUnlessEqual(out, "")
203 self.failUnlessEqual(rc, 0)
204 self.failUnless(os.path.exists(n1))
205 self.failUnless(os.path.exists(os.path.join(n1, tac)))
208 # tahoe.cfg should exist, and should have storage enabled for
209 # 'create-node', and disabled for 'create-client'.
210 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
211 self.failUnless(os.path.exists(tahoe_cfg))
212 content = open(tahoe_cfg).read()
214 self.failUnless("\n[storage]\nenabled = false\n" in content)
216 self.failUnless("\n[storage]\nenabled = true\n" in content)
218 # creating the node a second time should be rejected
219 rc, out, err = self.run_tahoe(argv)
220 self.failIfEqual(rc, 0, str((out, err, rc)))
221 self.failUnlessEqual(out, "")
222 self.failUnless("is not empty." in err)
224 # Fail if there is a non-empty line that doesn't end with a
226 for line in err.splitlines():
227 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
229 # test that the non --basedir form works too
230 n2 = os.path.join(basedir, command + "-n2")
231 argv = ["--quiet", command, n2]
232 rc, out, err = self.run_tahoe(argv)
233 self.failUnlessEqual(err, "")
234 self.failUnlessEqual(out, "")
235 self.failUnlessEqual(rc, 0)
236 self.failUnless(os.path.exists(n2))
237 self.failUnless(os.path.exists(os.path.join(n2, tac)))
239 # test the --node-directory form
240 n3 = os.path.join(basedir, command + "-n3")
241 argv = ["--quiet", command, "--node-directory", n3]
242 rc, out, err = self.run_tahoe(argv)
243 self.failUnlessEqual(err, "")
244 self.failUnlessEqual(out, "")
245 self.failUnlessEqual(rc, 0)
246 self.failUnless(os.path.exists(n3))
247 self.failUnless(os.path.exists(os.path.join(n3, tac)))
249 # make sure it rejects too many arguments
250 argv = [command, "basedir", "extraarg"]
251 self.failUnlessRaises(usage.UsageError,
255 # when creating a non-client, there is no default for the basedir
258 self.failUnlessRaises(usage.UsageError,
264 self.do_create("node")
266 def test_client(self):
267 # create-client should behave like create-node --no-storage.
268 self.do_create("client")
270 def test_introducer(self):
271 self.do_create("introducer")
273 def test_key_generator(self):
274 self.do_create("key-generator")
276 def test_stats_gatherer(self):
277 self.do_create("stats-gatherer")
279 def test_subcommands(self):
280 # no arguments should trigger a command listing, via UsageError
281 self.failUnlessRaises(usage.UsageError,
287 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
289 # exercise "tahoe start", for both introducer, client node, and
290 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
291 # get us figleaf-based line-level coverage, but it does a better job of
292 # confirming that the user can actually run "./bin/tahoe start" and
293 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
294 # the like correctly.
296 # This doesn't work on cygwin (it hangs forever), so we skip this test
297 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
298 # work on cygwin: twisted seems unable to provide a version of
299 # spawnProcess which really works there.
301 def workdir(self, name):
302 basedir = os.path.join("test_runner", "RunNode", name)
303 fileutil.make_dirs(basedir)
306 def test_introducer(self):
307 self.skip_if_cannot_daemonize()
308 basedir = self.workdir("test_introducer")
309 c1 = os.path.join(basedir, "c1")
310 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
311 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
312 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
314 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
316 out, err, rc_or_sig = res
317 self.failUnlessEqual(rc_or_sig, 0)
318 # by writing this file, we get ten seconds before the node will
319 # exit. This insures that even if the test fails (and the 'stop'
320 # command doesn't work), the client should still terminate.
321 open(HOTLINE_FILE, "w").write("")
322 # now it's safe to start the node
325 def _then_start_the_node(res):
326 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
327 d.addCallback(_then_start_the_node)
330 out, err, rc_or_sig = res
332 open(HOTLINE_FILE, "w").write("")
333 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
334 self.failUnlessEqual(rc_or_sig, 0, errstr)
335 self.failUnlessEqual(out, "", errstr)
336 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
338 # the parent (twistd) has exited. However, twistd writes the pid
339 # from the child, not the parent, so we can't expect twistd.pid
340 # to exist quite yet.
342 # the node is running, but it might not have made it past the
343 # first reactor turn yet, and if we kill it too early, it won't
344 # remove the twistd.pid file. So wait until it does something
345 # that we know it won't do until after the first turn.
348 def _node_has_started():
349 return os.path.exists(INTRODUCER_FURL_FILE)
350 d.addCallback(lambda res: self.poll(_node_has_started))
353 open(HOTLINE_FILE, "w").write("")
354 self.failUnless(os.path.exists(TWISTD_PID_FILE))
355 # rm this so we can detect when the second incarnation is ready
356 os.unlink(INTRODUCER_FURL_FILE)
357 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
358 d.addCallback(_started)
361 out, err, rc_or_sig = res
362 open(HOTLINE_FILE, "w").write("")
363 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
364 self.failUnlessEqual(rc_or_sig, 0, errstr)
365 self.failUnlessEqual(out, "", errstr)
366 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
369 # again, the second incarnation of the node might not be ready yet,
370 # so poll until it is
371 d.addCallback(lambda res: self.poll(_node_has_started))
373 # now we can kill it. TODO: On a slow machine, the node might kill
374 # itself before we get a chance too, especially if spawning the
375 # 'tahoe stop' command takes a while.
377 open(HOTLINE_FILE, "w").write("")
378 self.failUnless(os.path.exists(TWISTD_PID_FILE))
380 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
383 def _after_stopping(res):
384 out, err, rc_or_sig = res
385 open(HOTLINE_FILE, "w").write("")
386 # the parent has exited by now
387 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
388 self.failUnlessEqual(rc_or_sig, 0, errstr)
389 self.failUnlessEqual(out, "", errstr)
390 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
391 # the parent was supposed to poll and wait until it sees
392 # twistd.pid go away before it exits, so twistd.pid should be
394 self.failIf(os.path.exists(TWISTD_PID_FILE))
395 d.addCallback(_after_stopping)
397 def _remove_hotline(res):
398 os.unlink(HOTLINE_FILE)
400 d.addBoth(_remove_hotline)
402 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
404 def test_client_no_noise(self):
405 self.skip_if_cannot_daemonize()
408 pkg_resources.require("Twisted>=9.0.0")
409 except pkg_resources.VersionConflict:
410 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
411 basedir = self.workdir("test_client_no_noise")
412 c1 = os.path.join(basedir, "c1")
413 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
414 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
415 PORTNUMFILE = os.path.join(c1, "client.port")
417 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
419 out, err, rc_or_sig = res
420 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
421 assert rc_or_sig == 0, errstr
422 self.failUnlessEqual(rc_or_sig, 0)
423 # By writing this file, we get forty seconds before the client will exit. This insures
424 # that even if the 'stop' command doesn't work (and the test fails), the client should
426 open(HOTLINE_FILE, "w").write("")
427 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
428 # now it's safe to start the node
432 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
433 d.addCallback(_start)
436 out, err, rc_or_sig = res
437 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
438 open(HOTLINE_FILE, "w").write("")
439 self.failUnlessEqual(rc_or_sig, 0, errstr)
440 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
441 errlines = err.split("\n")
442 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
443 and "from pkg_resources import load_entry_point" not in line)], errstr)
445 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
447 # the parent (twistd) has exited. However, twistd writes the pid
448 # from the child, not the parent, so we can't expect twistd.pid
449 # to exist quite yet.
451 # the node is running, but it might not have made it past the
452 # first reactor turn yet, and if we kill it too early, it won't
453 # remove the twistd.pid file. So wait until it does something
454 # that we know it won't do until after the first turn.
457 def _node_has_started():
458 return os.path.exists(PORTNUMFILE)
459 d.addCallback(lambda res: self.poll(_node_has_started))
461 # now we can kill it. TODO: On a slow machine, the node might kill
462 # itself before we get a chance too, especially if spawning the
463 # 'tahoe stop' command takes a while.
465 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
466 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
470 def test_client(self):
471 self.skip_if_cannot_daemonize()
472 basedir = self.workdir("test_client")
473 c1 = os.path.join(basedir, "c1")
474 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
475 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
476 PORTNUMFILE = os.path.join(c1, "client.port")
478 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
480 out, err, rc_or_sig = res
481 self.failUnlessEqual(rc_or_sig, 0)
482 # By writing this file, we get sixty seconds before the client will exit. This insures
483 # that even if the 'stop' command doesn't work (and the test fails), the client should
485 open(HOTLINE_FILE, "w").write("")
486 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
487 # now it's safe to start the node
491 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
492 d.addCallback(_start)
495 out, err, rc_or_sig = res
496 open(HOTLINE_FILE, "w").write("")
497 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
498 self.failUnlessEqual(rc_or_sig, 0, errstr)
499 self.failUnlessEqual(out, "", errstr)
500 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
502 # the parent (twistd) has exited. However, twistd writes the pid
503 # from the child, not the parent, so we can't expect twistd.pid
504 # to exist quite yet.
506 # the node is running, but it might not have made it past the
507 # first reactor turn yet, and if we kill it too early, it won't
508 # remove the twistd.pid file. So wait until it does something
509 # that we know it won't do until after the first turn.
512 def _node_has_started():
513 return os.path.exists(PORTNUMFILE)
514 d.addCallback(lambda res: self.poll(_node_has_started))
517 open(HOTLINE_FILE, "w").write("")
518 self.failUnless(os.path.exists(TWISTD_PID_FILE))
519 # rm this so we can detect when the second incarnation is ready
520 os.unlink(PORTNUMFILE)
522 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
523 d.addCallback(_started)
526 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.
535 # again, the second incarnation of the node might not be ready yet,
536 # so poll until it is
537 d.addCallback(lambda res: self.poll(_node_has_started))
539 # now we can kill it. TODO: On a slow machine, the node might kill
540 # itself before we get a chance too, especially if spawning the
541 # 'tahoe stop' command takes a while.
543 open(HOTLINE_FILE, "w").write("")
544 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
545 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
549 out, err, rc_or_sig = res
551 open(HOTLINE_FILE, "w").write("")
552 # the parent has exited by now
553 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
554 self.failUnlessEqual(rc_or_sig, 0, errstr)
555 self.failUnlessEqual(out, "", errstr)
556 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
557 # the parent was supposed to poll and wait until it sees
558 # twistd.pid go away before it exits, so twistd.pid should be
560 self.failIf(os.path.exists(TWISTD_PID_FILE))
562 def _remove_hotline(res):
563 os.unlink(HOTLINE_FILE)
565 d.addBoth(_remove_hotline)
568 def test_baddir(self):
569 self.skip_if_cannot_daemonize()
570 basedir = self.workdir("test_baddir")
571 fileutil.make_dirs(basedir)
573 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
575 out, err, rc_or_sig = res
576 self.failUnlessEqual(rc_or_sig, 1)
577 self.failUnless("does not look like a node directory" in err, err)
580 def _then_stop_it(res):
581 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
582 d.addCallback(_then_stop_it)
585 out, err, rc_or_sig = res
586 self.failUnlessEqual(rc_or_sig, 2)
587 self.failUnless("does not look like a running node directory" in err)
590 def _then_start_in_bogus_basedir(res):
591 not_a_dir = os.path.join(basedir, "bogus")
592 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
593 d.addCallback(_then_start_in_bogus_basedir)
596 out, err, rc_or_sig = res
597 self.failUnlessEqual(rc_or_sig, 1)
598 self.failUnless("does not look like a directory at all" in err, err)
602 def test_keygen(self):
603 self.skip_if_cannot_daemonize()
604 basedir = self.workdir("test_keygen")
605 c1 = os.path.join(basedir, "c1")
606 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
607 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
609 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
611 out, err, rc_or_sig = res
612 self.failUnlessEqual(rc_or_sig, 0)
616 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
617 d.addCallback(_start)
620 out, err, rc_or_sig = res
621 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
622 self.failUnlessEqual(rc_or_sig, 0, errstr)
623 self.failUnlessEqual(out, "", errstr)
624 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
626 # the parent (twistd) has exited. However, twistd writes the pid
627 # from the child, not the parent, so we can't expect twistd.pid
628 # to exist quite yet.
630 # the node is running, but it might not have made it past the
631 # first reactor turn yet, and if we kill it too early, it won't
632 # remove the twistd.pid file. So wait until it does something
633 # that we know it won't do until after the first turn.
636 def _node_has_started():
637 return os.path.exists(KEYGEN_FURL_FILE)
638 d.addCallback(lambda res: self.poll(_node_has_started))
641 self.failUnless(os.path.exists(TWISTD_PID_FILE))
642 # rm this so we can detect when the second incarnation is ready
643 os.unlink(KEYGEN_FURL_FILE)
644 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
645 d.addCallback(_started)
648 out, err, rc_or_sig = res
649 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
650 self.failUnlessEqual(rc_or_sig, 0, errstr)
651 self.failUnlessEqual(out, "", errstr)
652 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
655 # again, the second incarnation of the node might not be ready yet,
656 # so poll until it is
657 d.addCallback(lambda res: self.poll(_node_has_started))
659 # now we can kill it. TODO: On a slow machine, the node might kill
660 # itself before we get a chance too, especially if spawning the
661 # 'tahoe stop' command takes a while.
663 self.failUnless(os.path.exists(TWISTD_PID_FILE))
664 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
668 out, err, rc_or_sig = res
669 # the parent has exited by now
670 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
671 self.failUnlessEqual(rc_or_sig, 0, errstr)
672 self.failUnlessEqual(out, "", errstr)
673 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
674 # the parent was supposed to poll and wait until it sees
675 # twistd.pid go away before it exits, so twistd.pid should be
677 self.failIf(os.path.exists(TWISTD_PID_FILE))