1 import os.path, re, sys, subprocess
2 from cStringIO import StringIO
4 from twisted.trial import unittest
6 from twisted.python import usage, runtime
7 from twisted.internet import threads
9 from allmydata.util import fileutil, pollmixin
10 from allmydata.util.encodingutil import unicode_to_argv, unicode_to_output, \
11 get_filesystem_encoding
12 from allmydata.scripts import runner
13 from allmydata.client import Client
14 from allmydata.test import common_util
20 def get_root_from_file(src):
21 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(src))))
23 root = os.path.dirname(srcdir)
24 if os.path.basename(srcdir) == 'site-packages':
25 if re.search(r'python.+\..+', os.path.basename(root)):
26 root = os.path.dirname(root)
27 root = os.path.dirname(root)
28 elif os.path.basename(root) == 'src':
29 root = os.path.dirname(root)
33 srcfile = allmydata.__file__
34 rootdir = get_root_from_file(srcfile)
36 if hasattr(sys, 'frozen'):
37 bintahoe = os.path.join(rootdir, 'tahoe')
38 if sys.platform == "win32" and os.path.exists(bintahoe + '.exe'):
41 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
42 if sys.platform == "win32":
43 bintahoe += '.pyscript'
44 if not os.path.exists(bintahoe):
45 alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
46 if os.path.exists(alt_bintahoe):
47 bintahoe = alt_bintahoe
50 class RunBinTahoeMixin:
51 def skip_if_cannot_run_bintahoe(self):
52 if not os.path.exists(bintahoe):
53 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,))
55 def skip_if_cannot_daemonize(self):
56 self.skip_if_cannot_run_bintahoe()
57 if runtime.platformType == "win32":
58 # twistd on windows doesn't daemonize. cygwin should work normally.
59 raise unittest.SkipTest("twistd does not fork under windows")
61 def run_bintahoe(self, args, stdin=None, python_options=[], env=None):
62 self.skip_if_cannot_run_bintahoe()
64 if hasattr(sys, 'frozen'):
66 raise unittest.SkipTest("This test doesn't apply to frozen builds.")
67 command = [bintahoe] + args
69 command = [sys.executable] + python_options + [bintahoe] + args
74 stdin_stream = subprocess.PIPE
77 p = subprocess.Popen(command, stdin=stdin_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
78 (out, err) = p.communicate(stdin)
79 return (out, err, p.returncode)
80 return threads.deferToThread(_run)
83 class BinTahoe(common_util.SignalMixin, unittest.TestCase, RunBinTahoeMixin):
84 def _check_right_code(self, file_to_check):
85 root_to_check = get_root_from_file(file_to_check)
86 if os.path.basename(root_to_check) == 'dist':
87 root_to_check = os.path.dirname(root_to_check)
89 cwd = os.path.normcase(os.path.realpath("."))
90 root_from_cwd = os.path.dirname(cwd)
91 if os.path.basename(root_from_cwd) == 'src':
92 root_from_cwd = os.path.dirname(root_from_cwd)
94 # This is needed if we are running in a temporary directory created by 'make tmpfstest'.
95 if os.path.basename(root_from_cwd).startswith('tmp'):
96 root_from_cwd = os.path.dirname(root_from_cwd)
98 same = (root_from_cwd == root_to_check)
101 same = os.path.samefile(root_from_cwd, root_to_check)
102 except AttributeError, e:
106 msg = ("We seem to be testing the code at %r,\n"
107 "(according to the source filename %r),\n"
108 "but expected to be testing the code at %r.\n"
109 % (root_to_check, file_to_check, root_from_cwd))
111 root_from_cwdu = os.path.dirname(os.path.normcase(os.path.normpath(os.getcwdu())))
112 if os.path.basename(root_from_cwdu) == u'src':
113 root_from_cwdu = os.path.dirname(root_from_cwdu)
115 # This is needed if we are running in a temporary directory created by 'make tmpfstest'.
116 if os.path.basename(root_from_cwdu).startswith(u'tmp'):
117 root_from_cwdu = os.path.dirname(root_from_cwdu)
119 if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
120 msg += ("However, this may be a false alarm because the current directory path\n"
121 "is not representable in the filesystem encoding. Please run the tests\n"
122 "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
123 raise unittest.SkipTest(msg)
125 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
128 def test_the_right_code(self):
129 self._check_right_code(srcfile)
131 def test_import_in_repl(self):
132 d = self.run_bintahoe(["debug", "repl"],
133 stdin="import allmydata; print; print allmydata.__file__")
135 out, err, rc_or_sig = res
136 self.failUnlessEqual(rc_or_sig, 0, str(res))
137 lines = out.splitlines()
138 self.failUnlessIn('>>>', lines[0], str(res))
139 self._check_right_code(lines[1])
142 # The timeout was exceeded on FreeStorm's CentOS5-i386.
143 test_import_in_repl.timeout = 480
146 d = self.run_bintahoe(["--version-and-path"])
148 from allmydata import normalized_version
150 out, err, rc_or_sig = res
151 self.failUnlessEqual(rc_or_sig, 0, str(res))
153 # Fail unless the __appname__ package is *this* version *and*
154 # was loaded from *this* source directory.
156 required_verstr = str(allmydata.__version__)
158 self.failIfEqual(required_verstr, "unknown",
159 "We don't know our version, because this distribution didn't come "
160 "with a _version.py and 'setup.py update_version' hasn't been run.")
162 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
163 info = repr((res, allmydata.__appname__, required_verstr, srcdir))
165 appverpath = out.split(')')[0]
166 (appverfull, path) = appverpath.split('] (')
167 (appver, comment) = appverfull.split(' [')
168 (branch, full_version) = comment.split(': ')
169 (app, ver) = appver.split(': ')
171 self.failUnlessEqual(app, allmydata.__appname__, info)
172 norm_ver = normalized_version(ver)
173 norm_required = normalized_version(required_verstr)
174 self.failUnlessEqual(norm_ver, norm_required, info)
175 self.failUnlessEqual(path, srcdir, info)
176 self.failUnlessEqual(branch, allmydata.branch)
177 self.failUnlessEqual(full_version, allmydata.full_version)
181 def test_unicode_arguments_and_output(self):
182 self.skip_if_cannot_run_bintahoe()
186 tricky_arg = unicode_to_argv(tricky, mangle=True)
187 tricky_out = unicode_to_output(tricky)
188 except UnicodeEncodeError:
189 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
191 d = self.run_bintahoe([tricky_arg])
193 out, err, rc_or_sig = res
194 self.failUnlessEqual(rc_or_sig, 1, str(res))
195 self.failUnlessIn("Unknown command: "+tricky_out, out)
199 def test_run_with_python_options(self):
200 # -t is a harmless option that warns about tabs.
201 d = self.run_bintahoe(["--version"], python_options=["-t"])
203 out, err, rc_or_sig = res
204 self.failUnlessEqual(rc_or_sig, 0, str(res))
205 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
209 def test_version_no_noise(self):
210 self.skip_if_cannot_run_bintahoe()
212 d = self.run_bintahoe(["--version"])
214 out, err, rc_or_sig = res
215 self.failUnlessEqual(rc_or_sig, 0, str(res))
216 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
217 self.failIfIn("DeprecationWarning", out, str(res))
218 errlines = err.split("\n")
219 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
220 and "from pkg_resources import load_entry_point" not in line)], str(res))
222 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
227 class CreateNode(unittest.TestCase):
228 # exercise "tahoe create-node", create-introducer,
229 # create-key-generator, and create-stats-gatherer, by calling the
230 # corresponding code as a subroutine.
232 def workdir(self, name):
233 basedir = os.path.join("test_runner", "CreateNode", name)
234 fileutil.make_dirs(basedir)
237 def run_tahoe(self, argv):
238 out,err = StringIO(), StringIO()
239 rc = runner.runner(argv, stdout=out, stderr=err)
240 return rc, out.getvalue(), err.getvalue()
242 def do_create(self, kind):
243 basedir = self.workdir("test_" + kind)
244 command = "create-" + kind
245 is_client = kind in ("node", "client")
246 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
248 n1 = os.path.join(basedir, command + "-n1")
249 argv = ["--quiet", command, "--basedir", n1]
250 rc, out, err = self.run_tahoe(argv)
251 self.failUnlessEqual(err, "")
252 self.failUnlessEqual(out, "")
253 self.failUnlessEqual(rc, 0)
254 self.failUnless(os.path.exists(n1))
255 self.failUnless(os.path.exists(os.path.join(n1, tac)))
258 # tahoe.cfg should exist, and should have storage enabled for
259 # 'create-node', and disabled for 'create-client'.
260 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
261 self.failUnless(os.path.exists(tahoe_cfg))
262 content = fileutil.read(tahoe_cfg).replace('\r\n', '\n')
264 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = false\n", content), content)
266 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = true\n", content), content)
267 self.failUnless("\nreserved_space = 1G\n" in content)
269 # creating the node a second time should be rejected
270 rc, out, err = self.run_tahoe(argv)
271 self.failIfEqual(rc, 0, str((out, err, rc)))
272 self.failUnlessEqual(out, "")
273 self.failUnless("is not empty." in err)
275 # Fail if there is a non-empty line that doesn't end with a
277 for line in err.splitlines():
278 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
280 # test that the non --basedir form works too
281 n2 = os.path.join(basedir, command + "-n2")
282 argv = ["--quiet", command, n2]
283 rc, out, err = self.run_tahoe(argv)
284 self.failUnlessEqual(err, "")
285 self.failUnlessEqual(out, "")
286 self.failUnlessEqual(rc, 0)
287 self.failUnless(os.path.exists(n2))
288 self.failUnless(os.path.exists(os.path.join(n2, tac)))
290 # test the --node-directory form
291 n3 = os.path.join(basedir, command + "-n3")
292 argv = ["--quiet", "--node-directory", n3, command]
293 rc, out, err = self.run_tahoe(argv)
294 self.failUnlessEqual(err, "")
295 self.failUnlessEqual(out, "")
296 self.failUnlessEqual(rc, 0)
297 self.failUnless(os.path.exists(n3))
298 self.failUnless(os.path.exists(os.path.join(n3, tac)))
300 if kind in ("client", "node", "introducer"):
301 # test that the output (without --quiet) includes the base directory
302 n4 = os.path.join(basedir, command + "-n4")
304 rc, out, err = self.run_tahoe(argv)
305 self.failUnlessEqual(err, "")
306 self.failUnlessIn(" created in ", out)
307 self.failUnlessIn(n4, out)
308 self.failIfIn("\\\\?\\", out)
309 self.failUnlessEqual(rc, 0)
310 self.failUnless(os.path.exists(n4))
311 self.failUnless(os.path.exists(os.path.join(n4, tac)))
313 # make sure it rejects too many arguments
314 argv = [command, "basedir", "extraarg"]
315 self.failUnlessRaises(usage.UsageError,
319 # when creating a non-client, there is no default for the basedir
322 self.failUnlessRaises(usage.UsageError,
328 self.do_create("node")
330 def test_client(self):
331 # create-client should behave like create-node --no-storage.
332 self.do_create("client")
334 def test_introducer(self):
335 self.do_create("introducer")
337 def test_key_generator(self):
338 self.do_create("key-generator")
340 def test_stats_gatherer(self):
341 self.do_create("stats-gatherer")
343 def test_subcommands(self):
344 # no arguments should trigger a command listing, via UsageError
345 self.failUnlessRaises(usage.UsageError,
351 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
353 # exercise "tahoe start", for both introducer, client node, and
354 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
355 # get us figleaf-based line-level coverage, but it does a better job of
356 # confirming that the user can actually run "./bin/tahoe start" and
357 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
358 # the like correctly.
360 # This doesn't work on cygwin (it hangs forever), so we skip this test
361 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
362 # work on cygwin: twisted seems unable to provide a version of
363 # spawnProcess which really works there.
365 def workdir(self, name):
366 basedir = os.path.join("test_runner", "RunNode", name)
367 fileutil.make_dirs(basedir)
370 def test_introducer(self):
371 self.skip_if_cannot_daemonize()
373 basedir = self.workdir("test_introducer")
374 c1 = os.path.join(basedir, "c1")
375 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
376 twistd_pid_file = os.path.join(c1, "twistd.pid")
377 introducer_furl_file = os.path.join(c1, "private", "introducer.furl")
378 portnum_file = os.path.join(c1, "introducer.port")
379 node_url_file = os.path.join(c1, "node.url")
380 config_file = os.path.join(c1, "tahoe.cfg")
382 d = self.run_bintahoe(["--quiet", "create-introducer", "--basedir", c1])
384 out, err, rc_or_sig = res
385 self.failUnlessEqual(rc_or_sig, 0)
387 # This makes sure that node.url is written, which allows us to
388 # detect when the introducer restarts in _node_has_restarted below.
389 config = fileutil.read(config_file)
390 self.failUnlessIn('\nweb.port = \n', config)
391 fileutil.write(config_file, config.replace('\nweb.port = \n', '\nweb.port = 0\n'))
393 # by writing this file, we get ten seconds before the node will
394 # exit. This insures that even if the test fails (and the 'stop'
395 # command doesn't work), the client should still terminate.
396 fileutil.write(exit_trigger_file, "")
397 # now it's safe to start the node
400 def _then_start_the_node(res):
401 return self.run_bintahoe(["--quiet", "start", c1])
402 d.addCallback(_then_start_the_node)
405 out, err, rc_or_sig = res
407 fileutil.write(exit_trigger_file, "")
408 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
409 self.failUnlessEqual(rc_or_sig, 0, errstr)
410 self.failUnlessEqual(out, "", errstr)
411 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
413 # the parent (twistd) has exited. However, twistd writes the pid
414 # from the child, not the parent, so we can't expect twistd.pid
415 # to exist quite yet.
417 # the node is running, but it might not have made it past the
418 # first reactor turn yet, and if we kill it too early, it won't
419 # remove the twistd.pid file. So wait until it does something
420 # that we know it won't do until after the first turn.
423 def _node_has_started():
424 return os.path.exists(introducer_furl_file)
425 d.addCallback(lambda res: self.poll(_node_has_started))
428 # read the introducer.furl and introducer.port files so we can
429 # check that their contents don't change on restart
430 self.furl = fileutil.read(introducer_furl_file)
431 self.failUnless(os.path.exists(portnum_file))
432 self.portnum = fileutil.read(portnum_file)
434 fileutil.write(exit_trigger_file, "")
435 self.failUnless(os.path.exists(twistd_pid_file))
436 self.failUnless(os.path.exists(node_url_file))
438 # rm this so we can detect when the second incarnation is ready
439 os.unlink(node_url_file)
440 return self.run_bintahoe(["--quiet", "restart", c1])
441 d.addCallback(_started)
444 out, err, rc_or_sig = res
445 fileutil.write(exit_trigger_file, "")
446 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
447 self.failUnlessEqual(rc_or_sig, 0, errstr)
448 self.failUnlessEqual(out, "", errstr)
449 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
452 # Again, the second incarnation of the node might not be ready yet,
453 # so poll until it is. This time introducer_furl_file already
454 # exists, so we check for the existence of node_url_file instead.
455 def _node_has_restarted():
456 return os.path.exists(node_url_file) and os.path.exists(portnum_file)
457 d.addCallback(lambda res: self.poll(_node_has_restarted))
459 def _check_same_furl_and_port(res):
460 self.failUnless(os.path.exists(introducer_furl_file))
461 self.failUnlessEqual(self.furl, fileutil.read(introducer_furl_file))
462 self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
463 d.addCallback(_check_same_furl_and_port)
465 # Now we can kill it. TODO: On a slow machine, the node might kill
466 # itself before we get a chance to, especially if spawning the
467 # 'tahoe stop' command takes a while.
469 fileutil.write(exit_trigger_file, "")
470 self.failUnless(os.path.exists(twistd_pid_file))
472 return self.run_bintahoe(["--quiet", "stop", c1])
475 def _after_stopping(res):
476 out, err, rc_or_sig = res
477 fileutil.write(exit_trigger_file, "")
478 # the parent has exited by now
479 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
480 self.failUnlessEqual(rc_or_sig, 0, errstr)
481 self.failUnlessEqual(out, "", errstr)
482 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
483 # the parent was supposed to poll and wait until it sees
484 # twistd.pid go away before it exits, so twistd.pid should be
486 self.failIf(os.path.exists(twistd_pid_file))
487 d.addCallback(_after_stopping)
488 d.addBoth(self._remove, exit_trigger_file)
490 # This test has hit a 240-second timeout on our feisty2.5 buildslave, and a 480-second timeout
491 # on Francois's Lenny-armv5tel buildslave.
492 test_introducer.timeout = 960
494 def test_client_no_noise(self):
495 self.skip_if_cannot_daemonize()
497 basedir = self.workdir("test_client_no_noise")
498 c1 = os.path.join(basedir, "c1")
499 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
500 twistd_pid_file = os.path.join(c1, "twistd.pid")
501 portnum_file = os.path.join(c1, "client.port")
503 d = self.run_bintahoe(["--quiet", "create-client", "--basedir", c1, "--webport", "0"])
505 out, err, rc_or_sig = res
506 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
507 assert rc_or_sig == 0, errstr
508 self.failUnlessEqual(rc_or_sig, 0)
510 # By writing this file, we get two minutes before the client will exit. This ensures
511 # that even if the 'stop' command doesn't work (and the test fails), the client should
513 fileutil.write(exit_trigger_file, "")
514 # now it's safe to start the node
518 return self.run_bintahoe(["--quiet", "start", c1])
519 d.addCallback(_start)
522 out, err, rc_or_sig = res
523 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
524 fileutil.write(exit_trigger_file, "")
525 self.failUnlessEqual(rc_or_sig, 0, errstr)
526 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
527 errlines = err.split("\n")
528 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
529 and "from pkg_resources import load_entry_point" not in line)], errstr)
531 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
533 # the parent (twistd) has exited. However, twistd writes the pid
534 # from the child, not the parent, so we can't expect twistd.pid
535 # to exist quite yet.
537 # the node is running, but it might not have made it past the
538 # first reactor turn yet, and if we kill it too early, it won't
539 # remove the twistd.pid file. So wait until it does something
540 # that we know it won't do until after the first turn.
543 def _node_has_started():
544 return os.path.exists(portnum_file)
545 d.addCallback(lambda res: self.poll(_node_has_started))
547 # now we can kill it. TODO: On a slow machine, the node might kill
548 # itself before we get a chance to, especially if spawning the
549 # 'tahoe stop' command takes a while.
551 self.failUnless(os.path.exists(twistd_pid_file),
552 (twistd_pid_file, os.listdir(os.path.dirname(twistd_pid_file))))
553 return self.run_bintahoe(["--quiet", "stop", c1])
555 d.addBoth(self._remove, exit_trigger_file)
558 def test_client(self):
559 self.skip_if_cannot_daemonize()
561 basedir = self.workdir("test_client")
562 c1 = os.path.join(basedir, "c1")
563 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
564 twistd_pid_file = os.path.join(c1, "twistd.pid")
565 portnum_file = os.path.join(c1, "client.port")
566 node_url_file = os.path.join(c1, "node.url")
567 config_file = os.path.join(c1, "tahoe.cfg")
569 d = self.run_bintahoe(["--quiet", "create-node", "--basedir", c1, "--webport", "0"])
571 out, err, rc_or_sig = res
572 self.failUnlessEqual(rc_or_sig, 0)
574 # Check that the --webport option worked.
575 config = fileutil.read(config_file)
576 self.failUnlessIn('\nweb.port = 0\n', config)
578 # By writing this file, we get two minutes before the client will exit. This ensures
579 # that even if the 'stop' command doesn't work (and the test fails), the client should
581 fileutil.write(exit_trigger_file, "")
582 # now it's safe to start the node
586 return self.run_bintahoe(["--quiet", "start", c1])
587 d.addCallback(_start)
590 out, err, rc_or_sig = res
591 fileutil.write(exit_trigger_file, "")
592 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
593 self.failUnlessEqual(rc_or_sig, 0, errstr)
594 self.failUnlessEqual(out, "", errstr)
595 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
597 # the parent (twistd) has exited. However, twistd writes the pid
598 # from the child, not the parent, so we can't expect twistd.pid
599 # to exist quite yet.
601 # the node is running, but it might not have made it past the
602 # first reactor turn yet, and if we kill it too early, it won't
603 # remove the twistd.pid file. So wait until it does something
604 # that we know it won't do until after the first turn.
607 def _node_has_started():
608 # this depends upon both files being created atomically
609 return os.path.exists(node_url_file) and os.path.exists(portnum_file)
610 d.addCallback(lambda res: self.poll(_node_has_started))
613 # read the client.port file so we can check that its contents
614 # don't change on restart
615 self.portnum = fileutil.read(portnum_file)
617 fileutil.write(exit_trigger_file, "")
618 self.failUnless(os.path.exists(twistd_pid_file))
620 # rm this so we can detect when the second incarnation is ready
621 os.unlink(node_url_file)
622 return self.run_bintahoe(["--quiet", "restart", c1])
623 d.addCallback(_started)
626 out, err, rc_or_sig = res
628 fileutil.write(exit_trigger_file, "")
629 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
630 self.failUnlessEqual(rc_or_sig, 0, errstr)
631 self.failUnlessEqual(out, "", errstr)
632 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
635 # again, the second incarnation of the node might not be ready yet,
636 # so poll until it is
637 d.addCallback(lambda res: self.poll(_node_has_started))
639 def _check_same_port(res):
640 self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
641 d.addCallback(_check_same_port)
643 # now we can kill it. TODO: On a slow machine, the node might kill
644 # itself before we get a chance to, especially if spawning the
645 # 'tahoe stop' command takes a while.
647 fileutil.write(exit_trigger_file, "")
648 self.failUnless(os.path.exists(twistd_pid_file),
649 (twistd_pid_file, os.listdir(os.path.dirname(twistd_pid_file))))
650 return self.run_bintahoe(["--quiet", "stop", c1])
654 out, err, rc_or_sig = res
656 fileutil.write(exit_trigger_file, "")
657 # the parent has exited by now
658 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
659 self.failUnlessEqual(rc_or_sig, 0, errstr)
660 self.failUnlessEqual(out, "", errstr)
661 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
662 # the parent was supposed to poll and wait until it sees
663 # twistd.pid go away before it exits, so twistd.pid should be
665 self.failIf(os.path.exists(twistd_pid_file))
667 d.addBoth(self._remove, exit_trigger_file)
670 def _remove(self, res, file):
671 fileutil.remove(file)
674 def test_baddir(self):
675 self.skip_if_cannot_daemonize()
676 basedir = self.workdir("test_baddir")
677 fileutil.make_dirs(basedir)
679 d = self.run_bintahoe(["--quiet", "start", "--basedir", basedir])
681 out, err, rc_or_sig = res
682 self.failUnlessEqual(rc_or_sig, 1)
683 self.failUnless("is not a recognizable node directory" in err, err)
686 def _then_stop_it(res):
687 return self.run_bintahoe(["--quiet", "stop", "--basedir", basedir])
688 d.addCallback(_then_stop_it)
691 out, err, rc_or_sig = res
692 self.failUnlessEqual(rc_or_sig, 2)
693 self.failUnless("does not look like a running node directory" in err)
696 def _then_start_in_bogus_basedir(res):
697 not_a_dir = os.path.join(basedir, "bogus")
698 return self.run_bintahoe(["--quiet", "start", "--basedir", not_a_dir])
699 d.addCallback(_then_start_in_bogus_basedir)
702 out, err, rc_or_sig = res
703 self.failUnlessEqual(rc_or_sig, 1)
704 self.failUnlessIn("does not look like a directory at all", err)
708 def test_keygen(self):
709 self.skip_if_cannot_daemonize()
711 basedir = self.workdir("test_keygen")
712 c1 = os.path.join(basedir, "c1")
713 twistd_pid_file = os.path.join(c1, "twistd.pid")
714 keygen_furl_file = os.path.join(c1, "key_generator.furl")
716 d = self.run_bintahoe(["--quiet", "create-key-generator", "--basedir", c1])
718 out, err, rc_or_sig = res
719 self.failUnlessEqual(rc_or_sig, 0)
723 return self.run_bintahoe(["--quiet", "start", c1])
724 d.addCallback(_start)
727 out, err, rc_or_sig = res
728 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
729 self.failUnlessEqual(rc_or_sig, 0, errstr)
730 self.failUnlessEqual(out, "", errstr)
731 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
733 # the parent (twistd) has exited. However, twistd writes the pid
734 # from the child, not the parent, so we can't expect twistd.pid
735 # to exist quite yet.
737 # the node is running, but it might not have made it past the
738 # first reactor turn yet, and if we kill it too early, it won't
739 # remove the twistd.pid file. So wait until it does something
740 # that we know it won't do until after the first turn.
743 def _node_has_started():
744 return os.path.exists(keygen_furl_file)
745 d.addCallback(lambda res: self.poll(_node_has_started))
748 self.failUnless(os.path.exists(twistd_pid_file))
749 # rm this so we can detect when the second incarnation is ready
750 os.unlink(keygen_furl_file)
751 return self.run_bintahoe(["--quiet", "restart", c1])
752 d.addCallback(_started)
755 out, err, rc_or_sig = res
756 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
757 self.failUnlessEqual(rc_or_sig, 0, errstr)
758 self.failUnlessEqual(out, "", errstr)
759 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
762 # again, the second incarnation of the node might not be ready yet,
763 # so poll until it is
764 d.addCallback(lambda res: self.poll(_node_has_started))
766 # now we can kill it. TODO: On a slow machine, the node might kill
767 # itself before we get a chance too, especially if spawning the
768 # 'tahoe stop' command takes a while.
770 self.failUnless(os.path.exists(twistd_pid_file))
771 return self.run_bintahoe(["--quiet", "stop", c1])
775 out, err, rc_or_sig = res
776 # the parent has exited by now
777 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
778 self.failUnlessEqual(rc_or_sig, 0, errstr)
779 self.failUnlessEqual(out, "", errstr)
780 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
781 # the parent was supposed to poll and wait until it sees
782 # twistd.pid go away before it exits, so twistd.pid should be
784 self.failIf(os.path.exists(twistd_pid_file))