2 from twisted.trial import unittest
5 from cStringIO import StringIO
6 from twisted.python import usage
8 from allmydata.scripts import runner, debug
9 from allmydata.util import fileutil
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, "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, "client.tac")))
42 self.failUnlessRaises(usage.UsageError,
44 ["create-client", "basedir", "extraarg"],
47 self.failUnlessRaises(usage.UsageError,
52 def test_introducer(self):
53 basedir = self.workdir("test_introducer")
54 c1 = os.path.join(basedir, "c1")
55 argv = ["--quiet", "create-introducer", "--basedir", c1]
56 out,err = StringIO(), StringIO()
57 rc = runner.runner(argv, stdout=out, stderr=err)
58 self.failUnlessEqual(err.getvalue(), "")
59 self.failUnlessEqual(out.getvalue(), "")
60 self.failUnlessEqual(rc, 0)
61 self.failUnless(os.path.exists(c1))
62 self.failUnless(os.path.exists(os.path.join(c1, "introducer.tac")))
64 # creating the introducer a second time should throw an exception
65 out,err = StringIO(), StringIO()
66 rc = runner.runner(argv, stdout=out, stderr=err)
67 self.failIfEqual(rc, 0)
68 self.failUnlessEqual(out.getvalue(), "")
69 self.failUnless("The base directory already exists" in err.getvalue())
71 c2 = os.path.join(basedir, "c2")
72 argv = ["--quiet", "create-introducer", c2]
74 self.failUnless(os.path.exists(c2))
75 self.failUnless(os.path.exists(os.path.join(c2, "introducer.tac")))
77 self.failUnlessRaises(usage.UsageError,
79 ["create-introducer", "basedir", "extraarg"],
82 self.failUnlessRaises(usage.UsageError,
84 ["create-introducer"],
87 def test_subcommands(self):
88 self.failUnlessRaises(usage.UsageError,
93 class Diagnostics(unittest.TestCase):
94 def test_dump_root_dirnode_failure(self):
96 config = {'basedirs': ["missing_basedir"]}
97 rc = debug.dump_root_dirnode(config, s)
99 self.failUnless("unable to read root dirnode file from" in output)
100 self.failIfEqual(rc, 0)
102 class RunNode(unittest.TestCase):
103 def workdir(self, name):
104 basedir = os.path.join("test_runner", "RunNode", name)
105 fileutil.make_dirs(basedir)
108 def test_client(self):
109 if sys.platform in ("win32", "cygwin"):
110 # thus might not be entirely true, but I've yet to see proper
111 # daemonization on a windows box. -warner
112 raise unittest.SkipTest("twistd does not fork under windows")
113 basedir = self.workdir("test_client")
114 c1 = os.path.join(basedir, "c1")
115 argv = ["--quiet", "create-client", "--basedir", c1]
116 out,err = StringIO(), StringIO()
117 rc = runner.runner(argv, stdout=out, stderr=err)
118 self.failUnlessEqual(rc, 0)
119 open(os.path.join(c1, "suicide_prevention_hotline_file"), "w").write("")
120 open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
121 # now it's safe to start the node
123 argv = ["--quiet", "start", c1]
124 out,err = StringIO(), StringIO()
125 rc = runner.runner(argv, stdout=out, stderr=err)
126 self.failUnlessEqual(rc, 0)
127 time.sleep(0.1) # the child process needs a moment to write the pidfile
128 self.failUnless(os.path.exists(os.path.join(c1, "twistd.pid")))
130 argv = ["--quiet", "restart", c1]
131 out,err = StringIO(), StringIO()
132 rc = runner.runner(argv, stdout=out, stderr=err)
133 self.failUnlessEqual(rc, 0)
135 self.failUnless(os.path.exists(os.path.join(c1, "twistd.pid")))
137 argv = ["--quiet", "stop", c1]
138 out,err = StringIO(), StringIO()
139 rc = runner.runner(argv, stdout=out, stderr=err)
140 self.failUnlessEqual(rc, 0)
141 self.failIf(os.path.exists(os.path.join(c1, "twistd.pid")))