2 from twisted.trial import unittest
4 from twisted.python import usage, runtime
5 from twisted.internet import utils
6 import os.path, re, sys, subprocess
7 from cStringIO import StringIO
8 from allmydata.util import fileutil, pollmixin
9 from allmydata.util.encodingutil import unicode_to_argv, unicode_to_output, get_filesystem_encoding
10 from allmydata.scripts import runner
12 from allmydata.test import common_util
17 def get_root_from_file(src):
18 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(src))))
20 root = os.path.dirname(srcdir)
21 if os.path.basename(srcdir) == 'site-packages':
22 if re.search(r'python.+\..+', os.path.basename(root)):
23 root = os.path.dirname(root)
24 root = os.path.dirname(root)
25 elif os.path.basename(root) == 'src':
26 root = os.path.dirname(root)
30 srcfile = allmydata.__file__
31 rootdir = get_root_from_file(srcfile)
33 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
34 if sys.platform == "win32":
35 bintahoe += ".pyscript"
36 if not os.path.exists(bintahoe):
37 alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
38 if os.path.exists(alt_bintahoe):
39 bintahoe = alt_bintahoe
43 def skip_if_cannot_run_bintahoe(self):
44 if "cygwin" in sys.platform.lower():
45 raise unittest.SkipTest("We don't know how to make this test work on cygwin: spawnProcess seems to hang forever. We don't know if 'bin/tahoe start' can be run on cygwin.")
46 if not os.path.exists(bintahoe):
47 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location (%s), and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of Tahoe. Perhaps running 'setup.py build' again will help." % (bintahoe,))
49 def skip_if_cannot_daemonize(self):
50 self.skip_if_cannot_run_bintahoe()
51 if runtime.platformType == "win32":
52 # twistd on windows doesn't daemonize. cygwin should work normally.
53 raise unittest.SkipTest("twistd does not fork under windows")
56 class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
57 def _check_right_code(self, file_to_check):
58 root_to_check = get_root_from_file(file_to_check)
59 cwd = os.path.normcase(os.path.realpath("."))
60 root_from_cwd = os.path.dirname(cwd)
61 if os.path.basename(root_from_cwd) == 'src':
62 root_from_cwd = os.path.dirname(root_from_cwd)
64 same = (root_from_cwd == root_to_check)
67 same = os.path.samefile(root_from_cwd, root_to_check)
68 except AttributeError, e:
72 msg = ("We seem to be testing the code at %r,\n"
73 "(according to the source filename %r),\n"
74 "but expected to be testing the code at %r.\n"
75 % (root_to_check, file_to_check, root_from_cwd))
77 root_from_cwdu = os.path.dirname(os.path.normcase(os.path.normpath(os.getcwdu())))
78 if os.path.basename(root_from_cwdu) == u'src':
79 root_from_cwdu = os.path.dirname(root_from_cwdu)
81 if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
82 msg += ("However, this may be a false alarm because the current directory path\n"
83 "is not representable in the filesystem encoding. Please run the tests\n"
84 "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
85 raise unittest.SkipTest(msg)
87 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
90 def test_the_right_code(self):
91 self._check_right_code(srcfile)
93 def test_import_in_repl(self):
94 self.skip_if_cannot_run_bintahoe()
96 p = subprocess.Popen([sys.executable, bintahoe, "debug", "repl"],
97 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98 (out, err) = p.communicate("import allmydata; print; print allmydata.__file__")
100 self.failUnlessEqual(p.returncode, 0)
101 lines = out.splitlines()
102 self.failUnlessIn('>>>', lines[0], (out, err))
103 self._check_right_code(lines[1])
106 self.skip_if_cannot_run_bintahoe()
107 d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
109 out, err, rc_or_sig = res
110 self.failUnlessEqual(rc_or_sig, 0, str(res))
112 # Fail unless the allmydata-tahoe package is *this* version *and*
113 # was loaded from *this* source directory.
115 verstr = str(allmydata.__version__)
117 self.failIfEqual(verstr, "unknown",
118 "We don't know our version, because this distribution didn't come "
119 "with a _version.py and 'setup.py darcsver' hasn't been run.")
121 # The Python "rational version numbering" convention
122 # disallows "-r$REV" but allows ".post$REV"
123 # instead. Eventually we'll probably move to that. When we
124 # do, this test won't go red:
126 ix = verstr.rfind('-r')
128 altverstr = verstr[:ix] + '.post' + verstr[ix+2:]
130 ix = verstr.rfind('.post')
132 altverstr = verstr[:ix] + '-r' + verstr[ix+5:]
136 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
137 required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, verstr, srcdir)
138 alt_required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, altverstr, srcdir)
140 self.failUnless(out.startswith(required_ver_and_path) or out.startswith(alt_required_ver_and_path), (out, err, rc_or_sig, required_ver_and_path))
144 def test_unicode_arguments_and_output(self):
145 self.skip_if_cannot_run_bintahoe()
149 tricky_arg = unicode_to_argv(tricky, mangle=True)
150 tricky_out = unicode_to_output(tricky)
151 except UnicodeEncodeError:
152 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
154 d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ)
156 out, err, rc_or_sig = res
157 self.failUnlessEqual(rc_or_sig, 1, str(res))
158 self.failUnlessIn("Unknown command: "+tricky_out, out)
162 def test_run_with_python_options(self):
163 self.skip_if_cannot_run_bintahoe()
165 # -t is a harmless option that warns about tabs.
166 d = utils.getProcessOutputAndValue(sys.executable, args=['-t', bintahoe, '--version'],
169 out, err, rc_or_sig = res
170 self.failUnlessEqual(rc_or_sig, 0, str(res))
171 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
175 def test_version_no_noise(self):
176 self.skip_if_cannot_run_bintahoe()
179 pkg_resources.require("Twisted>=9.0.0")
180 except pkg_resources.VersionConflict:
181 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
183 d = utils.getProcessOutputAndValue(bintahoe, args=["--version"], env=os.environ)
185 out, err, rc_or_sig = res
186 self.failUnlessEqual(rc_or_sig, 0, str(res))
187 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
188 self.failIfIn("DeprecationWarning", out, str(res))
189 errlines = err.split("\n")
190 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
191 and "from pkg_resources import load_entry_point" not in line)], str(res))
193 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
198 class CreateNode(unittest.TestCase):
199 # exercise "tahoe create-node", create-introducer,
200 # create-key-generator, and create-stats-gatherer, by calling the
201 # corresponding code as a subroutine.
203 def workdir(self, name):
204 basedir = os.path.join("test_runner", "CreateNode", name)
205 fileutil.make_dirs(basedir)
208 def run_tahoe(self, argv):
209 out,err = StringIO(), StringIO()
210 rc = runner.runner(argv, stdout=out, stderr=err)
211 return rc, out.getvalue(), err.getvalue()
213 def do_create(self, kind):
214 basedir = self.workdir("test_" + kind)
215 command = "create-" + kind
216 is_client = kind in ("node", "client")
217 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
219 n1 = os.path.join(basedir, command + "-n1")
220 argv = ["--quiet", command, "--basedir", n1]
221 rc, out, err = self.run_tahoe(argv)
222 self.failUnlessEqual(err, "")
223 self.failUnlessEqual(out, "")
224 self.failUnlessEqual(rc, 0)
225 self.failUnless(os.path.exists(n1))
226 self.failUnless(os.path.exists(os.path.join(n1, tac)))
229 # tahoe.cfg should exist, and should have storage enabled for
230 # 'create-node', and disabled for 'create-client'.
231 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
232 self.failUnless(os.path.exists(tahoe_cfg))
233 content = open(tahoe_cfg).read()
235 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = false\n", content), content)
237 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = true\n", content), content)
238 self.failUnless("\nreserved_space = 1G\n" in content)
240 # creating the node a second time should be rejected
241 rc, out, err = self.run_tahoe(argv)
242 self.failIfEqual(rc, 0, str((out, err, rc)))
243 self.failUnlessEqual(out, "")
244 self.failUnless("is not empty." in err)
246 # Fail if there is a non-empty line that doesn't end with a
248 for line in err.splitlines():
249 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
251 # test that the non --basedir form works too
252 n2 = os.path.join(basedir, command + "-n2")
253 argv = ["--quiet", command, n2]
254 rc, out, err = self.run_tahoe(argv)
255 self.failUnlessEqual(err, "")
256 self.failUnlessEqual(out, "")
257 self.failUnlessEqual(rc, 0)
258 self.failUnless(os.path.exists(n2))
259 self.failUnless(os.path.exists(os.path.join(n2, tac)))
261 # test the --node-directory form
262 n3 = os.path.join(basedir, command + "-n3")
263 argv = ["--quiet", command, "--node-directory", n3]
264 rc, out, err = self.run_tahoe(argv)
265 self.failUnlessEqual(err, "")
266 self.failUnlessEqual(out, "")
267 self.failUnlessEqual(rc, 0)
268 self.failUnless(os.path.exists(n3))
269 self.failUnless(os.path.exists(os.path.join(n3, tac)))
271 # make sure it rejects too many arguments
272 argv = [command, "basedir", "extraarg"]
273 self.failUnlessRaises(usage.UsageError,
277 # when creating a non-client, there is no default for the basedir
280 self.failUnlessRaises(usage.UsageError,
286 self.do_create("node")
288 def test_client(self):
289 # create-client should behave like create-node --no-storage.
290 self.do_create("client")
292 def test_introducer(self):
293 self.do_create("introducer")
295 def test_key_generator(self):
296 self.do_create("key-generator")
298 def test_stats_gatherer(self):
299 self.do_create("stats-gatherer")
301 def test_subcommands(self):
302 # no arguments should trigger a command listing, via UsageError
303 self.failUnlessRaises(usage.UsageError,
309 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
311 # exercise "tahoe start", for both introducer, client node, and
312 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
313 # get us figleaf-based line-level coverage, but it does a better job of
314 # confirming that the user can actually run "./bin/tahoe start" and
315 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
316 # the like correctly.
318 # This doesn't work on cygwin (it hangs forever), so we skip this test
319 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
320 # work on cygwin: twisted seems unable to provide a version of
321 # spawnProcess which really works there.
323 def workdir(self, name):
324 basedir = os.path.join("test_runner", "RunNode", name)
325 fileutil.make_dirs(basedir)
328 def test_introducer(self):
329 self.skip_if_cannot_daemonize()
330 basedir = self.workdir("test_introducer")
331 c1 = os.path.join(basedir, "c1")
332 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
333 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
334 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
336 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
338 out, err, rc_or_sig = res
339 self.failUnlessEqual(rc_or_sig, 0)
340 # by writing this file, we get ten seconds before the node will
341 # exit. This insures that even if the test fails (and the 'stop'
342 # command doesn't work), the client should still terminate.
343 open(HOTLINE_FILE, "w").write("")
344 # now it's safe to start the node
347 def _then_start_the_node(res):
348 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
349 d.addCallback(_then_start_the_node)
352 out, err, rc_or_sig = res
354 open(HOTLINE_FILE, "w").write("")
355 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
356 self.failUnlessEqual(rc_or_sig, 0, errstr)
357 self.failUnlessEqual(out, "", errstr)
358 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
360 # the parent (twistd) has exited. However, twistd writes the pid
361 # from the child, not the parent, so we can't expect twistd.pid
362 # to exist quite yet.
364 # the node is running, but it might not have made it past the
365 # first reactor turn yet, and if we kill it too early, it won't
366 # remove the twistd.pid file. So wait until it does something
367 # that we know it won't do until after the first turn.
370 def _node_has_started():
371 return os.path.exists(INTRODUCER_FURL_FILE)
372 d.addCallback(lambda res: self.poll(_node_has_started))
375 open(HOTLINE_FILE, "w").write("")
376 self.failUnless(os.path.exists(TWISTD_PID_FILE))
377 # rm this so we can detect when the second incarnation is ready
378 os.unlink(INTRODUCER_FURL_FILE)
379 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
380 d.addCallback(_started)
383 out, err, rc_or_sig = res
384 open(HOTLINE_FILE, "w").write("")
385 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
386 self.failUnlessEqual(rc_or_sig, 0, errstr)
387 self.failUnlessEqual(out, "", errstr)
388 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
391 # again, the second incarnation of the node might not be ready yet,
392 # so poll until it is
393 d.addCallback(lambda res: self.poll(_node_has_started))
395 # now we can kill it. TODO: On a slow machine, the node might kill
396 # itself before we get a chance too, especially if spawning the
397 # 'tahoe stop' command takes a while.
399 open(HOTLINE_FILE, "w").write("")
400 self.failUnless(os.path.exists(TWISTD_PID_FILE))
402 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
405 def _after_stopping(res):
406 out, err, rc_or_sig = res
407 open(HOTLINE_FILE, "w").write("")
408 # the parent has exited by now
409 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
410 self.failUnlessEqual(rc_or_sig, 0, errstr)
411 self.failUnlessEqual(out, "", errstr)
412 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
413 # the parent was supposed to poll and wait until it sees
414 # twistd.pid go away before it exits, so twistd.pid should be
416 self.failIf(os.path.exists(TWISTD_PID_FILE))
417 d.addCallback(_after_stopping)
419 def _remove_hotline(res):
420 os.unlink(HOTLINE_FILE)
422 d.addBoth(_remove_hotline)
424 test_introducer.timeout = 480 # This hit the 120-second timeout on "Francois Lenny-armv5tel", then it hit a 240-second timeout on our feisty2.5 buildslave: http://allmydata.org/buildbot/builders/feisty2.5/builds/2381/steps/test/logs/test.log
426 def test_client_no_noise(self):
427 self.skip_if_cannot_daemonize()
430 pkg_resources.require("Twisted>=9.0.0")
431 except pkg_resources.VersionConflict:
432 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
433 basedir = self.workdir("test_client_no_noise")
434 c1 = os.path.join(basedir, "c1")
435 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
436 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
437 PORTNUMFILE = os.path.join(c1, "client.port")
439 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
441 out, err, rc_or_sig = res
442 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
443 assert rc_or_sig == 0, errstr
444 self.failUnlessEqual(rc_or_sig, 0)
445 # By writing this file, we get forty seconds before the client will exit. This insures
446 # that even if the 'stop' command doesn't work (and the test fails), the client should
448 open(HOTLINE_FILE, "w").write("")
449 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
450 # now it's safe to start the node
454 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
455 d.addCallback(_start)
458 out, err, rc_or_sig = res
459 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
460 open(HOTLINE_FILE, "w").write("")
461 self.failUnlessEqual(rc_or_sig, 0, errstr)
462 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
463 errlines = err.split("\n")
464 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
465 and "from pkg_resources import load_entry_point" not in line)], errstr)
467 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
469 # the parent (twistd) has exited. However, twistd writes the pid
470 # from the child, not the parent, so we can't expect twistd.pid
471 # to exist quite yet.
473 # the node is running, but it might not have made it past the
474 # first reactor turn yet, and if we kill it too early, it won't
475 # remove the twistd.pid file. So wait until it does something
476 # that we know it won't do until after the first turn.
479 def _node_has_started():
480 return os.path.exists(PORTNUMFILE)
481 d.addCallback(lambda res: self.poll(_node_has_started))
483 # now we can kill it. TODO: On a slow machine, the node might kill
484 # itself before we get a chance to, especially if spawning the
485 # 'tahoe stop' command takes a while.
487 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
488 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
492 def test_client(self):
493 self.skip_if_cannot_daemonize()
494 basedir = self.workdir("test_client")
495 c1 = os.path.join(basedir, "c1")
496 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
497 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
498 PORTNUMFILE = os.path.join(c1, "client.port")
500 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
502 out, err, rc_or_sig = res
503 self.failUnlessEqual(rc_or_sig, 0)
504 # By writing this file, we get sixty seconds before the client will exit. This insures
505 # that even if the 'stop' command doesn't work (and the test fails), the client should
507 open(HOTLINE_FILE, "w").write("")
508 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
509 # now it's safe to start the node
513 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
514 d.addCallback(_start)
517 out, err, rc_or_sig = res
518 open(HOTLINE_FILE, "w").write("")
519 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
520 self.failUnlessEqual(rc_or_sig, 0, errstr)
521 self.failUnlessEqual(out, "", errstr)
522 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
524 # the parent (twistd) has exited. However, twistd writes the pid
525 # from the child, not the parent, so we can't expect twistd.pid
526 # to exist quite yet.
528 # the node is running, but it might not have made it past the
529 # first reactor turn yet, and if we kill it too early, it won't
530 # remove the twistd.pid file. So wait until it does something
531 # that we know it won't do until after the first turn.
534 def _node_has_started():
535 return os.path.exists(PORTNUMFILE)
536 d.addCallback(lambda res: self.poll(_node_has_started))
539 open(HOTLINE_FILE, "w").write("")
540 self.failUnless(os.path.exists(TWISTD_PID_FILE))
541 # rm this so we can detect when the second incarnation is ready
542 os.unlink(PORTNUMFILE)
544 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
545 d.addCallback(_started)
548 out, err, rc_or_sig = res
550 open(HOTLINE_FILE, "w").write("")
551 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
552 self.failUnlessEqual(rc_or_sig, 0, errstr)
553 self.failUnlessEqual(out, "", errstr)
554 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
557 # again, the second incarnation of the node might not be ready yet,
558 # so poll until it is
559 d.addCallback(lambda res: self.poll(_node_has_started))
561 # now we can kill it. TODO: On a slow machine, the node might kill
562 # itself before we get a chance too, especially if spawning the
563 # 'tahoe stop' command takes a while.
565 open(HOTLINE_FILE, "w").write("")
566 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
567 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
571 out, err, rc_or_sig = res
573 open(HOTLINE_FILE, "w").write("")
574 # the parent has exited by now
575 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
576 self.failUnlessEqual(rc_or_sig, 0, errstr)
577 self.failUnlessEqual(out, "", errstr)
578 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
579 # the parent was supposed to poll and wait until it sees
580 # twistd.pid go away before it exits, so twistd.pid should be
582 self.failIf(os.path.exists(TWISTD_PID_FILE))
584 def _remove_hotline(res):
585 os.unlink(HOTLINE_FILE)
587 d.addBoth(_remove_hotline)
590 def test_baddir(self):
591 self.skip_if_cannot_daemonize()
592 basedir = self.workdir("test_baddir")
593 fileutil.make_dirs(basedir)
595 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
597 out, err, rc_or_sig = res
598 self.failUnlessEqual(rc_or_sig, 1)
599 self.failUnless("does not look like a node directory" in err, err)
602 def _then_stop_it(res):
603 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
604 d.addCallback(_then_stop_it)
607 out, err, rc_or_sig = res
608 self.failUnlessEqual(rc_or_sig, 2)
609 self.failUnless("does not look like a running node directory" in err)
612 def _then_start_in_bogus_basedir(res):
613 not_a_dir = os.path.join(basedir, "bogus")
614 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
615 d.addCallback(_then_start_in_bogus_basedir)
618 out, err, rc_or_sig = res
619 self.failUnlessEqual(rc_or_sig, 1)
620 self.failUnless("does not look like a directory at all" in err, err)
624 def test_keygen(self):
625 self.skip_if_cannot_daemonize()
626 basedir = self.workdir("test_keygen")
627 c1 = os.path.join(basedir, "c1")
628 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
629 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
631 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
633 out, err, rc_or_sig = res
634 self.failUnlessEqual(rc_or_sig, 0)
638 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
639 d.addCallback(_start)
642 out, err, rc_or_sig = res
643 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
644 self.failUnlessEqual(rc_or_sig, 0, errstr)
645 self.failUnlessEqual(out, "", errstr)
646 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
648 # the parent (twistd) has exited. However, twistd writes the pid
649 # from the child, not the parent, so we can't expect twistd.pid
650 # to exist quite yet.
652 # the node is running, but it might not have made it past the
653 # first reactor turn yet, and if we kill it too early, it won't
654 # remove the twistd.pid file. So wait until it does something
655 # that we know it won't do until after the first turn.
658 def _node_has_started():
659 return os.path.exists(KEYGEN_FURL_FILE)
660 d.addCallback(lambda res: self.poll(_node_has_started))
663 self.failUnless(os.path.exists(TWISTD_PID_FILE))
664 # rm this so we can detect when the second incarnation is ready
665 os.unlink(KEYGEN_FURL_FILE)
666 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
667 d.addCallback(_started)
670 out, err, rc_or_sig = res
671 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
672 self.failUnlessEqual(rc_or_sig, 0, errstr)
673 self.failUnlessEqual(out, "", errstr)
674 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
677 # again, the second incarnation of the node might not be ready yet,
678 # so poll until it is
679 d.addCallback(lambda res: self.poll(_node_has_started))
681 # now we can kill it. TODO: On a slow machine, the node might kill
682 # itself before we get a chance too, especially if spawning the
683 # 'tahoe stop' command takes a while.
685 self.failUnless(os.path.exists(TWISTD_PID_FILE))
686 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
690 out, err, rc_or_sig = res
691 # the parent has exited by now
692 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
693 self.failUnlessEqual(rc_or_sig, 0, errstr)
694 self.failUnlessEqual(out, "", errstr)
695 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
696 # the parent was supposed to poll and wait until it sees
697 # twistd.pid go away before it exits, so twistd.pid should be
699 self.failIf(os.path.exists(TWISTD_PID_FILE))