]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_runner.py
decentralized directories: integration and testing
[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
8 from allmydata.scripts import runner, debug
9 from allmydata.util import fileutil, testutil
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("The base directory already exists" in err.getvalue())
35
36         c2 = os.path.join(basedir, "c2")
37         argv = ["--quiet", "create-client", c2]
38         runner.runner(argv)
39         self.failUnless(os.path.exists(c2))
40         self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
41
42         self.failUnlessRaises(usage.UsageError,
43                               runner.runner,
44                               ["create-client", "basedir", "extraarg"],
45                               run_by_human=False)
46
47     def test_introducer(self):
48         basedir = self.workdir("test_introducer")
49         c1 = os.path.join(basedir, "c1")
50         argv = ["--quiet", "create-introducer", "--basedir", c1]
51         out,err = StringIO(), StringIO()
52         rc = runner.runner(argv, stdout=out, stderr=err)
53         self.failUnlessEqual(err.getvalue(), "")
54         self.failUnlessEqual(out.getvalue(), "")
55         self.failUnlessEqual(rc, 0)
56         self.failUnless(os.path.exists(c1))
57         self.failUnless(os.path.exists(os.path.join(c1,
58                                                     "tahoe-introducer.tac")))
59
60         # creating the introducer a second time should throw an exception
61         out,err = StringIO(), StringIO()
62         rc = runner.runner(argv, stdout=out, stderr=err)
63         self.failIfEqual(rc, 0)
64         self.failUnlessEqual(out.getvalue(), "")
65         self.failUnless("The base directory already exists" in err.getvalue())
66
67         c2 = os.path.join(basedir, "c2")
68         argv = ["--quiet", "create-introducer", c2]
69         runner.runner(argv)
70         self.failUnless(os.path.exists(c2))
71         self.failUnless(os.path.exists(os.path.join(c2,
72                                                     "tahoe-introducer.tac")))
73
74         self.failUnlessRaises(usage.UsageError,
75                               runner.runner,
76                               ["create-introducer", "basedir", "extraarg"],
77                               run_by_human=False)
78
79         self.failUnlessRaises(usage.UsageError,
80                               runner.runner,
81                               ["create-introducer"],
82                               run_by_human=False)
83
84     def test_subcommands(self):
85         self.failUnlessRaises(usage.UsageError,
86                               runner.runner,
87                               [],
88                               run_by_human=False)
89
90 class RunNode(unittest.TestCase, testutil.PollMixin):
91     def workdir(self, name):
92         basedir = os.path.join("test_runner", "RunNode", name)
93         fileutil.make_dirs(basedir)
94         return basedir
95
96     def test_client(self):
97         if runtime.platformType == "win32":
98             # twistd on windows doesn't daemonize. cygwin works normally.
99             raise unittest.SkipTest("twistd does not fork under windows")
100         basedir = self.workdir("test_client")
101         c1 = os.path.join(basedir, "c1")
102         argv = ["--quiet", "create-client", "--basedir", c1, "--webport", "0"]
103         out,err = StringIO(), StringIO()
104         rc = runner.runner(argv, stdout=out, stderr=err)
105         self.failUnlessEqual(rc, 0)
106         # by writing this file, we get ten seconds before the client will
107         # exit. This insures that even if the test fails (and the 'stop'
108         # command doesn't work), the client should still terminate.
109         HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
110         open(HOTLINE_FILE, "w").write("")
111         open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
112         # now it's safe to start the node
113
114         TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
115
116         d = defer.succeed(None)
117         def _start(res):
118             argv = ["--quiet", "start", c1]
119             out,err = StringIO(), StringIO()
120             rc = runner.runner(argv, stdout=out, stderr=err)
121             open(HOTLINE_FILE, "w").write("")
122             outs = out.getvalue() ; errs = err.getvalue()
123             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
124             self.failUnlessEqual(rc, 0, errstr)
125             self.failUnlessEqual(outs, "", errstr)
126             self.failUnlessEqual(errs, "", errstr)
127
128             # the parent (twistd) has exited. However, twistd writes the pid
129             # from the child, not the parent, so we can't expect twistd.pid
130             # to exist quite yet.
131
132             # the node is running, but it might not have made it past the
133             # first reactor turn yet, and if we kill it too early, it won't
134             # remove the twistd.pid file. So wait until it does something
135             # that we know it won't do until after the first turn.
136
137         d.addCallback(_start)
138
139         PORTNUMFILE = os.path.join(c1, "client.port")
140         def _node_has_started():
141             return os.path.exists(PORTNUMFILE)
142         d.addCallback(lambda res: self.poll(_node_has_started))
143
144         def _started(res):
145             open(HOTLINE_FILE, "w").write("")
146             self.failUnless(os.path.exists(TWISTD_PID_FILE))
147             # rm this so we can detect when the second incarnation is ready
148             os.unlink(PORTNUMFILE)
149             argv = ["--quiet", "restart", c1]
150             out,err = StringIO(), StringIO()
151             rc = runner.runner(argv, stdout=out, stderr=err)
152             open(HOTLINE_FILE, "w").write("")
153             outs = out.getvalue() ; errs = err.getvalue()
154             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
155             self.failUnlessEqual(rc, 0, errstr)
156             self.failUnlessEqual(outs, "", errstr)
157             self.failUnlessEqual(errs, "", errstr)
158         d.addCallback(_started)
159
160         # again, the second incarnation of the node might not be ready yet,
161         # so poll until it is
162         d.addCallback(lambda res: self.poll(_node_has_started))
163
164         # now we can kill it. TODO: On a slow machine, the node might kill
165         # itself before we get a chance too, especially if spawning the
166         # 'tahoe stop' command takes a while.
167         def _stop(res):
168             open(HOTLINE_FILE, "w").write("")
169             self.failUnless(os.path.exists(TWISTD_PID_FILE))
170             argv = ["--quiet", "stop", c1]
171             out,err = StringIO(), StringIO()
172             rc = runner.runner(argv, stdout=out, stderr=err)
173             open(HOTLINE_FILE, "w").write("")
174             # the parent has exited by now
175             outs = out.getvalue() ; errs = err.getvalue()
176             errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc, outs, errs)
177             self.failUnlessEqual(rc, 0, errstr)
178             self.failUnlessEqual(outs, "", errstr)
179             self.failUnlessEqual(errs, "", errstr)
180             # the parent was supposed to poll and wait until it sees
181             # twistd.pid go away before it exits, so twistd.pid should be
182             # gone by now.
183             self.failIf(os.path.exists(TWISTD_PID_FILE))
184         d.addCallback(_stop)
185         def _remove_hotline(res):
186             os.unlink(HOTLINE_FILE)
187             return res
188         d.addBoth(_remove_hotline)
189         return d
190
191     def test_baddir(self):
192         basedir = self.workdir("test_baddir")
193         fileutil.make_dirs(basedir)
194         argv = ["--quiet", "start", "--basedir", basedir]
195         out,err = StringIO(), StringIO()
196         rc = runner.runner(argv, stdout=out, stderr=err)
197         self.failUnlessEqual(rc, 1)
198         self.failUnless("does not look like a node directory" in err.getvalue())
199
200         argv = ["--quiet", "stop", "--basedir", basedir]
201         out,err = StringIO(), StringIO()
202         rc = runner.runner(argv, stdout=out, stderr=err)
203         self.failUnlessEqual(rc, 2)
204         self.failUnless("does not look like a running node directory"
205                         in err.getvalue())
206
207         not_a_dir = os.path.join(basedir, "bogus")
208         argv = ["--quiet", "start", "--basedir", not_a_dir]
209         out,err = StringIO(), StringIO()
210         rc = runner.runner(argv, stdout=out, stderr=err)
211         self.failUnlessEqual(rc, 1)
212         self.failUnless("does not look like a node directory" in err.getvalue())
213         self.failUnless("doesn't look like a directory at all"
214                         in err.getvalue())
215
216