2016-09-12 23:01:23 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
import os
|
|
|
|
|
|
|
|
from twisted.python.filepath import FilePath
|
|
|
|
from twisted.trial import unittest
|
2018-01-28 01:27:25 -07:00
|
|
|
from twisted.internet import defer
|
2016-09-12 23:01:23 +00:00
|
|
|
from allmydata.util import yamlutil
|
2017-09-05 19:08:35 -06:00
|
|
|
from allmydata.client import create_client
|
2016-09-12 23:01:23 +00:00
|
|
|
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")
|
2016-09-14 15:47:08 -07:00
|
|
|
config = {'hide-ip':False, 'listen': 'tcp',
|
|
|
|
'port': None, 'location': None, 'hostname': 'example.net'}
|
2016-09-12 23:01:23 +00:00
|
|
|
write_node_config(c, config)
|
|
|
|
c.write("[storage]\n")
|
|
|
|
c.write("enabled = false\n")
|
|
|
|
c.close()
|
|
|
|
os.mkdir(os.path.join(self.basedir,"private"))
|
2016-09-12 17:30:06 -07:00
|
|
|
self.yaml_path = FilePath(os.path.join(self.basedir, "private",
|
|
|
|
"introducers.yaml"))
|
2016-09-12 23:01:23 +00:00
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-09-12 23:01:23 +00:00
|
|
|
def test_introducer_count(self):
|
2020-11-14 11:09:40 -05:00
|
|
|
"""
|
|
|
|
If there are two introducers configured in ``introducers.yaml`` then
|
|
|
|
``Client`` creates two introducer clients.
|
|
|
|
"""
|
2018-01-28 01:27:25 -07:00
|
|
|
connections = {
|
|
|
|
'introducers': {
|
|
|
|
u'intro1':{ 'furl': 'furl1' },
|
|
|
|
u'intro2':{ 'furl': 'furl4' },
|
|
|
|
},
|
2016-09-12 23:01:23 +00:00
|
|
|
}
|
2016-09-12 17:30:06 -07:00
|
|
|
self.yaml_path.setContent(yamlutil.safe_dump(connections))
|
2016-09-12 23:01:23 +00:00
|
|
|
# get a client and count of introducer_clients
|
2018-01-28 01:27:25 -07:00
|
|
|
myclient = yield create_client(self.basedir)
|
2016-09-12 23:01:23 +00:00
|
|
|
ic_count = len(myclient.introducer_clients)
|
|
|
|
|
|
|
|
# assertions
|
2020-11-14 11:09:40 -05:00
|
|
|
self.failUnlessEqual(ic_count, len(connections["introducers"]))
|
2016-09-12 23:01:23 +00:00
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-09-12 23:01:23 +00:00
|
|
|
def test_read_introducer_furl_from_tahoecfg(self):
|
2020-11-14 11:09:40 -05:00
|
|
|
"""
|
|
|
|
The deprecated [client]introducer.furl item is still read and respected.
|
|
|
|
"""
|
2016-09-12 23:01:23 +00:00
|
|
|
# create a custom tahoe.cfg
|
|
|
|
c = open(os.path.join(self.basedir, "tahoe.cfg"), "w")
|
2016-09-14 15:47:08 -07:00
|
|
|
config = {'hide-ip':False, 'listen': 'tcp',
|
|
|
|
'port': None, 'location': None, 'hostname': 'example.net'}
|
2016-09-12 23:01:23 +00:00
|
|
|
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 01:27:25 -07:00
|
|
|
myclient = yield create_client(self.basedir)
|
2018-03-04 20:08:11 -07:00
|
|
|
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
|
2016-09-12 23:01:23 +00:00
|
|
|
|
|
|
|
# assertions
|
|
|
|
self.failUnlessEqual(fake_furl, tahoe_cfg_furl)
|
2020-11-14 11:09:40 -05:00
|
|
|
self.assertEqual(
|
|
|
|
list(
|
|
|
|
warning["message"]
|
|
|
|
for warning
|
|
|
|
in self.flushWarnings()
|
|
|
|
if warning["category"] is DeprecationWarning
|
|
|
|
),
|
|
|
|
["tahoe.cfg [client]introducer.furl is deprecated; "
|
|
|
|
"use private/introducers.yaml instead."],
|
|
|
|
)
|
2016-09-12 23:01:23 +00:00
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-09-12 17:30:06 -07:00
|
|
|
def test_reject_default_in_yaml(self):
|
2020-11-14 11:09:40 -05:00
|
|
|
"""
|
2020-11-16 15:01:21 -05:00
|
|
|
If an introducer is configured in tahoe.cfg with the deprecated
|
|
|
|
[client]introducer.furl then a "default" introducer in
|
2020-11-14 11:09:40 -05:00
|
|
|
introducers.yaml is rejected.
|
|
|
|
"""
|
|
|
|
connections = {
|
|
|
|
'introducers': {
|
|
|
|
u'default': { 'furl': 'furl1' },
|
|
|
|
},
|
|
|
|
}
|
2016-09-12 17:30:06 -07:00
|
|
|
self.yaml_path.setContent(yamlutil.safe_dump(connections))
|
2020-11-14 11:09:40 -05:00
|
|
|
FilePath(self.basedir).child("tahoe.cfg").setContent(
|
|
|
|
"[client]\n"
|
|
|
|
"introducer.furl = furl1\n"
|
|
|
|
)
|
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
with self.assertRaises(ValueError) as ctx:
|
|
|
|
yield create_client(self.basedir)
|
|
|
|
|
|
|
|
self.assertEquals(
|
|
|
|
str(ctx.exception),
|
2020-11-14 11:09:40 -05:00
|
|
|
"'default' introducer furl cannot be specified in tahoe.cfg and introducers.yaml; "
|
|
|
|
"please fix impossible configuration.",
|
2018-01-28 01:27:25 -07:00
|
|
|
)
|
2016-09-12 17:30:06 -07:00
|
|
|
|
2017-01-08 15:17:18 -08: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-12 17:30:06 -07: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")
|
2016-09-14 15:47:08 -07:00
|
|
|
config = {'hide-ip':False, 'listen': 'tcp',
|
|
|
|
'port': None, 'location': None, 'hostname': 'example.net'}
|
2016-09-12 17:30:06 -07:00
|
|
|
write_node_config(c, config)
|
|
|
|
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 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2016-09-12 17:30:06 -07:00
|
|
|
def test_ok(self):
|
|
|
|
connections = {'introducers': {
|
|
|
|
u'one': { 'furl': 'furl1' },
|
|
|
|
}}
|
|
|
|
self.yaml_path.setContent(yamlutil.safe_dump(connections))
|
2018-01-28 01:27:25 -07:00
|
|
|
myclient = yield create_client(self.basedir)
|
2018-03-04 20:08:11 -07:00
|
|
|
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
|
2016-09-12 17:30:06 -07:00
|
|
|
self.assertEquals(tahoe_cfg_furl, 'furl1')
|
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2017-01-08 15:17:18 -08:00
|
|
|
def test_real_yaml(self):
|
|
|
|
self.yaml_path.setContent(SIMPLE_YAML)
|
2018-01-28 01:27:25 -07:00
|
|
|
myclient = yield create_client(self.basedir)
|
2018-03-04 20:08:11 -07:00
|
|
|
tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl
|
2017-01-08 15:17:18 -08:00
|
|
|
self.assertEquals(tahoe_cfg_furl, 'furl1')
|
|
|
|
|
2018-01-28 01:27:25 -07:00
|
|
|
@defer.inlineCallbacks
|
2017-01-08 15:17:18 -08:00
|
|
|
def test_invalid_equals_yaml(self):
|
|
|
|
self.yaml_path.setContent(EQUALS_YAML)
|
2018-01-28 01:27:25 -07:00
|
|
|
with self.assertRaises(TypeError) as ctx:
|
|
|
|
yield create_client(self.basedir)
|
2019-12-21 18:01:42 -07:00
|
|
|
self.assertIsInstance(
|
|
|
|
ctx.exception,
|
|
|
|
TypeError,
|
2018-01-28 01:27:25 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-09-12 17:45:17 -07:00
|
|
|
def test_introducerless(self):
|
|
|
|
connections = {'introducers': {} }
|
|
|
|
self.yaml_path.setContent(yamlutil.safe_dump(connections))
|
2018-01-28 01:27:25 -07:00
|
|
|
myclient = yield create_client(self.basedir)
|
2018-03-04 20:08:11 -07:00
|
|
|
self.assertEquals(len(myclient.introducer_clients), 0)
|
2016-09-12 17:45:17 -07:00
|
|
|
|
2016-09-12 23:01:23 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|