]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
test: extend timeout on the hotline file that prevents the client from stopping itself
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_runner.py
1
2 from twisted.trial import unittest
3
4 from cStringIO import StringIO
5 from twisted.python import usage, runtime
6 from twisted.internet import defer
7 import os.path, re
8 from allmydata.scripts import runner
9 from allmydata.util import fileutil, pollmixin
10
11 class CreateNode(unittest.TestCase):
12     def workdir(self, name):
13         basedir = os.path.join("test_runner", "CreateNode", name)
14         fileutil.make_dirs(basedir)
15         return basedir
16
17     def test_client(self):
18         basedir = self.workdir("test_client")
19         c1 = os.path.join(basedir, "c1")
20         argv = ["--quiet", "create-client", "--basedir", c1]
21         out,err = StringIO(), StringIO()
22         rc = runner.runner(argv, stdout=out, stderr=err)
23         self.failUnlessEqual(err.getvalue(), "")
24         self.failUnlessEqual(out.getvalue(), "")
25         self.failUnlessEqual(rc, 0)
26         self.failUnless(os.path.exists(c1))
27         self.failUnless(os.path.exists(os.path.join(c1, "tahoe-client.tac")))
28
29         # creating the client a second time should throw an exception
30         out,err = StringIO(), StringIO()
31         rc = runner.runner(argv, stdout=out, stderr=err)
32         self.failIfEqual(rc, 0)
33         self.failUnlessEqual(out.getvalue(), "")
34         self.failUnless("is not empty." in err.getvalue())
35
36         # Fail if there is a line that doesn't end with a PUNCTUATION MARK.
37         self.failIf(re.search("[^\.!?]\n", err.getvalue()), err.getvalue())
38
39         c2 = os.path.join(basedir, "c2")
40         argv = ["--quiet", "create-client", c2]
41         runner.runner(argv)
42         self.failUnless(os.path.exists(c2))
43         self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
44
45         self.failUnlessRaises(usage.UsageError,
46                               runner.runner,
47                               ["create-client", "basedir", "extraarg"],
48                               run_by_human=False)
49
50     def test_introducer(self):
51         basedir = self.workdir("test_introducer")
52         c1 = os.path.join(basedir, "c1")
53         argv = ["--quiet", "create-introducer", "--basedir", c1]
54         out,err = StringIO(), StringIO()
55         rc = runner.runner(argv, stdout=out, stderr=err)
56         self.failUnlessEqual(err.getvalue(), "")
57         self.failUnlessEqual(out.getvalue(), "")
58         self.failUnlessEqual(rc, 0)
59         self.failUnless(os.path.exists(c1))
60         self.failUnless(os.path.exists(os.path.join(c1,
61                                                     "tahoe-introducer.tac")))
62
63         # creating the introducer a second time should throw an exception
64         out,err = StringIO(), StringIO()
65         rc = runner.runner(argv, stdout=out, stderr=err)
66         self.failIfEqual(rc, 0)
67         self.failUnlessEqual(out.getvalue(), "")
68         self.failUnless("is not empty" in err.getvalue())
69
70         # Fail if there is a line that doesn't end with a PUNCTUATION MARK.
71         self.failIf(re.search("[^\.!?]\n", err.getvalue()), err.getvalue())
72
73         c2 = os.path.join(basedir, "c2")
74         argv = ["--quiet", "create-introducer", c2]
75         runner.runner(argv)
76         self.failUnless(os.path.exists(c2))
77         self.failUnless(os.path.exists(os.path.join(c2,
78                                                     "tahoe-introducer.tac")))
79
80         self.failUnlessRaises(usage.UsageError,
81                               runner.runner,
82                               ["create-introducer", "basedir", "extraarg"],
83                               run_by_human=False)
84
85         self.failUnlessRaises(usage.UsageError,
86                               runner.runner,
87                               ["create-introducer"],
88                               run_by_human=False)
89
90     def test_subcommands(self):
91         self.failUnlessRaises(usage.UsageError,
92                               runner.runner,
93                               [],
94                               run_by_human=False)
95
96 class RunNode(unittest.TestCase, pollmixin.PollMixin):
97     def workdir(self, name):
98         basedir = os.path.join("test_runner", "RunNode", name)
99         fileutil.make_dirs(basedir)
100         return basedir
101
102     def test_introducer(self):
103         if runtime.platformType == "win32":
104             # twistd on windows doesn't daemonize. cygwin works normally.
105             raise unittest.SkipTest("twistd does not fork under windows")
106         basedir = self.workdir("test_introducer")
107         c1 = os.path.join(basedir, "c1")
108         argv = ["--quiet", "create-introducer", "--basedir", c1]
109         out,err = StringIO(), StringIO()
110         rc = runner.runner(argv, stdout=out, stderr=err)
111         self.failUnlessEqual(rc, 0)
112         # by writing this file, we get ten seconds before the node will
113         # exit. This insures that even if the test fails (and the 'stop'
114         # command doesn't work), the client should still terminate.
115         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
116         open(HOTLINE_FILE, "w").write("")
117         # now it's safe to start the node
118
119         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
120
121         d = defer.succeed(None)
122         def _start(res):
123             argv = ["--quiet", "start", c1]
124             out,err = StringIO(), StringIO()
125             rc = runner.runner(argv, stdout=out, stderr=err)
126             open(HOTLINE_FILE, "w").write("")
127             outs = out.getvalue() ; errs = err.getvalue()
128             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
129             self.failUnlessEqual(rc, 0, errstr)
130             self.failUnlessEqual(outs, "", errstr)
131             self.failUnlessEqual(errs, "", errstr)
132
133             # the parent (twistd) has exited. However, twistd writes the pid
134             # from the child, not the parent, so we can't expect twistd.pid
135             # to exist quite yet.
136
137             # the node is running, but it might not have made it past the
138             # first reactor turn yet, and if we kill it too early, it won't
139             # remove the twistd.pid file. So wait until it does something
140             # that we know it won't do until after the first turn.
141
142         d.addCallback(_start)
143
144         INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
145         def _node_has_started():
146             return os.path.exists(INTRODUCER_FURL_FILE)
147         d.addCallback(lambda res: self.poll(_node_has_started))
148
149         def _started(res):
150             open(HOTLINE_FILE, "w").write("")
151             self.failUnless(os.path.exists(TWISTD_PID_FILE))
152             # rm this so we can detect when the second incarnation is ready
153             os.unlink(INTRODUCER_FURL_FILE)
154             argv = ["--quiet", "restart", c1]
155             out,err = StringIO(), StringIO()
156             rc = runner.runner(argv, stdout=out, stderr=err)
157             open(HOTLINE_FILE, "w").write("")
158             outs = out.getvalue() ; errs = err.getvalue()
159             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
160             self.failUnlessEqual(rc, 0, errstr)
161             self.failUnlessEqual(outs, "", errstr)
162             self.failUnlessEqual(errs, "", errstr)
163         d.addCallback(_started)
164
165         # again, the second incarnation of the node might not be ready yet,
166         # so poll until it is
167         d.addCallback(lambda res: self.poll(_node_has_started))
168
169         # now we can kill it. TODO: On a slow machine, the node might kill
170         # itself before we get a chance too, especially if spawning the
171         # 'tahoe stop' command takes a while.
172         def _stop(res):
173             open(HOTLINE_FILE, "w").write("")
174             self.failUnless(os.path.exists(TWISTD_PID_FILE))
175             argv = ["--quiet", "stop", c1]
176             out,err = StringIO(), StringIO()
177             rc = runner.runner(argv, stdout=out, stderr=err)
178             open(HOTLINE_FILE, "w").write("")
179             # the parent has exited by now
180             outs = out.getvalue() ; errs = err.getvalue()
181             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
182             self.failUnlessEqual(rc, 0, errstr)
183             self.failUnlessEqual(outs, "", errstr)
184             self.failUnlessEqual(errs, "", errstr)
185             # the parent was supposed to poll and wait until it sees
186             # twistd.pid go away before it exits, so twistd.pid should be
187             # gone by now.
188             self.failIf(os.path.exists(TWISTD_PID_FILE))
189         d.addCallback(_stop)
190         def _remove_hotline(res):
191             os.unlink(HOTLINE_FILE)
192             return res
193         d.addBoth(_remove_hotline)
194         return d
195
196     def test_client(self):
197         if runtime.platformType == "win32":
198             # twistd on windows doesn't daemonize. cygwin works normally.
199             raise unittest.SkipTest("twistd does not fork under windows")
200         basedir = self.workdir("test_client")
201         c1 = os.path.join(basedir, "c1")
202         argv = ["--quiet", "create-client", "--basedir", c1, "--webport", "0"]
203         out,err = StringIO(), StringIO()
204         rc = runner.runner(argv, stdout=out, stderr=err)
205         self.failUnlessEqual(rc, 0)
206         # By writing this file, we get forty seconds before the client will exit. This insures
207         # that even if the 'stop' command doesn't work (and the test fails), the client should
208         # still terminate.
209         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
210         open(HOTLINE_FILE, "w").write("")
211         open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
212         # now it's safe to start the node
213
214         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
215
216         d = defer.succeed(None)
217         def _start(res):
218             argv = ["--quiet", "start", c1]
219             out,err = StringIO(), StringIO()
220             rc = runner.runner(argv, stdout=out, stderr=err)
221             open(HOTLINE_FILE, "w").write("")
222             outs = out.getvalue() ; errs = err.getvalue()
223             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
224             self.failUnlessEqual(rc, 0, errstr)
225             self.failUnlessEqual(outs, "", errstr)
226             self.failUnlessEqual(errs, "", errstr)
227
228             # the parent (twistd) has exited. However, twistd writes the pid
229             # from the child, not the parent, so we can't expect twistd.pid
230             # to exist quite yet.
231
232             # the node is running, but it might not have made it past the
233             # first reactor turn yet, and if we kill it too early, it won't
234             # remove the twistd.pid file. So wait until it does something
235             # that we know it won't do until after the first turn.
236
237         d.addCallback(_start)
238
239         PORTNUMFILE = os.path.join(c1, "client.port")
240         def _node_has_started():
241             return os.path.exists(PORTNUMFILE)
242         d.addCallback(lambda res: self.poll(_node_has_started))
243
244         def _started(res):
245             open(HOTLINE_FILE, "w").write("")
246             self.failUnless(os.path.exists(TWISTD_PID_FILE))
247             # rm this so we can detect when the second incarnation is ready
248             os.unlink(PORTNUMFILE)
249             argv = ["--quiet", "restart", c1]
250             out,err = StringIO(), StringIO()
251             rc = runner.runner(argv, stdout=out, stderr=err)
252             open(HOTLINE_FILE, "w").write("")
253             outs = out.getvalue() ; errs = err.getvalue()
254             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
255             self.failUnlessEqual(rc, 0, errstr)
256             self.failUnlessEqual(outs, "", errstr)
257             self.failUnlessEqual(errs, "", errstr)
258         d.addCallback(_started)
259
260         # again, the second incarnation of the node might not be ready yet,
261         # so poll until it is
262         d.addCallback(lambda res: self.poll(_node_has_started))
263
264         # now we can kill it. TODO: On a slow machine, the node might kill
265         # itself before we get a chance too, especially if spawning the
266         # 'tahoe stop' command takes a while.
267         def _stop(res):
268             open(HOTLINE_FILE, "w").write("")
269             self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
270             argv = ["--quiet", "stop", c1]
271             out,err = StringIO(), StringIO()
272             rc = runner.runner(argv, stdout=out, stderr=err)
273             open(HOTLINE_FILE, "w").write("")
274             # the parent has exited by now
275             outs = out.getvalue() ; errs = err.getvalue()
276             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
277             self.failUnlessEqual(rc, 0, errstr)
278             self.failUnlessEqual(outs, "", errstr)
279             self.failUnlessEqual(errs, "", errstr)
280             # the parent was supposed to poll and wait until it sees
281             # twistd.pid go away before it exits, so twistd.pid should be
282             # gone by now.
283             self.failIf(os.path.exists(TWISTD_PID_FILE))
284         d.addCallback(_stop)
285         def _remove_hotline(res):
286             os.unlink(HOTLINE_FILE)
287             return res
288         d.addBoth(_remove_hotline)
289         return d
290
291     def test_baddir(self):
292         basedir = self.workdir("test_baddir")
293         fileutil.make_dirs(basedir)
294         argv = ["--quiet", "start", "--basedir", basedir]
295         out,err = StringIO(), StringIO()
296         rc = runner.runner(argv, stdout=out, stderr=err)
297         self.failUnlessEqual(rc, 1)
298         self.failUnless("does not look like a node directory" in err.getvalue())
299
300         argv = ["--quiet", "stop", "--basedir", basedir]
301         out,err = StringIO(), StringIO()
302         rc = runner.runner(argv, stdout=out, stderr=err)
303         self.failUnlessEqual(rc, 2)
304         self.failUnless("does not look like a running node directory"
305                         in err.getvalue())
306
307         not_a_dir = os.path.join(basedir, "bogus")
308         argv = ["--quiet", "start", "--basedir", not_a_dir]
309         out,err = StringIO(), StringIO()
310         rc = runner.runner(argv, stdout=out, stderr=err)
311         self.failUnlessEqual(rc, 1)
312         self.failUnless("does not look like a directory at all"
313                         in err.getvalue(), err.getvalue())
314
315     def test_keygen(self):
316         if runtime.platformType == "win32":
317             # twistd on windows doesn't daemonize. cygwin works normally.
318             raise unittest.SkipTest("twistd does not fork under windows")
319         basedir = self.workdir("test_keygen")
320         c1 = os.path.join(basedir, "c1")
321         argv = ["--quiet", "create-key-generator", "--basedir", c1]
322         out,err = StringIO(), StringIO()
323         rc = runner.runner(argv, stdout=out, stderr=err)
324         self.failUnlessEqual(rc, 0)
325
326         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
327
328         d = defer.succeed(None)
329         def _start(res):
330             argv = ["--quiet", "start", c1]
331             out,err = StringIO(), StringIO()
332             rc = runner.runner(argv, stdout=out, stderr=err)
333             outs = out.getvalue() ; errs = err.getvalue()
334             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
335             self.failUnlessEqual(rc, 0, errstr)
336             self.failUnlessEqual(outs, "", errstr)
337             self.failUnlessEqual(errs, "", errstr)
338
339             # the parent (twistd) has exited. However, twistd writes the pid
340             # from the child, not the parent, so we can't expect twistd.pid
341             # to exist quite yet.
342
343             # the node is running, but it might not have made it past the
344             # first reactor turn yet, and if we kill it too early, it won't
345             # remove the twistd.pid file. So wait until it does something
346             # that we know it won't do until after the first turn.
347
348         d.addCallback(_start)
349
350         KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
351         def _node_has_started():
352             return os.path.exists(KEYGEN_FURL_FILE)
353         d.addCallback(lambda res: self.poll(_node_has_started))
354
355         def _started(res):
356             self.failUnless(os.path.exists(TWISTD_PID_FILE))
357             # rm this so we can detect when the second incarnation is ready
358             os.unlink(KEYGEN_FURL_FILE)
359             argv = ["--quiet", "restart", c1]
360             out,err = StringIO(), StringIO()
361             rc = runner.runner(argv, stdout=out, stderr=err)
362             outs = out.getvalue() ; errs = err.getvalue()
363             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
364             self.failUnlessEqual(rc, 0, errstr)
365             self.failUnlessEqual(outs, "", errstr)
366             self.failUnlessEqual(errs, "", errstr)
367         d.addCallback(_started)
368
369         # again, the second incarnation of the node might not be ready yet,
370         # so poll until it is
371         d.addCallback(lambda res: self.poll(_node_has_started))
372
373         # now we can kill it. TODO: On a slow machine, the node might kill
374         # itself before we get a chance too, especially if spawning the
375         # 'tahoe stop' command takes a while.
376         def _stop(res):
377             self.failUnless(os.path.exists(TWISTD_PID_FILE))
378             argv = ["--quiet", "stop", c1]
379             out,err = StringIO(), StringIO()
380             rc = runner.runner(argv, stdout=out, stderr=err)
381             # the parent has exited by now
382             outs = out.getvalue() ; errs = err.getvalue()
383             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
384             self.failUnlessEqual(rc, 0, errstr)
385             self.failUnlessEqual(outs, "", errstr)
386             self.failUnlessEqual(errs, "", errstr)
387             # the parent was supposed to poll and wait until it sees
388             # twistd.pid go away before it exits, so twistd.pid should be
389             # gone by now.
390             self.failIf(os.path.exists(TWISTD_PID_FILE))
391         d.addCallback(_stop)
392         return d