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, debug
9 from allmydata.util import fileutil, testutil
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("The base directory already exists" in err.getvalue())
36 c2 = os.path.join(basedir, "c2")
37 argv = ["--quiet", "create-client", c2]
39 self.failUnless(os.path.exists(c2))
40 self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
42 self.failUnlessRaises(usage.UsageError,
44 ["create-client", "basedir", "extraarg"],
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")))
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())
67 c2 = os.path.join(basedir, "c2")
68 argv = ["--quiet", "create-introducer", c2]
70 self.failUnless(os.path.exists(c2))
71 self.failUnless(os.path.exists(os.path.join(c2,
72 "tahoe-introducer.tac")))
74 self.failUnlessRaises(usage.UsageError,
76 ["create-introducer", "basedir", "extraarg"],
79 self.failUnlessRaises(usage.UsageError,
81 ["create-introducer"],
84 def test_subcommands(self):
85 self.failUnlessRaises(usage.UsageError,
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)
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
114 TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
116 d = defer.succeed(None)
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)
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.
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.
137 d.addCallback(_start)
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))
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)
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))
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.
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
183 self.failIf(os.path.exists(TWISTD_PID_FILE))
185 def _remove_hotline(res):
186 os.unlink(HOTLINE_FILE)
188 d.addBoth(_remove_hotline)
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())
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"
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"