1 from twisted.trial import unittest
3 from twisted.python import usage, runtime
4 from twisted.internet import threads
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 if hasattr(sys, 'frozen'):
34 bintahoe = os.path.join(rootdir, 'tahoe')
35 if sys.platform == "win32" and os.path.exists(bintahoe + '.exe'):
38 bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
39 if sys.platform == "win32":
40 bintahoe += '.pyscript'
41 if not os.path.exists(bintahoe):
42 alt_bintahoe = os.path.join(rootdir, 'Scripts', 'tahoe.pyscript')
43 if os.path.exists(alt_bintahoe):
44 bintahoe = alt_bintahoe
47 class RunBinTahoeMixin:
48 def skip_if_cannot_run_bintahoe(self):
49 if not os.path.exists(bintahoe):
50 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,))
52 def skip_if_cannot_daemonize(self):
53 self.skip_if_cannot_run_bintahoe()
54 if runtime.platformType == "win32":
55 # twistd on windows doesn't daemonize. cygwin should work normally.
56 raise unittest.SkipTest("twistd does not fork under windows")
58 def run_bintahoe(self, args, stdin=None, python_options=[], env=None):
59 self.skip_if_cannot_run_bintahoe()
61 if hasattr(sys, 'frozen'):
63 raise unittest.SkipTest("This test doesn't apply to frozen builds.")
64 command = [bintahoe] + args
66 command = [sys.executable] + python_options + [bintahoe] + args
71 stdin_stream = subprocess.PIPE
74 p = subprocess.Popen(command, stdin=stdin_stream, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
75 (out, err) = p.communicate(stdin)
76 return (out, err, p.returncode)
77 return threads.deferToThread(_run)
80 class BinTahoe(common_util.SignalMixin, unittest.TestCase, RunBinTahoeMixin):
81 def _check_right_code(self, file_to_check):
82 root_to_check = get_root_from_file(file_to_check)
83 if os.path.basename(root_to_check) == 'dist':
84 root_to_check = os.path.dirname(root_to_check)
86 cwd = os.path.normcase(os.path.realpath("."))
87 root_from_cwd = os.path.dirname(cwd)
88 if os.path.basename(root_from_cwd) == 'src':
89 root_from_cwd = os.path.dirname(root_from_cwd)
91 same = (root_from_cwd == root_to_check)
94 same = os.path.samefile(root_from_cwd, root_to_check)
95 except AttributeError, e:
99 msg = ("We seem to be testing the code at %r,\n"
100 "(according to the source filename %r),\n"
101 "but expected to be testing the code at %r.\n"
102 % (root_to_check, file_to_check, root_from_cwd))
104 root_from_cwdu = os.path.dirname(os.path.normcase(os.path.normpath(os.getcwdu())))
105 if os.path.basename(root_from_cwdu) == u'src':
106 root_from_cwdu = os.path.dirname(root_from_cwdu)
108 if not isinstance(root_from_cwd, unicode) and root_from_cwd.decode(get_filesystem_encoding(), 'replace') != root_from_cwdu:
109 msg += ("However, this may be a false alarm because the current directory path\n"
110 "is not representable in the filesystem encoding. Please run the tests\n"
111 "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
112 raise unittest.SkipTest(msg)
114 msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
117 def test_the_right_code(self):
118 self._check_right_code(srcfile)
120 def test_import_in_repl(self):
121 d = self.run_bintahoe(["debug", "repl"],
122 stdin="import allmydata; print; print allmydata.__file__")
124 out, err, rc_or_sig = res
125 self.failUnlessEqual(rc_or_sig, 0, str(res))
126 lines = out.splitlines()
127 self.failUnlessIn('>>>', lines[0], str(res))
128 self._check_right_code(lines[1])
131 # The timeout was exceeded on FreeStorm's CentOS5-i386.
132 test_import_in_repl.timeout = 480
135 d = self.run_bintahoe(["--version-and-path"])
137 from allmydata import normalized_version
139 out, err, rc_or_sig = res
140 self.failUnlessEqual(rc_or_sig, 0, str(res))
142 # Fail unless the allmydata-tahoe package is *this* version *and*
143 # was loaded from *this* source directory.
145 required_verstr = str(allmydata.__version__)
147 self.failIfEqual(required_verstr, "unknown",
148 "We don't know our version, because this distribution didn't come "
149 "with a _version.py and 'setup.py darcsver' hasn't been run.")
151 srcdir = os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
152 info = repr((res, allmydata.__appname__, required_verstr, srcdir))
154 appverpath = out.split(')')[0]
155 (appver, path) = appverpath.split(' (')
156 (app, ver) = appver.split(': ')
158 self.failUnlessEqual(app, allmydata.__appname__, info)
159 norm_ver = normalized_version(ver)
160 norm_required = normalized_version(required_verstr)
161 self.failUnlessEqual(norm_ver, norm_required, info)
162 self.failUnlessEqual(path, srcdir, info)
166 def test_unicode_arguments_and_output(self):
167 self.skip_if_cannot_run_bintahoe()
171 tricky_arg = unicode_to_argv(tricky, mangle=True)
172 tricky_out = unicode_to_output(tricky)
173 except UnicodeEncodeError:
174 raise unittest.SkipTest("A non-ASCII argument/output could not be encoded on this platform.")
176 d = self.run_bintahoe([tricky_arg])
178 out, err, rc_or_sig = res
179 self.failUnlessEqual(rc_or_sig, 1, str(res))
180 self.failUnlessIn("Unknown command: "+tricky_out, out)
184 def test_run_with_python_options(self):
185 # -t is a harmless option that warns about tabs.
186 d = self.run_bintahoe(["--version"], python_options=["-t"])
188 out, err, rc_or_sig = res
189 self.failUnlessEqual(rc_or_sig, 0, str(res))
190 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
194 def test_version_no_noise(self):
195 self.skip_if_cannot_run_bintahoe()
197 from allmydata import get_package_versions, normalized_version
198 twisted_ver = get_package_versions()['Twisted']
200 if not normalized_version(twisted_ver) >= normalized_version('9.0.0'):
201 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
203 d = self.run_bintahoe(["--version"])
205 out, err, rc_or_sig = res
206 self.failUnlessEqual(rc_or_sig, 0, str(res))
207 self.failUnless(out.startswith(allmydata.__appname__+':'), str(res))
208 self.failIfIn("DeprecationWarning", out, str(res))
209 errlines = err.split("\n")
210 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
211 and "from pkg_resources import load_entry_point" not in line)], str(res))
213 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
218 class CreateNode(unittest.TestCase):
219 # exercise "tahoe create-node", create-introducer,
220 # create-key-generator, and create-stats-gatherer, by calling the
221 # corresponding code as a subroutine.
223 def workdir(self, name):
224 basedir = os.path.join("test_runner", "CreateNode", name)
225 fileutil.make_dirs(basedir)
228 def run_tahoe(self, argv):
229 out,err = StringIO(), StringIO()
230 rc = runner.runner(argv, stdout=out, stderr=err)
231 return rc, out.getvalue(), err.getvalue()
233 def do_create(self, kind):
234 basedir = self.workdir("test_" + kind)
235 command = "create-" + kind
236 is_client = kind in ("node", "client")
237 tac = is_client and "tahoe-client.tac" or ("tahoe-" + kind + ".tac")
239 n1 = os.path.join(basedir, command + "-n1")
240 argv = ["--quiet", command, "--basedir", n1]
241 rc, out, err = self.run_tahoe(argv)
242 self.failUnlessEqual(err, "")
243 self.failUnlessEqual(out, "")
244 self.failUnlessEqual(rc, 0)
245 self.failUnless(os.path.exists(n1))
246 self.failUnless(os.path.exists(os.path.join(n1, tac)))
249 # tahoe.cfg should exist, and should have storage enabled for
250 # 'create-node', and disabled for 'create-client'.
251 tahoe_cfg = os.path.join(n1, "tahoe.cfg")
252 self.failUnless(os.path.exists(tahoe_cfg))
253 content = fileutil.read(tahoe_cfg).replace('\r\n', '\n')
255 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = false\n", content), content)
257 self.failUnless(re.search(r"\n\[storage\]\n#.*\nenabled = true\n", content), content)
258 self.failUnless("\nreserved_space = 1G\n" in content)
260 self.failUnless(re.search(r"\n\[drop_upload\]\n#.*\nenabled = false\n", content), content)
262 # creating the node a second time should be rejected
263 rc, out, err = self.run_tahoe(argv)
264 self.failIfEqual(rc, 0, str((out, err, rc)))
265 self.failUnlessEqual(out, "")
266 self.failUnless("is not empty." in err)
268 # Fail if there is a non-empty line that doesn't end with a
270 for line in err.splitlines():
271 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
273 # test that the non --basedir form works too
274 n2 = os.path.join(basedir, command + "-n2")
275 argv = ["--quiet", command, n2]
276 rc, out, err = self.run_tahoe(argv)
277 self.failUnlessEqual(err, "")
278 self.failUnlessEqual(out, "")
279 self.failUnlessEqual(rc, 0)
280 self.failUnless(os.path.exists(n2))
281 self.failUnless(os.path.exists(os.path.join(n2, tac)))
283 # test the --node-directory form
284 n3 = os.path.join(basedir, command + "-n3")
285 argv = ["--quiet", command, "--node-directory", n3]
286 rc, out, err = self.run_tahoe(argv)
287 self.failUnlessEqual(err, "")
288 self.failUnlessEqual(out, "")
289 self.failUnlessEqual(rc, 0)
290 self.failUnless(os.path.exists(n3))
291 self.failUnless(os.path.exists(os.path.join(n3, tac)))
293 # make sure it rejects too many arguments
294 argv = [command, "basedir", "extraarg"]
295 self.failUnlessRaises(usage.UsageError,
299 # when creating a non-client, there is no default for the basedir
302 self.failUnlessRaises(usage.UsageError,
308 self.do_create("node")
310 def test_client(self):
311 # create-client should behave like create-node --no-storage.
312 self.do_create("client")
314 def test_introducer(self):
315 self.do_create("introducer")
317 def test_key_generator(self):
318 self.do_create("key-generator")
320 def test_stats_gatherer(self):
321 self.do_create("stats-gatherer")
323 def test_subcommands(self):
324 # no arguments should trigger a command listing, via UsageError
325 self.failUnlessRaises(usage.UsageError,
331 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
333 # exercise "tahoe start", for both introducer, client node, and
334 # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
335 # get us figleaf-based line-level coverage, but it does a better job of
336 # confirming that the user can actually run "./bin/tahoe start" and
337 # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
338 # the like correctly.
340 # This doesn't work on cygwin (it hangs forever), so we skip this test
341 # when we're on cygwin. It is likely that "tahoe start" itself doesn't
342 # work on cygwin: twisted seems unable to provide a version of
343 # spawnProcess which really works there.
345 def workdir(self, name):
346 basedir = os.path.join("test_runner", "RunNode", name)
347 fileutil.make_dirs(basedir)
350 def test_introducer(self):
351 self.skip_if_cannot_daemonize()
352 basedir = self.workdir("test_introducer")
353 c1 = os.path.join(basedir, "c1")
354 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
355 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
356 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
357 PORTNUM_FILE = os.path.join(c1, "introducer.port")
358 NODE_URL_FILE = os.path.join(c1, "node.url")
359 CONFIG_FILE = os.path.join(c1, "tahoe.cfg")
361 d = self.run_bintahoe(["--quiet", "create-introducer", "--basedir", c1])
363 out, err, rc_or_sig = res
364 self.failUnlessEqual(rc_or_sig, 0)
366 # This makes sure that node.url is written, which allows us to
367 # detect when the introducer restarts in _node_has_restarted below.
368 config = fileutil.read(CONFIG_FILE)
369 self.failUnlessIn('\nweb.port = \n', config)
370 fileutil.write(CONFIG_FILE, config.replace('\nweb.port = \n', '\nweb.port = 0\n'))
372 # by writing this file, we get ten seconds before the node will
373 # exit. This insures that even if the test fails (and the 'stop'
374 # command doesn't work), the client should still terminate.
375 fileutil.write(HOTLINE_FILE, "")
376 # now it's safe to start the node
379 def _then_start_the_node(res):
380 return self.run_bintahoe(["--quiet", "start", c1])
381 d.addCallback(_then_start_the_node)
384 out, err, rc_or_sig = res
386 fileutil.write(HOTLINE_FILE, "")
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.
392 # the parent (twistd) has exited. However, twistd writes the pid
393 # from the child, not the parent, so we can't expect twistd.pid
394 # to exist quite yet.
396 # the node is running, but it might not have made it past the
397 # first reactor turn yet, and if we kill it too early, it won't
398 # remove the twistd.pid file. So wait until it does something
399 # that we know it won't do until after the first turn.
402 def _node_has_started():
403 return os.path.exists(INTRODUCER_FURL_FILE)
404 d.addCallback(lambda res: self.poll(_node_has_started))
407 # read the introducer.furl and introducer.port files so we can check that their
408 # contents don't change on restart
409 self.furl = fileutil.read(INTRODUCER_FURL_FILE)
410 self.failUnless(os.path.exists(PORTNUM_FILE))
411 self.portnum = fileutil.read(PORTNUM_FILE)
413 fileutil.write(HOTLINE_FILE, "")
414 self.failUnless(os.path.exists(TWISTD_PID_FILE))
415 self.failUnless(os.path.exists(NODE_URL_FILE))
417 # rm this so we can detect when the second incarnation is ready
418 os.unlink(NODE_URL_FILE)
419 return self.run_bintahoe(["--quiet", "restart", c1])
420 d.addCallback(_started)
423 out, err, rc_or_sig = res
424 fileutil.write(HOTLINE_FILE, "")
425 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
426 self.failUnlessEqual(rc_or_sig, 0, errstr)
427 self.failUnlessEqual(out, "", errstr)
428 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
431 # again, the second incarnation of the node might not be ready yet,
432 # so poll until it is. This time INTRODUCER_FURL_FILE already
433 # exists, so we check for the existence of NODE_URL_FILE instead.
434 def _node_has_restarted():
435 return os.path.exists(NODE_URL_FILE) and os.path.exists(PORTNUM_FILE)
436 d.addCallback(lambda res: self.poll(_node_has_restarted))
438 def _check_same_furl_and_port(res):
439 self.failUnless(os.path.exists(INTRODUCER_FURL_FILE))
440 self.failUnlessEqual(self.furl, fileutil.read(INTRODUCER_FURL_FILE))
441 self.failUnlessEqual(self.portnum, fileutil.read(PORTNUM_FILE))
442 d.addCallback(_check_same_furl_and_port)
444 # now we can kill it. TODO: On a slow machine, the node might kill
445 # itself before we get a chance to, especially if spawning the
446 # 'tahoe stop' command takes a while.
448 fileutil.write(HOTLINE_FILE, "")
449 self.failUnless(os.path.exists(TWISTD_PID_FILE))
451 return self.run_bintahoe(["--quiet", "stop", c1])
454 def _after_stopping(res):
455 out, err, rc_or_sig = res
456 fileutil.write(HOTLINE_FILE, "")
457 # the parent has exited by now
458 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
459 self.failUnlessEqual(rc_or_sig, 0, errstr)
460 self.failUnlessEqual(out, "", errstr)
461 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
462 # the parent was supposed to poll and wait until it sees
463 # twistd.pid go away before it exits, so twistd.pid should be
465 self.failIf(os.path.exists(TWISTD_PID_FILE))
466 d.addCallback(_after_stopping)
467 d.addBoth(self._remove, HOTLINE_FILE)
469 # This test has hit a 240-second timeout on our feisty2.5 buildslave, and a 480-second timeout
470 # on Francois's Lenny-armv5tel buildslave.
471 test_introducer.timeout = 960
473 def test_client_no_noise(self):
474 self.skip_if_cannot_daemonize()
476 from allmydata import get_package_versions, normalized_version
477 twisted_ver = get_package_versions()['Twisted']
479 if not normalized_version(twisted_ver) >= normalized_version('9.0.0'):
480 raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
482 basedir = self.workdir("test_client_no_noise")
483 c1 = os.path.join(basedir, "c1")
484 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
485 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
486 PORTNUM_FILE = os.path.join(c1, "client.port")
488 d = self.run_bintahoe(["--quiet", "create-client", "--basedir", c1, "--webport", "0"])
490 out, err, rc_or_sig = res
491 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
492 assert rc_or_sig == 0, errstr
493 self.failUnlessEqual(rc_or_sig, 0)
495 # By writing this file, we get two minutes before the client will exit. This ensures
496 # that even if the 'stop' command doesn't work (and the test fails), the client should
498 fileutil.write(HOTLINE_FILE, "")
499 # now it's safe to start the node
503 return self.run_bintahoe(["--quiet", "start", c1])
504 d.addCallback(_start)
507 out, err, rc_or_sig = res
508 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
509 fileutil.write(HOTLINE_FILE, "")
510 self.failUnlessEqual(rc_or_sig, 0, errstr)
511 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
512 errlines = err.split("\n")
513 self.failIf([True for line in errlines if (line != "" and "UserWarning: Unbuilt egg for setuptools" not in line
514 and "from pkg_resources import load_entry_point" not in line)], errstr)
516 raise unittest.SkipTest("This test is known not to pass on Ubuntu Lucid; see #1235.")
518 # the parent (twistd) has exited. However, twistd writes the pid
519 # from the child, not the parent, so we can't expect twistd.pid
520 # to exist quite yet.
522 # the node is running, but it might not have made it past the
523 # first reactor turn yet, and if we kill it too early, it won't
524 # remove the twistd.pid file. So wait until it does something
525 # that we know it won't do until after the first turn.
528 def _node_has_started():
529 return os.path.exists(PORTNUM_FILE)
530 d.addCallback(lambda res: self.poll(_node_has_started))
532 # now we can kill it. TODO: On a slow machine, the node might kill
533 # itself before we get a chance to, especially if spawning the
534 # 'tahoe stop' command takes a while.
536 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
537 return self.run_bintahoe(["--quiet", "stop", c1])
539 d.addBoth(self._remove, HOTLINE_FILE)
542 def test_client(self):
543 self.skip_if_cannot_daemonize()
544 basedir = self.workdir("test_client")
545 c1 = os.path.join(basedir, "c1")
546 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
547 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
548 PORTNUM_FILE = os.path.join(c1, "client.port")
549 NODE_URL_FILE = os.path.join(c1, "node.url")
550 CONFIG_FILE = os.path.join(c1, "tahoe.cfg")
552 d = self.run_bintahoe(["--quiet", "create-node", "--basedir", c1, "--webport", "0"])
554 out, err, rc_or_sig = res
555 self.failUnlessEqual(rc_or_sig, 0)
557 # Check that the --webport option worked.
558 config = fileutil.read(CONFIG_FILE)
559 self.failUnlessIn('\nweb.port = 0\n', config)
561 # By writing this file, we get two minutes before the client will exit. This ensures
562 # that even if the 'stop' command doesn't work (and the test fails), the client should
564 fileutil.write(HOTLINE_FILE, "")
565 # now it's safe to start the node
569 return self.run_bintahoe(["--quiet", "start", c1])
570 d.addCallback(_start)
573 out, err, rc_or_sig = res
574 fileutil.write(HOTLINE_FILE, "")
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.
580 # the parent (twistd) has exited. However, twistd writes the pid
581 # from the child, not the parent, so we can't expect twistd.pid
582 # to exist quite yet.
584 # the node is running, but it might not have made it past the
585 # first reactor turn yet, and if we kill it too early, it won't
586 # remove the twistd.pid file. So wait until it does something
587 # that we know it won't do until after the first turn.
590 def _node_has_started():
591 # this depends upon both files being created atomically
592 return os.path.exists(NODE_URL_FILE) and os.path.exists(PORTNUM_FILE)
593 d.addCallback(lambda res: self.poll(_node_has_started))
596 # read the client.port file so we can check that its contents
597 # don't change on restart
598 self.portnum = fileutil.read(PORTNUM_FILE)
600 fileutil.write(HOTLINE_FILE, "")
601 self.failUnless(os.path.exists(TWISTD_PID_FILE))
603 # rm this so we can detect when the second incarnation is ready
604 os.unlink(NODE_URL_FILE)
605 return self.run_bintahoe(["--quiet", "restart", c1])
606 d.addCallback(_started)
609 out, err, rc_or_sig = res
611 fileutil.write(HOTLINE_FILE, "")
612 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
613 self.failUnlessEqual(rc_or_sig, 0, errstr)
614 self.failUnlessEqual(out, "", errstr)
615 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
618 # again, the second incarnation of the node might not be ready yet,
619 # so poll until it is
620 d.addCallback(lambda res: self.poll(_node_has_started))
622 def _check_same_port(res):
623 self.failUnlessEqual(self.portnum, fileutil.read(PORTNUM_FILE))
624 d.addCallback(_check_same_port)
626 # now we can kill it. TODO: On a slow machine, the node might kill
627 # itself before we get a chance to, especially if spawning the
628 # 'tahoe stop' command takes a while.
630 fileutil.write(HOTLINE_FILE, "")
631 self.failUnless(os.path.exists(TWISTD_PID_FILE),
633 os.listdir(os.path.dirname(TWISTD_PID_FILE))))
634 return self.run_bintahoe(["--quiet", "stop", c1])
638 out, err, rc_or_sig = res
640 fileutil.write(HOTLINE_FILE, "")
641 # the parent has exited by now
642 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
643 self.failUnlessEqual(rc_or_sig, 0, errstr)
644 self.failUnlessEqual(out, "", errstr)
645 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
646 # the parent was supposed to poll and wait until it sees
647 # twistd.pid go away before it exits, so twistd.pid should be
649 self.failIf(os.path.exists(TWISTD_PID_FILE))
651 d.addBoth(self._remove, HOTLINE_FILE)
654 def _remove(self, res, file):
655 fileutil.remove(file)
658 def test_baddir(self):
659 self.skip_if_cannot_daemonize()
660 basedir = self.workdir("test_baddir")
661 fileutil.make_dirs(basedir)
663 d = self.run_bintahoe(["--quiet", "start", "--basedir", basedir])
665 out, err, rc_or_sig = res
666 self.failUnlessEqual(rc_or_sig, 1)
667 self.failUnless("does not look like a node directory" in err, err)
670 def _then_stop_it(res):
671 return self.run_bintahoe(["--quiet", "stop", "--basedir", basedir])
672 d.addCallback(_then_stop_it)
675 out, err, rc_or_sig = res
676 self.failUnlessEqual(rc_or_sig, 2)
677 self.failUnless("does not look like a running node directory" in err)
680 def _then_start_in_bogus_basedir(res):
681 not_a_dir = os.path.join(basedir, "bogus")
682 return self.run_bintahoe(["--quiet", "start", "--basedir", not_a_dir])
683 d.addCallback(_then_start_in_bogus_basedir)
686 out, err, rc_or_sig = res
687 self.failUnlessEqual(rc_or_sig, 1)
688 self.failUnless("does not look like a directory at all" in err, err)
692 def test_keygen(self):
693 self.skip_if_cannot_daemonize()
694 basedir = self.workdir("test_keygen")
695 c1 = os.path.join(basedir, "c1")
696 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
697 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
699 d = self.run_bintahoe(["--quiet", "create-key-generator", "--basedir", c1])
701 out, err, rc_or_sig = res
702 self.failUnlessEqual(rc_or_sig, 0)
706 return self.run_bintahoe(["--quiet", "start", c1])
707 d.addCallback(_start)
710 out, err, rc_or_sig = res
711 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
712 self.failUnlessEqual(rc_or_sig, 0, errstr)
713 self.failUnlessEqual(out, "", errstr)
714 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
716 # the parent (twistd) has exited. However, twistd writes the pid
717 # from the child, not the parent, so we can't expect twistd.pid
718 # to exist quite yet.
720 # the node is running, but it might not have made it past the
721 # first reactor turn yet, and if we kill it too early, it won't
722 # remove the twistd.pid file. So wait until it does something
723 # that we know it won't do until after the first turn.
726 def _node_has_started():
727 return os.path.exists(KEYGEN_FURL_FILE)
728 d.addCallback(lambda res: self.poll(_node_has_started))
731 self.failUnless(os.path.exists(TWISTD_PID_FILE))
732 # rm this so we can detect when the second incarnation is ready
733 os.unlink(KEYGEN_FURL_FILE)
734 return self.run_bintahoe(["--quiet", "restart", c1])
735 d.addCallback(_started)
738 out, err, rc_or_sig = res
739 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
740 self.failUnlessEqual(rc_or_sig, 0, errstr)
741 self.failUnlessEqual(out, "", errstr)
742 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
745 # again, the second incarnation of the node might not be ready yet,
746 # so poll until it is
747 d.addCallback(lambda res: self.poll(_node_has_started))
749 # now we can kill it. TODO: On a slow machine, the node might kill
750 # itself before we get a chance too, especially if spawning the
751 # 'tahoe stop' command takes a while.
753 self.failUnless(os.path.exists(TWISTD_PID_FILE))
754 return self.run_bintahoe(["--quiet", "stop", c1])
758 out, err, rc_or_sig = res
759 # the parent has exited by now
760 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
761 self.failUnlessEqual(rc_or_sig, 0, errstr)
762 self.failUnlessEqual(out, "", errstr)
763 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
764 # the parent was supposed to poll and wait until it sees
765 # twistd.pid go away before it exits, so twistd.pid should be
767 self.failIf(os.path.exists(TWISTD_PID_FILE))