2 from twisted.trial import unittest
4 from cStringIO import StringIO
5 from twisted.python import usage, runtime
6 from twisted.internet import defer
8 from allmydata.scripts import runner
9 from allmydata.util import fileutil, pollmixin
11 class CreateNode(unittest.TestCase):
12 def workdir(self, name):
13 basedir = os.path.join("test_runner", "CreateNode", name)
14 fileutil.make_dirs(basedir)
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")))
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())
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())
39 c2 = os.path.join(basedir, "c2")
40 argv = ["--quiet", "create-client", c2]
42 self.failUnless(os.path.exists(c2))
43 self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
45 self.failUnlessRaises(usage.UsageError,
47 ["create-client", "basedir", "extraarg"],
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")))
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())
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())
73 c2 = os.path.join(basedir, "c2")
74 argv = ["--quiet", "create-introducer", c2]
76 self.failUnless(os.path.exists(c2))
77 self.failUnless(os.path.exists(os.path.join(c2,
78 "tahoe-introducer.tac")))
80 self.failUnlessRaises(usage.UsageError,
82 ["create-introducer", "basedir", "extraarg"],
85 self.failUnlessRaises(usage.UsageError,
87 ["create-introducer"],
90 def test_subcommands(self):
91 self.failUnlessRaises(usage.UsageError,
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)
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
119 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
121 d = defer.succeed(None)
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)
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.
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.
142 d.addCallback(_start)
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))
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)
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))
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.
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
188 self.failIf(os.path.exists(TWISTD_PID_FILE))
190 def _remove_hotline(res):
191 os.unlink(HOTLINE_FILE)
193 d.addBoth(_remove_hotline)
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 ten seconds before the client will
207 # exit. This insures that even if the test fails (and the 'stop'
208 # command doesn't work), the client should 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
214 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
216 d = defer.succeed(None)
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)
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.
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.
237 d.addCallback(_start)
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))
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)
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))
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.
268 open(HOTLINE_FILE, "w").write("")
269 self.failUnless(os.path.exists(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
283 self.failIf(os.path.exists(TWISTD_PID_FILE))
285 def _remove_hotline(res):
286 os.unlink(HOTLINE_FILE)
288 d.addBoth(_remove_hotline)
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())
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"
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())