tahoe-lafs/src/allmydata/test/test_multi_introducers.py

174 lines
6.3 KiB
Python
Raw Normal View History

#!/usr/bin/python
import os
from twisted.python.filepath import FilePath
from twisted.trial import unittest
2018-01-28 08:27:25 +00:00
from twisted.internet import defer
from allmydata.util import yamlutil
from allmydata.client import create_client
from allmydata.scripts.create_node import write_node_config
INTRODUCERS_CFG_FURLS=['furl1', 'furl2']
INTRODUCERS_CFG_FURLS_COMMENTED="""introducers:
'intro1': {furl: furl1}
# 'intro2': {furl: furl4}
"""
class MultiIntroTests(unittest.TestCase):
def setUp(self):
# setup tahoe.cfg and basedir/private/introducers
# create a custom tahoe.cfg
self.basedir = os.path.dirname(self.mktemp())
c = open(os.path.join(self.basedir, "tahoe.cfg"), "w")
config = {'hide-ip':False, 'listen': 'tcp',
'port': None, 'location': None, 'hostname': 'example.net'}
write_node_config(c, config)
fake_furl = "furl1"
c.write("[client]\n")
c.write("introducer.furl = %s\n" % fake_furl)
c.write("[storage]\n")
c.write("enabled = false\n")
c.close()
os.mkdir(os.path.join(self.basedir,"private"))
2016-09-13 00:30:06 +00:00
self.yaml_path = FilePath(os.path.join(self.basedir, "private",
"introducers.yaml"))
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
def test_introducer_count(self):
""" Ensure that the Client creates same number of introducer clients
as found in "basedir/private/introducers" config file. """
2018-01-28 08:27:25 +00:00
connections = {
'introducers': {
u'intro1':{ 'furl': 'furl1' },
u'intro2':{ 'furl': 'furl4' },
},
}
2016-09-13 00:30:06 +00:00
self.yaml_path.setContent(yamlutil.safe_dump(connections))
# get a client and count of introducer_clients
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
ic_count = len(myclient.introducer_clients)
# assertions
self.failUnlessEqual(ic_count, 3)
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
def test_introducer_count_commented(self):
""" Ensure that the Client creates same number of introducer clients
as found in "basedir/private/introducers" config file when there is one
commented."""
2016-09-13 00:30:06 +00:00
self.yaml_path.setContent(INTRODUCERS_CFG_FURLS_COMMENTED)
# get a client and count of introducer_clients
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
ic_count = len(myclient.introducer_clients)
# assertions
self.failUnlessEqual(ic_count, 2)
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
def test_read_introducer_furl_from_tahoecfg(self):
""" Ensure that the Client reads the introducer.furl config item from
the tahoe.cfg file. """
# create a custom tahoe.cfg
c = open(os.path.join(self.basedir, "tahoe.cfg"), "w")
config = {'hide-ip':False, 'listen': 'tcp',
'port': None, 'location': None, 'hostname': 'example.net'}
write_node_config(c, config)
fake_furl = "furl1"
c.write("[client]\n")
c.write("introducer.furl = %s\n" % fake_furl)
c.write("[storage]\n")
c.write("enabled = false\n")
c.close()
# get a client and first introducer_furl
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
# assertions
self.failUnlessEqual(fake_furl, tahoe_cfg_furl)
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
2016-09-13 00:30:06 +00:00
def test_reject_default_in_yaml(self):
connections = {'introducers': {
u'default': { 'furl': 'furl1' },
}}
self.yaml_path.setContent(yamlutil.safe_dump(connections))
2018-01-28 08:27:25 +00:00
with self.assertRaises(ValueError) as ctx:
yield create_client(self.basedir)
self.assertEquals(
str(ctx.exception),
"'default' introducer furl cannot be specified in introducers.yaml; please "
"fix impossible configuration.",
)
2016-09-13 00:30:06 +00:00
SIMPLE_YAML = """
introducers:
one:
furl: furl1
"""
# this format was recommended in docs/configuration.rst in 1.12.0, but it
# isn't correct (the "furl = furl1" line is recorded as the string value of
# the ["one"] key, instead of being parsed as a single-key dictionary).
EQUALS_YAML = """
introducers:
one: furl = furl1
"""
2016-09-13 00:30:06 +00:00
class NoDefault(unittest.TestCase):
def setUp(self):
# setup tahoe.cfg and basedir/private/introducers
# create a custom tahoe.cfg
self.basedir = os.path.dirname(self.mktemp())
c = open(os.path.join(self.basedir, "tahoe.cfg"), "w")
config = {'hide-ip':False, 'listen': 'tcp',
'port': None, 'location': None, 'hostname': 'example.net'}
2016-09-13 00:30:06 +00:00
write_node_config(c, config)
c.write("[client]\n")
c.write("# introducer.furl =\n") # omit default
c.write("[storage]\n")
c.write("enabled = false\n")
c.close()
os.mkdir(os.path.join(self.basedir,"private"))
self.yaml_path = FilePath(os.path.join(self.basedir, "private",
"introducers.yaml"))
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
2016-09-13 00:30:06 +00:00
def test_ok(self):
connections = {'introducers': {
u'one': { 'furl': 'furl1' },
}}
self.yaml_path.setContent(yamlutil.safe_dump(connections))
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
2016-09-13 00:30:06 +00:00
self.assertEquals(tahoe_cfg_furl, 'furl1')
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
def test_real_yaml(self):
self.yaml_path.setContent(SIMPLE_YAML)
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
self.assertEquals(tahoe_cfg_furl, 'furl1')
2018-01-28 08:27:25 +00:00
@defer.inlineCallbacks
def test_invalid_equals_yaml(self):
self.yaml_path.setContent(EQUALS_YAML)
2018-01-28 08:27:25 +00:00
with self.assertRaises(TypeError) as ctx:
yield create_client(self.basedir)
self.assertEquals(
str(ctx.exception),
"string indices must be integers",
)
@defer.inlineCallbacks
def test_introducerless(self):
connections = {'introducers': {} }
self.yaml_path.setContent(yamlutil.safe_dump(connections))
2018-01-28 08:27:25 +00:00
myclient = yield create_client(self.basedir)
self.assertEquals(len(myclient.introducer_clients), 0)
if __name__ == "__main__":
unittest.main()