]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
tests: don't let bench_dirnode.py do stuff and have side-effects at import time ...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_runner.py
1 # -*- coding: utf-8 -*-
2
3 from twisted.trial import unittest
4
5 from twisted.python import usage, runtime
6 from twisted.internet import utils
7 import os.path, re, sys
8 from cStringIO import StringIO
9 from allmydata.util import fileutil, pollmixin
10 from allmydata.scripts import runner
11
12 from allmydata.test import common_util
13 import allmydata
14
15 bintahoe = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(allmydata.__file__))), 'bin', 'tahoe')
16 if sys.platform == "win32":  # TODO: should this include cygwin?
17     bintahoe += ".exe"
18
19
20 class SkipMixin:
21     def skip_if_cannot_run_bintahoe(self):
22         if "cygwin" in sys.platform.lower():
23             raise unittest.SkipTest("We don't know how to make this test work on cygwin: spawnProcess seems to hang forever. We don't know if 'bin/tahoe start' can be run on cygwin.")
24         if not os.path.exists(bintahoe):
25             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,))
26
27     def skip_if_cannot_daemonize(self):
28         self.skip_if_cannot_run_bintahoe()
29         if runtime.platformType == "win32":
30             # twistd on windows doesn't daemonize. cygwin should work normally.
31             raise unittest.SkipTest("twistd does not fork under windows")
32
33
34 class TheRightCode(common_util.SignalMixin, unittest.TestCase, SkipMixin):
35     def test_path(self):
36         self.skip_if_cannot_run_bintahoe()
37         d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
38         def _cb(res):
39             out, err, rc_or_sig = res
40             self.failUnlessEqual(rc_or_sig, 0)
41
42             # Fail unless the allmydata-tahoe package is *this* version *and*
43             # was loaded from *this* source directory.
44             ad = os.path.dirname(os.path.dirname(os.path.realpath(allmydata.__file__)))
45             required_ver_and_path = "allmydata-tahoe: %s (%s)" % (allmydata.__version__, ad)
46             self.failUnless(out.startswith(required_ver_and_path),
47                             (out, err, rc_or_sig, required_ver_and_path))
48         d.addCallback(_cb)
49         return d
50
51
52 class CreateNode(unittest.TestCase):
53     # exercise "tahoe create-node", create-introducer,
54     # create-key-generator, and create-stats-gatherer, by calling the
55     # corresponding code as a subroutine.
56
57     def workdir(self, name):
58         basedir = os.path.join("test_runner", "CreateNode", name)
59         fileutil.make_dirs(basedir)
60         return basedir
61
62     def run_tahoe(self, argv):
63         out,err = StringIO(), StringIO()
64         rc = runner.runner(argv, stdout=out, stderr=err)
65         return rc, out.getvalue(), err.getvalue()
66
67     def do_create(self, command, basedir):
68         c1 = os.path.join(basedir, command + "-c1")
69         argv = ["--quiet", command, "--basedir", c1]
70         rc, out, err = self.run_tahoe(argv)
71         self.failUnlessEqual(err, "")
72         self.failUnlessEqual(out, "")
73         self.failUnlessEqual(rc, 0)
74         self.failUnless(os.path.exists(c1))
75         self.failUnless(os.path.exists(os.path.join(c1, "tahoe-client.tac")))
76
77         # tahoe.cfg should exist, and should have storage enabled for
78         # 'create-node', and disabled for 'create-client'.
79         tahoe_cfg = os.path.join(c1, "tahoe.cfg")
80         self.failUnless(os.path.exists(tahoe_cfg))
81         content = open(tahoe_cfg).read()
82         if command == "create-client":
83             self.failUnless("\n[storage]\nenabled = false\n" in content)
84         else:
85             self.failUnless("\n[storage]\nenabled = true\n" in content)
86
87         # creating the client a second time should be rejected
88         rc, out, err = self.run_tahoe(argv)
89         self.failIfEqual(rc, 0, str((out, err, rc)))
90         self.failUnlessEqual(out, "")
91         self.failUnless("is not empty." in err)
92
93         # Fail if there is a non-empty line that doesn't end with a
94         # punctuation mark.
95         for line in err.splitlines():
96             self.failIf(re.search("[\S][^\.!?]$", line), (line,))
97
98         # test that the non --basedir form works too
99         c2 = os.path.join(basedir, command + "c2")
100         argv = ["--quiet", command, c2]
101         rc, out, err = self.run_tahoe(argv)
102         self.failUnless(os.path.exists(c2))
103         self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
104
105         # make sure it rejects too many arguments
106         argv = [command, "basedir", "extraarg"]
107         self.failUnlessRaises(usage.UsageError,
108                               runner.runner, argv,
109                               run_by_human=False)
110
111     def test_node(self):
112         basedir = self.workdir("test_node")
113         self.do_create("create-node", basedir)
114
115     def test_client(self):
116         # create-client should behave like create-node --no-storage.
117         basedir = self.workdir("test_client")
118         self.do_create("create-client", basedir)
119
120     def test_introducer(self):
121         basedir = self.workdir("test_introducer")
122         c1 = os.path.join(basedir, "c1")
123         argv = ["--quiet", "create-introducer", "--basedir", c1]
124         rc, out, err = self.run_tahoe(argv)
125         self.failUnlessEqual(err, "", err)
126         self.failUnlessEqual(out, "")
127         self.failUnlessEqual(rc, 0)
128         self.failUnless(os.path.exists(c1))
129         self.failUnless(os.path.exists(os.path.join(c1,"tahoe-introducer.tac")))
130
131         # creating the introducer a second time should be rejected
132         rc, out, err = self.run_tahoe(argv)
133         self.failIfEqual(rc, 0)
134         self.failUnlessEqual(out, "")
135         self.failUnless("is not empty" in err)
136
137         # Fail if there is a non-empty line that doesn't end with a
138         # punctuation mark.
139         for line in err.splitlines():
140             self.failIf(re.search("[\S][^\.!?]$", line), (line,))
141
142         # test the non --basedir form
143         c2 = os.path.join(basedir, "c2")
144         argv = ["--quiet", "create-introducer", c2]
145         rc, out, err = self.run_tahoe(argv)
146         self.failUnlessEqual(err, "", err)
147         self.failUnlessEqual(out, "")
148         self.failUnlessEqual(rc, 0)
149         self.failUnless(os.path.exists(c2))
150         self.failUnless(os.path.exists(os.path.join(c2,"tahoe-introducer.tac")))
151
152         # reject extra arguments
153         argv = ["create-introducer", "basedir", "extraarg"]
154         self.failUnlessRaises(usage.UsageError,
155                               runner.runner, argv,
156                               run_by_human=False)
157         # and require basedir to be provided in some form
158         argv = ["create-introducer"]
159         self.failUnlessRaises(usage.UsageError,
160                               runner.runner, argv,
161                               run_by_human=False)
162
163     def test_key_generator(self):
164         basedir = self.workdir("test_key_generator")
165         kg1 = os.path.join(basedir, "kg1")
166         argv = ["--quiet", "create-key-generator", "--basedir", kg1]
167         rc, out, err = self.run_tahoe(argv)
168         self.failUnlessEqual(err, "")
169         self.failUnlessEqual(out, "")
170         self.failUnlessEqual(rc, 0)
171         self.failUnless(os.path.exists(kg1))
172         self.failUnless(os.path.exists(os.path.join(kg1, "tahoe-key-generator.tac")))
173
174         # creating it a second time should be rejected
175         rc, out, err = self.run_tahoe(argv)
176         self.failIfEqual(rc, 0, str((out, err, rc)))
177         self.failUnlessEqual(out, "")
178         self.failUnless("is not empty." in err)
179
180         # make sure it rejects too many arguments
181         argv = ["create-key-generator", "basedir", "extraarg"]
182         self.failUnlessRaises(usage.UsageError,
183                               runner.runner, argv,
184                               run_by_human=False)
185
186         # make sure it rejects a missing basedir specification
187         argv = ["create-key-generator"]
188         rc, out, err = self.run_tahoe(argv)
189         self.failIfEqual(rc, 0, str((out, err, rc)))
190         self.failUnlessEqual(out, "")
191         self.failUnless("a basedir was not provided" in err)
192
193     def test_stats_gatherer(self):
194         basedir = self.workdir("test_stats_gatherer")
195         sg1 = os.path.join(basedir, "sg1")
196         argv = ["--quiet", "create-stats-gatherer", "--basedir", sg1]
197         rc, out, err = self.run_tahoe(argv)
198         self.failUnlessEqual(err, "")
199         self.failUnlessEqual(out, "")
200         self.failUnlessEqual(rc, 0)
201         self.failUnless(os.path.exists(sg1))
202         self.failUnless(os.path.exists(os.path.join(sg1, "tahoe-stats-gatherer.tac")))
203
204         # creating it a second time should be rejected
205         rc, out, err = self.run_tahoe(argv)
206         self.failIfEqual(rc, 0, str((out, err, rc)))
207         self.failUnlessEqual(out, "")
208         self.failUnless("is not empty." in err)
209
210         # test the non --basedir form
211         kg2 = os.path.join(basedir, "kg2")
212         argv = ["--quiet", "create-stats-gatherer", kg2]
213         rc, out, err = self.run_tahoe(argv)
214         self.failUnlessEqual(err, "", err)
215         self.failUnlessEqual(out, "")
216         self.failUnlessEqual(rc, 0)
217         self.failUnless(os.path.exists(kg2))
218         self.failUnless(os.path.exists(os.path.join(kg2,"tahoe-stats-gatherer.tac")))
219
220         # make sure it rejects too many arguments
221         argv = ["create-stats-gatherer", "basedir", "extraarg"]
222         self.failUnlessRaises(usage.UsageError,
223                               runner.runner, argv,
224                               run_by_human=False)
225
226         # make sure it rejects a missing basedir specification
227         argv = ["create-stats-gatherer"]
228         rc, out, err = self.run_tahoe(argv)
229         self.failIfEqual(rc, 0, str((out, err, rc)))
230         self.failUnlessEqual(out, "")
231         self.failUnless("a basedir was not provided" in err)
232
233     def test_subcommands(self):
234         # no arguments should trigger a command listing, via UsageError
235         self.failUnlessRaises(usage.UsageError,
236                               runner.runner,
237                               [],
238                               run_by_human=False)
239
240
241 class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
242               SkipMixin):
243     # exercise "tahoe start", for both introducer, client node, and
244     # key-generator, by spawning "tahoe start" as a subprocess. This doesn't
245     # get us figleaf-based line-level coverage, but it does a better job of
246     # confirming that the user can actually run "./bin/tahoe start" and
247     # expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
248     # the like correctly.
249
250     # This doesn't work on cygwin (it hangs forever), so we skip this test
251     # when we're on cygwin. It is likely that "tahoe start" itself doesn't
252     # work on cygwin: twisted seems unable to provide a version of
253     # spawnProcess which really works there.
254
255     def workdir(self, name):
256         basedir = os.path.join("test_runner", "RunNode", name)
257         fileutil.make_dirs(basedir)
258         return basedir
259
260     def test_introducer(self):
261         self.skip_if_cannot_daemonize()
262         basedir = self.workdir("test_introducer")
263         c1 = os.path.join(basedir, "c1")
264         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
265         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
266         INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
267
268         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
269         def _cb(res):
270             out, err, rc_or_sig = res
271             self.failUnlessEqual(rc_or_sig, 0)
272             # by writing this file, we get ten seconds before the node will
273             # exit. This insures that even if the test fails (and the 'stop'
274             # command doesn't work), the client should still terminate.
275             open(HOTLINE_FILE, "w").write("")
276             # now it's safe to start the node
277         d.addCallback(_cb)
278
279         def _then_start_the_node(res):
280             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
281         d.addCallback(_then_start_the_node)
282
283         def _cb2(res):
284             out, err, rc_or_sig = res
285
286             open(HOTLINE_FILE, "w").write("")
287             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
288             self.failUnlessEqual(rc_or_sig, 0, errstr)
289             self.failUnlessEqual(out, "", errstr)
290             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
291
292             # the parent (twistd) has exited. However, twistd writes the pid
293             # from the child, not the parent, so we can't expect twistd.pid
294             # to exist quite yet.
295
296             # the node is running, but it might not have made it past the
297             # first reactor turn yet, and if we kill it too early, it won't
298             # remove the twistd.pid file. So wait until it does something
299             # that we know it won't do until after the first turn.
300         d.addCallback(_cb2)
301
302         def _node_has_started():
303             return os.path.exists(INTRODUCER_FURL_FILE)
304         d.addCallback(lambda res: self.poll(_node_has_started))
305
306         def _started(res):
307             open(HOTLINE_FILE, "w").write("")
308             self.failUnless(os.path.exists(TWISTD_PID_FILE))
309             # rm this so we can detect when the second incarnation is ready
310             os.unlink(INTRODUCER_FURL_FILE)
311             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
312         d.addCallback(_started)
313
314         def _then(res):
315             out, err, rc_or_sig = res
316             open(HOTLINE_FILE, "w").write("")
317             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
318             self.failUnlessEqual(rc_or_sig, 0, errstr)
319             self.failUnlessEqual(out, "", errstr)
320             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
321         d.addCallback(_then)
322
323         # again, the second incarnation of the node might not be ready yet,
324         # so poll until it is
325         d.addCallback(lambda res: self.poll(_node_has_started))
326
327         # now we can kill it. TODO: On a slow machine, the node might kill
328         # itself before we get a chance too, especially if spawning the
329         # 'tahoe stop' command takes a while.
330         def _stop(res):
331             open(HOTLINE_FILE, "w").write("")
332             self.failUnless(os.path.exists(TWISTD_PID_FILE))
333
334             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
335         d.addCallback(_stop)
336
337         def _after_stopping(res):
338             out, err, rc_or_sig = res
339             open(HOTLINE_FILE, "w").write("")
340             # the parent has exited by now
341             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
342             self.failUnlessEqual(rc_or_sig, 0, errstr)
343             self.failUnlessEqual(out, "", errstr)
344             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
345             # the parent was supposed to poll and wait until it sees
346             # twistd.pid go away before it exits, so twistd.pid should be
347             # gone by now.
348             self.failIf(os.path.exists(TWISTD_PID_FILE))
349         d.addCallback(_after_stopping)
350
351         def _remove_hotline(res):
352             os.unlink(HOTLINE_FILE)
353             return res
354         d.addBoth(_remove_hotline)
355         return d
356     test_introducer.timeout = 480 # This hit the 120-second timeout on "François Lenny-armv5tel", then it hit a 240-second timeout on our feisty2.5 buildslave: http://allmydata.org/buildbot/builders/feisty2.5/builds/2381/steps/test/logs/test.log
357
358     def test_client_no_noise(self):
359         self.skip_if_cannot_daemonize()
360         import pkg_resources
361         try:
362             pkg_resources.require("Twisted>=9.0.0")
363         except pkg_resources.VersionConflict:
364             raise unittest.SkipTest("We pass this test only with Twisted >= v9.0.0")
365         basedir = self.workdir("test_client_no_noise")
366         c1 = os.path.join(basedir, "c1")
367         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
368         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
369         PORTNUMFILE = os.path.join(c1, "client.port")
370
371         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
372         def _cb(res):
373             out, err, rc_or_sig = res
374             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
375             assert rc_or_sig == 0, errstr
376             self.failUnlessEqual(rc_or_sig, 0)
377             # By writing this file, we get forty seconds before the client will exit. This insures
378             # that even if the 'stop' command doesn't work (and the test fails), the client should
379             # still terminate.
380             open(HOTLINE_FILE, "w").write("")
381             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
382             # now it's safe to start the node
383         d.addCallback(_cb)
384
385         def _start(res):
386             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
387         d.addCallback(_start)
388
389         def _cb2(res):
390             out, err, rc_or_sig = res
391             errstr = "cc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
392             open(HOTLINE_FILE, "w").write("")
393             self.failUnlessEqual(rc_or_sig, 0, errstr)
394             self.failUnlessEqual(out, "", errstr) # If you emit noise, you fail this test.
395             self.failUnlessEqual(err, "", errstr)
396
397             # the parent (twistd) has exited. However, twistd writes the pid
398             # from the child, not the parent, so we can't expect twistd.pid
399             # to exist quite yet.
400
401             # the node is running, but it might not have made it past the
402             # first reactor turn yet, and if we kill it too early, it won't
403             # remove the twistd.pid file. So wait until it does something
404             # that we know it won't do until after the first turn.
405         d.addCallback(_cb2)
406
407         def _node_has_started():
408             return os.path.exists(PORTNUMFILE)
409         d.addCallback(lambda res: self.poll(_node_has_started))
410
411         # now we can kill it. TODO: On a slow machine, the node might kill
412         # itself before we get a chance too, especially if spawning the
413         # 'tahoe stop' command takes a while.
414         def _stop(res):
415             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
416             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
417         d.addCallback(_stop)
418         return d
419
420     def test_client(self):
421         self.skip_if_cannot_daemonize()
422         basedir = self.workdir("test_client")
423         c1 = os.path.join(basedir, "c1")
424         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
425         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
426         PORTNUMFILE = os.path.join(c1, "client.port")
427
428         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-node", "--basedir", c1, "--webport", "0"], env=os.environ)
429         def _cb(res):
430             out, err, rc_or_sig = res
431             self.failUnlessEqual(rc_or_sig, 0)
432             # By writing this file, we get sixty seconds before the client will exit. This insures
433             # that even if the 'stop' command doesn't work (and the test fails), the client should
434             # still terminate.
435             open(HOTLINE_FILE, "w").write("")
436             open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
437             # now it's safe to start the node
438         d.addCallback(_cb)
439
440         def _start(res):
441             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
442         d.addCallback(_start)
443
444         def _cb2(res):
445             out, err, rc_or_sig = res
446             open(HOTLINE_FILE, "w").write("")
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.
451
452             # the parent (twistd) has exited. However, twistd writes the pid
453             # from the child, not the parent, so we can't expect twistd.pid
454             # to exist quite yet.
455
456             # the node is running, but it might not have made it past the
457             # first reactor turn yet, and if we kill it too early, it won't
458             # remove the twistd.pid file. So wait until it does something
459             # that we know it won't do until after the first turn.
460         d.addCallback(_cb2)
461
462         def _node_has_started():
463             return os.path.exists(PORTNUMFILE)
464         d.addCallback(lambda res: self.poll(_node_has_started))
465
466         def _started(res):
467             open(HOTLINE_FILE, "w").write("")
468             self.failUnless(os.path.exists(TWISTD_PID_FILE))
469             # rm this so we can detect when the second incarnation is ready
470             os.unlink(PORTNUMFILE)
471
472             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
473         d.addCallback(_started)
474
475         def _cb3(res):
476             out, err, rc_or_sig = res
477
478             open(HOTLINE_FILE, "w").write("")
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         d.addCallback(_cb3)
484
485         # again, the second incarnation of the node might not be ready yet,
486         # so poll until it is
487         d.addCallback(lambda res: self.poll(_node_has_started))
488
489         # now we can kill it. TODO: On a slow machine, the node might kill
490         # itself before we get a chance too, especially if spawning the
491         # 'tahoe stop' command takes a while.
492         def _stop(res):
493             open(HOTLINE_FILE, "w").write("")
494             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
495             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
496         d.addCallback(_stop)
497
498         def _cb4(res):
499             out, err, rc_or_sig = res
500
501             open(HOTLINE_FILE, "w").write("")
502             # the parent has exited by now
503             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
504             self.failUnlessEqual(rc_or_sig, 0, errstr)
505             self.failUnlessEqual(out, "", errstr)
506             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
507             # the parent was supposed to poll and wait until it sees
508             # twistd.pid go away before it exits, so twistd.pid should be
509             # gone by now.
510             self.failIf(os.path.exists(TWISTD_PID_FILE))
511         d.addCallback(_cb4)
512         def _remove_hotline(res):
513             os.unlink(HOTLINE_FILE)
514             return res
515         d.addBoth(_remove_hotline)
516         return d
517
518     def test_baddir(self):
519         self.skip_if_cannot_daemonize()
520         basedir = self.workdir("test_baddir")
521         fileutil.make_dirs(basedir)
522
523         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
524         def _cb(res):
525             out, err, rc_or_sig = res
526             self.failUnlessEqual(rc_or_sig, 1)
527             self.failUnless("does not look like a node directory" in err, err)
528         d.addCallback(_cb)
529
530         def _then_stop_it(res):
531             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
532         d.addCallback(_then_stop_it)
533
534         def _cb2(res):
535             out, err, rc_or_sig = res
536             self.failUnlessEqual(rc_or_sig, 2)
537             self.failUnless("does not look like a running node directory" in err)
538         d.addCallback(_cb2)
539
540         def _then_start_in_bogus_basedir(res):
541             not_a_dir = os.path.join(basedir, "bogus")
542             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
543         d.addCallback(_then_start_in_bogus_basedir)
544
545         def _cb3(res):
546             out, err, rc_or_sig = res
547             self.failUnlessEqual(rc_or_sig, 1)
548             self.failUnless("does not look like a directory at all" in err, err)
549         d.addCallback(_cb3)
550         return d
551
552     def test_keygen(self):
553         self.skip_if_cannot_daemonize()
554         basedir = self.workdir("test_keygen")
555         c1 = os.path.join(basedir, "c1")
556         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
557         KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
558
559         d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
560         def _cb(res):
561             out, err, rc_or_sig = res
562             self.failUnlessEqual(rc_or_sig, 0)
563         d.addCallback(_cb)
564
565         def _start(res):
566             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
567         d.addCallback(_start)
568
569         def _cb2(res):
570             out, err, rc_or_sig = res
571             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
572             self.failUnlessEqual(rc_or_sig, 0, errstr)
573             self.failUnlessEqual(out, "", errstr)
574             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
575
576             # the parent (twistd) has exited. However, twistd writes the pid
577             # from the child, not the parent, so we can't expect twistd.pid
578             # to exist quite yet.
579
580             # the node is running, but it might not have made it past the
581             # first reactor turn yet, and if we kill it too early, it won't
582             # remove the twistd.pid file. So wait until it does something
583             # that we know it won't do until after the first turn.
584         d.addCallback(_cb2)
585
586         def _node_has_started():
587             return os.path.exists(KEYGEN_FURL_FILE)
588         d.addCallback(lambda res: self.poll(_node_has_started))
589
590         def _started(res):
591             self.failUnless(os.path.exists(TWISTD_PID_FILE))
592             # rm this so we can detect when the second incarnation is ready
593             os.unlink(KEYGEN_FURL_FILE)
594             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
595         d.addCallback(_started)
596
597         def _cb3(res):
598             out, err, rc_or_sig = res
599             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
600             self.failUnlessEqual(rc_or_sig, 0, errstr)
601             self.failUnlessEqual(out, "", errstr)
602             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
603         d.addCallback(_cb3)
604
605         # again, the second incarnation of the node might not be ready yet,
606         # so poll until it is
607         d.addCallback(lambda res: self.poll(_node_has_started))
608
609         # now we can kill it. TODO: On a slow machine, the node might kill
610         # itself before we get a chance too, especially if spawning the
611         # 'tahoe stop' command takes a while.
612         def _stop(res):
613             self.failUnless(os.path.exists(TWISTD_PID_FILE))
614             return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
615         d.addCallback(_stop)
616
617         def _cb4(res):
618             out, err, rc_or_sig = res
619             # the parent has exited by now
620             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
621             self.failUnlessEqual(rc_or_sig, 0, errstr)
622             self.failUnlessEqual(out, "", errstr)
623             # self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
624             # the parent was supposed to poll and wait until it sees
625             # twistd.pid go away before it exits, so twistd.pid should be
626             # gone by now.
627             self.failIf(os.path.exists(TWISTD_PID_FILE))
628         d.addCallback(_cb4)
629         return d