1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | from six import ensure_text |
---|
6 | |
---|
7 | import os |
---|
8 | import tempfile |
---|
9 | from io import BytesIO, StringIO |
---|
10 | from os.path import join |
---|
11 | |
---|
12 | from twisted.trial import unittest |
---|
13 | from twisted.internet import defer |
---|
14 | |
---|
15 | from allmydata.mutable.publish import MutableData |
---|
16 | from allmydata.scripts.common_http import BadResponse |
---|
17 | from allmydata.scripts.tahoe_status import _handle_response_for_fragment |
---|
18 | from allmydata.scripts.tahoe_status import _get_request_parameters_for_fragment |
---|
19 | from allmydata.scripts.tahoe_status import pretty_progress |
---|
20 | from allmydata.scripts.tahoe_status import do_status |
---|
21 | from allmydata.web.status import marshal_json |
---|
22 | |
---|
23 | from allmydata.immutable.upload import UploadStatus |
---|
24 | from allmydata.immutable.downloader.status import DownloadStatus |
---|
25 | from allmydata.mutable.publish import PublishStatus |
---|
26 | from allmydata.mutable.retrieve import RetrieveStatus |
---|
27 | from allmydata.mutable.servermap import UpdateStatus |
---|
28 | from allmydata.util import jsonbytes as json |
---|
29 | |
---|
30 | from ..no_network import GridTestMixin |
---|
31 | from ..common_web import do_http |
---|
32 | from .common import CLITestMixin |
---|
33 | |
---|
34 | |
---|
35 | class FakeStatus(object): |
---|
36 | def __init__(self): |
---|
37 | self.status = [] |
---|
38 | |
---|
39 | def setServiceParent(self, p): |
---|
40 | pass |
---|
41 | |
---|
42 | def get_status(self): |
---|
43 | return self.status |
---|
44 | |
---|
45 | def get_storage_index(self): |
---|
46 | return None |
---|
47 | |
---|
48 | def get_size(self): |
---|
49 | return None |
---|
50 | |
---|
51 | |
---|
52 | class ProgressBar(unittest.TestCase): |
---|
53 | |
---|
54 | def test_ascii0(self): |
---|
55 | prog = pretty_progress(80.0, size=10, output_ascii=True) |
---|
56 | self.assertEqual('########. ', prog) |
---|
57 | |
---|
58 | def test_ascii1(self): |
---|
59 | prog = pretty_progress(10.0, size=10, output_ascii=True) |
---|
60 | self.assertEqual('#. ', prog) |
---|
61 | |
---|
62 | def test_ascii2(self): |
---|
63 | prog = pretty_progress(13.0, size=10, output_ascii=True) |
---|
64 | self.assertEqual('#o ', prog) |
---|
65 | |
---|
66 | def test_ascii3(self): |
---|
67 | prog = pretty_progress(90.0, size=10, output_ascii=True) |
---|
68 | self.assertEqual('#########.', prog) |
---|
69 | |
---|
70 | def test_unicode0(self): |
---|
71 | self.assertEqual( |
---|
72 | pretty_progress(82.0, size=10, output_ascii=False), |
---|
73 | u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258e ', |
---|
74 | ) |
---|
75 | |
---|
76 | def test_unicode1(self): |
---|
77 | self.assertEqual( |
---|
78 | pretty_progress(100.0, size=10, output_ascii=False), |
---|
79 | u'\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588', |
---|
80 | ) |
---|
81 | |
---|
82 | |
---|
83 | class _FakeOptions(dict): |
---|
84 | def __init__(self): |
---|
85 | self._tmp = tempfile.mkdtemp() |
---|
86 | os.mkdir(join(self._tmp, 'private'), 0o777) |
---|
87 | with open(join(self._tmp, 'private', 'api_auth_token'), 'w') as f: |
---|
88 | f.write('a' * 32) |
---|
89 | with open(join(self._tmp, 'node.url'), 'w') as f: |
---|
90 | f.write('localhost:9000') |
---|
91 | |
---|
92 | self['node-directory'] = self._tmp |
---|
93 | self['verbose'] = True |
---|
94 | self.stdout = StringIO() |
---|
95 | self.stderr = StringIO() |
---|
96 | |
---|
97 | |
---|
98 | class Integration(GridTestMixin, CLITestMixin, unittest.TestCase): |
---|
99 | |
---|
100 | @defer.inlineCallbacks |
---|
101 | def setUp(self): |
---|
102 | yield super(Integration, self).setUp() |
---|
103 | self.basedir = "cli/status" |
---|
104 | self.set_up_grid() |
---|
105 | |
---|
106 | # upload something |
---|
107 | c0 = self.g.clients[0] |
---|
108 | data = MutableData(b"data" * 100) |
---|
109 | filenode = yield c0.create_mutable_file(data) |
---|
110 | self.uri = filenode.get_uri() |
---|
111 | |
---|
112 | # make sure our web-port is actually answering |
---|
113 | yield do_http("get", 'http://127.0.0.1:{}/status?t=json'.format(self.client_webports[0])) |
---|
114 | |
---|
115 | def test_simple(self): |
---|
116 | d = self.do_cli('status')# '--verbose') |
---|
117 | |
---|
118 | def _check(ign): |
---|
119 | code, stdout, stderr = ign |
---|
120 | self.assertEqual(code, 0, stderr) |
---|
121 | self.assertTrue('Skipped 1' in stdout) |
---|
122 | d.addCallback(_check) |
---|
123 | return d |
---|
124 | |
---|
125 | @defer.inlineCallbacks |
---|
126 | def test_help(self): |
---|
127 | rc, _, _ = yield self.do_cli('status', '--help') |
---|
128 | self.assertEqual(rc, 0) |
---|
129 | |
---|
130 | |
---|
131 | class CommandStatus(unittest.TestCase): |
---|
132 | """ |
---|
133 | These tests just exercise the renderers and ensure they don't |
---|
134 | catastrophically fail. |
---|
135 | """ |
---|
136 | |
---|
137 | def setUp(self): |
---|
138 | self.options = _FakeOptions() |
---|
139 | |
---|
140 | def test_no_operations(self): |
---|
141 | values = [ |
---|
142 | StringIO(ensure_text(json.dumps({ |
---|
143 | "active": [], |
---|
144 | "recent": [], |
---|
145 | }))), |
---|
146 | StringIO(ensure_text(json.dumps({ |
---|
147 | "counters": { |
---|
148 | "bytes_downloaded": 0, |
---|
149 | }, |
---|
150 | "stats": { |
---|
151 | "node.uptime": 0, |
---|
152 | } |
---|
153 | }))), |
---|
154 | ] |
---|
155 | def do_http(*args, **kw): |
---|
156 | return values.pop(0) |
---|
157 | do_status(self.options, do_http) |
---|
158 | |
---|
159 | def test_simple(self): |
---|
160 | recent_items = active_items = [ |
---|
161 | UploadStatus(), |
---|
162 | DownloadStatus(b"abcd", 12345), |
---|
163 | PublishStatus(), |
---|
164 | RetrieveStatus(), |
---|
165 | UpdateStatus(), |
---|
166 | FakeStatus(), |
---|
167 | ] |
---|
168 | values = [ |
---|
169 | BytesIO(json.dumps({ |
---|
170 | "active": list( |
---|
171 | marshal_json(item) |
---|
172 | for item |
---|
173 | in active_items |
---|
174 | ), |
---|
175 | "recent": list( |
---|
176 | marshal_json(item) |
---|
177 | for item |
---|
178 | in recent_items |
---|
179 | ), |
---|
180 | }).encode("utf-8")), |
---|
181 | BytesIO(json.dumps({ |
---|
182 | "counters": { |
---|
183 | "bytes_downloaded": 0, |
---|
184 | }, |
---|
185 | "stats": { |
---|
186 | "node.uptime": 0, |
---|
187 | } |
---|
188 | }).encode("utf-8")), |
---|
189 | ] |
---|
190 | def do_http(*args, **kw): |
---|
191 | return values.pop(0) |
---|
192 | do_status(self.options, do_http) |
---|
193 | |
---|
194 | def test_fetch_error(self): |
---|
195 | def do_http(*args, **kw): |
---|
196 | raise RuntimeError("boom") |
---|
197 | do_status(self.options, do_http) |
---|
198 | |
---|
199 | |
---|
200 | class JsonHelpers(unittest.TestCase): |
---|
201 | |
---|
202 | def test_bad_response(self): |
---|
203 | def do_http(*args, **kw): |
---|
204 | return |
---|
205 | with self.assertRaises(RuntimeError) as ctx: |
---|
206 | _handle_response_for_fragment( |
---|
207 | BadResponse('the url', 'some err'), |
---|
208 | 'http://localhost:1234', |
---|
209 | ) |
---|
210 | self.assertIn( |
---|
211 | "Failed to get", |
---|
212 | str(ctx.exception), |
---|
213 | ) |
---|
214 | |
---|
215 | def test_happy_path(self): |
---|
216 | resp = _handle_response_for_fragment( |
---|
217 | StringIO('{"some": "json"}'), |
---|
218 | 'http://localhost:1234/', |
---|
219 | ) |
---|
220 | self.assertEqual(resp, dict(some='json')) |
---|
221 | |
---|
222 | def test_happy_path_post(self): |
---|
223 | resp = _handle_response_for_fragment( |
---|
224 | StringIO('{"some": "json"}'), |
---|
225 | 'http://localhost:1234/', |
---|
226 | ) |
---|
227 | self.assertEqual(resp, dict(some='json')) |
---|
228 | |
---|
229 | def test_no_data_returned(self): |
---|
230 | with self.assertRaises(RuntimeError) as ctx: |
---|
231 | _handle_response_for_fragment(StringIO('null'), 'http://localhost:1234') |
---|
232 | self.assertIn('No data from', str(ctx.exception)) |
---|
233 | |
---|
234 | def test_no_post_args(self): |
---|
235 | with self.assertRaises(ValueError) as ctx: |
---|
236 | _get_request_parameters_for_fragment( |
---|
237 | {'node-url': 'http://localhost:1234'}, |
---|
238 | '/fragment', |
---|
239 | method='POST', |
---|
240 | post_args=None, |
---|
241 | ) |
---|
242 | self.assertIn( |
---|
243 | "Must pass post_args", |
---|
244 | str(ctx.exception), |
---|
245 | ) |
---|
246 | |
---|
247 | def test_post_args_for_get(self): |
---|
248 | with self.assertRaises(ValueError) as ctx: |
---|
249 | _get_request_parameters_for_fragment( |
---|
250 | {'node-url': 'http://localhost:1234'}, |
---|
251 | '/fragment', |
---|
252 | method='GET', |
---|
253 | post_args={'foo': 'bar'} |
---|
254 | ) |
---|
255 | self.assertIn( |
---|
256 | "only valid for POST", |
---|
257 | str(ctx.exception), |
---|
258 | ) |
---|