1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | from six import ensure_binary |
---|
6 | |
---|
7 | import os |
---|
8 | |
---|
9 | from twisted.python.filepath import FilePath |
---|
10 | from twisted.trial import unittest |
---|
11 | from twisted.internet import defer |
---|
12 | from allmydata.util import yamlutil |
---|
13 | from allmydata.client import create_client |
---|
14 | from allmydata.scripts.create_node import write_node_config |
---|
15 | |
---|
16 | INTRODUCERS_CFG_FURLS=['furl1', 'furl2'] |
---|
17 | INTRODUCERS_CFG_FURLS_COMMENTED="""introducers: |
---|
18 | 'intro1': {furl: furl1} |
---|
19 | # 'intro2': {furl: furl4} |
---|
20 | """ |
---|
21 | |
---|
22 | class MultiIntroTests(unittest.TestCase): |
---|
23 | |
---|
24 | async def setUp(self): |
---|
25 | # setup tahoe.cfg and basedir/private/introducers |
---|
26 | # create a custom tahoe.cfg |
---|
27 | self.basedir = os.path.dirname(self.mktemp()) |
---|
28 | c = open(os.path.join(self.basedir, "tahoe.cfg"), "w") |
---|
29 | config = {'hide-ip':False, 'listen': 'tcp', |
---|
30 | 'port': None, 'location': None, 'hostname': 'example.net'} |
---|
31 | await write_node_config(c, config) |
---|
32 | c.write("[storage]\n") |
---|
33 | c.write("enabled = false\n") |
---|
34 | c.close() |
---|
35 | os.mkdir(os.path.join(self.basedir,"private")) |
---|
36 | self.yaml_path = FilePath(os.path.join(self.basedir, "private", |
---|
37 | "introducers.yaml")) |
---|
38 | |
---|
39 | @defer.inlineCallbacks |
---|
40 | def test_introducer_count(self): |
---|
41 | """ |
---|
42 | If there are two introducers configured in ``introducers.yaml`` then |
---|
43 | ``Client`` creates two introducer clients. |
---|
44 | """ |
---|
45 | connections = { |
---|
46 | 'introducers': { |
---|
47 | u'intro1':{ 'furl': 'furl1' }, |
---|
48 | u'intro2':{ 'furl': 'furl4' }, |
---|
49 | }, |
---|
50 | } |
---|
51 | self.yaml_path.setContent(ensure_binary(yamlutil.safe_dump(connections))) |
---|
52 | # get a client and count of introducer_clients |
---|
53 | myclient = yield create_client(self.basedir) |
---|
54 | ic_count = len(myclient.introducer_clients) |
---|
55 | |
---|
56 | # assertions |
---|
57 | self.failUnlessEqual(ic_count, len(connections["introducers"])) |
---|
58 | |
---|
59 | async def test_read_introducer_furl_from_tahoecfg(self): |
---|
60 | """ |
---|
61 | The deprecated [client]introducer.furl item is still read and respected. |
---|
62 | """ |
---|
63 | # create a custom tahoe.cfg |
---|
64 | c = open(os.path.join(self.basedir, "tahoe.cfg"), "w") |
---|
65 | config = {'hide-ip':False, 'listen': 'tcp', |
---|
66 | 'port': None, 'location': None, 'hostname': 'example.net'} |
---|
67 | await write_node_config(c, config) |
---|
68 | fake_furl = "furl1" |
---|
69 | c.write("[client]\n") |
---|
70 | c.write("introducer.furl = %s\n" % fake_furl) |
---|
71 | c.write("[storage]\n") |
---|
72 | c.write("enabled = false\n") |
---|
73 | c.close() |
---|
74 | |
---|
75 | # get a client and first introducer_furl |
---|
76 | myclient = yield create_client(self.basedir) |
---|
77 | tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl |
---|
78 | |
---|
79 | # assertions |
---|
80 | self.failUnlessEqual(fake_furl, str(tahoe_cfg_furl, "utf-8")) |
---|
81 | self.assertEqual( |
---|
82 | list( |
---|
83 | warning["message"] |
---|
84 | for warning |
---|
85 | in self.flushWarnings() |
---|
86 | if warning["category"] is DeprecationWarning |
---|
87 | ), |
---|
88 | ["tahoe.cfg [client]introducer.furl is deprecated; " |
---|
89 | "use private/introducers.yaml instead."], |
---|
90 | ) |
---|
91 | |
---|
92 | @defer.inlineCallbacks |
---|
93 | def test_reject_default_in_yaml(self): |
---|
94 | """ |
---|
95 | If an introducer is configured in tahoe.cfg with the deprecated |
---|
96 | [client]introducer.furl then a "default" introducer in |
---|
97 | introducers.yaml is rejected. |
---|
98 | """ |
---|
99 | connections = { |
---|
100 | 'introducers': { |
---|
101 | u'default': { 'furl': 'furl1' }, |
---|
102 | }, |
---|
103 | } |
---|
104 | self.yaml_path.setContent(ensure_binary(yamlutil.safe_dump(connections))) |
---|
105 | FilePath(self.basedir).child("tahoe.cfg").setContent( |
---|
106 | b"[client]\n" |
---|
107 | b"introducer.furl = furl1\n" |
---|
108 | ) |
---|
109 | |
---|
110 | with self.assertRaises(ValueError) as ctx: |
---|
111 | yield create_client(self.basedir) |
---|
112 | |
---|
113 | self.assertEquals( |
---|
114 | str(ctx.exception), |
---|
115 | "'default' introducer furl cannot be specified in tahoe.cfg and introducers.yaml; " |
---|
116 | "please fix impossible configuration.", |
---|
117 | ) |
---|
118 | |
---|
119 | SIMPLE_YAML = b""" |
---|
120 | introducers: |
---|
121 | one: |
---|
122 | furl: furl1 |
---|
123 | """ |
---|
124 | |
---|
125 | # this format was recommended in docs/configuration.rst in 1.12.0, but it |
---|
126 | # isn't correct (the "furl = furl1" line is recorded as the string value of |
---|
127 | # the ["one"] key, instead of being parsed as a single-key dictionary). |
---|
128 | EQUALS_YAML = b""" |
---|
129 | introducers: |
---|
130 | one: furl = furl1 |
---|
131 | """ |
---|
132 | |
---|
133 | class NoDefault(unittest.TestCase): |
---|
134 | async def setUp(self): |
---|
135 | # setup tahoe.cfg and basedir/private/introducers |
---|
136 | # create a custom tahoe.cfg |
---|
137 | self.basedir = os.path.dirname(self.mktemp()) |
---|
138 | c = open(os.path.join(self.basedir, "tahoe.cfg"), "w") |
---|
139 | config = {'hide-ip':False, 'listen': 'tcp', |
---|
140 | 'port': None, 'location': None, 'hostname': 'example.net'} |
---|
141 | await write_node_config(c, config) |
---|
142 | c.write("[storage]\n") |
---|
143 | c.write("enabled = false\n") |
---|
144 | c.close() |
---|
145 | os.mkdir(os.path.join(self.basedir,"private")) |
---|
146 | self.yaml_path = FilePath(os.path.join(self.basedir, "private", |
---|
147 | "introducers.yaml")) |
---|
148 | |
---|
149 | @defer.inlineCallbacks |
---|
150 | def test_ok(self): |
---|
151 | connections = {'introducers': { |
---|
152 | u'one': { 'furl': 'furl1' }, |
---|
153 | }} |
---|
154 | self.yaml_path.setContent(ensure_binary(yamlutil.safe_dump(connections))) |
---|
155 | myclient = yield create_client(self.basedir) |
---|
156 | tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl |
---|
157 | self.assertEquals(tahoe_cfg_furl, b'furl1') |
---|
158 | |
---|
159 | @defer.inlineCallbacks |
---|
160 | def test_real_yaml(self): |
---|
161 | self.yaml_path.setContent(SIMPLE_YAML) |
---|
162 | myclient = yield create_client(self.basedir) |
---|
163 | tahoe_cfg_furl = myclient.introducer_clients[0].introducer_furl |
---|
164 | self.assertEquals(tahoe_cfg_furl, b'furl1') |
---|
165 | |
---|
166 | @defer.inlineCallbacks |
---|
167 | def test_invalid_equals_yaml(self): |
---|
168 | self.yaml_path.setContent(EQUALS_YAML) |
---|
169 | with self.assertRaises(TypeError) as ctx: |
---|
170 | yield create_client(self.basedir) |
---|
171 | self.assertIsInstance( |
---|
172 | ctx.exception, |
---|
173 | TypeError, |
---|
174 | ) |
---|
175 | |
---|
176 | @defer.inlineCallbacks |
---|
177 | def test_introducerless(self): |
---|
178 | connections = {'introducers': {} } |
---|
179 | self.yaml_path.setContent(ensure_binary(yamlutil.safe_dump(connections))) |
---|
180 | myclient = yield create_client(self.basedir) |
---|
181 | self.assertEquals(len(myclient.introducer_clients), 0) |
---|