2007-05-23 22:08:55 +00:00
|
|
|
|
2007-11-02 00:29:15 +00:00
|
|
|
import os, time
|
|
|
|
from zope.interface import implements
|
2007-05-23 22:08:55 +00:00
|
|
|
from twisted.trial import unittest
|
2007-05-24 00:55:04 +00:00
|
|
|
from twisted.internet import defer
|
2007-05-23 22:08:55 +00:00
|
|
|
from twisted.python import log
|
|
|
|
|
2007-11-02 00:29:15 +00:00
|
|
|
from foolscap import Tub, Referenceable
|
2007-05-23 22:08:55 +00:00
|
|
|
from foolscap.eventual import flushEventualQueue
|
|
|
|
from twisted.application import service
|
2007-11-02 00:29:15 +00:00
|
|
|
import allmydata
|
2007-10-31 07:54:42 +00:00
|
|
|
from allmydata.node import Node, formatTimeTahoeStyle
|
2007-11-02 00:29:15 +00:00
|
|
|
from allmydata.util import testutil, fileutil
|
|
|
|
from allmydata import logpublisher
|
2007-05-23 22:08:55 +00:00
|
|
|
|
|
|
|
class LoggingMultiService(service.MultiService):
|
|
|
|
def log(self, msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TestNode(Node):
|
|
|
|
CERTFILE='DEFAULT_CERTFILE_BLANK'
|
|
|
|
PORTNUMFILE='DEFAULT_PORTNUMFILE_BLANK'
|
|
|
|
|
|
|
|
class TestCase(unittest.TestCase, testutil.SignalMixin):
|
|
|
|
def setUp(self):
|
|
|
|
self.parent = LoggingMultiService()
|
|
|
|
self.parent.startService()
|
|
|
|
def tearDown(self):
|
|
|
|
log.msg("%s.tearDown" % self.__class__.__name__)
|
|
|
|
d = defer.succeed(None)
|
|
|
|
d.addCallback(lambda res: self.parent.stopService())
|
|
|
|
d.addCallback(flushEventualQueue)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_advertised_ip_addresses(self):
|
2007-11-02 00:29:15 +00:00
|
|
|
basedir = "test_node/test_advertised_ip_addresses"
|
|
|
|
fileutil.make_dirs(basedir)
|
|
|
|
f = open(os.path.join(basedir, 'advertised_ip_addresses'),'w')
|
|
|
|
f.write('1.2.3.4:5')
|
|
|
|
f.close()
|
2007-05-23 22:08:55 +00:00
|
|
|
|
2007-11-02 00:29:15 +00:00
|
|
|
n = TestNode(basedir)
|
2007-05-23 22:08:55 +00:00
|
|
|
n.setServiceParent(self.parent)
|
|
|
|
d = n.when_tub_ready()
|
|
|
|
|
|
|
|
def _check_addresses(ignored_result):
|
2007-11-02 00:29:15 +00:00
|
|
|
furl = n.tub.registerReference(n)
|
|
|
|
self.failUnless("1.2.3.4:5" in furl, furl)
|
2007-05-23 22:08:55 +00:00
|
|
|
|
|
|
|
d.addCallback(_check_addresses)
|
|
|
|
return d
|
|
|
|
|
2007-10-31 07:54:42 +00:00
|
|
|
def test_log(self):
|
2007-11-02 00:29:15 +00:00
|
|
|
basedir = "test_node/test_log"
|
|
|
|
fileutil.make_dirs(basedir)
|
|
|
|
n = TestNode(basedir)
|
2007-10-31 07:54:42 +00:00
|
|
|
n.log("this is a message")
|
|
|
|
n.log("with %d %s %s", args=(2, "interpolated", "parameters"))
|
2007-10-31 07:56:59 +00:00
|
|
|
n.log("with bogus %d expansion", args=("not an integer",))
|
2007-10-31 07:54:42 +00:00
|
|
|
|
2007-11-02 00:29:15 +00:00
|
|
|
def test_logpublisher(self):
|
|
|
|
basedir = "test_node/test_logpublisher"
|
|
|
|
fileutil.make_dirs(basedir)
|
|
|
|
n = TestNode(basedir)
|
|
|
|
n.setServiceParent(self.parent)
|
|
|
|
d = n.when_tub_ready()
|
|
|
|
def _ready(res):
|
|
|
|
n.log("starting up")
|
|
|
|
flogport = open(os.path.join(n.basedir,"logport.furl"), "r").read()
|
|
|
|
return n.tub.getReference(flogport.strip())
|
|
|
|
d.addCallback(_ready)
|
|
|
|
def _got_logport(logport):
|
|
|
|
d = logport.callRemote("get_versions")
|
|
|
|
def _check(versions):
|
|
|
|
self.failUnlessEqual(versions["allmydata"],
|
|
|
|
allmydata.__version__)
|
|
|
|
d.addCallback(_check)
|
|
|
|
return d
|
|
|
|
d.addCallback(_got_logport)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def test_log_gatherer(self):
|
|
|
|
t = Tub()
|
|
|
|
t.setServiceParent(self.parent)
|
|
|
|
t.listenOn("tcp:0:interface=127.0.0.1")
|
|
|
|
l = t.getListeners()[0]
|
|
|
|
portnum = l.getPortnum()
|
|
|
|
t.setLocation("127.0.0.1:%d" % portnum)
|
|
|
|
gatherer = Gatherer()
|
|
|
|
gatherer.d = defer.Deferred()
|
|
|
|
gatherer_furl = t.registerReference(gatherer)
|
|
|
|
|
|
|
|
basedir = "test_node/test_log_gatherer"
|
|
|
|
fileutil.make_dirs(basedir)
|
|
|
|
f = open(os.path.join(basedir, "log_gatherer.furl"), "w")
|
|
|
|
f.write(gatherer_furl + "\n")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
n = TestNode(basedir)
|
|
|
|
n.setServiceParent(self.parent)
|
|
|
|
d = n.when_tub_ready()
|
|
|
|
def _ready(res):
|
|
|
|
n.log("starting up")
|
|
|
|
# about now, the node will be contacting the Gatherer and
|
|
|
|
# offering its logport.
|
|
|
|
return gatherer.d
|
|
|
|
d.addCallback(_ready)
|
|
|
|
return d
|
|
|
|
|
2007-10-31 07:54:42 +00:00
|
|
|
def test_timestamp(self):
|
|
|
|
# this modified logger doesn't seem to get used during the tests,
|
|
|
|
# probably because we don't modify the LogObserver that trial
|
|
|
|
# installs (only the one that twistd installs). So manually exercise
|
|
|
|
# it a little bit.
|
|
|
|
t = formatTimeTahoeStyle("ignored", time.time())
|
|
|
|
self.failUnless("Z" in t)
|
|
|
|
t2 = formatTimeTahoeStyle("ignored", int(time.time()))
|
|
|
|
self.failUnless("Z" in t2)
|
|
|
|
|
2007-11-02 00:29:15 +00:00
|
|
|
class Gatherer(Referenceable):
|
|
|
|
implements(logpublisher.RILogGatherer)
|
|
|
|
def remote_logport(self, nodeid, logport):
|
|
|
|
d = logport.callRemote("get_versions")
|
|
|
|
d.addCallback(self.d.callback)
|