1
# -*- coding: utf-8 -*-
3
from twisted.trial import unittest
5
from twisted.python import usage, runtime
6
from twisted.internet import utils
7
import os.path, re, sys
8
from cStringIO import StringIO
9
from allmydata.util import fileutil, pollmixin
10
from allmydata.scripts import runner
12
from allmydata.test import common_util
15
bintahoe = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(allmydata.__file__))), 'bin', 'tahoe')
16
if sys.platform == "win32":
19
class SkipOnCygwinMixin:
20
def skip_on_cygwin(self):
21
if "cygwin" in sys.platform.lower():
22
raise unittest.SkipTest("We don't know how to make this test work on cygwin: spawnProcess seems to hang forever. We don't know if 'bin/tahoe start' can be run on cygwin.")
24
class TheRightCode(common_util.SignalMixin, unittest.TestCase,
28
if not os.path.exists(bintahoe):
29
raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of tahoe.")
30
d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
32
out, err, rc_or_sig = res
33
self.failUnlessEqual(rc_or_sig, 0)
35
# Fail unless the allmydata-tahoe package is *this* version *and*
36
# was loaded from *this* source directory.
37
ad = os.path.dirname(os.path.dirname(os.path.realpath(allmydata.__file__)))
38
required_ver_and_path = "allmydata-tahoe: %s (%s)" % (allmydata.__version__, ad)
39
self.failUnless(out.startswith(required_ver_and_path),
40
(out, err, rc_or_sig, required_ver_and_path))
44
class CreateNode(unittest.TestCase):
45
# exercise "tahoe create-client", create-introducer,
46
# create-key-generator, and create-stats-gatherer, by calling the
47
# corresponding code as a subroutine.
49
def workdir(self, name):
50
basedir = os.path.join("test_runner", "CreateNode", name)
51
fileutil.make_dirs(basedir)
54
def run_tahoe(self, argv):
55
out,err = StringIO(), StringIO()
56
rc = runner.runner(argv, stdout=out, stderr=err)
57
return rc, out.getvalue(), err.getvalue()
59
def test_client(self):
60
basedir = self.workdir("test_client")
61
c1 = os.path.join(basedir, "c1")
62
argv = ["--quiet", "create-client", "--basedir", c1]
63
rc, out, err = self.run_tahoe(argv)
64
self.failUnlessEqual(err, "")
65
self.failUnlessEqual(out, "")
66
self.failUnlessEqual(rc, 0)
67
self.failUnless(os.path.exists(c1))
68
self.failUnless(os.path.exists(os.path.join(c1, "tahoe-client.tac")))
70
# creating the client a second time should be rejected
71
rc, out, err = self.run_tahoe(argv)
72
self.failIfEqual(rc, 0, str((out, err, rc)))
73
self.failUnlessEqual(out, "")
74
self.failUnless("is not empty." in err)
76
# Fail if there is a non-empty line that doesn't end with a
78
for line in err.splitlines():
79
self.failIf(re.search("[\S][^\.!?]$", line), (line,))
81
# test that the non --basedir form works too
82
c2 = os.path.join(basedir, "c2")
83
argv = ["--quiet", "create-client", c2]
84
rc, out, err = self.run_tahoe(argv)
85
self.failUnless(os.path.exists(c2))
86
self.failUnless(os.path.exists(os.path.join(c2, "tahoe-client.tac")))
88
# make sure it rejects too many arguments
89
argv = ["create-client", "basedir", "extraarg"]
90
self.failUnlessRaises(usage.UsageError,
94
def test_introducer(self):
95
basedir = self.workdir("test_introducer")
96
c1 = os.path.join(basedir, "c1")
97
argv = ["--quiet", "create-introducer", "--basedir", c1]
98
rc, out, err = self.run_tahoe(argv)
99
self.failUnlessEqual(err, "", err)
100
self.failUnlessEqual(out, "")
101
self.failUnlessEqual(rc, 0)
102
self.failUnless(os.path.exists(c1))
103
self.failUnless(os.path.exists(os.path.join(c1,"tahoe-introducer.tac")))
105
# creating the introducer a second time should be rejected
106
rc, out, err = self.run_tahoe(argv)
107
self.failIfEqual(rc, 0)
108
self.failUnlessEqual(out, "")
109
self.failUnless("is not empty" in err)
111
# Fail if there is a non-empty line that doesn't end with a
113
for line in err.splitlines():
114
self.failIf(re.search("[\S][^\.!?]$", line), (line,))
116
# test the non --basedir form
117
c2 = os.path.join(basedir, "c2")
118
argv = ["--quiet", "create-introducer", c2]
119
rc, out, err = self.run_tahoe(argv)
120
self.failUnlessEqual(err, "", err)
121
self.failUnlessEqual(out, "")
122
self.failUnlessEqual(rc, 0)
123
self.failUnless(os.path.exists(c2))
124
self.failUnless(os.path.exists(os.path.join(c2,"tahoe-introducer.tac")))
126
# reject extra arguments
127
argv = ["create-introducer", "basedir", "extraarg"]
128
self.failUnlessRaises(usage.UsageError,
131
# and require basedir to be provided in some form
132
argv = ["create-introducer"]
133
self.failUnlessRaises(usage.UsageError,
137
def test_key_generator(self):
138
basedir = self.workdir("test_key_generator")
139
kg1 = os.path.join(basedir, "kg1")
140
argv = ["--quiet", "create-key-generator", "--basedir", kg1]
141
rc, out, err = self.run_tahoe(argv)
142
self.failUnlessEqual(err, "")
143
self.failUnlessEqual(out, "")
144
self.failUnlessEqual(rc, 0)
145
self.failUnless(os.path.exists(kg1))
146
self.failUnless(os.path.exists(os.path.join(kg1, "tahoe-key-generator.tac")))
148
# creating it a second time should be rejected
149
rc, out, err = self.run_tahoe(argv)
150
self.failIfEqual(rc, 0, str((out, err, rc)))
151
self.failUnlessEqual(out, "")
152
self.failUnless("is not empty." in err)
154
# make sure it rejects too many arguments
155
argv = ["create-key-generator", "basedir", "extraarg"]
156
self.failUnlessRaises(usage.UsageError,
160
# make sure it rejects a missing basedir specification
161
argv = ["create-key-generator"]
162
rc, out, err = self.run_tahoe(argv)
163
self.failIfEqual(rc, 0, str((out, err, rc)))
164
self.failUnlessEqual(out, "")
165
self.failUnless("a basedir was not provided" in err)
167
def test_stats_gatherer(self):
168
basedir = self.workdir("test_stats_gatherer")
169
sg1 = os.path.join(basedir, "sg1")
170
argv = ["--quiet", "create-stats-gatherer", "--basedir", sg1]
171
rc, out, err = self.run_tahoe(argv)
172
self.failUnlessEqual(err, "")
173
self.failUnlessEqual(out, "")
174
self.failUnlessEqual(rc, 0)
175
self.failUnless(os.path.exists(sg1))
176
self.failUnless(os.path.exists(os.path.join(sg1, "tahoe-stats-gatherer.tac")))
178
# creating it a second time should be rejected
179
rc, out, err = self.run_tahoe(argv)
180
self.failIfEqual(rc, 0, str((out, err, rc)))
181
self.failUnlessEqual(out, "")
182
self.failUnless("is not empty." in err)
184
# test the non --basedir form
185
kg2 = os.path.join(basedir, "kg2")
186
argv = ["--quiet", "create-stats-gatherer", kg2]
187
rc, out, err = self.run_tahoe(argv)
188
self.failUnlessEqual(err, "", err)
189
self.failUnlessEqual(out, "")
190
self.failUnlessEqual(rc, 0)
191
self.failUnless(os.path.exists(kg2))
192
self.failUnless(os.path.exists(os.path.join(kg2,"tahoe-stats-gatherer.tac")))
194
# make sure it rejects too many arguments
195
argv = ["create-stats-gatherer", "basedir", "extraarg"]
196
self.failUnlessRaises(usage.UsageError,
200
# make sure it rejects a missing basedir specification
201
argv = ["create-stats-gatherer"]
202
rc, out, err = self.run_tahoe(argv)
203
self.failIfEqual(rc, 0, str((out, err, rc)))
204
self.failUnlessEqual(out, "")
205
self.failUnless("a basedir was not provided" in err)
207
def test_subcommands(self):
208
# no arguments should trigger a command listing, via UsageError
209
self.failUnlessRaises(usage.UsageError,
215
class RunNode(common_util.SignalMixin, unittest.TestCase, pollmixin.PollMixin,
217
# exercise "tahoe start", for both introducer, client node, and
218
# key-generator, by spawning "tahoe start" as a subprocess. This doesn't
219
# get us figleaf-based line-level coverage, but it does a better job of
220
# confirming that the user can actually run "./bin/tahoe start" and
221
# expect it to work. This verifies that bin/tahoe sets up PYTHONPATH and
222
# the like correctly.
224
# This doesn't work on cygwin (it hangs forever), so we skip this test
225
# when we're on cygwin. It is likely that "tahoe start" itself doesn't
226
# work on cygwin: twisted seems unable to provide a version of
227
# spawnProcess which really works there.
229
def workdir(self, name):
230
basedir = os.path.join("test_runner", "RunNode", name)
231
fileutil.make_dirs(basedir)
234
def test_introducer(self):
235
self.skip_on_cygwin()
236
if not os.path.exists(bintahoe):
237
raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of tahoe.")
238
if runtime.platformType == "win32":
239
# twistd on windows doesn't daemonize. cygwin works normally.
240
raise unittest.SkipTest("twistd does not fork under windows")
241
basedir = self.workdir("test_introducer")
242
c1 = os.path.join(basedir, "c1")
243
HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
244
TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
245
INTRODUCER_FURL_FILE = os.path.join(c1, "introducer.furl")
247
d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-introducer", "--basedir", c1], env=os.environ)
249
out, err, rc_or_sig = res
250
self.failUnlessEqual(rc_or_sig, 0)
251
# by writing this file, we get ten seconds before the node will
252
# exit. This insures that even if the test fails (and the 'stop'
253
# command doesn't work), the client should still terminate.
254
open(HOTLINE_FILE, "w").write("")
255
# now it's safe to start the node
258
def _then_start_the_node(res):
259
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
260
d.addCallback(_then_start_the_node)
263
out, err, rc_or_sig = res
265
open(HOTLINE_FILE, "w").write("")
266
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
267
self.failUnlessEqual(rc_or_sig, 0, errstr)
268
self.failUnlessEqual(out, "", errstr)
269
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
271
# the parent (twistd) has exited. However, twistd writes the pid
272
# from the child, not the parent, so we can't expect twistd.pid
273
# to exist quite yet.
275
# the node is running, but it might not have made it past the
276
# first reactor turn yet, and if we kill it too early, it won't
277
# remove the twistd.pid file. So wait until it does something
278
# that we know it won't do until after the first turn.
281
def _node_has_started():
282
return os.path.exists(INTRODUCER_FURL_FILE)
283
d.addCallback(lambda res: self.poll(_node_has_started))
286
open(HOTLINE_FILE, "w").write("")
287
self.failUnless(os.path.exists(TWISTD_PID_FILE))
288
# rm this so we can detect when the second incarnation is ready
289
os.unlink(INTRODUCER_FURL_FILE)
290
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
291
d.addCallback(_started)
294
out, err, rc_or_sig = res
295
open(HOTLINE_FILE, "w").write("")
296
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
297
self.failUnlessEqual(rc_or_sig, 0, errstr)
298
self.failUnlessEqual(out, "", errstr)
299
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
302
# again, the second incarnation of the node might not be ready yet,
303
# so poll until it is
304
d.addCallback(lambda res: self.poll(_node_has_started))
306
# now we can kill it. TODO: On a slow machine, the node might kill
307
# itself before we get a chance too, especially if spawning the
308
# 'tahoe stop' command takes a while.
310
open(HOTLINE_FILE, "w").write("")
311
self.failUnless(os.path.exists(TWISTD_PID_FILE))
313
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
316
def _after_stopping(res):
317
out, err, rc_or_sig = res
318
open(HOTLINE_FILE, "w").write("")
319
# the parent has exited by now
320
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
321
self.failUnlessEqual(rc_or_sig, 0, errstr)
322
self.failUnlessEqual(out, "", errstr)
323
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
324
# the parent was supposed to poll and wait until it sees
325
# twistd.pid go away before it exits, so twistd.pid should be
327
self.failIf(os.path.exists(TWISTD_PID_FILE))
328
d.addCallback(_after_stopping)
330
def _remove_hotline(res):
331
os.unlink(HOTLINE_FILE)
333
d.addBoth(_remove_hotline)
335
test_introducer.timeout = 240 # This hit the 120-second timeout on "FranƧois Lenny-armv5tel"
337
def test_client(self):
338
self.skip_on_cygwin()
339
if not os.path.exists(bintahoe):
340
raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of tahoe.")
341
if runtime.platformType == "win32":
342
# twistd on windows doesn't daemonize. cygwin works normally.
343
raise unittest.SkipTest("twistd does not fork under windows")
344
basedir = self.workdir("test_client")
345
c1 = os.path.join(basedir, "c1")
346
HOTLINE_FILE = os.path.join(c1, "suicide_prevention_hotline")
347
TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
348
PORTNUMFILE = os.path.join(c1, "client.port")
350
d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-client", "--basedir", c1, "--webport", "0"], env=os.environ)
352
out, err, rc_or_sig = res
353
self.failUnlessEqual(rc_or_sig, 0)
354
# By writing this file, we get sixty seconds before the client will exit. This insures
355
# that even if the 'stop' command doesn't work (and the test fails), the client should
357
open(HOTLINE_FILE, "w").write("")
358
open(os.path.join(c1, "introducer.furl"), "w").write("pb://xrndsskn2zuuian5ltnxrte7lnuqdrkz@127.0.0.1:55617/introducer\n")
359
# now it's safe to start the node
363
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
364
d.addCallback(_start)
367
out, err, rc_or_sig = res
368
open(HOTLINE_FILE, "w").write("")
369
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
370
self.failUnlessEqual(rc_or_sig, 0, errstr)
371
self.failUnlessEqual(out, "", errstr)
372
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
374
# the parent (twistd) has exited. However, twistd writes the pid
375
# from the child, not the parent, so we can't expect twistd.pid
376
# to exist quite yet.
378
# the node is running, but it might not have made it past the
379
# first reactor turn yet, and if we kill it too early, it won't
380
# remove the twistd.pid file. So wait until it does something
381
# that we know it won't do until after the first turn.
384
def _node_has_started():
385
return os.path.exists(PORTNUMFILE)
386
d.addCallback(lambda res: self.poll(_node_has_started))
389
open(HOTLINE_FILE, "w").write("")
390
self.failUnless(os.path.exists(TWISTD_PID_FILE))
391
# rm this so we can detect when the second incarnation is ready
392
os.unlink(PORTNUMFILE)
394
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
395
d.addCallback(_started)
398
out, err, rc_or_sig = res
400
open(HOTLINE_FILE, "w").write("")
401
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
402
self.failUnlessEqual(rc_or_sig, 0, errstr)
403
self.failUnlessEqual(out, "", errstr)
404
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
407
# again, the second incarnation of the node might not be ready yet,
408
# so poll until it is
409
d.addCallback(lambda res: self.poll(_node_has_started))
411
# now we can kill it. TODO: On a slow machine, the node might kill
412
# itself before we get a chance too, especially if spawning the
413
# 'tahoe stop' command takes a while.
415
open(HOTLINE_FILE, "w").write("")
416
self.failUnless(os.path.exists(TWISTD_PID_FILE), (TWISTD_PID_FILE, os.listdir(os.path.dirname(TWISTD_PID_FILE))))
417
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
421
out, err, rc_or_sig = res
423
open(HOTLINE_FILE, "w").write("")
424
# the parent has exited by now
425
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
426
self.failUnlessEqual(rc_or_sig, 0, errstr)
427
self.failUnlessEqual(out, "", errstr)
428
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
429
# the parent was supposed to poll and wait until it sees
430
# twistd.pid go away before it exits, so twistd.pid should be
432
self.failIf(os.path.exists(TWISTD_PID_FILE))
434
def _remove_hotline(res):
435
os.unlink(HOTLINE_FILE)
437
d.addBoth(_remove_hotline)
440
def test_baddir(self):
441
self.skip_on_cygwin()
442
if not os.path.exists(bintahoe):
443
raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of tahoe.")
444
basedir = self.workdir("test_baddir")
445
fileutil.make_dirs(basedir)
447
d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", basedir], env=os.environ)
449
out, err, rc_or_sig = res
450
self.failUnlessEqual(rc_or_sig, 1)
451
self.failUnless("does not look like a node directory" in err)
454
def _then_stop_it(res):
455
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", "--basedir", basedir], env=os.environ)
456
d.addCallback(_then_stop_it)
459
out, err, rc_or_sig = res
460
self.failUnlessEqual(rc_or_sig, 2)
461
self.failUnless("does not look like a running node directory" in err)
464
def _then_start_in_bogus_basedir(res):
465
not_a_dir = os.path.join(basedir, "bogus")
466
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", "--basedir", not_a_dir], env=os.environ)
467
d.addCallback(_then_start_in_bogus_basedir)
470
out, err, rc_or_sig = res
471
self.failUnlessEqual(rc_or_sig, 1)
472
self.failUnless("does not look like a directory at all" in err, err)
476
def test_keygen(self):
477
self.skip_on_cygwin()
478
if not os.path.exists(bintahoe):
479
raise unittest.SkipTest("The bin/tahoe script isn't to be found in the expected location, and I don't want to test a 'tahoe' executable that I find somewhere else, in case it isn't the right executable for this version of tahoe.")
480
if runtime.platformType == "win32":
481
# twistd on windows doesn't daemonize. cygwin works normally.
482
raise unittest.SkipTest("twistd does not fork under windows")
483
basedir = self.workdir("test_keygen")
484
c1 = os.path.join(basedir, "c1")
485
TWISTD_PID_FILE = os.path.join(c1, "twistd.pid")
486
KEYGEN_FURL_FILE = os.path.join(c1, "key_generator.furl")
488
d = utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "create-key-generator", "--basedir", c1], env=os.environ)
490
out, err, rc_or_sig = res
491
self.failUnlessEqual(rc_or_sig, 0)
495
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "start", c1], env=os.environ)
496
d.addCallback(_start)
499
out, err, rc_or_sig = res
500
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
501
self.failUnlessEqual(rc_or_sig, 0, errstr)
502
self.failUnlessEqual(out, "", errstr)
503
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
505
# the parent (twistd) has exited. However, twistd writes the pid
506
# from the child, not the parent, so we can't expect twistd.pid
507
# to exist quite yet.
509
# the node is running, but it might not have made it past the
510
# first reactor turn yet, and if we kill it too early, it won't
511
# remove the twistd.pid file. So wait until it does something
512
# that we know it won't do until after the first turn.
515
def _node_has_started():
516
return os.path.exists(KEYGEN_FURL_FILE)
517
d.addCallback(lambda res: self.poll(_node_has_started))
520
self.failUnless(os.path.exists(TWISTD_PID_FILE))
521
# rm this so we can detect when the second incarnation is ready
522
os.unlink(KEYGEN_FURL_FILE)
523
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "restart", c1], env=os.environ)
524
d.addCallback(_started)
527
out, err, rc_or_sig = res
528
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
529
self.failUnlessEqual(rc_or_sig, 0, errstr)
530
self.failUnlessEqual(out, "", errstr)
531
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
534
# again, the second incarnation of the node might not be ready yet,
535
# so poll until it is
536
d.addCallback(lambda res: self.poll(_node_has_started))
538
# now we can kill it. TODO: On a slow machine, the node might kill
539
# itself before we get a chance too, especially if spawning the
540
# 'tahoe stop' command takes a while.
542
self.failUnless(os.path.exists(TWISTD_PID_FILE))
543
return utils.getProcessOutputAndValue(bintahoe, args=["--quiet", "stop", c1], env=os.environ)
547
out, err, rc_or_sig = res
548
# the parent has exited by now
549
errstr = "rc=%d, OUT: '%s', ERR: '%s'" % (rc_or_sig, out, err)
550
self.failUnlessEqual(rc_or_sig, 0, errstr)
551
self.failUnlessEqual(out, "", errstr)
552
# self.failUnlessEqual(err, "", errstr) # See test_client_no_noise -- for now we ignore noise.
553
# the parent was supposed to poll and wait until it sees
554
# twistd.pid go away before it exits, so twistd.pid should be
556
self.failIf(os.path.exists(TWISTD_PID_FILE))