2 from twisted.trial import unittest
4 from cStringIO import StringIO
5 from twisted.python import runtime
6 from twisted.internet import utils
8 from allmydata.scripts import runner
9 from allmydata.util import fileutil, pollmixin
11 from allmydata.test import common_util
14 bintahoe = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(allmydata.__file__))), 'bin', 'tahoe')
16 class TheRightCode(unittest.TestCase, common_util.SignalMixin):
18 if not os.path.exists(bintahoe):
19 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
20 d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
22 out, err, rc_or_sig = res
23 self.failUnlessEqual(rc_or_sig, 0)
25 # Fail unless the allmydata-tahoe package is *this* version *and* was loaded from *this* source directory.
26 required_ver_and_path = "allmydata-tahoe: %s (%s)" % (allmydata.__version__, os.path.dirname(os.path.dirname(allmydata.__file__)))
27 self.failUnless(out.startswith(required_ver_and_path), (out, err, rc_or_sig))
31 class CreateNode(unittest.TestCase, common_util.SignalMixin):
32 def workdir(self, name):
33 basedir = os.path.join("test_runner", "CreateNode", name)
34 fileutil.make_dirs(basedir)
37 def test_client(self):
38 if not os.path.exists(bintahoe):
39 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
40 basedir = self.workdir("test_client")
41 c1 = os.path.join(basedir, "c1")
42 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1], env=os.environ)
44 out, err, rc_or_sig = res
45 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
46 self.failUnlessEqual(out, "")
47 self.failUnlessEqual(rc_or_sig, 0)
48 self.failUnless(os.path.exists(c1))
49 self.failUnless(os.path.exists(os.path.join(c1, "tahoe-client.tac")))
52 def _then_try_again(unused=None):
53 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1], env=os.environ)
54 d.addCallback(_then_try_again)
57 out, err, rc_or_sig = res
58 # creating the client a second time should throw an exception
59 self.failIfEqual(rc_or_sig, 0, str((out, err, rc_or_sig)))
60 self.failUnlessEqual(out, "")
61 self.failUnless("is not empty." in err)
63 # Fail if there is a non-empty line that doesn't end with a PUNCTUATION MARK.
64 for line in err.splitlines():
65 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
68 c2 = os.path.join(basedir, "c2")
69 def _then_try_new_dir(unused=None):
70 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", c2], env=os.environ)
71 d.addCallback(_then_try_new_dir)
74 out, err, rc_or_sig = res
75 self.failUnless(os.path.exists(c2))
76 self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
79 def _then_try_badarg(unused=None):
80 return utils.getProcessOutputAndValue(bintahoe, args=["create-client", "basedir", "extraarg"], env=os.environ)
81 d.addCallback(_then_try_badarg)
84 out, err, rc_or_sig = res
85 self.failUnlessEqual(rc_or_sig, 1)
86 self.failUnless(out.startswith("Usage"), out)
90 def test_introducer(self):
91 if not os.path.exists(bintahoe):
92 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
93 basedir = self.workdir("test_introducer")
94 c1 = os.path.join(basedir, "c1")
95 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
97 out, err, rc_or_sig = res
98 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
99 self.failUnlessEqual(out, "")
100 self.failUnlessEqual(rc_or_sig, 0)
101 self.failUnless(os.path.exists(c1))
102 self.failUnless(os.path.exists(os.path.join(c1,
103 "tahoe-introducer.tac")))
106 def _then_try_again(unused=None):
107 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
108 d.addCallback(_then_try_again)
111 out, err, rc_or_sig = res
112 # creating the introducer a second time should throw an exception
113 self.failIfEqual(rc_or_sig, 0)
114 self.failUnlessEqual(out, "")
115 self.failUnless("is not empty" in err)
117 # Fail if there is a non-empty line that doesn't end with a PUNCTUATION MARK.
118 for line in err.splitlines():
119 self.failIf(re.search("[\S][^\.!?]$", line), (line,))
122 c2 = os.path.join(basedir, "c2")
123 def _then_try_new_dir(unused=None):
124 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", c2], env=os.environ)
125 d.addCallback(_then_try_new_dir)
128 out, err, rc_or_sig = res
129 self.failUnless(os.path.exists(c2))
130 self.failUnless(os.path.exists(os.path.join(c2,
131 "tahoe-introducer.tac")))
134 def _then_try_badarg(unused=None):
135 return utils.getProcessOutputAndValue(bintahoe, args=["create-introducer", "basedir", "extraarg"], env=os.environ)
136 d.addCallback(_then_try_badarg)
139 out, err, rc_or_sig = res
140 self.failUnlessEqual(rc_or_sig, 1)
141 self.failUnless(out.startswith("Usage"), out)
144 def _then_try_badarg_again(unused=None):
145 return utils.getProcessOutputAndValue(bintahoe, args=["create-introducer"], env=os.environ)
146 d.addCallback(_then_try_badarg_again)
149 out, err, rc_or_sig = res
150 self.failUnlessEqual(rc_or_sig, 1)
151 self.failUnless(out.startswith("Usage"), out)
155 class RunNode(unittest.TestCase, pollmixin.PollMixin, common_util.SignalMixin):
156 def workdir(self, name):
157 basedir = os.path.join("test_runner", "RunNode", name)
158 fileutil.make_dirs(basedir)
161 def test_introducer(self):
162 if not os.path.exists(bintahoe):
163 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
164 if runtime.platformType == "win32":
165 # twistd on windows doesn't daemonize. cygwin works normally.
166 raise unittest.SkipTest("twistd does not fork under windows")
167 basedir = self.workdir("test_introducer")
168 c1 = os.path.join(basedir, "c1")
169 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
170 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
171 INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
173 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
175 out, err, rc_or_sig = res
176 self.failUnlessEqual(rc_or_sig, 0)
177 # by writing this file, we get ten seconds before the node will
178 # exit. This insures that even if the test fails (and the 'stop'
179 # command doesn't work), the client should still terminate.
180 open(HOTLINE_FILE, "w").write("")
181 # now it's safe to start the node
184 def _then_start_the_node(res):
185 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
186 d.addCallback(_then_start_the_node)
189 out, err, rc_or_sig = res
191 open(HOTLINE_FILE, "w").write("")
192 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
193 self.failUnlessEqual(rc_or_sig, 0, errstr)
194 self.failUnlessEqual(out, "", errstr)
195 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
197 # the parent (twistd) has exited. However, twistd writes the pid
198 # from the child, not the parent, so we can't expect twistd.pid
199 # to exist quite yet.
201 # the node is running, but it might not have made it past the
202 # first reactor turn yet, and if we kill it too early, it won't
203 # remove the twistd.pid file. So wait until it does something
204 # that we know it won't do until after the first turn.
207 def _node_has_started():
208 return os.path.exists(INTRODUCER_FURL_FILE)
209 d.addCallback(lambda res: self.poll(_node_has_started))
212 open(HOTLINE_FILE, "w").write("")
213 self.failUnless(os.path.exists(TWISTD_PID_FILE))
214 # rm this so we can detect when the second incarnation is ready
215 os.unlink(INTRODUCER_FURL_FILE)
216 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
217 d.addCallback(_started)
220 out, err, rc_or_sig = res
221 open(HOTLINE_FILE, "w").write("")
222 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
223 self.failUnlessEqual(rc_or_sig, 0, errstr)
224 self.failUnlessEqual(out, "", errstr)
225 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
228 # again, the second incarnation of the node might not be ready yet,
229 # so poll until it is
230 d.addCallback(lambda res: self.poll(_node_has_started))
232 # now we can kill it. TODO: On a slow machine, the node might kill
233 # itself before we get a chance too, especially if spawning the
234 # 'tahoe stop' command takes a while.
236 open(HOTLINE_FILE, "w").write("")
237 self.failUnless(os.path.exists(TWISTD_PID_FILE))
239 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
242 def _after_stopping(res):
243 out, err, rc_or_sig = res
244 open(HOTLINE_FILE, "w").write("")
245 # the parent has exited by now
246 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
247 self.failUnlessEqual(rc_or_sig, 0, errstr)
248 self.failUnlessEqual(out, "", errstr)
249 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
250 # the parent was supposed to poll and wait until it sees
251 # twistd.pid go away before it exits, so twistd.pid should be
253 self.failIf(os.path.exists(TWISTD_PID_FILE))
254 d.addCallback(_after_stopping)
256 def _remove_hotline(res):
257 os.unlink(HOTLINE_FILE)
259 d.addBoth(_remove_hotline)
262 def test_client_no_noise(self):
263 if not os.path.exists(bintahoe):
264 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
265 basedir = self.workdir("test_client_no_noise")
266 c1 = os.path.join(basedir, "c1")
267 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
268 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
269 PORTNUMFILE = os.path.join(c1, "client.port")
271 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
273 out, err, rc_or_sig = res
274 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
275 assert rc_or_sig == 0, errstr
276 self.failUnlessEqual(rc_or_sig, 0)
277 # By writing this file, we get forty seconds before the client will exit. This insures
278 # that even if the 'stop' command doesn't work (and the test fails), the client should
280 open(HOTLINE_FILE, "w").write("")
281 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
282 # now it's safe to start the node
286 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
287 d.addCallback(_start)
290 out, err, rc_or_sig = res
291 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
292 open(HOTLINE_FILE, "w").write("")
293 self.failUnlessEqual(rc_or_sig, 0, errstr)
294 self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
295 self.failUnlessEqual(err, "", errstr)
297 # the parent (twistd) has exited. However, twistd writes the pid
298 # from the child, not the parent, so we can't expect twistd.pid
299 # to exist quite yet.
301 # the node is running, but it might not have made it past the
302 # first reactor turn yet, and if we kill it too early, it won't
303 # remove the twistd.pid file. So wait until it does something
304 # that we know it won't do until after the first turn.
307 def _node_has_started():
308 return os.path.exists(PORTNUMFILE)
309 d.addCallback(lambda res: self.poll(_node_has_started))
311 # now we can kill it. TODO: On a slow machine, the node might kill
312 # itself before we get a chance too, especially if spawning the
313 # 'tahoe stop' command takes a while.
315 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
316 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
319 test_client_no_noise.todo = "We submitted a patch to Nevow to silence this warning: http://divmod.org/trac/ticket/2830"
321 def test_client(self):
322 if not os.path.exists(bintahoe):
323 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
324 if runtime.platformType == "win32":
325 # twistd on windows doesn't daemonize. cygwin works normally.
326 raise unittest.SkipTest("twistd does not fork under windows")
327 basedir = self.workdir("test_client")
328 c1 = os.path.join(basedir, "c1")
329 HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
330 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
331 PORTNUMFILE = os.path.join(c1, "client.port")
333 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
335 out, err, rc_or_sig = res
336 self.failUnlessEqual(rc_or_sig, 0)
337 # By writing this file, we get forty seconds before the client will exit. This insures
338 # that even if the 'stop' command doesn't work (and the test fails), the client should
340 open(HOTLINE_FILE, "w").write("")
341 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
342 # now it's safe to start the node
346 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
347 d.addCallback(_start)
350 out, err, rc_or_sig = res
351 open(HOTLINE_FILE, "w").write("")
352 errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
353 self.failUnlessEqual(rc_or_sig, 0, errstr)
354 self.failUnlessEqual(out, "", errstr)
355 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
357 # the parent (twistd) has exited. However, twistd writes the pid
358 # from the child, not the parent, so we can't expect twistd.pid
359 # to exist quite yet.
361 # the node is running, but it might not have made it past the
362 # first reactor turn yet, and if we kill it too early, it won't
363 # remove the twistd.pid file. So wait until it does something
364 # that we know it won't do until after the first turn.
367 def _node_has_started():
368 return os.path.exists(PORTNUMFILE)
369 d.addCallback(lambda res: self.poll(_node_has_started))
372 open(HOTLINE_FILE, "w").write("")
373 self.failUnless(os.path.exists(TWISTD_PID_FILE))
374 # rm this so we can detect when the second incarnation is ready
375 os.unlink(PORTNUMFILE)
377 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
378 d.addCallback(_started)
381 out, err, rc_or_sig = res
383 open(HOTLINE_FILE, "w").write("")
384 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
385 self.failUnlessEqual(rc_or_sig, 0, errstr)
386 self.failUnlessEqual(out, "", errstr)
387 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
390 # again, the second incarnation of the node might not be ready yet,
391 # so poll until it is
392 d.addCallback(lambda res: self.poll(_node_has_started))
394 # now we can kill it. TODO: On a slow machine, the node might kill
395 # itself before we get a chance too, especially if spawning the
396 # 'tahoe stop' command takes a while.
398 open(HOTLINE_FILE, "w").write("")
399 self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
400 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
404 out, err, rc_or_sig = res
406 open(HOTLINE_FILE, "w").write("")
407 # the parent has exited by now
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.
412 # the parent was supposed to poll and wait until it sees
413 # twistd.pid go away before it exits, so twistd.pid should be
415 self.failIf(os.path.exists(TWISTD_PID_FILE))
417 def _remove_hotline(res):
418 os.unlink(HOTLINE_FILE)
420 d.addBoth(_remove_hotline)
423 def test_baddir(self):
424 if not os.path.exists(bintahoe):
425 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
426 basedir = self.workdir("test_baddir")
427 fileutil.make_dirs(basedir)
429 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
431 out, err, rc_or_sig = res
432 self.failUnlessEqual(rc_or_sig, 1)
433 self.failUnless("does not look like a node directory" in err)
436 def _then_stop_it(res):
437 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
438 d.addCallback(_then_stop_it)
441 out, err, rc_or_sig = res
442 self.failUnlessEqual(rc_or_sig, 2)
443 self.failUnless("does not look like a running node directory" in err)
446 def _then_start_in_bogus_basedir(res):
447 not_a_dir = os.path.join(basedir, "bogus")
448 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
449 d.addCallback(_then_start_in_bogus_basedir)
452 out, err, rc_or_sig = res
453 self.failUnlessEqual(rc_or_sig, 1)
454 self.failUnless("does not look like a directory at all" in err, err)
458 def test_keygen(self):
459 if not os.path.exists(bintahoe):
460 raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, 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.")
461 if runtime.platformType == "win32":
462 # twistd on windows doesn't daemonize. cygwin works normally.
463 raise unittest.SkipTest("twistd does not fork under windows")
464 basedir = self.workdir("test_keygen")
465 c1 = os.path.join(basedir, "c1")
466 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
467 KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
469 d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
471 out, err, rc_or_sig = res
472 self.failUnlessEqual(rc_or_sig, 0)
476 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
477 d.addCallback(_start)
480 out, err, rc_or_sig = res
481 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
482 self.failUnlessEqual(rc_or_sig, 0, errstr)
483 self.failUnlessEqual(out, "", errstr)
484 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
486 # the parent (twistd) has exited. However, twistd writes the pid
487 # from the child, not the parent, so we can't expect twistd.pid
488 # to exist quite yet.
490 # the node is running, but it might not have made it past the
491 # first reactor turn yet, and if we kill it too early, it won't
492 # remove the twistd.pid file. So wait until it does something
493 # that we know it won't do until after the first turn.
496 def _node_has_started():
497 return os.path.exists(KEYGEN_FURL_FILE)
498 d.addCallback(lambda res: self.poll(_node_has_started))
501 self.failUnless(os.path.exists(TWISTD_PID_FILE))
502 # rm this so we can detect when the second incarnation is ready
503 os.unlink(KEYGEN_FURL_FILE)
504 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
505 d.addCallback(_started)
508 out, err, rc_or_sig = res
509 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
510 self.failUnlessEqual(rc_or_sig, 0, errstr)
511 self.failUnlessEqual(out, "", errstr)
512 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
515 # again, the second incarnation of the node might not be ready yet,
516 # so poll until it is
517 d.addCallback(lambda res: self.poll(_node_has_started))
519 # now we can kill it. TODO: On a slow machine, the node might kill
520 # itself before we get a chance too, especially if spawning the
521 # 'tahoe stop' command takes a while.
523 self.failUnless(os.path.exists(TWISTD_PID_FILE))
524 return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
528 out, err, rc_or_sig = res
529 # the parent has exited by now
530 errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
531 self.failUnlessEqual(rc_or_sig, 0, errstr)
532 self.failUnlessEqual(out, "", errstr)
533 # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
534 # the parent was supposed to poll and wait until it sees
535 # twistd.pid go away before it exits, so twistd.pid should be
537 self.failIf(os.path.exists(TWISTD_PID_FILE))