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, get_filesystem_encoding
11 from allmydata.scripts import runner
12 from allmydata.client import Client
13 from allmydata.test import common_util
19 def get_root_from_file(src):
20 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(src))))
22 root = os.path.dirname(srcdir)
23 if os.path.basename(srcdir) == 'site-packages':
24 if re.search(r'python.+\..+', os.path.basename(root)):
25 root = os.path.dirname(root)
26 root = os.path.dirname(root)
27 elif os.path.basename(root) == 'src':
28 root = os.path.dirname(root)
32 srcfile = allmydata.__file__
33 rootdir = get_root_from_file(srcfile)
35 if hasattr(sys, 'frozen'):
36 bintahoe = os.path.join(rootdir, 'tahoe')
37 if sys.platform == "win32" and os.path.exists(bintahoe + '.exe'):
40 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
41 if sys.platform == "win32":
42 bintahoe += '.pyscript'
43 if not os.path.exists(bintahoe):
44 alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
45 if os.path.exists(alt_bintahoe):
46 bintahoe = alt_bintahoe
49 class RunBinTahoeMixin:
50 def skip_if_cannot_run_bintahoe(self):
51 if not os.path.exists(bintahoe):
52 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,))
54 def skip_if_cannot_daemonize(self):
55 self.skip_if_cannot_run_bintahoe()
56 if runtime.platformType == "win32":
57 # twistd on windows doesn't daemonize. cygwin should work normally.
58 raise unittest.SkipTest("twistd does not fork under windows")
60 def run_bintahoe(self, args, stdin=None, python_options=[], env=None):
61 self.skip_if_cannot_run_bintahoe()
63 if hasattr(sys, 'frozen'):
65 raise unittest.SkipTest("This test doesn't apply to frozen builds.")
66 command = [bintahoe] + args
68 command = [sys.executable] + python_options + [bintahoe] + args
73 stdin_stream = subprocess.PIPE
76 p = subprocess.Popen(command, stdin=stdin_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
77 (out, err) = p.communicate(stdin)
78 return (out, err, p.returncode)
79 return threads.deferToThread(_run)
82 class BinTahoe(common_util.SignalMixin, unittest.TestCase, RunBinTahoeMixin):
83 def _check_right_code(self, file_to_check):
84 root_to_check = get_root_from_file(file_to_check)
85 if os.path.basename(root_to_check) == 'dist':
86 root_to_check = os.path.dirname(root_to_check)
88 cwd = os.path.normcase(os.path.realpath("."))
89 root_from_cwd = os.path.dirname(cwd)
90 if os.path.basename(root_from_cwd) == 'src':
91 root_from_cwd = os.path.dirname(root_from_cwd)
93 # This is needed if we are running in a temporary directory created by 'make tmpfstest'.
94 if os.path.basename(root_from_cwd).startswith('tmp'):
95 root_from_cwd = os.path.dirname(root_from_cwd)
97 same = (root_from_cwd == root_to_check)
100 same = os.path.samefile(root_from_cwd, root_to_check)
101 except AttributeError, e:
105 msg = ("We seem to be testing the code at %r,\n"
106 "(according to the source filename %r),\n"
107 "but expected to be testing the code at %r.\n"
108 % (root_to_check, file_to_check, root_from_cwd))
110 root_from_cwdu = os.path.dirname(os.path.normcase(os.path.normpath(os.getcwdu())))
111 if os.path.basename(root_from_cwdu) == u'src':
112 root_from_cwdu = os.path.dirname(root_from_cwdu)
114 # This is needed if we are running in a temporary directory created by 'make tmpfstest'.
115 if os.path.basename(root_from_cwdu).startswith(u'tmp'):
116 root_from_cwdu = os.path.dirname(root_from_cwdu)
118 if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
119 msg += ("However, this may be a false alarm because the current directory path\n"
120 "is not representable in the filesystem encoding. Please run the tests\n"
121 "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
122 raise unittest.SkipTest(msg)
124 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
127 def test_the_right_code(self):
128 self._check_right_code(srcfile)
130 def test_import_in_repl(self):
131 d = self.run_bintahoe(["debug", "repl"],
132 stdin="import allmydata; print; print allmydata.__file__")
134 out, err, rc_or_sig = res
135 self.failUnlessEqual(rc_or_sig, 0, str(res))
136 lines = out.splitlines()
137 self.failUnlessIn('>>>', lines[0], str(res))
138 self._check_right_code(lines[1])
141 # The timeout was exceeded on FreeStorm's CentOS5-i386.
142 test_import_in_repl.timeout = 480
145 d = self.run_bintahoe(["--version-and-path"])
147 from allmydata import normalized_version
149 out, err, rc_or_sig = res
150 self.failUnlessEqual(rc_or_sig, 0, str(res))
152 # Fail unless the __appname__ package is *this* version *and*
153 # was loaded from *this* source directory.
155 required_verstr = str(allmydata.__version__)
157 self.failIfEqual(required_verstr, "unknown",
158 "We don't know our version, because this distribution didn't come "
159 "with a _version.py and 'setup.py update_version' hasn't been run.")
161 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
162 info = repr((res, allmydata.__appname__, required_verstr, srcdir))
164 appverpath = out.split(')')[0]
165 (appverfull, path) = appverpath.split('] (')
166 (appver, comment) = appverfull.split(' [')
167 (branch, full_version) = comment.split(': ')
168 (app, ver) = appver.split(': ')
170 self.failUnlessEqual(app, allmydata.__appname__, info)
171 norm_ver = normalized_version(ver)
172 norm_required = normalized_version(required_verstr)
173 self.failUnlessEqual(norm_ver, norm_required, info)
174 self.failUnlessEqual(path, srcdir, info)
175 self.failUnlessEqual(branch, allmydata.branch)
176 self.failUnlessEqual(full_version, allmydata.full_version)
180 def test_unicode_arguments_and_output(self):
181 self.skip_if_cannot_run_bintahoe()
185 tricky_arg = unicode_to_argv(tricky, mangle=True)
186 tricky_out = unicode_to_output(tricky)
187 except UnicodeEncodeError:
188 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
190 d = self.run_bintahoe([tricky_arg])
192 out, err, rc_or_sig = res
193 self.failUnlessEqual(rc_or_sig, 1, str(res))
194 self.failUnlessIn("Unknown command: "+tricky_out, out)
198 def test_run_with_python_options(self):
199 # -t is a harmless option that warns about tabs.
200 d = self.run_bintahoe(["--version"], python_options=["-t"])
202 out, err, rc_or_sig = res
203 self.failUnlessEqual(rc_or_sig, 0, str(res))
204 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
208 def test_version_no_noise(self):
209 self.skip_if_cannot_run_bintahoe()
211 d = self.run_bintahoe(["--version"])
213 out, err, rc_or_sig = res
214 self.failUnlessEqual(rc_or_sig, 0, str(res))
215 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
216 self.failIfIn("DeprecationWarning", out, str(res))
217 errlines = err.split("\n")
218 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
219 and "from pkg_resources import load_entry_point" not in line)], str(res))
221 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
226 class CreateNode(unittest.TestCase):
227 # exercise "tahoe create-node", create-introducer,
228 # create-key-generator, and create-stats-gatherer, by calling the
229 # corresponding code as a subroutine.
231 def workdir(self, name):
232 basedir = os.path.join("test_runner", "CreateNode", name)
233 fileutil.make_dirs(basedir)
236 def run_tahoe(self, argv):
237 out,err = StringIO(), StringIO()
238 rc = runner.runner(argv, stdout=out, stderr=err)
239 return rc, out.getvalue(), err.getvalue()
241 def do_create(self, kind):
242 basedir = self.workdir("test_" + kind)
243 command = "create-" + kind
244 is_client = kind in ("node", "client")
245 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
247 n1 = os.path.join(basedir, command + "-n1")
248 argv = ["--quiet", command, "--basedir", n1]
249 rc, out, err = self.run_tahoe(argv)
250 self.failUnlessEqual(err, "")
251 self.failUnlessEqual(out, "")
252 self.failUnlessEqual(rc, 0)
253 self.failUnless(os.path.exists(n1))
254 self.failUnless(os.path.exists(os.path.join(n1, tac)))
257 # tahoe.cfg should exist, and should have storage enabled for
258 # 'create-node', and disabled for 'create-client'.
259 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
260 self.failUnless(os.path.exists(tahoe_cfg))
261 content = fileutil.read(tahoe_cfg).replace('\r\n', '\n')
263 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = false\n", content), content)
265 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = true\n", content), content)
266 self.failUnless("\nreserved_space = 1G\n" in content)
268 self.failUnless(re.search(r"\n\[magic_folder\]\n#.*\n#enabled = false\n", content), content)
270 # creating the node a second time should be rejected
271 rc, out, err = self.run_tahoe(argv)
272 self.failIfEqual(rc, 0, str((out, err, rc)))
273 self.failUnlessEqual(out, "")
274 self.failUnless("is not empty." in err)
276 # Fail if there is a non-empty line that doesn't end with a
278 for line in err.splitlines():
279 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
281 # test that the non --basedir form works too
282 n2 = os.path.join(basedir, command + "-n2")
283 argv = ["--quiet", command, n2]
284 rc, out, err = self.run_tahoe(argv)
285 self.failUnlessEqual(err, "")
286 self.failUnlessEqual(out, "")
287 self.failUnlessEqual(rc, 0)
288 self.failUnless(os.path.exists(n2))
289 self.failUnless(os.path.exists(os.path.join(n2, tac)))
291 # test the --node-directory form
292 n3 = os.path.join(basedir, command + "-n3")
293 argv = ["--quiet", "--node-directory", n3, command]
294 rc, out, err = self.run_tahoe(argv)
295 self.failUnlessEqual(err, "")
296 self.failUnlessEqual(out, "")
297 self.failUnlessEqual(rc, 0)
298 self.failUnless(os.path.exists(n3))
299 self.failUnless(os.path.exists(os.path.join(n3, tac)))
301 if kind in ("client", "node", "introducer"):
302 # test that the output (without --quiet) includes the base directory
303 n4 = os.path.join(basedir, command + "-n4")
305 rc, out, err = self.run_tahoe(argv)
306 self.failUnlessEqual(err, "")
307 self.failUnlessIn(" created in ", out)
308 self.failUnlessIn(n4, out)
309 self.failIfIn("\\\\?\\", out)
310 self.failUnlessEqual(rc, 0)
311 self.failUnless(os.path.exists(n4))
312 self.failUnless(os.path.exists(os.path.join(n4, tac)))
314 # make sure it rejects too many arguments
315 argv = [command, "basedir", "extraarg"]
316 self.failUnlessRaises(usage.UsageError,
320 # when creating a non-client, there is no default for the basedir
323 self.failUnlessRaises(usage.UsageError,
329 self.do_create("node")
331 def test_client(self):
332 # create-client should behave like create-node --no-storage.
333 self.do_create("client")
335 def test_introducer(self):
336 self.do_create("introducer")
338 def test_key_generator(self):
339 self.do_create("key-generator")
341 def test_stats_gatherer(self):
342 self.do_create("stats-gatherer")
344 def test_subcommands(self):
345 # no arguments should trigger a command listing, via UsageError
346 self.failUnlessRaises(usage.UsageError,
352 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
354 # exercise "tahoe start", for both introducer, client node, and
355 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
356 # get us figleaf-based line-level coverage, but it does a better job of
357 # confirming that the user can actually run "./bin/tahoe start" and
358 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
359 # the like correctly.
361 # This doesn't work on cygwin (it hangs forever), so we skip this test
362 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
363 # work on cygwin: twisted seems unable to provide a version of
364 # spawnProcess which really works there.
366 def workdir(self, name):
367 basedir = os.path.join("test_runner", "RunNode", name)
368 fileutil.make_dirs(basedir)
371 def test_introducer(self):
372 self.skip_if_cannot_daemonize()
374 basedir = self.workdir("test_introducer")
375 c1 = os.path.join(basedir, "c1")
376 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
377 twistd_pid_file = os.path.join(c1, "twistd.pid")
378 introducer_furl_file = os.path.join(c1, "private", "introducer.furl")
379 portnum_file = os.path.join(c1, "introducer.port")
380 node_url_file = os.path.join(c1, "node.url")
381 config_file = os.path.join(c1, "tahoe.cfg")
383 d = self.run_bintahoe(["--quiet", "create-introducer", "--basedir", c1])
385 out, err, rc_or_sig = res
386 self.failUnlessEqual(rc_or_sig, 0)
388 # This makes sure that node.url is written, which allows us to
389 # detect when the introducer restarts in _node_has_restarted below.
390 config = fileutil.read(config_file)
391 self.failUnlessIn('\nweb.port = \n', config)
392 fileutil.write(config_file, config.replace('\nweb.port = \n', '\nweb.port = 0\n'))
394 # by writing this file, we get ten seconds before the node will
395 # exit. This insures that even if the test fails (and the 'stop'
396 # command doesn't work), the client should still terminate.
397 fileutil.write(exit_trigger_file, "")
398 # now it's safe to start the node
401 def _then_start_the_node(res):
402 return self.run_bintahoe(["--quiet", "start", c1])
403 d.addCallback(_then_start_the_node)
406 out, err, rc_or_sig = res
408 fileutil.write(exit_trigger_file, "")
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.
414 # the parent (twistd) has exited. However, twistd writes the pid
415 # from the child, not the parent, so we can't expect twistd.pid
416 # to exist quite yet.
418 # the node is running, but it might not have made it past the
419 # first reactor turn yet, and if we kill it too early, it won't
420 # remove the twistd.pid file. So wait until it does something
421 # that we know it won't do until after the first turn.
424 def _node_has_started():
425 return os.path.exists(introducer_furl_file)
426 d.addCallback(lambda res: self.poll(_node_has_started))
429 # read the introducer.furl and introducer.port files so we can
430 # check that their contents don't change on restart
431 self.furl = fileutil.read(introducer_furl_file)
432 self.failUnless(os.path.exists(portnum_file))
433 self.portnum = fileutil.read(portnum_file)
435 fileutil.write(exit_trigger_file, "")
436 self.failUnless(os.path.exists(twistd_pid_file))
437 self.failUnless(os.path.exists(node_url_file))
439 # rm this so we can detect when the second incarnation is ready
440 os.unlink(node_url_file)
441 return self.run_bintahoe(["--quiet", "restart", c1])
442 d.addCallback(_started)
445 out, err, rc_or_sig = res
446 fileutil.write(exit_trigger_file, "")
447 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
448 self.failUnlessEqual(rc_or_sig, 0, errstr)
449 self.failUnlessEqual(out, "", errstr)
450 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
453 # Again, the second incarnation of the node might not be ready yet,
454 # so poll until it is. This time introducer_furl_file already
455 # exists, so we check for the existence of node_url_file instead.
456 def _node_has_restarted():
457 return os.path.exists(node_url_file) and os.path.exists(portnum_file)
458 d.addCallback(lambda res: self.poll(_node_has_restarted))
460 def _check_same_furl_and_port(res):
461 self.failUnless(os.path.exists(introducer_furl_file))
462 self.failUnlessEqual(self.furl, fileutil.read(introducer_furl_file))
463 self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
464 d.addCallback(_check_same_furl_and_port)
466 # Now we can kill it. TODO: On a slow machine, the node might kill
467 # itself before we get a chance to, especially if spawning the
468 # 'tahoe stop' command takes a while.
470 fileutil.write(exit_trigger_file, "")
471 self.failUnless(os.path.exists(twistd_pid_file))
473 return self.run_bintahoe(["--quiet", "stop", c1])
476 def _after_stopping(res):
477 out, err, rc_or_sig = res
478 fileutil.write(exit_trigger_file, "")
479 # the parent has exited by now
480 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
481 self.failUnlessEqual(rc_or_sig, 0, errstr)
482 self.failUnlessEqual(out, "", errstr)
483 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
484 # the parent was supposed to poll and wait until it sees
485 # twistd.pid go away before it exits, so twistd.pid should be
487 self.failIf(os.path.exists(twistd_pid_file))
488 d.addCallback(_after_stopping)
489 d.addBoth(self._remove, exit_trigger_file)
491 # This test has hit a 240-second timeout on our feisty2.5 buildslave, and a 480-second timeout
492 # on Francois's Lenny-armv5tel buildslave.
493 test_introducer.timeout = 960
495 def test_client_no_noise(self):
496 self.skip_if_cannot_daemonize()
498 basedir = self.workdir("test_client_no_noise")
499 c1 = os.path.join(basedir, "c1")
500 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
501 twistd_pid_file = os.path.join(c1, "twistd.pid")
502 portnum_file = os.path.join(c1, "client.port")
504 d = self.run_bintahoe(["--quiet", "create-client", "--basedir", c1, "--webport", "0"])
506 out, err, rc_or_sig = res
507 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
508 assert rc_or_sig == 0, errstr
509 self.failUnlessEqual(rc_or_sig, 0)
511 # By writing this file, we get two minutes before the client will exit. This ensures
512 # that even if the 'stop' command doesn't work (and the test fails), the client should
514 fileutil.write(exit_trigger_file, "")
515 # now it's safe to start the node
519 return self.run_bintahoe(["--quiet", "start", c1])
520 d.addCallback(_start)
523 out, err, rc_or_sig = res
524 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
525 fileutil.write(exit_trigger_file, "")
526 self.failUnlessEqual(rc_or_sig, 0, errstr)
527 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
528 errlines = err.split("\n")
529 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
530 and "from pkg_resources import load_entry_point" not in line)], errstr)
532 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
534 # the parent (twistd) has exited. However, twistd writes the pid
535 # from the child, not the parent, so we can't expect twistd.pid
536 # to exist quite yet.
538 # the node is running, but it might not have made it past the
539 # first reactor turn yet, and if we kill it too early, it won't
540 # remove the twistd.pid file. So wait until it does something
541 # that we know it won't do until after the first turn.
544 def _node_has_started():
545 return os.path.exists(portnum_file)
546 d.addCallback(lambda res: self.poll(_node_has_started))
548 # now we can kill it. TODO: On a slow machine, the node might kill
549 # itself before we get a chance to, especially if spawning the
550 # 'tahoe stop' command takes a while.
552 self.failUnless(os.path.exists(twistd_pid_file),
553 (twistd_pid_file, os.listdir(os.path.dirname(twistd_pid_file))))
554 return self.run_bintahoe(["--quiet", "stop", c1])
556 d.addBoth(self._remove, exit_trigger_file)
559 def test_client(self):
560 self.skip_if_cannot_daemonize()
562 basedir = self.workdir("test_client")
563 c1 = os.path.join(basedir, "c1")
564 exit_trigger_file = os.path.join(c1, Client.EXIT_TRIGGER_FILE)
565 twistd_pid_file = os.path.join(c1, "twistd.pid")
566 portnum_file = os.path.join(c1, "client.port")
567 node_url_file = os.path.join(c1, "node.url")
568 config_file = os.path.join(c1, "tahoe.cfg")
570 d = self.run_bintahoe(["--quiet", "create-node", "--basedir", c1, "--webport", "0"])
572 out, err, rc_or_sig = res
573 self.failUnlessEqual(rc_or_sig, 0)
575 # Check that the --webport option worked.
576 config = fileutil.read(config_file)
577 self.failUnlessIn('\nweb.port = 0\n', config)
579 # By writing this file, we get two minutes before the client will exit. This ensures
580 # that even if the 'stop' command doesn't work (and the test fails), the client should
582 fileutil.write(exit_trigger_file, "")
583 # now it's safe to start the node
587 return self.run_bintahoe(["--quiet", "start", c1])
588 d.addCallback(_start)
591 out, err, rc_or_sig = res
592 fileutil.write(exit_trigger_file, "")
593 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
594 self.failUnlessEqual(rc_or_sig, 0, errstr)
595 self.failUnlessEqual(out, "", errstr)
596 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
598 # the parent (twistd) has exited. However, twistd writes the pid
599 # from the child, not the parent, so we can't expect twistd.pid
600 # to exist quite yet.
602 # the node is running, but it might not have made it past the
603 # first reactor turn yet, and if we kill it too early, it won't
604 # remove the twistd.pid file. So wait until it does something
605 # that we know it won't do until after the first turn.
608 def _node_has_started():
609 # this depends upon both files being created atomically
610 return os.path.exists(node_url_file) and os.path.exists(portnum_file)
611 d.addCallback(lambda res: self.poll(_node_has_started))
614 # read the client.port file so we can check that its contents
615 # don't change on restart
616 self.portnum = fileutil.read(portnum_file)
618 fileutil.write(exit_trigger_file, "")
619 self.failUnless(os.path.exists(twistd_pid_file))
621 # rm this so we can detect when the second incarnation is ready
622 os.unlink(node_url_file)
623 return self.run_bintahoe(["--quiet", "restart", c1])
624 d.addCallback(_started)
627 out, err, rc_or_sig = res
629 fileutil.write(exit_trigger_file, "")
630 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
631 self.failUnlessEqual(rc_or_sig, 0, errstr)
632 self.failUnlessEqual(out, "", errstr)
633 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
636 # again, the second incarnation of the node might not be ready yet,
637 # so poll until it is
638 d.addCallback(lambda res: self.poll(_node_has_started))
640 def _check_same_port(res):
641 self.failUnlessEqual(self.portnum, fileutil.read(portnum_file))
642 d.addCallback(_check_same_port)
644 # now we can kill it. TODO: On a slow machine, the node might kill
645 # itself before we get a chance to, especially if spawning the
646 # 'tahoe stop' command takes a while.
648 fileutil.write(exit_trigger_file, "")
649 self.failUnless(os.path.exists(twistd_pid_file),
650 (twistd_pid_file, os.listdir(os.path.dirname(twistd_pid_file))))
651 return self.run_bintahoe(["--quiet", "stop", c1])
655 out, err, rc_or_sig = res
657 fileutil.write(exit_trigger_file, "")
658 # the parent has exited by now
659 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
660 self.failUnlessEqual(rc_or_sig, 0, errstr)
661 self.failUnlessEqual(out, "", errstr)
662 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
663 # the parent was supposed to poll and wait until it sees
664 # twistd.pid go away before it exits, so twistd.pid should be
666 self.failIf(os.path.exists(twistd_pid_file))
668 d.addBoth(self._remove, exit_trigger_file)
671 def _remove(self, res, file):
672 fileutil.remove(file)
675 def test_baddir(self):
676 self.skip_if_cannot_daemonize()
677 basedir = self.workdir("test_baddir")
678 fileutil.make_dirs(basedir)
680 d = self.run_bintahoe(["--quiet", "start", "--basedir", basedir])
682 out, err, rc_or_sig = res
683 self.failUnlessEqual(rc_or_sig, 1)
684 self.failUnless("is not a recognizable node directory" in err, err)
687 def _then_stop_it(res):
688 return self.run_bintahoe(["--quiet", "stop", "--basedir", basedir])
689 d.addCallback(_then_stop_it)
692 out, err, rc_or_sig = res
693 self.failUnlessEqual(rc_or_sig, 2)
694 self.failUnless("does not look like a running node directory" in err)
697 def _then_start_in_bogus_basedir(res):
698 not_a_dir = os.path.join(basedir, "bogus")
699 return self.run_bintahoe(["--quiet", "start", "--basedir", not_a_dir])
700 d.addCallback(_then_start_in_bogus_basedir)
703 out, err, rc_or_sig = res
704 self.failUnlessEqual(rc_or_sig, 1)
705 self.failUnlessIn("does not look like a directory at all", err)
709 def test_keygen(self):
710 self.skip_if_cannot_daemonize()
712 basedir = self.workdir("test_keygen")
713 c1 = os.path.join(basedir, "c1")
714 twistd_pid_file = os.path.join(c1, "twistd.pid")
715 keygen_furl_file = os.path.join(c1, "key_generator.furl")
717 d = self.run_bintahoe(["--quiet", "create-key-generator", "--basedir", c1])
719 out, err, rc_or_sig = res
720 self.failUnlessEqual(rc_or_sig, 0)
724 return self.run_bintahoe(["--quiet", "start", c1])
725 d.addCallback(_start)
728 out, err, rc_or_sig = res
729 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
730 self.failUnlessEqual(rc_or_sig, 0, errstr)
731 self.failUnlessEqual(out, "", errstr)
732 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
734 # the parent (twistd) has exited. However, twistd writes the pid
735 # from the child, not the parent, so we can't expect twistd.pid
736 # to exist quite yet.
738 # the node is running, but it might not have made it past the
739 # first reactor turn yet, and if we kill it too early, it won't
740 # remove the twistd.pid file. So wait until it does something
741 # that we know it won't do until after the first turn.
744 def _node_has_started():
745 return os.path.exists(keygen_furl_file)
746 d.addCallback(lambda res: self.poll(_node_has_started))
749 self.failUnless(os.path.exists(twistd_pid_file))
750 # rm this so we can detect when the second incarnation is ready
751 os.unlink(keygen_furl_file)
752 return self.run_bintahoe(["--quiet", "restart", c1])
753 d.addCallback(_started)
756 out, err, rc_or_sig = res
757 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
758 self.failUnlessEqual(rc_or_sig, 0, errstr)
759 self.failUnlessEqual(out, "", errstr)
760 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
763 # again, the second incarnation of the node might not be ready yet,
764 # so poll until it is
765 d.addCallback(lambda res: self.poll(_node_has_started))
767 # now we can kill it. TODO: On a slow machine, the node might kill
768 # itself before we get a chance too, especially if spawning the
769 # 'tahoe stop' command takes a while.
771 self.failUnless(os.path.exists(twistd_pid_file))
772 return self.run_bintahoe(["--quiet", "stop", c1])
776 out, err, rc_or_sig = res
777 # the parent has exited by now
778 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
779 self.failUnlessEqual(rc_or_sig, 0, errstr)
780 self.failUnlessEqual(out, "", errstr)
781 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
782 # the parent was supposed to poll and wait until it sees
783 # twistd.pid go away before it exits, so twistd.pid should be
785 self.failIf(os.path.exists(twistd_pid_file))